Skip to content

Commit 81c1168

Browse files
committed
add test for Cython pattern of casting void* to long and using Python comparison
1 parent 805bfb5 commit 81c1168

File tree

1 file changed

+24
-0
lines changed
  • graalpython/com.oracle.graal.python.test/src/tests/cpyext

1 file changed

+24
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_long.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,30 @@ def compile_module(self, name):
271271
cmpfunc=unhandled_error_compare
272272
)
273273

274+
# We get a pattern like this in Cython generated code
275+
test_PyLong_FromAndToVoidPtrAllocated = CPyExtFunction(
276+
lambda args: True,
277+
lambda: ((None,),),
278+
code="""PyObject* PyLong_FromAndToVoidPtrAllocated(PyObject* none) {
279+
void* dummyPtr = malloc(sizeof(size_t));
280+
PyObject* obj = PyLong_FromVoidPtr(dummyPtr);
281+
int r = PyObject_RichCompareBool(obj, Py_False, Py_LT);
282+
if (r < 0) {
283+
return Py_None;
284+
}
285+
unsigned long l = PyLong_AsUnsignedLong(obj);
286+
void* unwrappedPtr = (void*)l;
287+
PyObject* result = unwrappedPtr == dummyPtr ? Py_True : Py_False;
288+
free(dummyPtr);
289+
return result;
290+
}
291+
""",
292+
resultspec="O",
293+
argspec='O',
294+
arguments=["PyObject* none"],
295+
cmpfunc=unhandled_error_compare
296+
)
297+
274298
test_PyLong_Check = CPyExtFunction(
275299
lambda args: isinstance(args[0], int),
276300
lambda: (

0 commit comments

Comments
 (0)