diff --git a/python/obitools3/files/uncompress.pxd b/python/obitools3/files/uncompress.pxd index 3d959b2..80c5b5d 100644 --- a/python/obitools3/files/uncompress.pxd +++ b/python/obitools3/files/uncompress.pxd @@ -1,10 +1,11 @@ #cython: language_level=3 cdef class MagicKeyFile: + cdef object stream cdef str stream_mode cdef object binary cdef bytes key - cdef int keylength + cdef int keylength cdef int pos cpdef bytes read(self,int size=?) diff --git a/python/obitools3/files/uncompress.pyx b/python/obitools3/files/uncompress.pyx index 4a871ac..ecd5d77 100644 --- a/python/obitools3/files/uncompress.pyx +++ b/python/obitools3/files/uncompress.pyx @@ -15,17 +15,20 @@ import io cdef class MagicKeyFile: def __init__(self,stream,length=2): - self.stream_mode = None + binary=stream + self.stream = stream + self.stream_mode = None if hasattr(stream, "mode"): self.stream_mode = stream.mode - if 'b' in stream.mode: - binary=stream - elif hasattr(stream, "buffer") and 'b' in stream.buffer.mode: + if (not 'b' in stream.mode and + hasattr(stream, "buffer") and + 'b' in stream.buffer.mode): binary=stream.buffer - else: - self.stream_mode = None - - if self.stream_mode is None: + + if (self.stream_mode is None and + not (hasattr(stream, 'headers') and + hasattr(stream.headers, "keys") and + 'Content-type' in stream.headers)): raise TypeError("stream does not present the good interface") self.binary=binary @@ -94,9 +97,12 @@ cdef class CompressedFile: if self.accessor is None: self.accessor = magic - - - if 'b' not in magic.stream_mode: + + if ((hasattr(stream, 'headers') and + hasattr(stream.headers, "keys") and + 'Content-type' in stream.headers and + stream.headers['Content-type'].startswith('text/')) or + 'b' not in magic.stream_mode): self.accessor = io.TextIOWrapper(self.accessor) @@ -105,7 +111,4 @@ cdef class CompressedFile: def __iter__(self): for x in self.accessor: - yield x - - - + yield x \ No newline at end of file