| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | import os | |
| 3 | 3 | ||
| 4 | 4 | from .exceptions import * # noqa: F403 | |
| 5 | - from .ext import ExtType, Timestamp | ||
| 5 | + from .ext import Bypass, ExtType, Timestamp | ||
| 6 | 6 | ||
| 7 | 7 | version = (1, 1, 2) | |
| 8 | 8 | __version__ = "1.1.2" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,7 @@ from cpython.datetime cimport ( | |||
| 8 | 8 | cdef ExtType | |
| 9 | 9 | cdef Timestamp | |
| 10 | 10 | ||
| 11 | - from .ext import ExtType, Timestamp | ||
| 11 | + from .ext import ExtType, Timestamp, Bypass | ||
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | 14 | cdef extern from "Python.h": | |
@@ -222,6 +222,8 @@ cdef class Packer: | |||
| 222 | 222 | llval = o.seconds | |
| 223 | 223 | ulval = o.nanoseconds | |
| 224 | 224 | 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)) | ||
| 225 | 227 | elif PyList_CheckExact(o) if strict else (PyTuple_Check(o) or PyList_Check(o)): | |
| 226 | 228 | L = Py_SIZE(o) | |
| 227 | 229 | if L > ITEM_LIMIT: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,17 @@ def __new__(cls, code, data): | |||
| 15 | 15 | raise ValueError("code must be 0~127") | |
| 16 | 16 | return super().__new__(cls, code, data) | |
| 17 | 17 | ||
| 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() | ||
| 18 | 29 | ||
| 19 | 30 | class Timestamp: | |
| 20 | 31 | """Timestamp represents the Timestamp extension type in msgpack. | |
@@ -156,7 +167,7 @@ def to_datetime(self): | |||
| 156 | 167 | ||
| 157 | 168 | :rtype: `datetime.datetime` | |
| 158 | 169 | """ | |
| 159 | - utc = datetime.timezone.utc | ||
| 170 | + utc = datetime.UTC | ||
| 160 | 171 | return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta( | |
| 161 | 172 | seconds=self.seconds, microseconds=self.nanoseconds // 1000 | |
| 162 | 173 | ) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,8 +37,9 @@ def newlist_hint(size): | |||
| 37 | 37 | return [] | |
| 38 | 38 | ||
| 39 | 39 | ||
| 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 | ||
| 42 | 43 | ||
| 43 | 44 | EX_SKIP = 0 | |
| 44 | 45 | EX_CONSTRUCT = 1 | |
@@ -773,6 +774,9 @@ def _pack( | |||
| 773 | 774 | self._buffer.write(struct.pack("b", code)) | |
| 774 | 775 | self._buffer.write(data) | |
| 775 | 776 | return | |
| 777 | + if check(obj, Bypass): | ||
| 778 | + self._buffer.write(obj.data) | ||
| 779 | + return | ||
| 776 | 780 | if check(obj, list_types): | |
| 777 | 781 | n = len(obj) | |
| 778 | 782 | self._pack_array_header(n) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments