File: streams.pyx

package info (click to toggle)
python-scipy 0.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,464 kB
  • ctags: 79,406
  • sloc: python: 143,495; cpp: 89,357; fortran: 81,650; ansic: 79,778; makefile: 364; sh: 265
file content (361 lines) | stat: -rw-r--r-- 10,718 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# -*- python -*- or near enough

import sys
import zlib

from cpython cimport PyBytes_FromStringAndSize, \
    PyBytes_AS_STRING, PyBytes_Size

from pyalloc cimport pyalloc_v

from libc.stdio cimport fread, fseek, ftell
from libc.string cimport memcpy

cdef extern from "Python.h":
    void *PyCObject_Import(char *, char *) except NULL
    ctypedef struct PyTypeObject:
        pass
    ctypedef struct PyObject:
        pass
    ctypedef struct FILE

cdef extern from "py3k.h":
    # From:
    # http://svn.pyamf.org/pyamf/tags/release-0.4rc2/cpyamf/util.pyx
    # (MIT license) - with thanks
    void PycString_IMPORT()
    int StringIO_cread "PycStringIO->cread" (object, char **, Py_ssize_t)
    int StringIO_creadline "PycStringIO->creadline" (object, char **)
    int StringIO_cwrite "PycStringIO->cwrite" (object, char *, Py_ssize_t)
    object StringIO_cgetvalue "PycStringIO->cgetvalue" (obj)
    bint PycStringIO_InputCheck(object O)
    bint PycStringIO_OutputCheck(object O)

    FILE* npy_PyFile_Dup(object file, char *mode) except NULL
    int npy_PyFile_DupClose(object file, FILE *handle) except -1
    int npy_PyFile_Check(object file)

       
# initialize cStringIO
PycString_IMPORT

DEF BLOCK_SIZE = 131072

cdef class GenericStream:

    def __init__(self, fobj):
        self.fobj = fobj

    cpdef int seek(self, long int offset, int whence=0) except -1:
        self.fobj.seek(offset, whence)
        return 0
        
    cpdef long int tell(self) except -1:
        return self.fobj.tell()

    def read(self, n_bytes):
        return self.fobj.read(n_bytes)

    cdef int read_into(self, void *buf, size_t n) except -1:
        """ Read n bytes from stream into pre-allocated buffer `buf`
        """
        cdef char *p
        cdef size_t read_size, count

        # Read data to buf in BLOCK_SIZE blocks
        count = 0
        p = <char*>buf
        while count < n:
            read_size = min(n - count, BLOCK_SIZE)
            data = self.fobj.read(read_size)
            read_size = len(data)
            if read_size == 0:
                break
            memcpy(p, <const char*>data, read_size)
            p += read_size
            count += read_size

        if count != n:
            raise IOError('could not read bytes')
        return 0

    cdef object read_string(self, size_t n, void **pp, int copy=True):
        """Make new memory, wrap with object"""
        if copy != True:
            data = self.fobj.read(n)
            if PyBytes_Size(data) != n:
                raise IOError('could not read bytes')
            pp[0] = <void*>PyBytes_AS_STRING(data)
            return data

        cdef object d_copy = pyalloc_v(n, pp)
        self.read_into(pp[0], n)
        return d_copy


cdef class ZlibInputStream(GenericStream):
    """
    File-like object uncompressing bytes from a zlib compressed stream.

    Parameters
    ----------
    stream : file-like
        Stream to read compressed data from.
    max_length : int
        Maximum number of bytes to read from the stream.

    Notes
    -----
    Some matlab files contain zlib streams without valid Z_STREAM_END
    termination.  To get round this, we use the decompressobj object, that
    allows you to decode an incomplete stream.  See discussion at
    http://bugs.python.org/issue8672

    """

    cdef ssize_t _max_length
    cdef object _decompressor
    cdef bytes _buffer
    cdef size_t _buffer_size
    cdef size_t _buffer_position
    cdef size_t _total_position
    cdef size_t _read_bytes

    def __init__(self, fobj, ssize_t max_length):
        self.fobj = fobj

        self._max_length = max_length
        self._decompressor = zlib.decompressobj()
        self._buffer = b''
        self._buffer_size = 0
        self._buffer_position = 0
        self._total_position = 0
        self._read_bytes = 0

    cdef inline void _fill_buffer(self) except *:
        cdef size_t read_size
        cdef bytes block

        if self._buffer_position < self._buffer_size:
            return

        read_size = min(BLOCK_SIZE, self._max_length - self._read_bytes)

        block = self.fobj.read(read_size)
        self._read_bytes += len(block)

        self._buffer_position = 0
        if not block:
            self._buffer = self._decompressor.flush()
        else:
            self._buffer = self._decompressor.decompress(block)
        self._buffer_size = len(self._buffer)

    cdef int read_into(self, void *buf, size_t n) except -1:
        """Read n bytes from stream into pre-allocated buffer `buf`
        """
        cdef char *dstp
        cdef char *srcp
        cdef size_t read_size, count, size

        dstp = <char*>buf
        count = 0
        while count < n:
            self._fill_buffer()
            if self._buffer_size == 0:
                break

            srcp = <char*>self._buffer
            srcp += self._buffer_position

            size = min(n - count, self._buffer_size - self._buffer_position)
            memcpy(dstp, srcp, size)

            count += size
            dstp += size
            self._buffer_position += size

        self._total_position += count

        if count != n:
            raise IOError('could not read bytes')

        return 0

    cdef object read_string(self, size_t n, void **pp, int copy=True):
        """Make new memory, wrap with object"""
        cdef object d_copy = pyalloc_v(n, pp)
        self.read_into(pp[0], n)
        return d_copy

    def read(self, n_bytes):
        cdef void *p
        return self.read_string(n_bytes, &p)

    cpdef int all_data_read(self):
        return (self._max_length == self._read_bytes) and \
               (self._buffer_size == self._buffer_position)

    cpdef long int tell(self) except -1:
        if self._total_position == -1:
            raise IOError("Invalid file position.")
        return self._total_position

    cpdef int seek(self, long int offset, int whence=0) except -1:
        cdef ssize_t new_pos, size
        if whence == 1:
            new_pos = <ssize_t>self._total_position + offset
        elif whence == 0:
            new_pos = offset
        elif whence == 2:
            raise IOError("Zlib stream cannot seek from file end")
        else:
            raise ValueError("Invalid value for whence")

        if new_pos < self._total_position:
            raise IOError("Zlib stream cannot seek backwards")

        while self._total_position < new_pos:
            self._fill_buffer()
            if self._buffer_size == 0:
                break

            size = min(new_pos - self._total_position, 
                       self._buffer_size - self._buffer_position)

            self._total_position += size
            self._buffer_position += size

        return 0


