Handle unmarshalling of large ints in Python bindings

This commit is contained in:
John J. Camilleri
2021-09-09 09:34:05 +02:00
parent 9739344ca6
commit 1d0c4e7c39
5 changed files with 26 additions and 12 deletions

View File

@@ -20,8 +20,9 @@
#define PyStringObject PyUnicodeObject
#define PyString_Check PyUnicode_Check
#define PyString_FromStringAndSize PyUnicode_FromStringAndSize
// #define PyString_FromFormat PyUnicode_FromFormat
#define PyString_FromFormat PyUnicode_FromFormat
// #define PyString_Concat(ps,s) {PyObject* tmp = *(ps); *(ps) = PyUnicode_Concat(tmp,s); Py_DECREF(tmp);}
#define PyString_Concat PyUnicode_Concat
#define PyString_Compare PyUnicode_Compare
#endif

View File

@@ -130,7 +130,10 @@ ExprLit_richcompare(ExprLitObject *t1, ExprLitObject *t2, int op)
if (t1->type != t2->type) goto done;
if (t1->type == 0) {
if (PyLong_AsLong(t1->value) != PyLong_AsLong(t2->value)) goto done;
int o1, o2;
int l1 = PyLong_AsLongAndOverflow(t1->value, &o1);
int l2 = PyLong_AsLongAndOverflow(t2->value, &o2);
if (!(l1 == l2 && o1 == o2)) goto done;
} else if (t1->type == 1) {
if (PyFloat_AsDouble(t1->value) != PyFloat_AsDouble(t2->value)) goto done;
} else if (t1->type == 2) {

View File

@@ -80,12 +80,22 @@ PgfExpr eimplarg(PgfUnmarshaller *this, PgfExpr expr)
PgfLiteral lint(PgfUnmarshaller *this, size_t size, uintmax_t *v)
{
intmax_t *v0 = (intmax_t *)v;
if (size > 1) {
PyErr_SetString(PyExc_NotImplementedError, "multi-part integers not implemented"); // TODO
Py_RETURN_NOTIMPLEMENTED;
if (size == 0) {
return (PgfLiteral) 0;
} else if (size > 1) {
// TODO: string concatenation works but probably not optimal
PyObject *sb = PyString_FromFormat("%ld", *v0);
for (size_t n = 1; n < size; n++) {
uintmax_t *vn = v + n;
PyObject *t = PyString_FromFormat("%lu", *vn);
sb = PyString_Concat(sb, t);
}
PyObject *i = PyLong_FromUnicodeObject(sb, 10);
return (PgfLiteral) i;
} else {
PyObject *i = PyLong_FromLong(*v0);
return (PgfLiteral) i;
}
PyObject *i = PyLong_FromLong(*v0);
return (PgfLiteral) i;
}
PgfLiteral lflt(PgfUnmarshaller *this, double v)

View File

@@ -10,7 +10,7 @@ if libraries==['']:
pgf_module = Extension('pgf',
sources = ['pypgf.c', 'marshaller.c', 'expr.c'],
extra_compile_args = ['-std=c99', '-Werror', '-Wno-error=int-conversion', '-Wno-comment'],
extra_compile_args = ['-std=c99', '-Werror', '-Wno-error=int-conversion', '-Wno-error=unused-variable', '-Wno-comment'],
include_dirs = includes,
library_dirs = libraries,
libraries = ['pgf'])

View File

@@ -139,11 +139,11 @@ def test_readExpr_equality_int():
def test_readExpr_equality_int_neg():
assert pgf.readExpr("-123") == pgf.readExpr("-123")
# def test_readExpr_equality_int_big():
# assert pgf.readExpr("774763251095801167872") == pgf.readExpr("774763251095801167872")
def test_readExpr_equality_int_big():
assert pgf.readExpr("774763251095801167872") == pgf.readExpr("774763251095801167872")
# def test_readExpr_equality_int_big_neg():
# assert pgf.readExpr("-774763251095801167872") == pgf.readExpr("-774763251095801167872")
def test_readExpr_equality_int_big_neg():
assert pgf.readExpr("-774763251095801167872") == pgf.readExpr("-774763251095801167872")
def test_readExpr_inequality_int():
assert pgf.readExpr("123") != pgf.readExpr("456")