Commits

Andrew Davison committed 58e2c81

Handle data files that look enough like a parameter file to cause TypeErrors when trying to interpret them (see #195).

  • Participants
  • Parent commits c5d365b

Comments (0)

Files changed (2)

File sumatra/parameters.py

             for name, value in initialiser.items():
                 self.values[name] = value
                 self.types[name] = type(value)
-        else:
-            if os.path.exists(initialiser):
+        elif isinstance(initialiser, basestring):
+            try:
+                path_exists = os.path.exists(initialiser)
+            except Exception:  # e.g. null bytes in initialiser
+                path_exists = False
+            if path_exists:
                 with open(initialiser) as f:
                     content = f.readlines()
                 self.source_file = initialiser
                         self.values[name] = eval(value)
                     except NameError:
                         self.values[name] = unicode(value)
+                    except TypeError as err:  # e.g. null bytes
+                        raise SyntaxError("File is not a valid simple parameter file. %s" % err)
                     if "#" in value:
                         comment = "#".join(value.split("#")[1:])  # this fails if the value is a string containing '#'
                         self.comments[name] = comment
                         pass
                     else:
                         raise SyntaxError("File is not a valid simple parameter file. This line caused the error: %s" % line)
+        else:
+            raise TypeError("Parameter set initialiser must be a filename, string or dict.")
 
     def __str__(self):
         return self.pretty()

File test/unittests/test_parameters.py

         init = "# some data\n1.0 2.0 3.0\n4.0 5.0 6.0"
         self.assertRaises(SyntaxError, SimpleParameterSet, init)
 
+    def test__handles_null_bytes(self):
+        init = "foo=\x00\na=3\n"
+        self.assertRaises(SyntaxError, SimpleParameterSet, init)
+
     def test__string_parameters_should_be_able_to_contain_equals_signs(self):
         init = 'equation = "e = mc^2"'
         P = SimpleParameterSet(init)