Skip to content

Commit d2e18f0

Browse files
authored
Upgrade to v0.3.2
Fixed fallback loads function what parsed negative integer from non negative Fixed C big integer packing issue.
2 parents f68ee0b + d574262 commit d2e18f0

File tree

6 files changed

+51
-25
lines changed

6 files changed

+51
-25
lines changed

benchmark/__main__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ def startBatch(i:int, data:Any, counter:int) -> None:
5252
32,
5353
)
5454
print("")
55-
56-
data = deepcopy(pickle.loads(zlib.decompress(open("{}/testData1.dat".format(dirname(__file__)), 'rb').read())))
55+
56+
with open("{}/testData1.dat".format(dirname(__file__)), 'rb') as fid:
57+
data = deepcopy(pickle.loads(zlib.decompress(fid.read())))
5758
startBatch(1, data, 1)
58-
data = deepcopy(pickle.loads(zlib.decompress(open("{}/testData2.dat".format(dirname(__file__)), 'rb').read())))
59+
with open("{}/testData2.dat".format(dirname(__file__)), 'rb') as fid:
60+
data = deepcopy(pickle.loads(zlib.decompress(fid.read())))
5961
startBatch(2, data, 1)
6062
data = deepcopy([data, {"data":(data, data)}])
6163
startBatch(3, data, 1)

fsPacker/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.3.1"
1+
__version__ = "0.3.2"
22
__doc__ = """
33
FS Message Packer v{}
44
Copyright (C) 2021 Fusion Solutions KFT <contact@fusionsolutions.io>

fsPacker/_fspacker.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -877,15 +877,11 @@ static int _fspacker_packer_ver2_int(packerObject_ver2 *self, PyObject *obj) {
877877
return _fspacker_buffer_append(self->output, &VER2_OP_ZERO_INTERGER, 1);
878878
}
879879
int isNeg = sign<0;
880-
Py_ssize_t nbits = _PyLong_NumBits(obj);
881-
Py_ssize_t nbytes = (nbits >> 3)+1;
882-
if (nbytes > ITEM_LIMIT) {
883-
PyErr_SetString(self->module->PackingError, "Too big integer to pack");
884-
return -1;
885-
}
886880
PyObject *absVal = NULL;
887881
unsigned char *pdata = NULL;
888882
PyObject *repr = NULL;
883+
Py_ssize_t nbits = 0;
884+
Py_ssize_t nbytes = 0;
889885
switch (_fspacker_packer_ver2_register(self, obj)) {
890886
case 1:
891887
goto done;
@@ -894,17 +890,44 @@ static int _fspacker_packer_ver2_int(packerObject_ver2 *self, PyObject *obj) {
894890
case -1:
895891
return -1;
896892
}
897-
repr = PyBytes_FromStringAndSize(NULL, nbytes);
898-
if (repr == NULL) {
899-
goto memoryError;
900-
}
901-
pdata = (unsigned char *)PyBytes_AS_STRING(repr);
902-
Py_DECREF(repr);
903-
absVal = isNeg ? PyNumber_Absolute(obj):NULL;
904-
if (_PyLong_AsByteArray((PyLongObject *)(absVal != NULL ? absVal:obj), pdata, nbytes, 1, 0) < 0) {
905-
goto memoryError;
893+
if ( isNeg ) {
894+
absVal = PyNumber_Absolute(obj);
895+
if ( absVal == NULL ) {
896+
goto memoryError;
897+
}
898+
nbits = _PyLong_NumBits(absVal);
899+
nbytes = (nbits >> 3)+1;
900+
if (nbytes > ITEM_LIMIT) {
901+
PyErr_SetString(self->module->PackingError, "Too big integer to pack");
902+
goto error;
903+
}
904+
repr = PyBytes_FromStringAndSize(NULL, nbytes);
905+
if (repr == NULL) {
906+
goto memoryError;
907+
}
908+
pdata = (unsigned char *)PyBytes_AS_STRING(repr);
909+
Py_DECREF(repr);
910+
if (_PyLong_AsByteArray((PyLongObject *)(absVal), pdata, nbytes, 1, 0) < 0) {
911+
goto memoryError;
912+
}
913+
Py_DECREF(absVal);
914+
} else {
915+
nbits = _PyLong_NumBits(obj);
916+
nbytes = (nbits >> 3)+1;
917+
if (nbytes > ITEM_LIMIT) {
918+
PyErr_SetString(self->module->PackingError, "Too big integer to pack");
919+
goto error;
920+
}
921+
repr = PyBytes_FromStringAndSize(NULL, nbytes);
922+
if (repr == NULL) {
923+
goto memoryError;
924+
}
925+
pdata = (unsigned char *)PyBytes_AS_STRING(repr);
926+
Py_DECREF(repr);
927+
if (_PyLong_AsByteArray((PyLongObject *)(obj), pdata, nbytes, 1, 0) < 0) {
928+
goto memoryError;
929+
}
906930
}
907-
Py_XDECREF(absVal);
908931
if ((nbytes*8)-nbits >= 8 ) {
909932
nbytes -= 1;
910933
}

fsPacker/fallback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def _parse(self) -> Any:
257257
elif op == self.OP_NEG_CHAR_INTERGER:
258258
r = -int.from_bytes(self._read(1), "little")
259259
elif op == self.OP_SHORT_INTERGER:
260-
r = -int.from_bytes(self._read(2), "little")
260+
r = int.from_bytes(self._read(2), "little")
261261
elif op == self.OP_NEG_SHORT_INTERGER:
262262
r = -int.from_bytes(self._read(2), "little")
263263
elif op == self.OP_INTERGER:

fsPacker/test/fsPacker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class FSPackerTest(unittest.TestCase):
2424
-1,
2525
1,
2626
1<<256,
27+
-(1<<256),
2728
0.0,
2829
0.1,
2930
-0.1,
@@ -43,8 +44,8 @@ class FSPackerTest(unittest.TestCase):
4344
tuple(),
4445
dict(),
4546
{"data":"ok"},
46-
{1:1},
47-
{(1,2,3):1},
47+
{None:0, 0:3, -1:4, 1:5, 255:6, -255:7, 0xFFFFFF:8, -0xFFFFFF:9, (1,2):10, 0.1:11, -0.1:12, float("inf"):13, \
48+
float("-inf"):14, "":15, "a":16, b"":17, b"a":18 },
4849
set(),
4950
set([1, "a", "test", "b", b"\x00"]),
5051
"F"*65000,

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
setup(
4040
name = "python-fspacker",
41-
version = "0.3.1",
41+
version = "0.3.2",
4242
description = "Fusion Solutions message packer",
4343
keywords = "message pack packer utility fusion solutions fusionsolutions",
4444
author = "Andor `iFA` Rajci - Fusions Solutions KFT",
@@ -63,4 +63,4 @@
6363
"Programming Language :: Python :: 3.10",
6464
"License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)",
6565
],
66-
)
66+
)

0 commit comments

Comments
 (0)