@@ -672,31 +672,60 @@ def test_heaptype_with_setattro(self):
672
672
self .assertEqual (obj .pvalue , 0 )
673
673
674
674
def test_heaptype_with_custom_metaclass (self ):
675
- self . assertTrue ( issubclass ( _testcapi .HeapCTypeMetaclass , type ))
676
- self .assertTrue (issubclass (_testcapi . HeapCTypeMetaclassCustomNew , type ))
675
+ metaclass = _testcapi .HeapCTypeMetaclass
676
+ self .assertTrue (issubclass (metaclass , type ))
677
677
678
- t = _testcapi .pytype_fromspec_meta (_testcapi .HeapCTypeMetaclass )
678
+ # Class creation from C
679
+ t = _testcapi .pytype_fromspec_meta (metaclass )
679
680
self .assertIsInstance (t , type )
680
681
self .assertEqual (t .__name__ , "HeapCTypeViaMetaclass" )
681
- self .assertIs (type (t ), _testcapi .HeapCTypeMetaclass )
682
+ self .assertIs (type (t ), metaclass )
683
+
684
+ # Class creation from Python
685
+ t = metaclass ("PyClassViaMetaclass" , (), {})
686
+ self .assertIsInstance (t , type )
687
+ self .assertEqual (t .__name__ , "PyClassViaMetaclass" )
688
+
689
+ def test_heaptype_with_custom_metaclass_null_new (self ):
690
+ metaclass = _testcapi .HeapCTypeMetaclassNullNew
691
+
692
+ self .assertTrue (issubclass (metaclass , type ))
693
+
694
+ # Class creation from C
695
+ t = _testcapi .pytype_fromspec_meta (metaclass )
696
+ self .assertIsInstance (t , type )
697
+ self .assertEqual (t .__name__ , "HeapCTypeViaMetaclass" )
698
+ self .assertIs (type (t ), metaclass )
699
+
700
+ # Class creation from Python
701
+ with self .assertRaisesRegex (TypeError , "cannot create .* instances" ):
702
+ metaclass ("PyClassViaMetaclass" , (), {})
703
+
704
+ def test_heaptype_with_custom_metaclass_custom_new (self ):
705
+ metaclass = _testcapi .HeapCTypeMetaclassCustomNew
706
+
707
+ self .assertTrue (issubclass (_testcapi .HeapCTypeMetaclassCustomNew , type ))
682
708
683
709
msg = "Metaclasses with custom tp_new are not supported."
684
710
with self .assertRaisesRegex (TypeError , msg ):
685
- t = _testcapi .pytype_fromspec_meta (_testcapi . HeapCTypeMetaclassCustomNew )
711
+ t = _testcapi .pytype_fromspec_meta (metaclass )
686
712
687
713
def test_heaptype_with_custom_metaclass_deprecation (self ):
714
+ metaclass = _testcapi .HeapCTypeMetaclassCustomNew
715
+
688
716
# gh-103968: a metaclass with custom tp_new is deprecated, but still
689
717
# allowed for functions that existed in 3.11
690
718
# (PyType_FromSpecWithBases is used here).
691
- class Base (metaclass = _testcapi . HeapCTypeMetaclassCustomNew ):
719
+ class Base (metaclass = metaclass ):
692
720
pass
693
721
722
+ # Class creation from C
694
723
with warnings_helper .check_warnings (
695
724
('.*custom tp_new.*in Python 3.14.*' , DeprecationWarning ),
696
725
):
697
726
sub = _testcapi .make_type_with_base (Base )
698
727
self .assertTrue (issubclass (sub , Base ))
699
- self .assertIsInstance (sub , _testcapi . HeapCTypeMetaclassCustomNew )
728
+ self .assertIsInstance (sub , metaclass )
700
729
701
730
def test_multiple_inheritance_ctypes_with_weakref_or_dict (self ):
702
731
0 commit comments