Codebase list python-msgpack / 39b5e88
Merge tag '1.0.3' into debian/yoga Thomas Goirand 2 years ago
20 changed file(s) with 283 addition(s) and 285 deletion(s). Raw diff Collapse all Expand all
33
44 jobs:
55 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
610 runs-on: ubuntu-latest
711 steps:
812 - name: Setup Python
55
66 jobs:
77 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
812 runs-on: ubuntu-latest
913 steps:
1014 - name: Checkout
4650 pytest -v test
4751
4852
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
4964 - name: Set up Python 3.9
5065 uses: actions/setup-python@v1
5166 with:
00 name: Build macOS Wheels
11 on:
22 push:
3 pull_request:
43 create:
54
65 jobs:
1110 - name: Checkout
1211 uses: actions/checkout@v1
1312
14 - name: Set up Python 3.8
13 # Python 3.9
14 - name: Set up Python 3.9
1515 uses: actions/setup-python@v1
1616 with:
17 python-version: "3.8"
17 python-version: "3.9"
1818
1919 - name: Cythonize
2020 run: |
2222 pip install -r requirements.txt
2323 make cython
2424
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
2632 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"
2943
3044 - name: Run test
3145 run: |
3246 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
3448 pytest -v test
3549
36
37 - name: Set up Python 3.9
50 # Python 3.8
51 - name: Set up Python 3.8
3852 uses: actions/setup-python@v1
3953 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"
4655
4756 - name: Run test
4857 run: |
4958 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
5160 pytest -v test
5261
53
62 # Python 3.7
5463 - name: Set up Python 3.7
5564 uses: actions/setup-python@v1
5665 with:
5766 python-version: "3.7"
5867
59 - name: Build wheel
60 run: |
61 pip install setuptools wheel
62 python setup.py bdist_wheel
63
6468 - name: Run test
6569 run: |
6670 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
8572 pytest -v test
8673
8774
8976 uses: actions/upload-artifact@v1
9077 with:
9178 name: macos-wheels
92 path: ./dist/
79 path: ./wheelhouse/
00 name: Build and test windows wheels
11 on:
22 push:
3 branches:
4 - master
5 - test
63 pull_request:
74 create:
85
96 jobs:
107 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
1112 runs-on: windows-latest
1213 steps:
1314 - name: Checkout
7677 run: |
7778 ci/runtests.sh
7879
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
7994 - name: Upload Wheels
8095 uses: actions/upload-artifact@v1
8196 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
09 1.0.2
110 =====
211
3030
3131 .PHONY: update-docker
3232 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
3535
3636 .PHONY: linux-wheel
3737 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
4040
4141 .PHONY: linux-arm64-wheel
4242 linux-arm64-wheel:
7070 But msgpack provides a pure Python implementation (`msgpack.fallback`)
7171 for PyPy and Python 2.
7272
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.
7573
7674
7775 ### Windows
66 ARCH=`uname -p`
77 echo "arch=$ARCH"
88
9 ls /opt/python
10
911 for V in "${PYTHON_VERSIONS[@]}"; do
1012 PYBIN=/opt/python/$V/bin
1113 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
1315 done
1416
1517 cd dist
00 PYTHON_VERSIONS=(
1 cp310-cp310
12 cp39-cp39
23 cp38-cp38
34 cp37-cp37m
45 cp36-cp36m
5 cp35-cp35m
66 )
284284 o = self._default(o)
285285 default_used = 1
286286 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)
287289 else:
288290 PyErr_Format(TypeError, b"can not serialize '%.200s' object", Py_TYPE(o).tp_name)
289291 return ret
22 from cpython cimport *
33 cdef extern from "Python.h":
44 ctypedef struct PyObject
5 cdef int PyObject_AsReadBuffer(object o, const void** buff, Py_ssize_t* buf_len) except -1
65 object PyMemoryView_GetContiguous(object obj, int buffertype, char order)
76
87 from libc.stdlib cimport *
98 from libc.string cimport *
109 from libc.limits cimport *
11 ctypedef unsigned long long uint64_t
10 from libc.stdint cimport uint64_t
1211
1312 from .exceptions import (
1413 BufferFull,
211210
212211
213212 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)
253252
254253 :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)
257283
258284 Example of streaming deserialize from file-like object::
259285
0 version = (1, 0, 2)
0 version = (1, 0, 3)
00 """Fallback pure Python implementation of msgpack"""
1
21 from datetime import datetime as _DateTime
32 import sys
43 import struct
147146 else:
148147 _unpack_from = struct.unpack_from
149148
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
150181
151182 class Unpacker(object):
152183 """Streaming unpacker.
228259
229260 Example of streaming deserialize from socket::
230261
231 unpacker = Unpacker(max_buffer_size)
262 unpacker = Unpacker()
232263 while True:
233264 buf = sock.recv(1024**2)
234265 if not buf:
353384 self._buffer.extend(view)
354385
355386 def _consume(self):
356 """ Gets rid of the used parts of the buffer. """
387 """Gets rid of the used parts of the buffer."""
357388 self._stream_offset += self._buff_i - self._buf_checkpoint
358389 self._buf_checkpoint = self._buff_i
359390
408439 self._buff_i = 0 # rollback
409440 raise OutOfData
410441
411 def _read_header(self, execute=EX_CONSTRUCT):
442 def _read_header(self):
412443 typ = TYPE_IMMEDIATE
413444 n = 0
414445 obj = None
423454 n = b & 0b00011111
424455 typ = TYPE_RAW
425456 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))
427458 obj = self._read(n)
428459 elif b & 0b11110000 == 0b10010000:
429460 n = b & 0b00001111
430461 typ = TYPE_ARRAY
431462 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 )
433466 elif b & 0b11110000 == 0b10000000:
434467 n = b & 0b00001111
435468 typ = TYPE_MAP
436469 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))
438471 elif b == 0xC0:
439472 obj = None
440473 elif b == 0xC2:
441474 obj = False
442475 elif b == 0xC3:
443476 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
449485 if n > self._max_bin_len:
450486 raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
451487 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
473493 if L > self._max_ext_len:
474494 raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
475495 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
572521 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))
574523 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
596529 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
609538 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))
619540 else:
620541 raise FormatError("Unknown header: 0x%x" % b)
621542 return typ, n, obj
622543
623544 def _unpack(self, execute=EX_CONSTRUCT):
624 typ, n, obj = self._read_header(execute)
545 typ, n, obj = self._read_header()
625546
626547 if execute == EX_READ_ARRAY_HEADER:
627548 if typ != TYPE_ARRAY:
952873 obj = self._default(obj)
953874 default_used = 1
954875 continue
876
877 if self._datetime and check(obj, _DateTime):
878 raise ValueError("Cannot serialize %r where tzinfo=None" % (obj,))
879
955880 raise TypeError("Cannot serialize %r" % (obj,))
956881
957882 def pack(self, obj):
192192 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)
193193 {
194194 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);
196196 return -1;
197197 }
198198 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"
0 # Also declared in pyproject.toml, if updating here please also update there
01 Cython~=0.29.13
8787 macros = [("__LITTLE_ENDIAN__", "1")]
8888
8989 ext_modules = []
90 if not PYPY and not PY2:
90 if not PYPY and not PY2 and not os.environ.get("MSGPACK_PUREPYTHON"):
9191 ext_modules.append(
9292 Extension(
9393 "msgpack._cmsgpack",
124124 },
125125 license="Apache 2.0",
126126 classifiers=[
127 "Programming Language :: Python :: 2",
128 "Programming Language :: Python :: 2.7",
129127 "Programming Language :: Python :: 3",
130 "Programming Language :: Python :: 3.5",
131128 "Programming Language :: Python :: 3.6",
132129 "Programming Language :: Python :: 3.7",
133130 "Programming Language :: Python :: 3.8",
134131 "Programming Language :: Python :: 3.9",
132 "Programming Language :: Python :: 3.10",
135133 "Programming Language :: Python :: Implementation :: CPython",
136134 "Programming Language :: Python :: Implementation :: PyPy",
137135 "Intended Audience :: Developers",
139139
140140 unpacked = msgpack.unpackb(packed, timestamp=3)
141141 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
8989 assert obj == unp
9090 assert pos == unpacker.tell()
9191 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()
44 {pypy,pypy3}-pure,
55 py27-x86,
66 py34-x86,
7
8 [variants:pure]
9 setenv=
10 MSGPACK_PUREPYTHON=x
7 isolated_build = true
118
129 [testenv]
1310 deps=
1815 c,x86: python -c 'from msgpack import _cmsgpack'
1916 c,x86: py.test
2017 pure: py.test
18 setenv=
19 pure: MSGPACK_PUREPYTHON=x
2120
2221 [testenv:py27-x86]
2322 basepython=python2.7-x86