Skip to content

Commit

Permalink
Fix test failure on Python 3.12 (fixes osandov#298)
Browse files Browse the repository at this point in the history
Running tests on Python 3.12, we get:

test_int (tests.test_language_c.TestLiteral.test_int) ... python3.12: /usr/include/python3.12/object.h:215: Py_SIZE: Assertion `ob->ob_type != &PyLong_Type' failed.
Aborted (core dumped)

We're relying on an implementation detail to check whether the object is
negative. Instead, catch an overflow error, negate and try again.
Genuine overflows will still overflow on the second time, but negative
numbers will succeed.

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
  • Loading branch information
brenns10 committed Jun 1, 2023
1 parent 3f3a957 commit 57e49b0
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions libdrgn/python/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ static int DrgnObject_literal(struct drgn_object *res, PyObject *literal)
if (PyBool_Check(literal)) {
err = drgn_object_bool_literal(res, literal == Py_True);
} else if (PyLong_Check(literal)) {
bool is_negative = Py_SIZE(literal) < 0;
if (is_negative) {
bool is_negative = false;
uint64_t uvalue = PyLong_AsUint64(literal);

/* Assume an overflow is due to a negative number and retry */
if (uvalue == (uint64_t)-1 && PyErr_Occurred() &&
PyErr_ExceptionMatches(PyExc_OverflowError)) {
is_negative = true;
PyErr_Clear();
literal = PyNumber_Negative(literal);
if (!literal)
return -1;
}
uint64_t uvalue = PyLong_AsUint64(literal);
if (is_negative)
uvalue = PyLong_AsUint64(literal);
Py_DECREF(literal);
}
if (uvalue == (uint64_t)-1 && PyErr_Occurred())
return -1;
err = drgn_object_integer_literal(res, uvalue);
Expand Down

0 comments on commit 57e49b0

Please sign in to comment.