packb supports use_single_float option.

This commit is contained in:
INADA Naoki 2012-09-21 14:15:30 +09:00
parent 397d772e11
commit 51335bbee4
3 changed files with 16 additions and 3 deletions

View file

@ -1,6 +1,11 @@
0.2.2
=======
:release date: NOT RELEASED YET
:release date: 2012-09-21
Changes
-------
* Add ``use_single_float`` option to ``Packer``. When it is true, packs float
object in single precision format.
Bugs fixed
-----------
@ -8,6 +13,8 @@ Bugs fixed
``unpack()`` doesn't control gc now instead of restoring gc state collectly.
User can control gc state when gc cause performance issue.
* ``Unpacker``'s ``read_size`` option didn't used.
0.2.1
=======
:release date: 2012-08-20

View file

@ -177,10 +177,11 @@ def pack(object o, object stream, default=None, encoding='utf-8', unicode_errors
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors)
stream.write(packer.pack(o))
def packb(object o, default=None, encoding='utf-8', unicode_errors='strict'):
def packb(object o, default=None, encoding='utf-8', unicode_errors='strict', use_single_float=False):
"""
pack o and return packed bytes."""
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors)
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors,
use_single_float=use_single_float)
return packer.pack(o)

View file

@ -2,6 +2,7 @@
# coding: utf-8
import six
import struct
from nose import main
from nose.tools import *
from nose.plugins.skip import SkipTest
@ -86,5 +87,9 @@ def testDecodeBinary():
re = unpackb(packb("abc"), encoding=None)
assert_equal(re, b"abc")
def testPackFloat():
assert_equal(packb(1.0, use_single_float=True), b'\xca' + struct.pack('>f', 1.0))
assert_equal(packb(1.0, use_single_float=False), b'\xcb' + struct.pack('>d', 1.0))
if __name__ == '__main__':
main()