From e7c0b46e31b827b8560f55f30f3a12efe2ff97ec Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 15 Nov 2026 15:16:41 +0100 Subject: [PATCH 1/5] gh-162047: Add PyFrozenDict_New() to the limited C API Add PEP 814 frozendict functions to the limited C API: * PyAnyDict_Check() * PyAnyDict_CheckExact() * PyFrozenDict_Check() * PyFrozenDict_CheckExact() * PyFrozenDict_New() Move test wrappers from _testcapi to _testlimitedcapi. --- Doc/whatsnew/3.16.rst | 10 +++++ Include/cpython/dictobject.h | 13 ------ Include/dictobject.h | 16 +++++++ Lib/test/test_capi/test_dict.py | 10 ++--- ...-11-15-15-30-40.gh-issue-162047.Zl2chW.rst | 9 ++++ Misc/stable_abi.toml | 10 +++++ Modules/_testcapi/dict.c | 42 ----------------- Modules/_testlimitedcapi/dict.c | 45 +++++++++++++++++++ 8 files changed, 95 insertions(+), 60 deletions(-) create mode 100644 Misc/NEWS.d/next/C_API/2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index 33328b2ee005219..99cd2936b6bc032 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -1574,6 +1574,16 @@ New features * Add the :ref:`PyUnicodeWriter API ` to the limited C API. (Contributed by Victor Stinner in :gh:`161172`.) +* Add :pep:`814` :class:`frozendict` functions to the limited C API: + + * :c:func:`PyAnyDict_CheckExact` + * :c:func:`PyAnyDict_Check` + * :c:func:`PyFrozenDict_CheckExact` + * :c:func:`PyFrozenDict_Check` + * :c:func:`PyFrozenDict_New` + + (Contributed by Victor Stinner in :gh:`162047`.) + * Add :c:func:`PyUnstable_InterpreterFrame_GetLocal` to read a local variable of an internal interpreter frame by its localsplus index, and :c:func:`PyUnstable_Code_GetLocalPlusNames` to get the matching variable diff --git a/Include/cpython/dictobject.h b/Include/cpython/dictobject.h index 9f05890852f5e4c..b8fe2e75faa37cb 100644 --- a/Include/cpython/dictobject.h +++ b/Include/cpython/dictobject.h @@ -32,16 +32,6 @@ typedef struct { PyDictValues *ma_values; } PyDictObject; -// frozendict -PyAPI_DATA(PyTypeObject) PyFrozenDict_Type; -#define PyFrozenDict_Check(op) PyObject_TypeCheck((op), &PyFrozenDict_Type) -#define PyFrozenDict_CheckExact(op) Py_IS_TYPE((op), &PyFrozenDict_Type) - -#define PyAnyDict_CheckExact(ob) \ - (PyDict_CheckExact(ob) || PyFrozenDict_CheckExact(ob)) -#define PyAnyDict_Check(ob) \ - (PyDict_Check(ob) || PyFrozenDict_Check(ob)) - PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key, Py_hash_t hash); // PyDict_GetItemStringRef() can be used instead @@ -104,9 +94,6 @@ PyAPI_FUNC(int) PyDict_ClearWatcher(int watcher_id); PyAPI_FUNC(int) PyDict_Watch(int watcher_id, PyObject* dict); PyAPI_FUNC(int) PyDict_Unwatch(int watcher_id, PyObject* dict); -// Create a frozendict. Create an empty dictionary if iterable is NULL. -PyAPI_FUNC(PyObject*) PyFrozenDict_New(PyObject *iterable); - // Create a frozendict from existing dict. // Transfers existing keys and values. PyAPI_FUNC(PyObject*) PyDict_AsFrozenDictAndClear(PyObject *dict); diff --git a/Include/dictobject.h b/Include/dictobject.h index 0384e3131dcdb52..f345dc3cea6f249 100644 --- a/Include/dictobject.h +++ b/Include/dictobject.h @@ -107,6 +107,22 @@ PyAPI_DATA(PyTypeObject) PyDictRevIterKey_Type; PyAPI_DATA(PyTypeObject) PyDictRevIterItem_Type; PyAPI_DATA(PyTypeObject) PyDictRevIterValue_Type; +/* Frozendict */ + +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 16) +PyAPI_DATA(PyTypeObject) PyFrozenDict_Type; +#define PyFrozenDict_Check(op) PyObject_TypeCheck((op), &PyFrozenDict_Type) +#define PyFrozenDict_CheckExact(op) Py_IS_TYPE((op), &PyFrozenDict_Type) + +#define PyAnyDict_CheckExact(ob) \ + (PyDict_CheckExact(ob) || PyFrozenDict_CheckExact(ob)) +#define PyAnyDict_Check(ob) \ + (PyDict_Check(ob) || PyFrozenDict_Check(ob)) + +// Create a frozendict. Create an empty dictionary if iterable is NULL. +PyAPI_FUNC(PyObject*) PyFrozenDict_New(PyObject *iterable); +#endif // Py_LIMITED_API + #ifndef Py_LIMITED_API # define Py_CPYTHON_DICTOBJECT_H diff --git a/Lib/test/test_capi/test_dict.py b/Lib/test/test_capi/test_dict.py index 9a2e79d4da7e715..7ea63c154d91686 100644 --- a/Lib/test/test_capi/test_dict.py +++ b/Lib/test/test_capi/test_dict.py @@ -642,7 +642,7 @@ class DictSubtype(dict): ... def test_frozendict_check(self): # Test PyFrozenDict_Check() - check = _testcapi.frozendict_check + check = _testlimitedcapi.frozendict_check for dict_type in FROZENDICT_TYPES: self.assertTrue(check(dict_type(x=1))) for dict_type in NOT_FROZENDICT_TYPES + OTHER_TYPES: @@ -651,7 +651,7 @@ def test_frozendict_check(self): def test_frozendict_checkexact(self): # Test PyFrozenDict_CheckExact() - check = _testcapi.frozendict_checkexact + check = _testlimitedcapi.frozendict_checkexact for dict_type in FROZENDICT_TYPES: self.assertEqual(check(dict_type(x=1)), dict_type == frozendict) for dict_type in NOT_FROZENDICT_TYPES + OTHER_TYPES: @@ -660,7 +660,7 @@ def test_frozendict_checkexact(self): def test_anydict_check(self): # Test PyAnyDict_Check() - check = _testcapi.anydict_check + check = _testlimitedcapi.anydict_check for dict_type in ANYDICT_TYPES: self.assertTrue(check(dict_type({1: 2}))) for test_type in NOT_ANYDICT_TYPES + OTHER_TYPES: @@ -669,7 +669,7 @@ def test_anydict_check(self): def test_anydict_checkexact(self): # Test PyAnyDict_CheckExact() - check = _testcapi.anydict_checkexact + check = _testlimitedcapi.anydict_checkexact for dict_type in ANYDICT_TYPES: self.assertEqual(check(dict_type(x=1)), dict_type in (dict, frozendict)) @@ -679,7 +679,7 @@ def test_anydict_checkexact(self): def test_frozendict_new(self): # Test PyFrozenDict_New() - frozendict_new = _testcapi.frozendict_new + frozendict_new = _testlimitedcapi.frozendict_new for dict_type in ANYDICT_TYPES: dct = frozendict_new(dict_type({'x': 1})) diff --git a/Misc/NEWS.d/next/C_API/2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst b/Misc/NEWS.d/next/C_API/2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst new file mode 100644 index 000000000000000..a8809db7134b34c --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst @@ -0,0 +1,9 @@ +Add :pep:`814` :class:`frozendict` functions to the limited C API: + +* :c:func:`PyAnyDict_CheckExact` +* :c:func:`PyAnyDict_Check` +* :c:func:`PyFrozenDict_CheckExact` +* :c:func:`PyFrozenDict_Check` +* :c:func:`PyFrozenDict_New` + +Patch by Victor Stinner. diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml index dc892fc5f737eee..b7c15d822694f37 100644 --- a/Misc/stable_abi.toml +++ b/Misc/stable_abi.toml @@ -2941,3 +2941,13 @@ added = '3.16' [function.PyUnicodeWriter_DecodeUTF8Stateful] added = '3.16' +[macro.PyFrozenDict_Check] + added = '3.16' +[macro.PyFrozenDict_CheckExact] + added = '3.16' +[macro.PyAnyDict_CheckExact] + added = '3.16' +[macro.PyAnyDict_Check] + added = '3.16' +[function.PyFrozenDict_New] + added = '3.16' diff --git a/Modules/_testcapi/dict.c b/Modules/_testcapi/dict.c index 5e169d99c179d57..87cf9772dfc72d5 100644 --- a/Modules/_testcapi/dict.c +++ b/Modules/_testcapi/dict.c @@ -265,43 +265,6 @@ dict_as_frozendict_and_clear(PyObject *self, PyObject *obj) } -static PyObject * -frozendict_check(PyObject *self, PyObject *obj) -{ - NULLABLE(obj); - return PyLong_FromLong(PyFrozenDict_Check(obj)); -} - -static PyObject * -frozendict_checkexact(PyObject *self, PyObject *obj) -{ - NULLABLE(obj); - return PyLong_FromLong(PyFrozenDict_CheckExact(obj)); -} - -static PyObject * -anydict_check(PyObject *self, PyObject *obj) -{ - NULLABLE(obj); - return PyLong_FromLong(PyAnyDict_Check(obj)); -} - -static PyObject * -anydict_checkexact(PyObject *self, PyObject *obj) -{ - NULLABLE(obj); - return PyLong_FromLong(PyAnyDict_CheckExact(obj)); -} - - -static PyObject * -frozendict_new(PyObject *self, PyObject *obj) -{ - NULLABLE(obj); - return PyFrozenDict_New(obj); -} - - static PyMethodDef test_methods[] = { {"dict_containsstring", dict_containsstring, METH_VARARGS}, {"dict_getitemref", dict_getitemref, METH_VARARGS}, @@ -314,11 +277,6 @@ static PyMethodDef test_methods[] = { {"dict_popstring_null", dict_popstring_null, METH_VARARGS}, {"test_dict_iteration", test_dict_iteration, METH_NOARGS}, {"dict_as_frozendict_and_clear", dict_as_frozendict_and_clear, METH_O}, - {"frozendict_check", frozendict_check, METH_O}, - {"frozendict_checkexact", frozendict_checkexact, METH_O}, - {"anydict_check", anydict_check, METH_O}, - {"anydict_checkexact", anydict_checkexact, METH_O}, - {"frozendict_new", frozendict_new, METH_O}, {NULL}, }; diff --git a/Modules/_testlimitedcapi/dict.c b/Modules/_testlimitedcapi/dict.c index b9acda00897ad45..49c9a590d2803f4 100644 --- a/Modules/_testlimitedcapi/dict.c +++ b/Modules/_testlimitedcapi/dict.c @@ -1,3 +1,6 @@ +// Need limited C API version 3.16 for PyFrozenDict_New() +#define Py_LIMITED_API 0x03100000 + #include "parts.h" #include "util.h" @@ -255,6 +258,43 @@ dict_mergefromseq2(PyObject *self, PyObject *args) } +static PyObject * +frozendict_check(PyObject *self, PyObject *obj) +{ + NULLABLE(obj); + return PyLong_FromLong(PyFrozenDict_Check(obj)); +} + +static PyObject * +frozendict_checkexact(PyObject *self, PyObject *obj) +{ + NULLABLE(obj); + return PyLong_FromLong(PyFrozenDict_CheckExact(obj)); +} + +static PyObject * +anydict_check(PyObject *self, PyObject *obj) +{ + NULLABLE(obj); + return PyLong_FromLong(PyAnyDict_Check(obj)); +} + +static PyObject * +anydict_checkexact(PyObject *self, PyObject *obj) +{ + NULLABLE(obj); + return PyLong_FromLong(PyAnyDict_CheckExact(obj)); +} + + +static PyObject * +frozendict_new(PyObject *self, PyObject *obj) +{ + NULLABLE(obj); + return PyFrozenDict_New(obj); +} + + static PyMethodDef test_methods[] = { {"dict_check", dict_check, METH_O}, {"dict_checkexact", dict_checkexact, METH_O}, @@ -278,6 +318,11 @@ static PyMethodDef test_methods[] = { {"dict_merge", dict_merge, METH_VARARGS}, {"dict_update", dict_update, METH_VARARGS}, {"dict_mergefromseq2", dict_mergefromseq2, METH_VARARGS}, + {"frozendict_check", frozendict_check, METH_O}, + {"frozendict_checkexact", frozendict_checkexact, METH_O}, + {"anydict_check", anydict_check, METH_O}, + {"anydict_checkexact", anydict_checkexact, METH_O}, + {"frozendict_new", frozendict_new, METH_O}, {NULL}, }; From 81c3552d2ee953e977eba76ae4d1014b4596a116 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 16 Nov 2026 18:06:54 +0100 Subject: [PATCH 2/5] Run make regen-limited-abi --- Doc/data/stable_abi.dat | 5 +++++ Lib/test/test_stable_abi_ctypes.py | 1 + PC/python3dll.c | 1 + 3 files changed, 7 insertions(+) diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat index 5f424391519d591..a516de316db773e 100644 --- a/Doc/data/stable_abi.dat +++ b/Doc/data/stable_abi.dat @@ -18,6 +18,8 @@ const,PyABIInfo_GIL,3.15,, const,PyABIInfo_STABLE,3.15,, macro,PyABIInfo_VAR,3.15,, function,PyAIter_Check,3.10,, +macro,PyAnyDict_Check,3.16,, +macro,PyAnyDict_CheckExact,3.16,, function,PyArg_Parse,3.2,, function,PyArg_ParseTuple,3.2,, function,PyArg_ParseTupleAndKeywords,3.2,, @@ -335,6 +337,9 @@ data,PyFloat_Type,3.2,, struct,PyFrameObject,3.2,,opaque function,PyFrame_GetCode,3.10,, function,PyFrame_GetLineNumber,3.10,, +macro,PyFrozenDict_Check,3.16,, +macro,PyFrozenDict_CheckExact,3.16,, +function,PyFrozenDict_New,3.16,, function,PyFrozenSet_New,3.2,, data,PyFrozenSet_Type,3.2,, function,PyGC_Collect,3.2,, diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py index f9125f395de0b4b..828542f49323b40 100644 --- a/Lib/test/test_stable_abi_ctypes.py +++ b/Lib/test/test_stable_abi_ctypes.py @@ -337,6 +337,7 @@ def test_windows_feature_macros(self): "PyFloat_Type", "PyFrame_GetCode", "PyFrame_GetLineNumber", + "PyFrozenDict_New", "PyFrozenSet_New", "PyFrozenSet_Type", "PyGC_Collect", diff --git a/PC/python3dll.c b/PC/python3dll.c index 412dd5ce66cbc30..adada3693f3cd52 100755 --- a/PC/python3dll.c +++ b/PC/python3dll.c @@ -301,6 +301,7 @@ EXPORT_FUNC(PyFloat_GetMax) EXPORT_FUNC(PyFloat_GetMin) EXPORT_FUNC(PyFrame_GetCode) EXPORT_FUNC(PyFrame_GetLineNumber) +EXPORT_FUNC(PyFrozenDict_New) EXPORT_FUNC(PyFrozenSet_New) EXPORT_FUNC(PyGC_Collect) EXPORT_FUNC(PyGC_Disable) From d48a9938ae551f524bb69b94416465809a2de996 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 16 Nov 2026 22:20:50 +0100 Subject: [PATCH 3/5] stable_abi.toml: add PyFrozenDict_Type --- Doc/data/stable_abi.dat | 1 + Lib/test/test_stable_abi_ctypes.py | 1 + Misc/stable_abi.toml | 2 ++ PC/python3dll.c | 1 + 4 files changed, 5 insertions(+) diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat index a516de316db773e..e17326b7e7a7ae4 100644 --- a/Doc/data/stable_abi.dat +++ b/Doc/data/stable_abi.dat @@ -340,6 +340,7 @@ function,PyFrame_GetLineNumber,3.10,, macro,PyFrozenDict_Check,3.16,, macro,PyFrozenDict_CheckExact,3.16,, function,PyFrozenDict_New,3.16,, +data,PyFrozenDict_Type,3.16,, function,PyFrozenSet_New,3.2,, data,PyFrozenSet_Type,3.2,, function,PyGC_Collect,3.2,, diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py index 828542f49323b40..0c83b787ff5d0ae 100644 --- a/Lib/test/test_stable_abi_ctypes.py +++ b/Lib/test/test_stable_abi_ctypes.py @@ -338,6 +338,7 @@ def test_windows_feature_macros(self): "PyFrame_GetCode", "PyFrame_GetLineNumber", "PyFrozenDict_New", + "PyFrozenDict_Type", "PyFrozenSet_New", "PyFrozenSet_Type", "PyGC_Collect", diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml index b7c15d822694f37..04e7a83a5b749f4 100644 --- a/Misc/stable_abi.toml +++ b/Misc/stable_abi.toml @@ -2951,3 +2951,5 @@ added = '3.16' [function.PyFrozenDict_New] added = '3.16' +[data.PyFrozenDict_Type] + added = '3.16' diff --git a/PC/python3dll.c b/PC/python3dll.c index adada3693f3cd52..fc617f3d4fbe6f4 100755 --- a/PC/python3dll.c +++ b/PC/python3dll.c @@ -960,6 +960,7 @@ EXPORT_DATA(PyExc_WindowsError) EXPORT_DATA(PyExc_ZeroDivisionError) EXPORT_DATA(PyFilter_Type) EXPORT_DATA(PyFloat_Type) +EXPORT_DATA(PyFrozenDict_Type) EXPORT_DATA(PyFrozenSet_Type) EXPORT_DATA(PyGetSetDescr_Type) EXPORT_DATA(PyList_Type) From d34dc4b67a0df488756b73c513d799a72661326c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 18 Nov 2026 12:33:42 +0100 Subject: [PATCH 4/5] Add also PyDict_AsFrozenDictAndClear() --- Doc/whatsnew/3.16.rst | 1 + Include/cpython/dictobject.h | 4 ---- Include/dictobject.h | 4 ++++ Lib/test/test_capi/test_dict.py | 2 +- ...2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst | 1 + Modules/_testcapi/dict.c | 8 -------- Modules/_testlimitedcapi/dict.c | 15 +++++++++++++++ 7 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index 99cd2936b6bc032..4af2402f651d377 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -1578,6 +1578,7 @@ New features * :c:func:`PyAnyDict_CheckExact` * :c:func:`PyAnyDict_Check` + * :c:func:`PyDict_AsFrozenDictAndClear` * :c:func:`PyFrozenDict_CheckExact` * :c:func:`PyFrozenDict_Check` * :c:func:`PyFrozenDict_New` diff --git a/Include/cpython/dictobject.h b/Include/cpython/dictobject.h index b8fe2e75faa37cb..89b97588ae73084 100644 --- a/Include/cpython/dictobject.h +++ b/Include/cpython/dictobject.h @@ -93,7 +93,3 @@ PyAPI_FUNC(int) PyDict_ClearWatcher(int watcher_id); // Mark given dictionary as "watched" (callback will be called if it is modified) PyAPI_FUNC(int) PyDict_Watch(int watcher_id, PyObject* dict); PyAPI_FUNC(int) PyDict_Unwatch(int watcher_id, PyObject* dict); - -// Create a frozendict from existing dict. -// Transfers existing keys and values. -PyAPI_FUNC(PyObject*) PyDict_AsFrozenDictAndClear(PyObject *dict); diff --git a/Include/dictobject.h b/Include/dictobject.h index f345dc3cea6f249..c1a9b0bf789d082 100644 --- a/Include/dictobject.h +++ b/Include/dictobject.h @@ -121,6 +121,10 @@ PyAPI_DATA(PyTypeObject) PyFrozenDict_Type; // Create a frozendict. Create an empty dictionary if iterable is NULL. PyAPI_FUNC(PyObject*) PyFrozenDict_New(PyObject *iterable); + +// Create a frozendict from existing dict. +// Transfers existing keys and values. +PyAPI_FUNC(PyObject*) PyDict_AsFrozenDictAndClear(PyObject *dict); #endif // Py_LIMITED_API diff --git a/Lib/test/test_capi/test_dict.py b/Lib/test/test_capi/test_dict.py index 7ea63c154d91686..2c8d6f6841eaa2f 100644 --- a/Lib/test/test_capi/test_dict.py +++ b/Lib/test/test_capi/test_dict.py @@ -611,7 +611,7 @@ def test_dict_popstring(self): def test_dict_as_frozendict_and_clear(self): # Test PyDict_AsFrozenDictAndClear() - as_frozendict = _testcapi.dict_as_frozendict_and_clear + as_frozendict = _testlimitedcapi.dict_as_frozendict_and_clear d = dict(x=1, y=2) f = as_frozendict(d) self.assertIs(type(f), frozendict) diff --git a/Misc/NEWS.d/next/C_API/2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst b/Misc/NEWS.d/next/C_API/2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst index a8809db7134b34c..508a6bf9b392613 100644 --- a/Misc/NEWS.d/next/C_API/2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst +++ b/Misc/NEWS.d/next/C_API/2026-11-15-15-30-40.gh-issue-162047.Zl2chW.rst @@ -2,6 +2,7 @@ Add :pep:`814` :class:`frozendict` functions to the limited C API: * :c:func:`PyAnyDict_CheckExact` * :c:func:`PyAnyDict_Check` +* :c:func:`PyDict_AsFrozenDictAndClear` * :c:func:`PyFrozenDict_CheckExact` * :c:func:`PyFrozenDict_Check` * :c:func:`PyFrozenDict_New` diff --git a/Modules/_testcapi/dict.c b/Modules/_testcapi/dict.c index 87cf9772dfc72d5..b7c73d7332bd4e9 100644 --- a/Modules/_testcapi/dict.c +++ b/Modules/_testcapi/dict.c @@ -257,13 +257,6 @@ test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored)) Py_RETURN_NONE; } -static PyObject * -dict_as_frozendict_and_clear(PyObject *self, PyObject *obj) -{ - NULLABLE(obj); - return PyDict_AsFrozenDictAndClear(obj); -} - static PyMethodDef test_methods[] = { {"dict_containsstring", dict_containsstring, METH_VARARGS}, @@ -276,7 +269,6 @@ static PyMethodDef test_methods[] = { {"dict_popstring", dict_popstring, METH_VARARGS}, {"dict_popstring_null", dict_popstring_null, METH_VARARGS}, {"test_dict_iteration", test_dict_iteration, METH_NOARGS}, - {"dict_as_frozendict_and_clear", dict_as_frozendict_and_clear, METH_O}, {NULL}, }; diff --git a/Modules/_testlimitedcapi/dict.c b/Modules/_testlimitedcapi/dict.c index 49c9a590d2803f4..fc185c40c8b0a00 100644 --- a/Modules/_testlimitedcapi/dict.c +++ b/Modules/_testlimitedcapi/dict.c @@ -258,6 +258,7 @@ dict_mergefromseq2(PyObject *self, PyObject *args) } +// Test PyFrozenDict_Check() static PyObject * frozendict_check(PyObject *self, PyObject *obj) { @@ -265,6 +266,7 @@ frozendict_check(PyObject *self, PyObject *obj) return PyLong_FromLong(PyFrozenDict_Check(obj)); } +// Test PyFrozenDict_CheckExact() static PyObject * frozendict_checkexact(PyObject *self, PyObject *obj) { @@ -272,6 +274,7 @@ frozendict_checkexact(PyObject *self, PyObject *obj) return PyLong_FromLong(PyFrozenDict_CheckExact(obj)); } +// Test PyAnyDict_Check() static PyObject * anydict_check(PyObject *self, PyObject *obj) { @@ -279,6 +282,7 @@ anydict_check(PyObject *self, PyObject *obj) return PyLong_FromLong(PyAnyDict_Check(obj)); } +// Test PyAnyDict_CheckExact() static PyObject * anydict_checkexact(PyObject *self, PyObject *obj) { @@ -287,6 +291,7 @@ anydict_checkexact(PyObject *self, PyObject *obj) } +// Test PyFrozenDict_New() static PyObject * frozendict_new(PyObject *self, PyObject *obj) { @@ -295,6 +300,15 @@ frozendict_new(PyObject *self, PyObject *obj) } +// Test PyDict_AsFrozenDictAndClear() +static PyObject * +dict_as_frozendict_and_clear(PyObject *self, PyObject *obj) +{ + NULLABLE(obj); + return PyDict_AsFrozenDictAndClear(obj); +} + + static PyMethodDef test_methods[] = { {"dict_check", dict_check, METH_O}, {"dict_checkexact", dict_checkexact, METH_O}, @@ -323,6 +337,7 @@ static PyMethodDef test_methods[] = { {"anydict_check", anydict_check, METH_O}, {"anydict_checkexact", anydict_checkexact, METH_O}, {"frozendict_new", frozendict_new, METH_O}, + {"dict_as_frozendict_and_clear", dict_as_frozendict_and_clear, METH_O}, {NULL}, }; From 386706b0fabe75a217c0e557b7f1702e6b3ba052 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 19 Nov 2026 00:54:25 +0100 Subject: [PATCH 5/5] Add PyDict_AsFrozenDictAndClear to stable_abi.toml --- Doc/data/stable_abi.dat | 1 + Lib/test/test_stable_abi_ctypes.py | 1 + Misc/stable_abi.toml | 2 ++ PC/python3dll.c | 1 + 4 files changed, 5 insertions(+) diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat index e17326b7e7a7ae4..5d68f90ec01b0d1 100644 --- a/Doc/data/stable_abi.dat +++ b/Doc/data/stable_abi.dat @@ -152,6 +152,7 @@ data,PyDictRevIterItem_Type,3.8,, data,PyDictRevIterKey_Type,3.8,, data,PyDictRevIterValue_Type,3.8,, data,PyDictValues_Type,3.2,, +function,PyDict_AsFrozenDictAndClear,3.16,, function,PyDict_Clear,3.2,, function,PyDict_Contains,3.2,, function,PyDict_Copy,3.2,, diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py index 0c83b787ff5d0ae..d9709c5cb2029d6 100644 --- a/Lib/test/test_stable_abi_ctypes.py +++ b/Lib/test/test_stable_abi_ctypes.py @@ -153,6 +153,7 @@ def test_windows_feature_macros(self): "PyDictRevIterKey_Type", "PyDictRevIterValue_Type", "PyDictValues_Type", + "PyDict_AsFrozenDictAndClear", "PyDict_Clear", "PyDict_Contains", "PyDict_Copy", diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml index 04e7a83a5b749f4..e099c315af6c7b1 100644 --- a/Misc/stable_abi.toml +++ b/Misc/stable_abi.toml @@ -2953,3 +2953,5 @@ added = '3.16' [data.PyFrozenDict_Type] added = '3.16' +[function.PyDict_AsFrozenDictAndClear] + added = '3.16' diff --git a/PC/python3dll.c b/PC/python3dll.c index fc617f3d4fbe6f4..3992c2e19bf24dc 100755 --- a/PC/python3dll.c +++ b/PC/python3dll.c @@ -183,6 +183,7 @@ EXPORT_FUNC(PyDescr_NewClassMethod) EXPORT_FUNC(PyDescr_NewGetSet) EXPORT_FUNC(PyDescr_NewMember) EXPORT_FUNC(PyDescr_NewMethod) +EXPORT_FUNC(PyDict_AsFrozenDictAndClear) EXPORT_FUNC(PyDict_Clear) EXPORT_FUNC(PyDict_Contains) EXPORT_FUNC(PyDict_Copy)