Skip to content

Commit 195de14

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

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ def __new__(cls, code, data):
1515
raise ValueError("code must be 0~127")
1616
return super().__new__(cls, code, data)
1717

18+
class Bypass:
19+
"""Bypass is a placeholder class to skip serialization and pass the bytes value as is."""
20+
21+
__slots__ = ["data"]
22+
23+
def __init__(self, data):
24+
if isinstance(data, bytes):
25+
self.data = data
26+
27+
else:
28+
self.data = memoryview(data).tobytes()
1829

1930
class Timestamp:
2031
"""Timestamp represents the Timestamp extension type in msgpack.
@@ -156,7 +167,7 @@ def to_datetime(self):
156167
157168
:rtype: `datetime.datetime`
158169
"""
159-
utc = datetime.timezone.utc
170+
utc = datetime.UTC
160171
return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta(
161172
seconds=self.seconds, microseconds=self.nanoseconds // 1000
162173
)

msgpack/fallback.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def newlist_hint(size):
3737
return []
3838

3939

40-
from .exceptions import BufferFull, ExtraData, FormatError, OutOfData, StackError
41-
from .ext import ExtType, Timestamp
40+
from .exceptions import (BufferFull, ExtraData, FormatError, OutOfData,
41+
StackError)
42+
from .ext import Bypass, ExtType, Timestamp
4243

4344
EX_SKIP = 0
4445
EX_CONSTRUCT = 1
@@ -773,6 +774,9 @@ def _pack(
773774
self._buffer.write(struct.pack("b", code))
774775
self._buffer.write(data)
775776
return
777+
if check(obj, Bypass):
778+
self._buffer.write(obj.data)
779+
return
776780
if check(obj, list_types):
777781
n = len(obj)
778782
self._pack_array_header(n)

0 commit comments

Comments
 (0)