We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c4e7d24 commit f63a8c3Copy full SHA for f63a8c3
Doc/c-api/dict.rst
@@ -36,6 +36,14 @@ Dictionary Objects
36
Return a new empty dictionary, or ``NULL`` on failure.
37
38
39
+.. c:function:: PyObject* PyDict_NewPresized(Py_ssize_t size)
40
+
41
+ Return a new empty dictionary with at least *size* preallocated items,
42
+ or ``NULL`` on failure.
43
44
+ .. versionadded:: next
45
46
47
.. c:function:: PyObject* PyDictProxy_New(PyObject *mapping)
48
49
Return a :class:`types.MappingProxyType` object for a mapping which
Doc/whatsnew/3.15.rst
@@ -852,6 +852,10 @@ New features
852
853
(Contributed by Victor Stinner in :gh:`129813`.)
854
855
+* Add :c:func:`PyDict_NewPresized` function to create a new empty dictionary
856
+ with at least *size* preallocated items.
857
+ (Contributed by Victor Stinner in :gh:`139772`.)
858
859
860
Porting to Python 3.15
861
----------------------
Include/cpython/dictobject.h
@@ -64,7 +64,12 @@ static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
64
65
PyAPI_FUNC(int) PyDict_ContainsString(PyObject *mp, const char *key);
66
67
-PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
+PyAPI_FUNC(PyObject*) PyDict_NewPresized(Py_ssize_t size);
68
+Py_DEPRECATED(3.15) static inline PyObject*
69
+_PyDict_NewPresized(Py_ssize_t size)
70
+{
71
+ return PyDict_NewPresized(size);
72
+}
73
74
PyAPI_FUNC(int) PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result);
75
PyAPI_FUNC(int) PyDict_PopString(PyObject *dict, const char *key, PyObject **result);
Lib/test/test_capi/test_dict.py
@@ -545,6 +545,17 @@ def test_dict_popstring(self):
545
# CRASHES dict_popstring({}, NULL)
546
# CRASHES dict_popstring({"a": 1}, NULL)
547
548
+ def test_dict_newpresized(self):
549
+ # Test PyDict_NewPresized()
550
+ dict_newpresized = _testcapi.dict_newpresized
551
+ d = dict_newpresized(3)
552
+ d[1] = 'a'
553
+ d[2] = 'b'
554
+ d[3] = 'c'
555
+ self.assertEqual(len(d), 3)
556
+ d[4] = 'd'
557
+ self.assertEqual(len(d), 4)
558
559
560
if __name__ == "__main__":
561
unittest.main()
Misc/NEWS.d/next/C_API/2025-10-08-15-26-21.gh-issue-139772.HBZeQA.rst
@@ -0,0 +1,2 @@
1
+Add :c:func:`PyDict_NewPresized` function to create a new empty dictionary with
2
+at least *size* preallocated items. Patch by Victor Stinner.
Modules/_testcapi/dict.c
@@ -258,6 +258,17 @@ test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored))
258
}
259
260
261
+static PyObject *
262
+dict_newpresized(PyObject *self, PyObject *args)
263
264
+ Py_ssize_t size;
265
+ if (!PyArg_ParseTuple(args, "n", &size)) {
266
+ return NULL;
267
+ }
268
269
270
271
272
static PyMethodDef test_methods[] = {
273
{"dict_containsstring", dict_containsstring, METH_VARARGS},
274
{"dict_getitemref", dict_getitemref, METH_VARARGS},
@@ -268,7 +279,8 @@ static PyMethodDef test_methods[] = {
279
{"dict_pop_null", dict_pop_null, METH_VARARGS},
280
{"dict_popstring", dict_popstring, METH_VARARGS},
281
{"dict_popstring_null", dict_popstring_null, METH_VARARGS},
- {"test_dict_iteration", test_dict_iteration, METH_NOARGS},
282
+ {"test_dict_iteration", test_dict_iteration, METH_NOARGS},
283
+ {"dict_newpresized", dict_newpresized, METH_VARARGS},
284
{NULL},
285
};
286
Objects/dictobject.c
@@ -2220,9 +2220,9 @@ dict_new_presized(Py_ssize_t minused, bool unicode)
2220
2221
2222
PyObject *
2223
-_PyDict_NewPresized(Py_ssize_t minused)
+PyDict_NewPresized(Py_ssize_t size)
2224
{
2225
- return dict_new_presized(minused, false);
+ return dict_new_presized(size, false);
2226
2227
2228
0 commit comments