Skip to content

Commit b3dd067

Browse files
committed
pythongh-106320: Make some PyDict C-API functions public that should have been public right away.
See python#108449
1 parent 9bb202a commit b3dd067

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

Doc/c-api/dict.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ Dictionary Objects
3636
Return a new empty dictionary, or ``NULL`` on failure.
3737
3838
39+
.. c:function:: PyObject* PyDict_NewPresized(Py_ssize_t minused)
40+
41+
This is the same as :c:func:`PyDict_New`, but makes sure that the dict can
42+
efficiently store at least *minused* entries without reallocation.
43+
44+
.. versionadded:: 3.13
45+
46+
3947
.. c:function:: PyObject* PyDictProxy_New(PyObject *mapping)
4048
4149
Return a :class:`types.MappingProxyType` object for a mapping which
@@ -173,6 +181,16 @@ Dictionary Objects
173181
174182
.. versionadded:: 3.4
175183
184+
185+
.. c:function:: PyObject* PyDict_Pop(PyObject *p, PyObject *key, PyObject *defaultobj)
186+
187+
This is the same as the Python-level :meth:`dict.pop`. It removes the *key*
188+
from the dictionary *p* and returns its value. If the key is not in the dict,
189+
the value *defaultobj* is returned instead if it is not ``NULL``, or a
190+
:exc:`KeyError` is raised if it is ``NULL``.
191+
192+
.. versionadded:: 3.13
193+
176194
.. c:function:: PyObject* PyDict_Items(PyObject *p)
177195
178196
Return a :c:type:`PyListObject` containing all the items from the dictionary.

Include/cpython/dictobject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
4545
#define PyDict_GET_SIZE(op) PyDict_GET_SIZE(_PyObject_CAST(op))
4646

4747
PyAPI_FUNC(int) PyDict_ContainsString(PyObject *mp, const char *key);
48-
48+
PyAPI_FUNC(PyObject *) PyDict_NewPresized(Py_ssize_t minused);
49+
PyAPI_FUNC(PyObject *) PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value);
4950

5051
/* Dictionary watchers */
5152

Include/internal/pycore_dict.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ extern int _PyDict_HasOnlyStringKeys(PyObject *mp);
4444

4545
extern void _PyDict_MaybeUntrack(PyObject *mp);
4646

47-
extern PyObject* _PyDict_NewPresized(Py_ssize_t minused);
48-
4947
// Export for '_ctypes' shared extension
5048
PyAPI_FUNC(Py_ssize_t) _PyDict_SizeOf(PyDictObject *);
5149

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add new C-API functions PyDict_NewPresized and PyDict_Pop
2+
to replace previously removed underscore functions.

Objects/dictobject.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ dict_new_presized(PyInterpreterState *interp, Py_ssize_t minused, bool unicode)
16081608
}
16091609

16101610
PyObject *
1611-
_PyDict_NewPresized(Py_ssize_t minused)
1611+
PyDict_NewPresized(Py_ssize_t minused)
16121612
{
16131613
PyInterpreterState *interp = _PyInterpreterState_GET();
16141614
return dict_new_presized(interp, minused, false);
@@ -2272,6 +2272,16 @@ _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
22722272
return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
22732273
}
22742274

2275+
PyObject *
2276+
PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
2277+
{
2278+
if (!PyDict_Check(dict)) {
2279+
PyErr_BadInternalCall();
2280+
return -1;
2281+
}
2282+
return _PyDict_Pop(dict, key, deflt);
2283+
}
2284+
22752285
/* Internal version of dict.from_keys(). It is subclass-friendly. */
22762286
PyObject *
22772287
_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)

0 commit comments

Comments
 (0)