Merge tag '1.0.3' into debian/yoga
Thomas Goirand
2 years ago
3 | 3 | |
4 | 4 | jobs: |
5 | 5 | black: |
6 | # We want to run on external PRs, but not on our own internal PRs as they'll be run | |
7 | # by the push to the branch. | |
8 | if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository | |
9 | ||
6 | 10 | runs-on: ubuntu-latest |
7 | 11 | steps: |
8 | 12 | - name: Setup Python |
5 | 5 | |
6 | 6 | jobs: |
7 | 7 | build: |
8 | # We want to run on external PRs, but not on our own internal PRs as they'll be run | |
9 | # by the push to the branch. | |
10 | if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository | |
11 | ||
8 | 12 | runs-on: ubuntu-latest |
9 | 13 | steps: |
10 | 14 | - name: Checkout |
46 | 50 | pytest -v test |
47 | 51 | |
48 | 52 | |
53 | - name: Set up Python 3.10 | |
54 | uses: actions/setup-python@v1 | |
55 | with: | |
56 | python-version: "3.10" | |
57 | ||
58 | - name: Run test (3.10) | |
59 | run: | | |
60 | pip install pytest | |
61 | pip install -v msgpack --only-binary :all: --no-index -f dist/wheelhouse | |
62 | pytest -v test | |
63 | ||
49 | 64 | - name: Set up Python 3.9 |
50 | 65 | uses: actions/setup-python@v1 |
51 | 66 | with: |
0 | 0 | name: Build macOS Wheels |
1 | 1 | on: |
2 | 2 | push: |
3 | pull_request: | |
4 | 3 | create: |
5 | 4 | |
6 | 5 | jobs: |
11 | 10 | - name: Checkout |
12 | 11 | uses: actions/checkout@v1 |
13 | 12 | |
14 | - name: Set up Python 3.8 | |
13 | # Python 3.9 | |
14 | - name: Set up Python 3.9 | |
15 | 15 | uses: actions/setup-python@v1 |
16 | 16 | with: |
17 | python-version: "3.8" | |
17 | python-version: "3.9" | |
18 | 18 | |
19 | 19 | - name: Cythonize |
20 | 20 | run: | |
22 | 22 | pip install -r requirements.txt |
23 | 23 | make cython |
24 | 24 | |
25 | - name: Build wheel | |
25 | - name: Build wheels | |
26 | uses: pypa/cibuildwheel@v2.2.2 | |
27 | env: | |
28 | CIBW_ARCHS_MACOS: x86_64 universal2 | |
29 | CIBW_SKIP: pp* | |
30 | ||
31 | - name: Run test | |
26 | 32 | run: | |
27 | pip install setuptools wheel | |
28 | python setup.py bdist_wheel | |
33 | ls wheelhouse/ | |
34 | pip install pytest | |
35 | pip install -v msgpack --only-binary :all: -f wheelhouse/ --no-index | |
36 | pytest -v test | |
37 | ||
38 | # Python 3.10 | |
39 | - name: Set up Python 3.10 | |
40 | uses: actions/setup-python@v1 | |
41 | with: | |
42 | python-version: "3.10" | |
29 | 43 | |
30 | 44 | - name: Run test |
31 | 45 | run: | |
32 | 46 | pip install pytest |
33 | pip install -v msgpack --only-binary :all: -f dist/ --no-index | |
47 | pip install -v msgpack --only-binary :all: -f wheelhouse/ --no-index | |
34 | 48 | pytest -v test |
35 | 49 | |
36 | ||
37 | - name: Set up Python 3.9 | |
50 | # Python 3.8 | |
51 | - name: Set up Python 3.8 | |
38 | 52 | uses: actions/setup-python@v1 |
39 | 53 | with: |
40 | python-version: "3.9" | |
41 | ||
42 | - name: Build wheel | |
43 | run: | | |
44 | pip install setuptools wheel | |
45 | python setup.py bdist_wheel | |
54 | python-version: "3.8" | |
46 | 55 | |
47 | 56 | - name: Run test |
48 | 57 | run: | |
49 | 58 | pip install pytest |
50 | pip install -v msgpack --only-binary :all: -f dist/ --no-index | |
59 | pip install -v msgpack --only-binary :all: -f wheelhouse/ --no-index | |
51 | 60 | pytest -v test |
52 | 61 | |
53 | ||
62 | # Python 3.7 | |
54 | 63 | - name: Set up Python 3.7 |
55 | 64 | uses: actions/setup-python@v1 |
56 | 65 | with: |
57 | 66 | python-version: "3.7" |
58 | 67 | |
59 | - name: Build wheel | |
60 | run: | | |
61 | pip install setuptools wheel | |
62 | python setup.py bdist_wheel | |
63 | ||
64 | 68 | - name: Run test |
65 | 69 | run: | |
66 | 70 | pip install pytest |
67 | pip install -v msgpack --only-binary :all: -f dist/ --no-index | |
68 | pytest -v test | |
69 | ||
70 | ||
71 | - name: Set up Python 3.6 | |
72 | uses: actions/setup-python@v1 | |
73 | with: | |
74 | python-version: "3.6" | |
75 | ||
76 | - name: Build wheel | |
77 | run: | | |
78 | pip install setuptools wheel | |
79 | python setup.py bdist_wheel | |
80 | ||
81 | - name: Run test | |
82 | run: | | |
83 | pip install pytest | |
84 | pip install -v msgpack --only-binary :all: -f dist/ --no-index | |
71 | pip install -v msgpack --only-binary :all: -f wheelhouse/ --no-index | |
85 | 72 | pytest -v test |
86 | 73 | |
87 | 74 | |
89 | 76 | uses: actions/upload-artifact@v1 |
90 | 77 | with: |
91 | 78 | name: macos-wheels |
92 | path: ./dist/ | |
79 | path: ./wheelhouse/ |
0 | 0 | name: Build and test windows wheels |
1 | 1 | on: |
2 | 2 | push: |
3 | branches: | |
4 | - master | |
5 | - test | |
6 | 3 | pull_request: |
7 | 4 | create: |
8 | 5 | |
9 | 6 | jobs: |
10 | 7 | build: |
8 | # We want to run on external PRs, but not on our own internal PRs as they'll be run | |
9 | # by the push to the branch. | |
10 | if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository | |
11 | ||
11 | 12 | runs-on: windows-latest |
12 | 13 | steps: |
13 | 14 | - name: Checkout |
76 | 77 | run: | |
77 | 78 | ci/runtests.sh |
78 | 79 | |
80 | - name: Python 3.10 (amd64) | |
81 | env: | |
82 | PYTHON: "py -3.10-64" | |
83 | shell: bash | |
84 | run: | | |
85 | ci/runtests.sh | |
86 | ||
87 | - name: Python 3.10 (x86) | |
88 | env: | |
89 | PYTHON: "py -3.10-32" | |
90 | shell: bash | |
91 | run: | | |
92 | ci/runtests.sh | |
93 | ||
79 | 94 | - name: Upload Wheels |
80 | 95 | uses: actions/upload-artifact@v1 |
81 | 96 | with: |
0 | 1.0.3 | |
1 | ===== | |
2 | ||
3 | Release Date: 2021-11-24 JST | |
4 | ||
5 | * Fix Docstring (#459) | |
6 | * Fix error formatting (#463) | |
7 | * Improve error message about strict_map_key (#485) | |
8 | ||
0 | 9 | 1.0.2 |
1 | 10 | ===== |
2 | 11 |
30 | 30 | |
31 | 31 | .PHONY: update-docker |
32 | 32 | update-docker: |
33 | docker pull quay.io/pypa/manylinux2010_i686 | |
34 | docker pull quay.io/pypa/manylinux2010_x86_64 | |
33 | docker pull quay.io/pypa/manylinux1_i686 | |
34 | docker pull quay.io/pypa/manylinux1_x86_64 | |
35 | 35 | |
36 | 36 | .PHONY: linux-wheel |
37 | 37 | linux-wheel: |
38 | docker run --rm -v `pwd`:/project -w /project quay.io/pypa/manylinux2010_i686 bash docker/buildwheel.sh | |
39 | docker run --rm -v `pwd`:/project -w /project quay.io/pypa/manylinux2010_x86_64 bash docker/buildwheel.sh | |
38 | docker run --rm -v `pwd`:/project -w /project quay.io/pypa/manylinux2014_i686 bash docker/buildwheel.sh | |
39 | docker run --rm -v `pwd`:/project -w /project quay.io/pypa/manylinux2014_x86_64 bash docker/buildwheel.sh | |
40 | 40 | |
41 | 41 | .PHONY: linux-arm64-wheel |
42 | 42 | linux-arm64-wheel: |
70 | 70 | But msgpack provides a pure Python implementation (`msgpack.fallback`) |
71 | 71 | for PyPy and Python 2. |
72 | 72 | |
73 | Since the [pip](https://pip.pypa.io/) uses the pure Python implementation, | |
74 | Python 2 support will not be dropped in the foreseeable future. | |
75 | 73 | |
76 | 74 | |
77 | 75 | ### Windows |
6 | 6 | ARCH=`uname -p` |
7 | 7 | echo "arch=$ARCH" |
8 | 8 | |
9 | ls /opt/python | |
10 | ||
9 | 11 | for V in "${PYTHON_VERSIONS[@]}"; do |
10 | 12 | PYBIN=/opt/python/$V/bin |
11 | 13 | rm -rf build/ # Avoid lib build by narrow Python is used by wide python |
12 | $PYBIN/python setup.py bdist_wheel | |
14 | $PYBIN/python -m build -w | |
13 | 15 | done |
14 | 16 | |
15 | 17 | cd dist |
0 | 0 | PYTHON_VERSIONS=( |
1 | cp310-cp310 | |
1 | 2 | cp39-cp39 |
2 | 3 | cp38-cp38 |
3 | 4 | cp37-cp37m |
4 | 5 | cp36-cp36m |
5 | cp35-cp35m | |
6 | 6 | ) |
284 | 284 | o = self._default(o) |
285 | 285 | default_used = 1 |
286 | 286 | continue |
287 | elif self.datetime and PyDateTime_CheckExact(o): | |
288 | PyErr_Format(ValueError, b"can not serialize '%.200s' object where tzinfo=None", Py_TYPE(o).tp_name) | |
287 | 289 | else: |
288 | 290 | PyErr_Format(TypeError, b"can not serialize '%.200s' object", Py_TYPE(o).tp_name) |
289 | 291 | return ret |
2 | 2 | from cpython cimport * |
3 | 3 | cdef extern from "Python.h": |
4 | 4 | ctypedef struct PyObject |
5 | cdef int PyObject_AsReadBuffer(object o, const void** buff, Py_ssize_t* buf_len) except -1 | |
6 | 5 | object PyMemoryView_GetContiguous(object obj, int buffertype, char order) |
7 | 6 | |
8 | 7 | from libc.stdlib cimport * |
9 | 8 | from libc.string cimport * |
10 | 9 | from libc.limits cimport * |
11 | ctypedef unsigned long long uint64_t | |
10 | from libc.stdint cimport uint64_t | |
12 | 11 | |
13 | 12 | from .exceptions import ( |
14 | 13 | BufferFull, |
211 | 210 | |
212 | 211 | |
213 | 212 | cdef class Unpacker(object): |
214 | """ | |
215 | MessagePack Packer | |
216 | ||
217 | Usage:: | |
218 | ||
219 | packer = Packer() | |
220 | astream.write(packer.pack(a)) | |
221 | astream.write(packer.pack(b)) | |
222 | ||
223 | Packer's constructor has some keyword arguments: | |
224 | ||
225 | :param callable default: | |
226 | Convert user type to builtin type that Packer supports. | |
227 | See also simplejson's document. | |
228 | ||
229 | :param bool use_single_float: | |
230 | Use single precision float type for float. (default: False) | |
231 | ||
232 | :param bool autoreset: | |
233 | Reset buffer after each pack and return its content as `bytes`. (default: True). | |
234 | If set this to false, use `bytes()` to get content and `.reset()` to clear buffer. | |
235 | ||
236 | :param bool use_bin_type: | |
237 | Use bin type introduced in msgpack spec 2.0 for bytes. | |
238 | It also enables str8 type for unicode. (default: True) | |
239 | ||
240 | :param bool strict_types: | |
241 | If set to true, types will be checked to be exact. Derived classes | |
242 | from serializable types will not be serialized and will be | |
243 | treated as unsupported type and forwarded to default. | |
244 | Additionally tuples will not be serialized as lists. | |
245 | This is useful when trying to implement accurate serialization | |
246 | for python types. | |
247 | ||
248 | :param bool datetime: | |
249 | If set to true, datetime with tzinfo is packed into Timestamp type. | |
250 | Note that the tzinfo is stripped in the timestamp. | |
251 | You can get UTC datetime with `timestamp=3` option of the Unpacker. | |
252 | (Python 2 is not supported). | |
213 | """Streaming unpacker. | |
214 | ||
215 | Arguments: | |
216 | ||
217 | :param file_like: | |
218 | File-like object having `.read(n)` method. | |
219 | If specified, unpacker reads serialized data from it and :meth:`feed()` is not usable. | |
220 | ||
221 | :param int read_size: | |
222 | Used as `file_like.read(read_size)`. (default: `min(16*1024, max_buffer_size)`) | |
223 | ||
224 | :param bool use_list: | |
225 | If true, unpack msgpack array to Python list. | |
226 | Otherwise, unpack to Python tuple. (default: True) | |
227 | ||
228 | :param bool raw: | |
229 | If true, unpack msgpack raw to Python bytes. | |
230 | Otherwise, unpack to Python str by decoding with UTF-8 encoding (default). | |
231 | ||
232 | :param int timestamp: | |
233 | Control how timestamp type is unpacked: | |
234 | ||
235 | 0 - Timestamp | |
236 | 1 - float (Seconds from the EPOCH) | |
237 | 2 - int (Nanoseconds from the EPOCH) | |
238 | 3 - datetime.datetime (UTC). Python 2 is not supported. | |
239 | ||
240 | :param bool strict_map_key: | |
241 | If true (default), only str or bytes are accepted for map (dict) keys. | |
242 | ||
243 | :param callable object_hook: | |
244 | When specified, it should be callable. | |
245 | Unpacker calls it with a dict argument after unpacking msgpack map. | |
246 | (See also simplejson) | |
247 | ||
248 | :param callable object_pairs_hook: | |
249 | When specified, it should be callable. | |
250 | Unpacker calls it with a list of key-value pairs after unpacking msgpack map. | |
251 | (See also simplejson) | |
253 | 252 | |
254 | 253 | :param str unicode_errors: |
255 | The error handler for encoding unicode. (default: 'strict') | |
256 | DO NOT USE THIS!! This option is kept for very specific usage. | |
254 | The error handler for decoding unicode. (default: 'strict') | |
255 | This option should be used only when you have msgpack data which | |
256 | contains invalid UTF-8 string. | |
257 | ||
258 | :param int max_buffer_size: | |
259 | Limits size of data waiting unpacked. 0 means 2**32-1. | |
260 | The default value is 100*1024*1024 (100MiB). | |
261 | Raises `BufferFull` exception when it is insufficient. | |
262 | You should set this parameter when unpacking data from untrusted source. | |
263 | ||
264 | :param int max_str_len: | |
265 | Deprecated, use *max_buffer_size* instead. | |
266 | Limits max length of str. (default: max_buffer_size) | |
267 | ||
268 | :param int max_bin_len: | |
269 | Deprecated, use *max_buffer_size* instead. | |
270 | Limits max length of bin. (default: max_buffer_size) | |
271 | ||
272 | :param int max_array_len: | |
273 | Limits max length of array. | |
274 | (default: max_buffer_size) | |
275 | ||
276 | :param int max_map_len: | |
277 | Limits max length of map. | |
278 | (default: max_buffer_size//2) | |
279 | ||
280 | :param int max_ext_len: | |
281 | Deprecated, use *max_buffer_size* instead. | |
282 | Limits max size of ext type. (default: max_buffer_size) | |
257 | 283 | |
258 | 284 | Example of streaming deserialize from file-like object:: |
259 | 285 |
0 | 0 | """Fallback pure Python implementation of msgpack""" |
1 | ||
2 | 1 | from datetime import datetime as _DateTime |
3 | 2 | import sys |
4 | 3 | import struct |
147 | 146 | else: |
148 | 147 | _unpack_from = struct.unpack_from |
149 | 148 | |
149 | _NO_FORMAT_USED = "" | |
150 | _MSGPACK_HEADERS = { | |
151 | 0xC4: (1, _NO_FORMAT_USED, TYPE_BIN), | |
152 | 0xC5: (2, ">H", TYPE_BIN), | |
153 | 0xC6: (4, ">I", TYPE_BIN), | |
154 | 0xC7: (2, "Bb", TYPE_EXT), | |
155 | 0xC8: (3, ">Hb", TYPE_EXT), | |
156 | 0xC9: (5, ">Ib", TYPE_EXT), | |
157 | 0xCA: (4, ">f"), | |
158 | 0xCB: (8, ">d"), | |
159 | 0xCC: (1, _NO_FORMAT_USED), | |
160 | 0xCD: (2, ">H"), | |
161 | 0xCE: (4, ">I"), | |
162 | 0xCF: (8, ">Q"), | |
163 | 0xD0: (1, "b"), | |
164 | 0xD1: (2, ">h"), | |
165 | 0xD2: (4, ">i"), | |
166 | 0xD3: (8, ">q"), | |
167 | 0xD4: (1, "b1s", TYPE_EXT), | |
168 | 0xD5: (2, "b2s", TYPE_EXT), | |
169 | 0xD6: (4, "b4s", TYPE_EXT), | |
170 | 0xD7: (8, "b8s", TYPE_EXT), | |
171 | 0xD8: (16, "b16s", TYPE_EXT), | |
172 | 0xD9: (1, _NO_FORMAT_USED, TYPE_RAW), | |
173 | 0xDA: (2, ">H", TYPE_RAW), | |
174 | 0xDB: (4, ">I", TYPE_RAW), | |
175 | 0xDC: (2, ">H", TYPE_ARRAY), | |
176 | 0xDD: (4, ">I", TYPE_ARRAY), | |
177 | 0xDE: (2, ">H", TYPE_MAP), | |
178 | 0xDF: (4, ">I", TYPE_MAP), | |
179 | } | |
180 | ||
150 | 181 | |
151 | 182 | class Unpacker(object): |
152 | 183 | """Streaming unpacker. |
228 | 259 | |
229 | 260 | Example of streaming deserialize from socket:: |
230 | 261 | |
231 | unpacker = Unpacker(max_buffer_size) | |
262 | unpacker = Unpacker() | |
232 | 263 | while True: |
233 | 264 | buf = sock.recv(1024**2) |
234 | 265 | if not buf: |
353 | 384 | self._buffer.extend(view) |
354 | 385 | |
355 | 386 | def _consume(self): |
356 | """ Gets rid of the used parts of the buffer. """ | |
387 | """Gets rid of the used parts of the buffer.""" | |
357 | 388 | self._stream_offset += self._buff_i - self._buf_checkpoint |
358 | 389 | self._buf_checkpoint = self._buff_i |
359 | 390 | |
408 | 439 | self._buff_i = 0 # rollback |
409 | 440 | raise OutOfData |
410 | 441 | |
411 | def _read_header(self, execute=EX_CONSTRUCT): | |
442 | def _read_header(self): | |
412 | 443 | typ = TYPE_IMMEDIATE |
413 | 444 | n = 0 |
414 | 445 | obj = None |
423 | 454 | n = b & 0b00011111 |
424 | 455 | typ = TYPE_RAW |
425 | 456 | if n > self._max_str_len: |
426 | raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len) | |
457 | raise ValueError("%s exceeds max_str_len(%s)" % (n, self._max_str_len)) | |
427 | 458 | obj = self._read(n) |
428 | 459 | elif b & 0b11110000 == 0b10010000: |
429 | 460 | n = b & 0b00001111 |
430 | 461 | typ = TYPE_ARRAY |
431 | 462 | if n > self._max_array_len: |
432 | raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len) | |
463 | raise ValueError( | |
464 | "%s exceeds max_array_len(%s)" % (n, self._max_array_len) | |
465 | ) | |
433 | 466 | elif b & 0b11110000 == 0b10000000: |
434 | 467 | n = b & 0b00001111 |
435 | 468 | typ = TYPE_MAP |
436 | 469 | if n > self._max_map_len: |
437 | raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len) | |
470 | raise ValueError("%s exceeds max_map_len(%s)" % (n, self._max_map_len)) | |
438 | 471 | elif b == 0xC0: |
439 | 472 | obj = None |
440 | 473 | elif b == 0xC2: |
441 | 474 | obj = False |
442 | 475 | elif b == 0xC3: |
443 | 476 | obj = True |
444 | elif b == 0xC4: | |
445 | typ = TYPE_BIN | |
446 | self._reserve(1) | |
447 | n = self._buffer[self._buff_i] | |
448 | self._buff_i += 1 | |
477 | elif 0xC4 <= b <= 0xC6: | |
478 | size, fmt, typ = _MSGPACK_HEADERS[b] | |
479 | self._reserve(size) | |
480 | if len(fmt) > 0: | |
481 | n = _unpack_from(fmt, self._buffer, self._buff_i)[0] | |
482 | else: | |
483 | n = self._buffer[self._buff_i] | |
484 | self._buff_i += size | |
449 | 485 | if n > self._max_bin_len: |
450 | 486 | raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len)) |
451 | 487 | obj = self._read(n) |
452 | elif b == 0xC5: | |
453 | typ = TYPE_BIN | |
454 | self._reserve(2) | |
455 | n = _unpack_from(">H", self._buffer, self._buff_i)[0] | |
456 | self._buff_i += 2 | |
457 | if n > self._max_bin_len: | |
458 | raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len)) | |
459 | obj = self._read(n) | |
460 | elif b == 0xC6: | |
461 | typ = TYPE_BIN | |
462 | self._reserve(4) | |
463 | n = _unpack_from(">I", self._buffer, self._buff_i)[0] | |
464 | self._buff_i += 4 | |
465 | if n > self._max_bin_len: | |
466 | raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len)) | |
467 | obj = self._read(n) | |
468 | elif b == 0xC7: # ext 8 | |
469 | typ = TYPE_EXT | |
470 | self._reserve(2) | |
471 | L, n = _unpack_from("Bb", self._buffer, self._buff_i) | |
472 | self._buff_i += 2 | |
488 | elif 0xC7 <= b <= 0xC9: | |
489 | size, fmt, typ = _MSGPACK_HEADERS[b] | |
490 | self._reserve(size) | |
491 | L, n = _unpack_from(fmt, self._buffer, self._buff_i) | |
492 | self._buff_i += size | |
473 | 493 | if L > self._max_ext_len: |
474 | 494 | raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len)) |
475 | 495 | obj = self._read(L) |
476 | elif b == 0xC8: # ext 16 | |
477 | typ = TYPE_EXT | |
478 | self._reserve(3) | |
479 | L, n = _unpack_from(">Hb", self._buffer, self._buff_i) | |
480 | self._buff_i += 3 | |
481 | if L > self._max_ext_len: | |
482 | raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len)) | |
483 | obj = self._read(L) | |
484 | elif b == 0xC9: # ext 32 | |
485 | typ = TYPE_EXT | |
486 | self._reserve(5) | |
487 | L, n = _unpack_from(">Ib", self._buffer, self._buff_i) | |
488 | self._buff_i += 5 | |
489 | if L > self._max_ext_len: | |
490 | raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len)) | |
491 | obj = self._read(L) | |
492 | elif b == 0xCA: | |
493 | self._reserve(4) | |
494 | obj = _unpack_from(">f", self._buffer, self._buff_i)[0] | |
495 | self._buff_i += 4 | |
496 | elif b == 0xCB: | |
497 | self._reserve(8) | |
498 | obj = _unpack_from(">d", self._buffer, self._buff_i)[0] | |
499 | self._buff_i += 8 | |
500 | elif b == 0xCC: | |
501 | self._reserve(1) | |
502 | obj = self._buffer[self._buff_i] | |
503 | self._buff_i += 1 | |
504 | elif b == 0xCD: | |
505 | self._reserve(2) | |
506 | obj = _unpack_from(">H", self._buffer, self._buff_i)[0] | |
507 | self._buff_i += 2 | |
508 | elif b == 0xCE: | |
509 | self._reserve(4) | |
510 | obj = _unpack_from(">I", self._buffer, self._buff_i)[0] | |
511 | self._buff_i += 4 | |
512 | elif b == 0xCF: | |
513 | self._reserve(8) | |
514 | obj = _unpack_from(">Q", self._buffer, self._buff_i)[0] | |
515 | self._buff_i += 8 | |
516 | elif b == 0xD0: | |
517 | self._reserve(1) | |
518 | obj = _unpack_from("b", self._buffer, self._buff_i)[0] | |
519 | self._buff_i += 1 | |
520 | elif b == 0xD1: | |
521 | self._reserve(2) | |
522 | obj = _unpack_from(">h", self._buffer, self._buff_i)[0] | |
523 | self._buff_i += 2 | |
524 | elif b == 0xD2: | |
525 | self._reserve(4) | |
526 | obj = _unpack_from(">i", self._buffer, self._buff_i)[0] | |
527 | self._buff_i += 4 | |
528 | elif b == 0xD3: | |
529 | self._reserve(8) | |
530 | obj = _unpack_from(">q", self._buffer, self._buff_i)[0] | |
531 | self._buff_i += 8 | |
532 | elif b == 0xD4: # fixext 1 | |
533 | typ = TYPE_EXT | |
534 | if self._max_ext_len < 1: | |
535 | raise ValueError("%s exceeds max_ext_len(%s)" % (1, self._max_ext_len)) | |
536 | self._reserve(2) | |
537 | n, obj = _unpack_from("b1s", self._buffer, self._buff_i) | |
538 | self._buff_i += 2 | |
539 | elif b == 0xD5: # fixext 2 | |
540 | typ = TYPE_EXT | |
541 | if self._max_ext_len < 2: | |
542 | raise ValueError("%s exceeds max_ext_len(%s)" % (2, self._max_ext_len)) | |
543 | self._reserve(3) | |
544 | n, obj = _unpack_from("b2s", self._buffer, self._buff_i) | |
545 | self._buff_i += 3 | |
546 | elif b == 0xD6: # fixext 4 | |
547 | typ = TYPE_EXT | |
548 | if self._max_ext_len < 4: | |
549 | raise ValueError("%s exceeds max_ext_len(%s)" % (4, self._max_ext_len)) | |
550 | self._reserve(5) | |
551 | n, obj = _unpack_from("b4s", self._buffer, self._buff_i) | |
552 | self._buff_i += 5 | |
553 | elif b == 0xD7: # fixext 8 | |
554 | typ = TYPE_EXT | |
555 | if self._max_ext_len < 8: | |
556 | raise ValueError("%s exceeds max_ext_len(%s)" % (8, self._max_ext_len)) | |
557 | self._reserve(9) | |
558 | n, obj = _unpack_from("b8s", self._buffer, self._buff_i) | |
559 | self._buff_i += 9 | |
560 | elif b == 0xD8: # fixext 16 | |
561 | typ = TYPE_EXT | |
562 | if self._max_ext_len < 16: | |
563 | raise ValueError("%s exceeds max_ext_len(%s)" % (16, self._max_ext_len)) | |
564 | self._reserve(17) | |
565 | n, obj = _unpack_from("b16s", self._buffer, self._buff_i) | |
566 | self._buff_i += 17 | |
567 | elif b == 0xD9: | |
568 | typ = TYPE_RAW | |
569 | self._reserve(1) | |
570 | n = self._buffer[self._buff_i] | |
571 | self._buff_i += 1 | |
496 | elif 0xCA <= b <= 0xD3: | |
497 | size, fmt = _MSGPACK_HEADERS[b] | |
498 | self._reserve(size) | |
499 | if len(fmt) > 0: | |
500 | obj = _unpack_from(fmt, self._buffer, self._buff_i)[0] | |
501 | else: | |
502 | obj = self._buffer[self._buff_i] | |
503 | self._buff_i += size | |
504 | elif 0xD4 <= b <= 0xD8: | |
505 | size, fmt, typ = _MSGPACK_HEADERS[b] | |
506 | if self._max_ext_len < size: | |
507 | raise ValueError( | |
508 | "%s exceeds max_ext_len(%s)" % (size, self._max_ext_len) | |
509 | ) | |
510 | self._reserve(size + 1) | |
511 | n, obj = _unpack_from(fmt, self._buffer, self._buff_i) | |
512 | self._buff_i += size + 1 | |
513 | elif 0xD9 <= b <= 0xDB: | |
514 | size, fmt, typ = _MSGPACK_HEADERS[b] | |
515 | self._reserve(size) | |
516 | if len(fmt) > 0: | |
517 | (n,) = _unpack_from(fmt, self._buffer, self._buff_i) | |
518 | else: | |
519 | n = self._buffer[self._buff_i] | |
520 | self._buff_i += size | |
572 | 521 | if n > self._max_str_len: |
573 | raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len) | |
522 | raise ValueError("%s exceeds max_str_len(%s)" % (n, self._max_str_len)) | |
574 | 523 | obj = self._read(n) |
575 | elif b == 0xDA: | |
576 | typ = TYPE_RAW | |
577 | self._reserve(2) | |
578 | (n,) = _unpack_from(">H", self._buffer, self._buff_i) | |
579 | self._buff_i += 2 | |
580 | if n > self._max_str_len: | |
581 | raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len) | |
582 | obj = self._read(n) | |
583 | elif b == 0xDB: | |
584 | typ = TYPE_RAW | |
585 | self._reserve(4) | |
586 | (n,) = _unpack_from(">I", self._buffer, self._buff_i) | |
587 | self._buff_i += 4 | |
588 | if n > self._max_str_len: | |
589 | raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len) | |
590 | obj = self._read(n) | |
591 | elif b == 0xDC: | |
592 | typ = TYPE_ARRAY | |
593 | self._reserve(2) | |
594 | (n,) = _unpack_from(">H", self._buffer, self._buff_i) | |
595 | self._buff_i += 2 | |
524 | elif 0xDC <= b <= 0xDD: | |
525 | size, fmt, typ = _MSGPACK_HEADERS[b] | |
526 | self._reserve(size) | |
527 | (n,) = _unpack_from(fmt, self._buffer, self._buff_i) | |
528 | self._buff_i += size | |
596 | 529 | if n > self._max_array_len: |
597 | raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len) | |
598 | elif b == 0xDD: | |
599 | typ = TYPE_ARRAY | |
600 | self._reserve(4) | |
601 | (n,) = _unpack_from(">I", self._buffer, self._buff_i) | |
602 | self._buff_i += 4 | |
603 | if n > self._max_array_len: | |
604 | raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len) | |
605 | elif b == 0xDE: | |
606 | self._reserve(2) | |
607 | (n,) = _unpack_from(">H", self._buffer, self._buff_i) | |
608 | self._buff_i += 2 | |
530 | raise ValueError( | |
531 | "%s exceeds max_array_len(%s)" % (n, self._max_array_len) | |
532 | ) | |
533 | elif 0xDE <= b <= 0xDF: | |
534 | size, fmt, typ = _MSGPACK_HEADERS[b] | |
535 | self._reserve(size) | |
536 | (n,) = _unpack_from(fmt, self._buffer, self._buff_i) | |
537 | self._buff_i += size | |
609 | 538 | if n > self._max_map_len: |
610 | raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len) | |
611 | typ = TYPE_MAP | |
612 | elif b == 0xDF: | |
613 | self._reserve(4) | |
614 | (n,) = _unpack_from(">I", self._buffer, self._buff_i) | |
615 | self._buff_i += 4 | |
616 | if n > self._max_map_len: | |
617 | raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len) | |
618 | typ = TYPE_MAP | |
539 | raise ValueError("%s exceeds max_map_len(%s)" % (n, self._max_map_len)) | |
619 | 540 | else: |
620 | 541 | raise FormatError("Unknown header: 0x%x" % b) |
621 | 542 | return typ, n, obj |
622 | 543 | |
623 | 544 | def _unpack(self, execute=EX_CONSTRUCT): |
624 | typ, n, obj = self._read_header(execute) | |
545 | typ, n, obj = self._read_header() | |
625 | 546 | |
626 | 547 | if execute == EX_READ_ARRAY_HEADER: |
627 | 548 | if typ != TYPE_ARRAY: |
952 | 873 | obj = self._default(obj) |
953 | 874 | default_used = 1 |
954 | 875 | continue |
876 | ||
877 | if self._datetime and check(obj, _DateTime): | |
878 | raise ValueError("Cannot serialize %r where tzinfo=None" % (obj,)) | |
879 | ||
955 | 880 | raise TypeError("Cannot serialize %r" % (obj,)) |
956 | 881 | |
957 | 882 | def pack(self, obj): |
192 | 192 | static inline int unpack_callback_map_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object k, msgpack_unpack_object v) |
193 | 193 | { |
194 | 194 | if (u->strict_map_key && !PyUnicode_CheckExact(k) && !PyBytes_CheckExact(k)) { |
195 | PyErr_Format(PyExc_ValueError, "%.100s is not allowed for map key", Py_TYPE(k)->tp_name); | |
195 | PyErr_Format(PyExc_ValueError, "%.100s is not allowed for map key when strict_map_key=True", Py_TYPE(k)->tp_name); | |
196 | 196 | return -1; |
197 | 197 | } |
198 | 198 | if (PyUnicode_CheckExact(k)) { |
0 | [build-system] | |
1 | requires = [ | |
2 | # Also declared in requirements.txt, if updating here please also update | |
3 | # there | |
4 | "Cython~=0.29.13", | |
5 | "setuptools >= 35.0.2", | |
6 | ] | |
7 | build-backend = "setuptools.build_meta" |
87 | 87 | macros = [("__LITTLE_ENDIAN__", "1")] |
88 | 88 | |
89 | 89 | ext_modules = [] |
90 | if not PYPY and not PY2: | |
90 | if not PYPY and not PY2 and not os.environ.get("MSGPACK_PUREPYTHON"): | |
91 | 91 | ext_modules.append( |
92 | 92 | Extension( |
93 | 93 | "msgpack._cmsgpack", |
124 | 124 | }, |
125 | 125 | license="Apache 2.0", |
126 | 126 | classifiers=[ |
127 | "Programming Language :: Python :: 2", | |
128 | "Programming Language :: Python :: 2.7", | |
129 | 127 | "Programming Language :: Python :: 3", |
130 | "Programming Language :: Python :: 3.5", | |
131 | 128 | "Programming Language :: Python :: 3.6", |
132 | 129 | "Programming Language :: Python :: 3.7", |
133 | 130 | "Programming Language :: Python :: 3.8", |
134 | 131 | "Programming Language :: Python :: 3.9", |
132 | "Programming Language :: Python :: 3.10", | |
135 | 133 | "Programming Language :: Python :: Implementation :: CPython", |
136 | 134 | "Programming Language :: Python :: Implementation :: PyPy", |
137 | 135 | "Intended Audience :: Developers", |
139 | 139 | |
140 | 140 | unpacked = msgpack.unpackb(packed, timestamp=3) |
141 | 141 | assert dt == unpacked |
142 | ||
143 | ||
144 | @pytest.mark.skipif(sys.version_info[0] == 2, reason="datetime support is PY3+ only") | |
145 | def test_pack_datetime_without_tzinfo(): | |
146 | dt = datetime.datetime(1970, 1, 1, 0, 0, 42, 14) | |
147 | with pytest.raises(ValueError, match="where tzinfo=None"): | |
148 | packed = msgpack.packb(dt, datetime=True) | |
149 | ||
150 | dt = datetime.datetime(1970, 1, 1, 0, 0, 42, 14) | |
151 | packed = msgpack.packb(dt, datetime=True, default=lambda x: None) | |
152 | assert packed == msgpack.packb(None) | |
153 | ||
154 | dt = datetime.datetime(1970, 1, 1, 0, 0, 42, 14, tzinfo=_utc) | |
155 | packed = msgpack.packb(dt, datetime=True) | |
156 | unpacked = msgpack.unpackb(packed, timestamp=3) | |
157 | assert unpacked == dt |
89 | 89 | assert obj == unp |
90 | 90 | assert pos == unpacker.tell() |
91 | 91 | assert unpacker.read_bytes(n) == raw |
92 | ||
93 | ||
94 | if __name__ == "__main__": | |
95 | test_unpack_array_header_from_file() | |
96 | test_unpacker_hook_refcnt() | |
97 | test_unpacker_ext_hook() | |
98 | test_unpacker_tell() |
4 | 4 | {pypy,pypy3}-pure, |
5 | 5 | py27-x86, |
6 | 6 | py34-x86, |
7 | ||
8 | [variants:pure] | |
9 | setenv= | |
10 | MSGPACK_PUREPYTHON=x | |
7 | isolated_build = true | |
11 | 8 | |
12 | 9 | [testenv] |
13 | 10 | deps= |
18 | 15 | c,x86: python -c 'from msgpack import _cmsgpack' |
19 | 16 | c,x86: py.test |
20 | 17 | pure: py.test |
18 | setenv= | |
19 | pure: MSGPACK_PUREPYTHON=x | |
21 | 20 | |
22 | 21 | [testenv:py27-x86] |
23 | 22 | basepython=python2.7-x86 |