diff options
author | Victor Stinner <vstinner@python.org> | 2021-04-30 12:46:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-30 12:46:15 +0200 |
commit | 3bb09947ec4837de75532e21dd4bd25db0a1f1b7 (patch) | |
tree | 46469716b18eda1c8af7c311b6187fda39a146c1 /Objects/structseq.c | |
parent | b73b5fb9ea08156991a065c1696e8d8cf7622482 (diff) | |
download | cpython-git-3bb09947ec4837de75532e21dd4bd25db0a1f1b7.tar.gz |
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.
Diffstat (limited to 'Objects/structseq.c')
-rw-r--r-- | Objects/structseq.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/Objects/structseq.c b/Objects/structseq.c index 88e63b658a..bf59f47fcf 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -459,8 +459,10 @@ initialize_members(PyStructSequence_Desc *desc, PyMemberDef* members, members[k].name = NULL; } + int -PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) +_PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc, + unsigned long tp_flags) { PyMemberDef *members; Py_ssize_t n_members, n_unnamed_members; @@ -488,7 +490,7 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) type->tp_base = &PyTuple_Type; type->tp_methods = structseq_methods; type->tp_new = structseq_new; - type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC; + type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | tp_flags; type->tp_traverse = (traverseproc) structseq_traverse; n_members = count_members(desc, &n_unnamed_members); @@ -516,6 +518,12 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) return 0; } +int +PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) +{ + return _PyStructSequence_InitType(type, desc, 0); +} + void PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) { |