Skip to content

Commit

Permalink
Close stream created in PrintCSV
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson committed May 31, 2019
1 parent 5d81b55 commit af6a811
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/string.gi
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,14 @@ local nohead,file,sep,f, line, fields, l, r, i,s,t,add,dir;
end);

InstallGlobalFunction(PrintCSV,function(arg)
local stream,l,printEntry, rf, r, i, j, oldStreamFormattingStatus;
local stream,l,printEntry, rf, r, i, j, oldStreamFormattingStatus, close;

if IsString(arg[1]) then
stream:=OutputTextFile(arg[1],false);
close:=true;
elif IsOutputStream(arg[1]) then
stream:=arg[1];
close:=false;
else
Error("PrintCSV: filename must be a string or an output stream");
fi;
Expand Down Expand Up @@ -1045,6 +1047,9 @@ InstallGlobalFunction(PrintCSV,function(arg)
AppendTo(stream,"\n");
od;
SetPrintFormattingStatus(stream,oldStreamFormattingStatus);
if close then
CloseStream(stream);
fi;
end);


Expand Down
11 changes: 6 additions & 5 deletions tst/testinstall/stringobj.tst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## This file tests various aspects of strings in IsStringRep
##
#@local OldCopyToStringRep,a2000,a3000,at2000,at3000,cp1,cp2,cp3
#@local ret2000,ret3000,s,tmp,tmpdir,fname,dir,filename,sstream,fstream,t,u
#@local ret2000,ret3000,s,tmp,tmpdir,fname,dir,filename,sstream,fstream,t,u,i
gap> START_TEST("stringobj.tst");

# RemoveCharacters
Expand Down Expand Up @@ -119,10 +119,11 @@ gap> s := ReadCSV( filename{[1..Length(filename)-4]}, ";" );
[ rec( f := 2, f1 := 1, f_2 := 3 ), rec( f1 := 4, f_2 := 6 ), rec( f1 := 7 ),
rec( f := 11, f_2 := 12 ), rec( f := "yy", f1 := "x", f_2 := "zzz" ) ]
gap> tmpdir := DirectoryTemporary();;
gap> fname := Filename(tmpdir, "output.csv");;
gap> PrintCSV( fname, s );
gap> s = ReadCSV( fname );
true
gap> for i in [1..1000] do
> fname := Filename(tmpdir, "output.csv");;
> PrintCSV( fname, s );
> if s <> ReadCSV( fname ) then Print("failed loop", i,"\n"); fi;
> od;
gap> s[2].emil:="\"Alas, poor Yorick\", the call went";;
gap> s[3].empty:="";;
gap> PrintCSV(fname,s);
Expand Down

0 comments on commit af6a811

Please sign in to comment.