Skip to content

Commit ce76a4d

Browse files
fangerertimfel
authored andcommitted
Add test for subclass of native float subclass
1 parent 7735acb commit ce76a4d

File tree

1 file changed

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

1 file changed

+30
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,41 @@ def ignore_test_float_subclass(self):
473473
nb_add="fp_add",
474474
tp_new="fp_tpnew",
475475
post_ready_code="testFloatSubclassPtr = &TestFloatSubclassType; Py_INCREF(testFloatSubclassPtr);"
476-
)
476+
)
477477
tester = TestFloatSubclass(41.0)
478478
res = tester + 1
479479
assert res == 42.0, "expected 42.0 but was %s" % res
480480
assert hash(tester) != 0
481481

482+
def test_float_subclass2(self):
483+
NativeFloatSubclass = CPyExtType(
484+
"NativeFloatSubclass",
485+
"""
486+
static PyObject* fp_tp_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
487+
PyObject *result = PyFloat_Type.tp_new(type, args, kwds);
488+
NativeFloatSubclassObject *nfs = (NativeFloatSubclassObject *)result;
489+
nfs->myobval = PyFloat_AsDouble(result);
490+
return result;
491+
}
492+
493+
static PyObject* fp_tp_repr(PyObject* self) {
494+
NativeFloatSubclassObject *nfs = (NativeFloatSubclassObject *)self;
495+
return PyUnicode_FromFormat("native %S", PyFloat_FromDouble(nfs->myobval));
496+
}
497+
""",
498+
struct_base="PyFloatObject base",
499+
cmembers="double myobval;",
500+
tp_base="&PyFloat_Type",
501+
tp_new="fp_tp_new",
502+
tp_repr="fp_tp_repr"
503+
)
504+
class MyFloat(NativeFloatSubclass):
505+
pass
506+
assert MyFloat() == 0.0
507+
assert MyFloat(123.0) == 123.0
508+
assert repr(MyFloat()) == "native 0.0"
509+
assert repr(MyFloat(123.0)) == "native 123.0"
510+
482511
def test_custom_basicsize(self):
483512
TestCustomBasicsize = CPyExtType("TestCustomBasicsize",
484513
'''

0 commit comments

Comments
 (0)