Skip to content

Commit e4ec35b

Browse files
committed
Add support for bypassing pre-packed data
1 parent f980636 commit e4ec35b

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

msgpack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33

44
from .exceptions import * # noqa: F403
5-
from .ext import ExtType, Timestamp
5+
from .ext import Bypass, ExtType, Timestamp
66

77
version = (1, 1, 2)
88
__version__ = "1.1.2"

msgpack/_packer.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ from cpython.datetime cimport (
88
cdef ExtType
99
cdef Timestamp
1010

11-
from .ext import ExtType, Timestamp
11+
from .ext import ExtType, Timestamp, Bypass
1212

1313

1414
cdef extern from "Python.h":
@@ -222,6 +222,8 @@ cdef class Packer:
222222
llval = o.seconds
223223
ulval = o.nanoseconds
224224
msgpack_pack_timestamp(&self.pk, llval, ulval)
225+
elif type(o) is Bypass:
226+
msgpack_pack_raw_body(&self.pk, o.data, len(o.data))
225227
elif PyList_CheckExact(o) if strict else (PyTuple_Check(o) or PyList_Check(o)):
226228
L = Py_SIZE(o)
227229
if L > ITEM_LIMIT:

msgpack/ext.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ def __new__(cls, code, data):
1616
return super().__new__(cls, code, data)
1717

1818

19+
class Bypass:
20+
"""Bypass is a placeholder class to skip serialization and pass the bytes value as is."""
21+
22+
__slots__ = ["data"]
23+
24+
def __init__(self, data):
25+
if isinstance(data, bytes):
26+
self.data = data
27+
28+
else:
29+
self.data = memoryview(data).tobytes()
30+
31+
1932
class Timestamp:
2033
"""Timestamp represents the Timestamp extension type in msgpack.
2134

msgpack/fallback.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def newlist_hint(size):
3838

3939

4040
from .exceptions import BufferFull, ExtraData, FormatError, OutOfData, StackError
41-
from .ext import ExtType, Timestamp
41+
from .ext import Bypass, ExtType, Timestamp
4242

4343
EX_SKIP = 0
4444
EX_CONSTRUCT = 1
@@ -773,6 +773,9 @@ def _pack(
773773
self._buffer.write(struct.pack("b", code))
774774
self._buffer.write(data)
775775
return
776+
if check(obj, Bypass):
777+
self._buffer.write(obj.data)
778+
return
776779
if check(obj, list_types):
777780
n = len(obj)
778781
self._pack_array_header(n)

0 commit comments

Comments
 (0)