From 3bb09947ec4837de75532e21dd4bd25db0a1f1b7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 30 Apr 2021 12:46:15 +0200 Subject: bpo-43916: Add Py_TPFLAGS_DISALLOW_INSTANTIATION type flag (GH-25721) Add a new Py_TPFLAGS_DISALLOW_INSTANTIATION type flag to disallow creating type instances: set tp_new to NULL and don't create the "__new__" key in the type dictionary. The flag is set automatically on static types if tp_base is NULL or &PyBaseObject_Type and tp_new is NULL. Use the flag on the following types: * _curses.ncurses_version type * _curses_panel.panel * _tkinter.Tcl_Obj * _tkinter.tkapp * _tkinter.tktimertoken * _xxsubinterpretersmodule.ChannelID * sys.flags type * sys.getwindowsversion() type * sys.version_info type Update MyStr example in the C API documentation to use Py_TPFLAGS_DISALLOW_INSTANTIATION. Add _PyStructSequence_InitType() function to create a structseq type with the Py_TPFLAGS_DISALLOW_INSTANTIATION flag set. type_new() calls _PyType_CheckConsistency() at exit. --- Modules/_cursesmodule.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'Modules/_cursesmodule.c') diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index d221cf1a92..bcf9ad1938 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -4793,9 +4793,11 @@ PyInit__curses(void) #ifdef NCURSES_VERSION /* ncurses_version */ if (NcursesVersionType.tp_name == NULL) { - if (PyStructSequence_InitType2(&NcursesVersionType, - &ncurses_version_desc) < 0) + if (_PyStructSequence_InitType(&NcursesVersionType, + &ncurses_version_desc, + Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) { return NULL; + } } v = make_ncurses_version(); if (v == NULL) { @@ -4803,15 +4805,6 @@ PyInit__curses(void) } PyDict_SetItemString(d, "ncurses_version", v); Py_DECREF(v); - - /* prevent user from creating new instances */ - NcursesVersionType.tp_init = NULL; - NcursesVersionType.tp_new = NULL; - if (PyDict_DelItemString(NcursesVersionType.tp_dict, "__new__") < 0 && - PyErr_ExceptionMatches(PyExc_KeyError)) - { - PyErr_Clear(); - } #endif /* NCURSES_VERSION */ SetDictInt("ERR", ERR); -- cgit v1.2.1