Skip to content

Commit bb4f9c7

Browse files
committed
Add C extension test 'TestFloatSubclass'.
1 parent 976c249 commit bb4f9c7

File tree

1 file changed

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

1 file changed

+47
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,53 @@ def test_slots_initialized(self):
218218
tester = TestSlotsInitialized(2012, 4, 4)
219219
assert tester.year == 2012, "year was %s "% tester.year
220220

221+
def test_float_subclass(self):
222+
TestFloatSubclass = CPyExtType("TestFloatSubclass",
223+
"""
224+
static PyTypeObject* testFloatSubclassPtr = NULL;
225+
226+
static PyObject* new_fp(double val) {
227+
PyFloatObject* fp = PyObject_New(PyFloatObject, testFloatSubclassPtr);
228+
fp->ob_fval = val;
229+
Py_XINCREF(fp);
230+
return (PyObject*)fp;
231+
}
232+
233+
static PyObject* fp_tpnew(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
234+
double dval = 0.0;
235+
if (!PyArg_ParseTuple(args, "d", &dval)) {{
236+
return NULL;
237+
}}
238+
return new_fp(dval);
239+
}
240+
241+
static PyObject* fp_add(PyObject* l, PyObject* r) {
242+
Py_XINCREF(l);
243+
Py_XINCREF(r);
244+
if (PyFloat_Check(l)) {
245+
if (PyFloat_Check(r)) {
246+
return new_fp(PyFloat_AS_DOUBLE(l) + PyFloat_AS_DOUBLE(r));
247+
} else if (PyLong_Check(r)) {
248+
return new_fp(PyFloat_AS_DOUBLE(l) + PyLong_AsLong(r));
249+
}
250+
} else if (PyLong_Check(l)) {
251+
if (PyFloat_Check(r)) {
252+
return new_fp(PyLong_AsLong(l) + PyFloat_AS_DOUBLE(r));
253+
} else if (PyLong_Check(r)) {
254+
return new_fp(PyLong_AsLong(l) + PyLong_AsLong(r));
255+
}
256+
}
257+
return Py_NotImplemented;
258+
}
259+
""",
260+
tp_base="&PyFloat_Type",
261+
nb_add="fp_add",
262+
tp_new="fp_tpnew",
263+
post_ready_code="testFloatSubclassPtr = &TestFloatSubclassType;"
264+
)
265+
tester = TestFloatSubclass(41.0)
266+
res = tester + 1
267+
assert res == 42.0, "expected 42.0 but was %s" % res
221268

222269

223270
class TestObjectFunctions(CPyExtTestCase):

0 commit comments

Comments
 (0)