Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close streams which are created in PrintCSV #3481

Merged
merged 1 commit into from
Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 8 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,13 @@ 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

# Output many CSVs, to check we correctly close streams
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;
fingolfin marked this conversation as resolved.
Show resolved Hide resolved
> od;
gap> s[2].emil:="\"Alas, poor Yorick\", the call went";;
gap> s[3].empty:="";;
gap> PrintCSV(fname,s);
Expand Down