cdef class cStringStream(GenericStream):
    
    cpdef int seek(self, long int offset, int whence=0) except -1:
        cdef char *ptr
        if whence == 1 and offset >=0: # forward, from here
            StringIO_cread(self.fobj, &ptr, offset)
            return 0
        else: # use python interface
            return GenericStream.seek(self, offset, whence)

    cdef int read_into(self, void *buf, size_t n) except -1:
        """ Read n bytes from stream into pre-allocated buffer `buf`
        """
        cdef:
            size_t n_red
            char* d_ptr
        n_red = StringIO_cread(self.fobj, &d_ptr, n)
        if n_red != n:
            raise IOError('could not read bytes')
        memcpy(buf, <void *>d_ptr, n)
        return 0

    cdef object read_string(self, size_t n, void **pp, int copy=True):
        """ Make new memory, wrap with object

        It's not obvious to me how to avoid a copy
        """
        cdef:
            char *d_ptr
            object obj
        cdef size_t n_red = StringIO_cread(self.fobj, &d_ptr, n)
        if n_red != n:
            raise IOError('could not read bytes')
        obj = pyalloc_v(n, pp)
        memcpy(pp[0], d_ptr, n)
        return obj

   
cdef class FileStream(GenericStream):
    cdef FILE* file

    def __init__(self, fobj):
        self.fobj = fobj
        self.file = npy_PyFile_Dup(fobj, "rb")

    def __del__(self):
        npy_PyFile_DupClose(self.fobj, self.file)

    cpdef int seek(self, long int offset, int whence=0) except -1:
        cdef int ret
        """ move `offset` bytes in stream

        Parameters
        ----------
        offset : long int
           number of bytes to move.  Positive for forward in file,
           negative for backward
        whence : int
           `whence` can be:
           
           * 0 - from beginning of file (`offset` should be >=0)
           * 1 - from current file position
           * 2 - from end of file (`offset` nearly always <=0)

        Returns
        -------
        ret : int
        """
        ret = fseek(self.file, offset, whence)
        if ret:
            raise IOError('Failed seek')
        return ret

    cpdef long int tell(self) except -1:
        cdef long int position = ftell(self.file)
        if position == -1:
            raise IOError("Invalid file position.")
        return position

    cdef int read_into(self, void *buf, size_t n) except -1:
        """ Read n bytes from stream into pre-allocated buffer `buf`
        """
        cdef:
            size_t n_red
            char* d_ptr
        n_red = fread(buf, 1, n, self.file)
        if n_red != n:
            raise IOError('Could not read bytes')
        return 0

    cdef object read_string(self, size_t n, void **pp, int copy=True):
        """ Make new memory, wrap with object """
        cdef object obj = pyalloc_v(n, pp)
        cdef size_t n_red = fread(pp[0], 1, n, self.file)
        if n_red != n:
            raise IOError('could not read bytes')
        return obj

def _read_into(GenericStream st, size_t n):
    # for testing only.  Use st.read instead
    cdef char * d_ptr
    my_str = b' ' * n
    d_ptr = my_str
    st.read_into(d_ptr, n)
    return my_str


def _read_string(GenericStream st, size_t n):
    # for testing only.  Use st.read instead
    cdef void *d_ptr
    cdef object obj = st.read_string(n, &d_ptr, True)
    my_str = b'A' * n
    cdef char *mys_ptr = my_str
    memcpy(mys_ptr, d_ptr, n)
    return my_str


cpdef GenericStream make_stream(object fobj):
    """ Make stream of correct type for file-like `fobj`
    """
    if npy_PyFile_Check(fobj):
        if <int>sys.version_info[0] >= 3:
            return GenericStream(fobj)
        else:
            return FileStream(fobj)
    elif PycStringIO_InputCheck(fobj) or PycStringIO_OutputCheck(fobj):
        return cStringStream(fobj)
    elif isinstance(fobj, GenericStream):
        return fobj
    return GenericStream(fobj)