@@ -473,12 +473,41 @@ def ignore_test_float_subclass(self):
473
473
nb_add = "fp_add" ,
474
474
tp_new = "fp_tpnew" ,
475
475
post_ready_code = "testFloatSubclassPtr = &TestFloatSubclassType; Py_INCREF(testFloatSubclassPtr);"
476
- )
476
+ )
477
477
tester = TestFloatSubclass (41.0 )
478
478
res = tester + 1
479
479
assert res == 42.0 , "expected 42.0 but was %s" % res
480
480
assert hash (tester ) != 0
481
481
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
+
482
511
def test_custom_basicsize (self ):
483
512
TestCustomBasicsize = CPyExtType ("TestCustomBasicsize" ,
484
513
'''
0 commit comments