Skip to content

Commit 9cede4b

Browse files
committed
use cython.critical_section
1 parent ef4f83d commit 9cede4b

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

msgpack/_cmsgpack.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# coding: utf-8
21
#cython: embedsignature=True, c_string_encoding=ascii, language_level=3
2+
#cython: freethreading_compatible = True
3+
import cython
34
from cpython.datetime cimport import_datetime, datetime_new
45
import_datetime()
56

msgpack/_packer.pyx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# coding: utf-8
2-
31
from cpython cimport *
42
from cpython.bytearray cimport PyByteArray_Check, PyByteArray_CheckExact
53
from cpython.datetime cimport (
@@ -129,6 +127,7 @@ cdef class Packer:
129127
if self.exports > 0:
130128
raise BufferError("Existing exports of data: Packer cannot be changed")
131129

130+
@cython.critical_section
132131
def __init__(self, *, default=None,
133132
bint use_single_float=False, bint autoreset=True, bint use_bin_type=True,
134133
bint strict_types=False, bint datetime=False, unicode_errors=None,
@@ -269,6 +268,7 @@ cdef class Packer:
269268
return ret
270269
return self._pack_inner(o, 0, nest_limit)
271270

271+
@cython.critical_section
272272
def pack(self, object obj):
273273
cdef int ret
274274
self._check_exports()
@@ -284,13 +284,15 @@ cdef class Packer:
284284
self.pk.length = 0
285285
return buf
286286

287+
@cython.critical_section
287288
def pack_ext_type(self, typecode, data):
288289
self._check_exports()
289290
if len(data) > ITEM_LIMIT:
290291
raise ValueError("ext data too large")
291292
msgpack_pack_ext(&self.pk, typecode, len(data))
292293
msgpack_pack_raw_body(&self.pk, data, len(data))
293294

295+
@cython.critical_section
294296
def pack_array_header(self, long long size):
295297
self._check_exports()
296298
if size > ITEM_LIMIT:
@@ -301,6 +303,7 @@ cdef class Packer:
301303
self.pk.length = 0
302304
return buf
303305

306+
@cython.critical_section
304307
def pack_map_header(self, long long size):
305308
self._check_exports()
306309
if size > ITEM_LIMIT:
@@ -311,6 +314,7 @@ cdef class Packer:
311314
self.pk.length = 0
312315
return buf
313316

317+
@cython.critical_section
314318
def pack_map_pairs(self, object pairs):
315319
"""
316320
Pack *pairs* as msgpack map type.
@@ -331,6 +335,7 @@ cdef class Packer:
331335
self.pk.length = 0
332336
return buf
333337

338+
@cython.critical_section
334339
def reset(self):
335340
"""Reset internal buffer.
336341
@@ -339,20 +344,24 @@ cdef class Packer:
339344
self._check_exports()
340345
self.pk.length = 0
341346

347+
@cython.critical_section
342348
def bytes(self):
343349
"""Return internal buffer contents as bytes object"""
344350
return PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
345351

352+
@cython.critical_section
346353
def getbuffer(self):
347354
"""Return memoryview of internal buffer.
348355
349356
Note: Packer now supports buffer protocol. You can use memoryview(packer).
350357
"""
351358
return memoryview(self)
352359

360+
@cython.critical_section
353361
def __getbuffer__(self, Py_buffer *buffer, int flags):
354362
PyBuffer_FillInfo(buffer, self, self.pk.buf, self.pk.length, 1, flags)
355363
self.exports += 1
356364

365+
@cython.critical_section
357366
def __releasebuffer__(self, Py_buffer *buffer):
358367
self.exports -= 1

msgpack/_unpacker.pyx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# coding: utf-8
2-
31
from cpython cimport *
42
cdef extern from "Python.h":
53
ctypedef struct PyObject
@@ -324,6 +322,7 @@ cdef class Unpacker:
324322
PyMem_Free(self.buf)
325323
self.buf = NULL
326324

325+
@cython.critical_section
327326
def __init__(self, file_like=None, *, Py_ssize_t read_size=0,
328327
bint use_list=True, bint raw=False, int timestamp=0, bint strict_map_key=True,
329328
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
@@ -384,6 +383,7 @@ cdef class Unpacker:
384383
max_str_len, max_bin_len, max_array_len,
385384
max_map_len, max_ext_len)
386385

386+
@cython.critical_section
387387
def feed(self, object next_bytes):
388388
"""Append `next_bytes` to internal buffer."""
389389
cdef Py_buffer pybuff
@@ -400,6 +400,7 @@ cdef class Unpacker:
400400
finally:
401401
PyBuffer_Release(&pybuff)
402402

403+
@cython.critical_section
403404
cdef append_buffer(self, void* _buf, Py_ssize_t _buf_len):
404405
cdef:
405406
char* buf = self.buf
@@ -484,6 +485,7 @@ cdef class Unpacker:
484485
else:
485486
raise ValueError("Unpack failed: error = %d" % (ret,))
486487

488+
@cython.critical_section
487489
def read_bytes(self, Py_ssize_t nbytes):
488490
"""Read a specified number of raw bytes from the stream"""
489491
cdef Py_ssize_t nread
@@ -496,20 +498,23 @@ cdef class Unpacker:
496498
self.stream_offset += nread
497499
return ret
498500

501+
@cython.critical_section
499502
def unpack(self):
500503
"""Unpack one object
501504
502505
Raises `OutOfData` when there are no more bytes to unpack.
503506
"""
504507
return self._unpack(unpack_construct)
505508

509+
@cython.critical_section
506510
def skip(self):
507511
"""Read and ignore one object, returning None
508512
509513
Raises `OutOfData` when there are no more bytes to unpack.
510514
"""
511515
return self._unpack(unpack_skip)
512516

517+
@cython.critical_section
513518
def read_array_header(self):
514519
"""assuming the next object is an array, return its size n, such that
515520
the next n unpack() calls will iterate over its contents.
@@ -518,6 +523,7 @@ cdef class Unpacker:
518523
"""
519524
return self._unpack(read_array_header)
520525

526+
@cython.critical_section
521527
def read_map_header(self):
522528
"""assuming the next object is a map, return its size n, such that the
523529
next n * 2 unpack() calls will iterate over its key-value pairs.
@@ -526,6 +532,7 @@ cdef class Unpacker:
526532
"""
527533
return self._unpack(read_map_header)
528534

535+
@cython.critical_section
529536
def tell(self):
530537
"""Returns the current position of the Unpacker in bytes, i.e., the
531538
number of bytes that were read from the input, also the starting
@@ -536,6 +543,7 @@ cdef class Unpacker:
536543
def __iter__(self):
537544
return self
538545

546+
@cython.critical_section
539547
def __next__(self):
540548
return self._unpack(unpack_construct, 1)
541549

0 commit comments

Comments
 (0)