summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
Diffstat (limited to 'Doc')
-rw-r--r--Doc/c-api/typeobj.rst28
-rw-r--r--Doc/whatsnew/3.10.rst4
2 files changed, 29 insertions, 3 deletions
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index 85f0262..5e731bd 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -1199,6 +1199,25 @@ and :c:type:`PyType_Type` effectively act as defaults.)
.. versionadded:: 3.10
+ .. data:: Py_TPFLAGS_DISALLOW_INSTANTIATION
+
+ Disallow creating instances of the type: set
+ :c:member:`~PyTypeObject.tp_new` to NULL and don't create the ``__new__``
+ key in the type dictionary.
+
+ The flag must be set before creating the type, not after. For example, it
+ must be set before :c:func:`PyType_Ready` is called on the type.
+
+ The flag is set automatically on :ref:`static types <static-types>` if
+ :c:member:`~PyTypeObject.tp_base` is NULL or ``&PyBaseObject_Type`` and
+ :c:member:`~PyTypeObject.tp_new` is NULL.
+
+ **Inheritance:**
+
+ This flag is not inherited.
+
+ .. versionadded:: 3.10
+
.. c:member:: const char* PyTypeObject.tp_doc
@@ -1761,6 +1780,9 @@ and :c:type:`PyType_Type` effectively act as defaults.)
in :c:member:`~PyTypeObject.tp_new`, while for mutable types, most initialization should be
deferred to :c:member:`~PyTypeObject.tp_init`.
+ Set the :const:`Py_TPFLAGS_DISALLOW_INSTANTIATION` flag to disallow creating
+ instances of the type in Python.
+
**Inheritance:**
This field is inherited by subtypes, except it is not inherited by
@@ -2596,7 +2618,8 @@ A type that supports weakrefs, instance dicts, and hashing::
};
A str subclass that cannot be subclassed and cannot be called
-to create instances (e.g. uses a separate factory func)::
+to create instances (e.g. uses a separate factory func) using
+:c:data:`Py_TPFLAGS_DISALLOW_INSTANTIATION` flag::
typedef struct {
PyUnicodeObject raw;
@@ -2609,8 +2632,7 @@ to create instances (e.g. uses a separate factory func)::
.tp_basicsize = sizeof(MyStr),
.tp_base = NULL, // set to &PyUnicode_Type in module init
.tp_doc = "my custom str",
- .tp_flags = Py_TPFLAGS_DEFAULT,
- .tp_new = NULL,
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_repr = (reprfunc)myobj_repr,
};
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index 2580a03..1a39066 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -1708,6 +1708,10 @@ New Features
These functions allow to activate, deactivate and query the state of the garbage collector from C code without
having to import the :mod:`gc` module.
+* Add a new :c:data:`Py_TPFLAGS_DISALLOW_INSTANTIATION` type flag to disallow
+ creating type instances.
+ (Contributed by Victor Stinner in :issue:`43916`.)
+
Porting to Python 3.10
----------------------