diff options
Diffstat (limited to 'silx/io/specfile.pyx')
-rw-r--r-- | silx/io/specfile.pyx | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/silx/io/specfile.pyx b/silx/io/specfile.pyx index 35f425b..39e8816 100644 --- a/silx/io/specfile.pyx +++ b/silx/io/specfile.pyx @@ -614,7 +614,7 @@ class Scan(object): def _string_to_char_star(string_): """Convert a string to ASCII encoded bytes when using python3""" - if sys.version.startswith("3") and not isinstance(string_, bytes): + if sys.version_info[0] >= 3 and not isinstance(string_, bytes): return bytes(string_, "ascii") return string_ @@ -629,15 +629,14 @@ def is_specfile(filename): """ if not os.path.isfile(filename): return False - # test for presence of #S or #F in first two lines - f = open(filename) - for i, line in enumerate(f): - if line.startswith("#S ") or line.startswith("#F "): - f.close() + # test for presence of #S or #F in first 10 lines + with open(filename, "rb") as f: + chunk = f.read(2500) + for i, line in enumerate(chunk.split(b"\n")): + if line.startswith(b"#S ") or line.startswith(b"#F "): return True if i >= 10: break - f.close() return False @@ -681,10 +680,15 @@ cdef class SpecFile(object): def __dealloc__(self): """Destructor: Calls SfClose(self.handle)""" + self.close() + + def close(self): + """Close the file descriptor""" # handle is NULL if SfOpen failed if self.handle: if specfile_wrapper.SfClose(self.handle): _logger.warning("Error while closing SpecFile") + self.handle = NULL def __len__(self): """Return the number of scans in the SpecFile |