@@ -671,31 +671,60 @@ def test_heaptype_with_setattro(self):
671671 self .assertEqual (obj .pvalue , 0 )
672672
673673 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 ))
676676
677- t = _testcapi .pytype_fromspec_meta (_testcapi .HeapCTypeMetaclass )
677+ # Class creation from C
678+ t = _testcapi .pytype_fromspec_meta (metaclass )
678679 self .assertIsInstance (t , type )
679680 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 ))
681707
682708 msg = "Metaclasses with custom tp_new are not supported."
683709 with self .assertRaisesRegex (TypeError , msg ):
684- t = _testcapi .pytype_fromspec_meta (_testcapi . HeapCTypeMetaclassCustomNew )
710+ t = _testcapi .pytype_fromspec_meta (metaclass )
685711
686712 def test_heaptype_with_custom_metaclass_deprecation (self ):
713+ metaclass = _testcapi .HeapCTypeMetaclassCustomNew
714+
687715 # gh-103968: a metaclass with custom tp_new is deprecated, but still
688716 # allowed for functions that existed in 3.11
689717 # (PyType_FromSpecWithBases is used here).
690- class Base (metaclass = _testcapi . HeapCTypeMetaclassCustomNew ):
718+ class Base (metaclass = metaclass ):
691719 pass
692720
721+ # Class creation from C
693722 with warnings_helper .check_warnings (
694723 ('.*custom tp_new.*in Python 3.14.*' , DeprecationWarning ),
695724 ):
696725 sub = _testcapi .make_type_with_base (Base )
697726 self .assertTrue (issubclass (sub , Base ))
698- self .assertIsInstance (sub , _testcapi . HeapCTypeMetaclassCustomNew )
727+ self .assertIsInstance (sub , metaclass )
699728
700729 def test_multiple_inheritance_ctypes_with_weakref_or_dict (self ):
701730
0 commit comments