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