bpo-43916: Apply Py_TPFLAGS_DISALLOW_INSTANTIATION to selected types … · python/cpython@9746cda (original) (raw)

20 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -40,6 +40,12 @@ def test_bad_constructor(self):
40 40 self.assertRaises(TypeError, array.array, 'xx')
41 41 self.assertRaises(ValueError, array.array, 'x')
42 42
43 +@support.cpython_only
44 +def test_disallow_instantiation(self):
45 +# Ensure that the type disallows instantiation (bpo-43916)
46 +tp = type(iter(array.array('I')))
47 +self.assertRaises(TypeError, tp)
48 +
43 49 @support.cpython_only
44 50 def test_immutable(self):
45 51 # bpo-43908: check that array.array is immutable
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
6 6 import tempfile
7 7 import unittest
8 8
9 -from test.support import requires, verbose, SaveSignals
9 +from test.support import requires, verbose, SaveSignals, cpython_only
10 10 from test.support.import_helper import import_module
11 11
12 12 # Optionally test curses module. This currently requires that the
@@ -1046,8 +1046,10 @@ def __del__(self):
1046 1046 panel.set_userptr(A())
1047 1047 panel.set_userptr(None)
1048 1048
1049 +@cpython_only
1049 1050 @requires_curses_func('panel')
1050 -def test_new_curses_panel(self):
1051 +def test_disallow_instantiation(self):
1052 +# Ensure that the type disallows instantiation (bpo-43916)
1051 1053 w = curses.newwin(10, 10)
1052 1054 panel = curses.panel.new_panel(w)
1053 1055 self.assertRaises(TypeError, type(panel))
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1 1 from test import support
2 -from test.support import import_helper
2 +from test.support import import_helper, cpython_only
3 3 gdbm = import_helper.import_module("dbm.gnu") #skip if not supported
4 4 import unittest
5 5 import os
@@ -27,6 +27,13 @@ def tearDown(self):
27 27 self.g.close()
28 28 unlink(filename)
29 29
30 +@cpython_only
31 +def test_disallow_instantiation(self):
32 +# Ensure that the type disallows instantiation (bpo-43916)
33 +self.g = gdbm.open(filename, 'c')
34 +tp = type(self.g)
35 +self.assertRaises(TypeError, tp)
36 +
30 37 def test_key_methods(self):
31 38 self.g = gdbm.open(filename, 'c')
32 39 self.assertEqual(self.g.keys(), [])
Original file line number Diff line number Diff line change
@@ -948,6 +948,12 @@ class TestCmpToKeyC(TestCmpToKey, unittest.TestCase):
948 948 if c_functools:
949 949 cmp_to_key = c_functools.cmp_to_key
950 950
951 +@support.cpython_only
952 +def test_disallow_instantiation(self):
953 +# Ensure that the type disallows instantiation (bpo-43916)
954 +tp = type(c_functools.cmp_to_key(None))
955 +self.assertRaises(TypeError, tp)
956 +
951 957
952 958 class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase):
953 959 cmp_to_key = staticmethod(py_functools.cmp_to_key)
Original file line number Diff line number Diff line change
@@ -2215,6 +2215,15 @@ def test_signedness(self):
2215 2215 self.assertGreaterEqual(sre_compile.MAXREPEAT, 0)
2216 2216 self.assertGreaterEqual(sre_compile.MAXGROUPS, 0)
2217 2217
2218 +@cpython_only
2219 +def test_disallow_instantiation(self):
2220 +# Ensure that the type disallows instantiation (bpo-43916)
2221 +self.assertRaises(TypeError, re.Match)
2222 +self.assertRaises(TypeError, re.Pattern)
2223 +pat = re.compile("")
2224 +tp = type(pat.scanner(""))
2225 +self.assertRaises(TypeError, tp)
2226 +
2218 2227
2219 2228 class ExternalTests(unittest.TestCase):
2220 2229
Original file line number Diff line number Diff line change
@@ -736,8 +736,11 @@ def check(value):
736 736 check('{\n')
737 737 check('}\n')
738 738
739 +@support.cpython_only
739 740 def test_new_tcl_obj(self):
740 741 self.assertRaises(TypeError, _tkinter.Tcl_Obj)
742 +self.assertRaises(TypeError, _tkinter.TkttType)
743 +self.assertRaises(TypeError, _tkinter.TkappType)
741 744
742 745 class BigmemTclTest(unittest.TestCase):
743 746
Original file line number Diff line number Diff line change
@@ -119,6 +119,13 @@ def func(): pass
119 119 thread = threading.Thread(target=func)
120 120 self.assertEqual(thread.name, "Thread-5 (func)")
121 121
122 +@cpython_only
123 +def test_disallow_instantiation(self):
124 +# Ensure that the type disallows instantiation (bpo-43916)
125 +lock = threading.Lock()
126 +tp = type(lock)
127 +self.assertRaises(TypeError, tp)
128 +
122 129 # Create a bunch of threads, let each do some work, wait until all are
123 130 # done.
124 131 def test_various_ops(self):
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@
11 11 import sys
12 12 import unicodedata
13 13 import unittest
14 -from test.support import open_urlresource, requires_resource, script_helper
14 +from test.support import (open_urlresource, requires_resource, script_helper,
15 +cpython_only)
15 16
16 17
17 18 class UnicodeMethodsTest(unittest.TestCase):
@@ -225,6 +226,11 @@ def test_east_asian_width_9_0_changes(self):
225 226
226 227 class UnicodeMiscTest(UnicodeDatabaseTest):
227 228
229 +@cpython_only
230 +def test_disallow_instantiation(self):
231 +# Ensure that the type disallows instantiation (bpo-43916)
232 +self.assertRaises(TypeError, unicodedata.UCD)
233 +
228 234 def test_failed_import_during_compiling(self):
229 235 # Issue 4367
230 236 # Decoding \N escapes requires the unicodedata module. If it can't be
Original file line number Diff line number Diff line change
@@ -129,6 +129,14 @@ def test_overflow(self):
129 129 with self.assertRaisesRegex(OverflowError, 'int too large'):
130 130 zlib.decompressobj().flush(sys.maxsize + 1)
131 131
132 +@support.cpython_only
133 +def test_disallow_instantiation(self):
134 +# Ensure that the type disallows instantiation (bpo-43916)
135 +comp_type = type(zlib.compressobj())
136 +decomp_type = type(zlib.decompressobj())
137 +self.assertRaises(TypeError, comp_type)
138 +self.assertRaises(TypeError, decomp_type)
139 +
132 140
133 141 class BaseCompressTestCase(object):
134 142 def check_big_compress_buffer(self, size, compress_func):
Original file line number Diff line number Diff line change
@@ -414,7 +414,7 @@ static PyType_Spec dbmtype_spec = {
414 414 // dbmtype_spec does not have Py_TPFLAGS_BASETYPE flag
415 415 // which prevents to create a subclass.
416 416 // So calling PyType_GetModuleState() in this file is always safe.
417 - .flags = Py_TPFLAGS_DEFAULT,
417 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
418 418 .slots = dbmtype_spec_slots,
419 419 };
420 420
Original file line number Diff line number Diff line change
@@ -546,7 +546,7 @@ static PyType_Slot keyobject_type_slots[] = {
546 546 static PyType_Spec keyobject_type_spec = {
547 547 .name = "functools.KeyWrapper",
548 548 .basicsize = sizeof(keyobject),
549 - .flags = Py_TPFLAGS_DEFAULT,
549 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
550 550 .slots = keyobject_type_slots
551 551 };
552 552
@@ -766,7 +766,7 @@ static PyType_Slot lru_list_elem_type_slots[] = {
766 766 static PyType_Spec lru_list_elem_type_spec = {
767 767 .name = "functools._lru_list_elem",
768 768 .basicsize = sizeof(lru_list_elem),
769 - .flags = Py_TPFLAGS_DEFAULT,
769 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
770 770 .slots = lru_list_elem_type_slots
771 771 };
772 772
Original file line number Diff line number Diff line change
@@ -570,7 +570,7 @@ static PyType_Spec gdbmtype_spec = {
570 570 // dbmtype_spec does not have Py_TPFLAGS_BASETYPE flag
571 571 // which prevents to create a subclass.
572 572 // So calling PyType_GetModuleState() in this file is always safe.
573 - .flags = Py_TPFLAGS_DEFAULT,
573 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
574 574 .slots = gdbmtype_spec_slots,
575 575 };
576 576
Original file line number Diff line number Diff line change
@@ -2690,7 +2690,8 @@ static PyType_Spec pattern_spec = {
2690 2690 .name = "re.Pattern",
2691 2691 .basicsize = sizeof(PatternObject),
2692 2692 .itemsize = sizeof(SRE_CODE),
2693 - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
2693 + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE
2694 +Py_TPFLAGS_DISALLOW_INSTANTIATION),
2694 2695 .slots = pattern_slots,
2695 2696 };
2696 2697
@@ -2755,7 +2756,8 @@ static PyType_Spec match_spec = {
2755 2756 .name = "re.Match",
2756 2757 .basicsize = sizeof(MatchObject),
2757 2758 .itemsize = sizeof(Py_ssize_t),
2758 - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
2759 + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE
2760 +Py_TPFLAGS_DISALLOW_INSTANTIATION),
2759 2761 .slots = match_slots,
2760 2762 };
2761 2763
@@ -2781,7 +2783,8 @@ static PyType_Slot scanner_slots[] = {
2781 2783 static PyType_Spec scanner_spec = {
2782 2784 .name = "_" SRE_MODULE ".SRE_Scanner",
2783 2785 .basicsize = sizeof(ScannerObject),
2784 - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
2786 + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE
2787 +Py_TPFLAGS_DISALLOW_INSTANTIATION),
2785 2788 .slots = scanner_slots,
2786 2789 };
2787 2790
Original file line number Diff line number Diff line change
@@ -312,7 +312,8 @@ static PyType_Slot lock_type_slots[] = {
312 312 static PyType_Spec lock_type_spec = {
313 313 .name = "_thread.lock",
314 314 .basicsize = sizeof(lockobject),
315 - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
315 + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
316 +Py_TPFLAGS_DISALLOW_INSTANTIATION),
316 317 .slots = lock_type_slots,
317 318 };
318 319
@@ -683,7 +684,7 @@ static PyType_Slot local_dummy_type_slots[] = {
683 684 static PyType_Spec local_dummy_type_spec = {
684 685 .name = "_thread._localdummy",
685 686 .basicsize = sizeof(localdummyobject),
686 - .flags = Py_TPFLAGS_DEFAULT,
687 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
687 688 .slots = local_dummy_type_slots,
688 689 };
689 690
Original file line number Diff line number Diff line change
@@ -331,7 +331,7 @@ static PyType_Slot winapi_overlapped_type_slots[] = {
331 331 static PyType_Spec winapi_overlapped_type_spec = {
332 332 .name = "_winapi.Overlapped",
333 333 .basicsize = sizeof(OverlappedObject),
334 - .flags = Py_TPFLAGS_DEFAULT,
334 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
335 335 .slots = winapi_overlapped_type_slots,
336 336 };
337 337
Original file line number Diff line number Diff line change
@@ -2987,7 +2987,8 @@ static PyType_Slot arrayiter_slots[] = {
2987 2987 static PyType_Spec arrayiter_spec = {
2988 2988 .name = "array.arrayiterator",
2989 2989 .basicsize = sizeof(arrayiterobject),
2990 - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
2990 + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
2991 +Py_TPFLAGS_DISALLOW_INSTANTIATION),
2991 2992 .slots = arrayiter_slots,
2992 2993 };
2993 2994
Original file line number Diff line number Diff line change
@@ -748,7 +748,8 @@ static PyType_Slot multibytecodec_slots[] = {
748 748 static PyType_Spec multibytecodec_spec = {
749 749 .name = MODULE_NAME ".MultibyteCodec",
750 750 .basicsize = sizeof(MultibyteCodecObject),
751 - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
751 + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
752 +Py_TPFLAGS_DISALLOW_INSTANTIATION),
752 753 .slots = multibytecodec_slots,
753 754 };
754 755
Original file line number Diff line number Diff line change
@@ -1506,7 +1506,8 @@ static PyType_Slot _xml_parse_type_spec_slots[] = {
1506 1506 static PyType_Spec _xml_parse_type_spec = {
1507 1507 .name = "pyexpat.xmlparser",
1508 1508 .basicsize = sizeof(xmlparseobject),
1509 - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
1509 + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
1510 +Py_TPFLAGS_DISALLOW_INSTANTIATION),
1510 1511 .slots = _xml_parse_type_spec_slots,
1511 1512 };
1512 1513
Original file line number Diff line number Diff line change
@@ -1454,7 +1454,7 @@ static PyType_Slot ucd_type_slots[] = {
1454 1454 static PyType_Spec ucd_type_spec = {
1455 1455 .name = "unicodedata.UCD",
1456 1456 .basicsize = sizeof(PreviousDBVersion),
1457 - .flags = Py_TPFLAGS_DEFAULT,
1457 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1458 1458 .slots = ucd_type_slots
1459 1459 };
1460 1460
Original file line number Diff line number Diff line change
@@ -1386,11 +1386,10 @@ static PyType_Slot Comptype_slots[] = {
1386 1386 };
1387 1387
1388 1388 static PyType_Spec Comptype_spec = {
1389 -"zlib.Compress",
1390 -sizeof(compobject),
1391 -0,
1392 -Py_TPFLAGS_DEFAULT,
1393 -Comptype_slots
1389 + .name = "zlib.Compress",
1390 + .basicsize = sizeof(compobject),
1391 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1392 + .slots= Comptype_slots,
1394 1393 };
1395 1394
1396 1395 static PyType_Slot Decomptype_slots[] = {
@@ -1401,11 +1400,10 @@ static PyType_Slot Decomptype_slots[] = {
1401 1400 };
1402 1401
1403 1402 static PyType_Spec Decomptype_spec = {
1404 -"zlib.Decompress",
1405 -sizeof(compobject),
1406 -0,
1407 -Py_TPFLAGS_DEFAULT,
1408 -Decomptype_slots
1403 + .name = "zlib.Decompress",
1404 + .basicsize = sizeof(compobject),
1405 + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1406 + .slots = Decomptype_slots,
1409 1407 };
1410 1408
1411 1409 PyDoc_STRVAR(zlib_module_documentation,