From 361625a12c7cfc126223f7f4c4b12756d8d4ecfa Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Sun, 28 Nov 2021 11:39:31 +0000
Subject: [PATCH] force file.open to use textmode unless binary is specified

---
 binarycpython/utils/dataIO.py | 26 ++++++++++++++++++++------
 binarycpython/utils/grid.py   |  7 ++++++-
 2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/binarycpython/utils/dataIO.py b/binarycpython/utils/dataIO.py
index 1bcf59a7a..f6e8990bc 100644
--- a/binarycpython/utils/dataIO.py
+++ b/binarycpython/utils/dataIO.py
@@ -634,8 +634,7 @@ class dataIO():
 
                     # error on open should be fatal
                     except Exception as e:
-                        if vb:
-                            print("Error in locked_open_for_write() : {}".format(e))
+                        print("Error in locked_open_for_write() : {}".format(e))
                         if fatal_open_errors:
                             if vb:
                                 print("fatal exit on open")
@@ -677,25 +676,40 @@ class dataIO():
         """
         Wrapper for open() with automatic compression based on the file extension.
         """
+
         if compression is None:
             compression = self.compression_type(file)
+        print("open() with mode = {mode}, compression {compression}".format(
+            compression=compression,
+            mode=mode))
         if compression:
             if compresslevel is None:
                 compresslevel = 9
+            if not 'b' in mode:
+                # if we don't specify binary-mode, the gzip module
+                # defaults to binary, which isn't compatible with JSON,
+                # so default to text if not specified otherwise
+                mode += 't'
+            else:
+                encoding = None
+                errors = None
+                newline = None
             if compression is "bzip2":
                 file_object = bz2.open(file,
                                        mode=mode,
                                        compresslevel=compresslevel,
                                        encoding=encoding,
-                                       error=errors,
-                                       newline=newline)
+                                       errors=errors,
+                                       newline=newline
+                                       )
             elif compression is "gzip":
                 file_object = gzip.open(file,
                                         mode=mode,
                                         compresslevel=compresslevel,
                                         encoding=encoding,
-                                        error=errors,
-                                        newline=newline)
+                                        errors=errors,
+                                        newline=newline
+                                        )
         else:
             file_object = open(file,
                                mode=mode,
diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index 4b1ec7011..39441f43b 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -622,8 +622,11 @@ class Population(analytics,
                     settings_name
                 )
 
+                print("ok")
+
                 # open locked settings file, then output if we get the lock
-                (f,lock) = self.locked_open_for_write(settings_fullname)
+                (f,lock) = self.locked_open_for_write(settings_fullname,vb=True)
+                print("ok")
 
                 if lock and f:
                     self.verbose_print(
@@ -638,7 +641,9 @@ class Population(analytics,
                         default=binaryc_json_serializer,
                         ensure_ascii=ensure_ascii
                     )
+                print("ok pre")
                 self.locked_close(f,lock)
+                print("ok ret")
                 return settings_fullname
             else:
                 msg = "Exporting all info without passing a value for `outfile` requires custom_options['data_dir'] to be present. That is not the cause. Either set the `data_dir` or pass a value for `outfile` "
-- 
GitLab