8000 Merge remote-tracking branch 'origin/master' into skip · peter80/msgpack-python@032df6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 032df6f

Browse files
committed
Merge remote-tracking branch 'origin/master' into skip
2 parents 28058fb + 5b66eda commit 032df6f

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

ChangeLog.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
0.2.2
22
=======
3-
:release date: NOT RELEASED YET
3+
:release date: 2012-09-21
4+
5+
Changes
6+
-------
7+
* Add ``use_single_float`` option to ``Packer``. When it is true, packs float
8+
object in single precision format.
49

510
Bugs fixed
611
-----------
712
* ``unpack()`` didn't restores gc state when it called with gc disabled.
813
``unpack()`` doesn't control gc now instead of restoring gc state collectly.
914
User can control gc state when gc cause performance issue.
1015

16+
* ``Unpacker``'s ``read_size`` option didn't used.
17+
1118
0.2.1
1219
=======
1320
:release date: 2012-08-20

msgpack/_msgpack.pyx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ cdef class Packer(object):
6565
self.pk.buf_size = buf_size
6666
self.pk.length = 0
6767

68-
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict', use_float=False):
69-
self.use_float = use_float
68+
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict', use_single_float=False):
69+
self.use_float = use_single_float
7070
if default is not None:
7171
if not PyCallable_Check(default):
7272
raise TypeError("default must be a callable.")
@@ -177,10 +177,11 @@ def pack(object o, object stream, default=None, encoding='utf-8', unicode_errors
177177
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors)
178178
stream.write(packer.pack(o))
179179

180-
def packb(object o, default=None, encoding='utf-8', unicode_errors='strict'):
180+
def packb(object o, default=None, encoding='utf-8', unicode_errors='strict', use_single_float=False):
181181
"""
182182
pack o and return packed bytes."""
183-
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors)
183+
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors,
184+
use_single_float=use_single_float)
184185
return packer.pack(o)
185186

186187

@@ -286,8 +287,8 @@ cdef class Unpacker(object):
286287
287288
`unicode_errors` is used for decoding bytes.
288289
289-
`max_buffer_size` limits size of data waiting unpacked. 0 means unlimited
290-
(default).
290+
`max_buffer_size` limits size of data waiting unpacked.
291+
0 means system's INT_MAX (default).
291292
Raises `BufferFull` exception when it is insufficient.
292293
You shoud set this parameter when unpacking data from untrasted source.
293294
@@ -340,11 +341,11 @@ cdef class Unpacker(object):
340341
raise ValueError("`file_like.read` must be a callable.")
341342
if not max_buffer_size:
342343
max_buffer_size = INT_MAX
344+
if read_size > max_buffer_size:
345+
raise ValueError("read_size should be less or equal to max_buffer_size")
343346
if not read_size:
344347
read_size = min(max_buffer_size, 1024**2)
345348
self.max_buffer_size = max_buffer_size
346-
if read_size > max_buffer_size:
347-
raise ValueError("read_size should be less or equal to max_buffer_size")
348349
self.read_size = read_size
349350
self.buf = <char*>malloc(read_size)
350351
if self.buf == NULL:
@@ -427,18 +428,15 @@ cdef class Unpacker(object):
427428
self.buf_size = buf_size
428429
self.buf_tail = tail + _buf_len
429430

430-
# prepare self.buf from file_like
431-
cdef fill_buffer(self):
432-
if self.file_like is not None:
433-
next_bytes = self.file_like_read(
434-
max(self.read_size,
435-
self.max_buffer_size - (self.buf_tail - self.buf_head)
436-
))
437-
if next_bytes:
438-
self.append_buffer(PyBytes_AsString(next_bytes),
439-
PyBytes_Size(next_bytes))
440-
else:
441-
self.file_like = None
431+
cdef read_from_file(self):
432+
next_bytes = self.file_like_read(
433+
min(self.read_size,
434+
self.max_buffer_size - (self.buf_tail - self.buf_head)
435+
))
436+
if next_bytes:
437+
self.append_buffer(PyBytes_AsString(next_bytes), PyBytes_Size(next_bytes))
438+
else:
439+
self.file_like = None
442440

443441
cpdef _unpack(self, bool construct):
444442
cdef int ret
@@ -448,7 +446,7 @@ cdef class Unpacker(object):
448446
return
449447
elif ret == 0:
450448
if self.file_like is not None:
451-
self.fill_buffer()
449+
self.read_from_file()
452450
continue
453451
raise StopIteration("No more unpack data.")
454452
else:

msgpack/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = (0, 2, 1)
1+
version = (0, 2, 2)

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env python
22
# coding: utf-8
3-
version = (0, 2, 1, 'dev1')
4-
53
import os
64
import sys
75
import shutil

test/test_pack.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# coding: utf-8
33

44
import six
5+
import struct
56
from nose import main
67
from nose.tools import *
78
from nose.plugins.skip import SkipTest
@@ -86,5 +87,9 @@ def testDecodeBinary():
8687
re = unpackb(packb("abc"), encoding=None)
8788
assert_equal(re, b"abc")
8889

90+
def testPackFloat():
91+
assert_equal(packb(1.0, use_single_float=True), b'\xca' + struct.pack('>f', 1.0))
92+
assert_equal(packb(1.0, use_single_float=False), b'\xcb' + struct.pack('>d', 1.0))
93+
8994
if __name__ == '__main__':
9095
main()

0 commit comments

Comments
 (0)
0