8000 bpo-34284: Null __new__ for _tkinter · python/cpython@2302b69 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2302b69

Browse files
committed
bpo-34284: Null __new__ for _tkinter
1 parent 96cefbf commit 2302b69

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest, test.support
2+
import _tkinter
3+
import re
4+
5+
class InternalsTest(unittest.TestCase):
6+
def assert_raise_on_new(self, tkinter_type):
7+
# Users are intentionally prevented from creating new instances of
8+
# _tkinter.TkappType, _tkinter.Tcl_Obj and _tkinter.TkttType
9+
tkinter_type_name = f'_tkinter.{tkinter_type.__name__}'
10+
11+
cant_create_message = re.escape(f"cannot create '{tkinter_type_name}' instances")
12+
object_message = re.escape(
13+
f'object.__new__({tkinter_type_name}) is not safe, '
14+
f'use {tkinter_type_name}.__new__()'
15+
)
16+
17+
with self.assertRaisesRegex(TypeError, cant_create_message):
18+
tkinter_type()
19+
with self.assertRaisesRegex(TypeError, cant_create_message):
20+
tkinter_type.__new__(tkinter_type)
21+
with self.assertRaisesRegex(TypeError, object_message):
22+
object.__new__(tkinter_type)
23+
24+
def test_tkapp_type_no_instantiation(self):
25+
self.assert_raise_on_new(_tkinter.TkappType)
26+
27+
def test_tck_obj_no_instantiation(self):
28+
self.assert_raise_on_new(_tkinter.Tcl_Obj)
29+
30+
def test_tktt_type_info_no_instantiation(self):
31+
self.assert_raise_on_new(_tkinter.TkttType)
32+
33+
34+
def test_main():
35+
test.support.run_unittest(InternalsTest,)
36+
37+
if __name__ == "__main__":
38+
test_main()

Modules/_tkinter.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ static PyGetSetDef PyTclObject_getsetlist[] = {
906906
};
907907

908908
static PyType_Slot PyTclObject_Type_slots[] = {
909+
{Py_tp_new, PyType_NullNew},
909910
{Py_tp_dealloc, (destructor)PyTclObject_dealloc},
910911
{Py_tp_repr, (reprfunc)PyTclObject_repr},
911912
{Py_tp_str, (reprfunc)PyTclObject_str},
@@ -3206,6 +3207,7 @@ static PyMethodDef Tktt_methods[] =
32063207
};
32073208

32083209
static PyType_Slot Tktt_Type_slots[] = {
3210+
{Py_tp_new, PyType_NullNew},
32093211
{Py_tp_dealloc, Tktt_Dealloc},
32103212
{Py_tp_repr, Tktt_Repr},
32113213
{Py_tp_methods, Tktt_methods},
@@ -3261,6 +3263,7 @@ static PyMethodDef Tkapp_methods[] =
32613263
};
32623264

32633265
static PyType_Slot Tkapp_Type_slots[] = {
3266+
{Py_tp_new, PyType_NullNew},
32643267
{Py_tp_dealloc, Tkapp_Dealloc},
32653268
{Py_tp_methods, Tkapp_methods},
32663269
{0, 0}
@@ -3459,7 +3462,6 @@ PyInit__tkinter(void)
34593462
Py_DECREF(m);
34603463
return NULL;
34613464
}
3462-
((PyTypeObject *)o)->tp_new = NULL;
34633465
if (PyModule_AddObject(m, "TkappType", o)) {
34643466
Py_DECREF(o);
34653467
Py_DECREF(m);
@@ -3472,7 +3474,6 @@ PyInit__tkinter(void)
34723474
Py_DECREF(m);
34733475
return NULL;
34743476
}
3475-
((PyTypeObject *)o)->tp_new = NULL;
34763477
if (PyModule_AddObject(m, "TkttType", o)) {
34773478
Py_DECREF(o);
34783479
Py_DECREF(m);
@@ -3485,7 +3486,6 @@ PyInit__tkinter(void)
34853486
Py_DECREF(m);
34863487
return NULL;
34873488
}
3488-
((PyTypeObject *)o)->tp_new = NULL;
34893489
if (PyModule_AddObject(m, "Tcl_Obj", o)) {
34903490
Py_DECREF(o);
34913491
Py_DECREF(m);

0 commit comments

Comments
 (0)
0