|
1 | 1 | # coding: utf-8
|
2 | 2 | #cython: embedsignature=True
|
3 | 3 |
|
4 |
| -from cpython cimport * |
| 4 | +from cpython.bytes cimport ( |
| 5 | + PyBytes_AsString, |
| 6 | + PyBytes_FromStringAndSize, |
| 7 | + PyBytes_Size, |
| 8 | +) |
| 9 | +from cpython.buffer cimport ( |
| 10 | + Py_buffer, |
| 11 | + PyBuffer_Release, |
| 12 | + PyObject_GetBuffer, |
| 13 | + PyBUF_SIMPLE, |
| 14 | +) |
| 15 | +from cpython.mem cimport PyMem_Malloc, PyMem_Free |
| 16 | +from cpython.object cimport PyCallable_Check |
| 17 | + |
5 | 18 | cdef extern from "Python.h":
|
6 | 19 | ctypedef struct PyObject
|
7 | 20 | cdef int PyObject_AsReadBuffer(object o, const void** buff, Py_ssize_t* buf_len) except -1
|
@@ -256,7 +269,7 @@ cdef class Unpacker(object):
|
256 | 269 | self.buf = NULL
|
257 | 270 |
|
258 | 271 | def __dealloc__(self):
|
259 |
| - free(self.buf) |
| 272 | + PyMem_Free(self.buf) |
260 | 273 | self.buf = NULL
|
261 | 274 |
|
262 | 275 | def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=1,
|
@@ -289,7 +302,7 @@ cdef class Unpacker(object):
|
289 | 302 | read_size = min(max_buffer_size, 1024**2)
|
290 | 303 | self.max_buffer_size = max_buffer_size
|
291 | 304 | self.read_size = read_size
|
292 |
| - self.buf = <char*>malloc(read_size) |
| 305 | + self.buf = <char*>PyMem_Malloc(read_size) |
293 | 306 | if self.buf == NULL:
|
294 | 307 | raise MemoryError("Unable to allocate internal buffer.")
|
295 | 308 | self.buf_size = read_size
|
@@ -352,13 +365,13 @@ cdef class Unpacker(object):
|
352 | 365 | if new_size > self.max_buffer_size:
|
353 | 366 | raise BufferFull
|
354 | 367 | new_size = min(new_size*2, self.max_buffer_size)
|
355 |
| - new_buf = <char*>malloc(new_size) |
| 368 | + new_buf = <char*>PyMem_Malloc(new_size) |
356 | 369 | if new_buf == NULL:
|
357 | 370 | # self.buf still holds old buffer and will be freed during
|
358 | 371 | # obj destruction
|
359 | 372 | raise MemoryError("Unable to enlarge internal buffer.")
|
360 | 373 | memcpy(new_buf, buf + head, tail - head)
|
361 |
| - free(buf) |
| 374 | + PyMem_Free(buf) |
362 | 375 |
|
363 | 376 | buf = new_buf
|
364 | 377 | buf_size = new_size
|
|
0 commit comments