Issue #28959: Added private macro PyDict_GET_SIZE for retrieving the size of dict.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 47d8f56..4908dde 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -3684,7 +3684,7 @@
must be the same as len(inargs) + len(kwds), otherwise we have
either too much or not enough arguments. */
- actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0);
+ actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_GET_SIZE(kwds) : 0);
if (actual_args != inargs_index) {
/* When we have default values or named parameters, this error
message is misleading. See unittests/test_paramflags.py
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 254e595..d11f4a9 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -3169,7 +3169,7 @@
PyErr_Clear();
state = Py_None;
dictptr = _PyObject_GetDictPtr(self);
- if (dictptr && *dictptr && PyDict_Size(*dictptr)) {
+ if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) {
state = *dictptr;
}
Py_INCREF(state);
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index 9e8625d..feaef2e 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -428,7 +428,7 @@
return DEC_INVALID_SIGNALS;
}
- if (PyDict_Size(val) != SIGNAL_MAP_LEN) {
+ if (PyDict_GET_SIZE(val) != SIGNAL_MAP_LEN) {
PyErr_SetString(PyExc_KeyError,
"invalid signal dict");
return DEC_INVALID_SIGNALS;
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index ba827e6..9e6f63b 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -150,7 +150,7 @@
static int
is_empty_dict(PyObject *obj)
{
- return PyDict_CheckExact(obj) && PyDict_Size(obj) == 0;
+ return PyDict_CheckExact(obj) && PyDict_GET_SIZE(obj) == 0;
}
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 19ca65b..6b5c008 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -84,7 +84,7 @@
}
Py_DECREF(nargs);
- if (pkw == NULL || PyDict_Size(pkw) == 0) {
+ if (pkw == NULL || PyDict_GET_SIZE(pkw) == 0) {
if (kw == NULL) {
pto->kw = PyDict_New();
}
@@ -155,7 +155,7 @@
assert(PyTuple_Check(argappl));
}
- if (PyDict_Size(pto->kw) == 0) {
+ if (PyDict_GET_SIZE(pto->kw) == 0) {
kwappl = kw;
Py_XINCREF(kwappl);
}
@@ -713,7 +713,7 @@
return args;
}
- if (kwds && PyDict_Size(kwds) > 0) {
+ if (kwds && PyDict_GET_SIZE(kwds) > 0) {
sorted_items = PyDict_Items(kwds);
if (!sorted_items)
return NULL;
@@ -933,7 +933,7 @@
}
lru_cache_append_link(self, link);
Py_INCREF(result); /* for return */
- self->full = (PyDict_Size(self->cache) >= self->maxsize);
+ self->full = (PyDict_GET_SIZE(self->cache) >= self->maxsize);
}
self->misses++;
return result;
@@ -1062,7 +1062,7 @@
{
return PyObject_CallFunction(self->cache_info_type, "nnOn",
self->hits, self->misses, self->maxsize_O,
- PyDict_Size(self->cache));
+ PyDict_GET_SIZE(self->cache));
}
static PyObject *
diff --git a/Modules/_operator.c b/Modules/_operator.c
index fb8eafc..f5b8612 100644
--- a/Modules/_operator.c
+++ b/Modules/_operator.c
@@ -1016,16 +1016,7 @@
return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name);
}
- if (mc->kwds != NULL) {
- numkwdargs = PyDict_Size(mc->kwds);
- if (numkwdargs < 0) {
- Py_ReprLeave((PyObject *)mc);
- return NULL;
- }
- } else {
- numkwdargs = 0;
- }
-
+ numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0;
numposargs = PyTuple_GET_SIZE(mc->args);
numtotalargs = numposargs + numkwdargs;
@@ -1092,7 +1083,7 @@
methodcaller_reduce(methodcallerobject *mc)
{
PyObject *newargs;
- if (!mc->kwds || PyDict_Size(mc->kwds) == 0) {
+ if (!mc->kwds || PyDict_GET_SIZE(mc->kwds) == 0) {
Py_ssize_t i;
Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args);
newargs = PyTuple_New(1 + callargcount);
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 947069a..b1e1d49 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -2787,10 +2787,10 @@
const char setitem_op = SETITEM;
const char setitems_op = SETITEMS;
- assert(obj != NULL);
+ assert(obj != NULL && PyDict_CheckExact(obj));
assert(self->proto > 0);
- dict_size = PyDict_Size(obj);
+ dict_size = PyDict_GET_SIZE(obj);
/* Special-case len(d) == 1 to save space. */
if (dict_size == 1) {
@@ -2819,7 +2819,7 @@
}
if (_Pickler_Write(self, &setitems_op, 1) < 0)
return -1;
- if (PyDict_Size(obj) != dict_size) {
+ if (PyDict_GET_SIZE(obj) != dict_size) {
PyErr_Format(
PyExc_RuntimeError,
"dictionary changed size during iteration");
@@ -2837,6 +2837,7 @@
char header[3];
Py_ssize_t len;
int status = 0;
+ assert(PyDict_Check(obj));
if (self->fast && !fast_save_enter(self, obj))
goto error;
@@ -2855,14 +2856,10 @@
if (_Pickler_Write(self, header, len) < 0)
goto error;
- /* Get dict size, and bow out early if empty. */
- if ((len = PyDict_Size(obj)) < 0)
- goto error;
-
if (memo_put(self, obj) < 0)
goto error;
- if (len != 0) {
+ if (PyDict_GET_SIZE(obj)) {
/* Save the dict items. */
if (PyDict_CheckExact(obj) && self->proto > 0) {
/* We can take certain shortcuts if we know this is a dict and
@@ -6878,7 +6875,7 @@
Py_ssize_t i = 0;
PyObject *key, *value;
- new_memo_size = PyDict_Size(obj);
+ new_memo_size = PyDict_GET_SIZE(obj);
new_memo = _Unpickler_NewMemo(new_memo_size);
if (new_memo == NULL)
return -1;
diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c
index 62c5893..72b1f2c 100644
--- a/Modules/_sqlite/cache.c
+++ b/Modules/_sqlite/cache.c
@@ -162,7 +162,7 @@
* entry in the cache, and make space if necessary by throwing the
* least used item out of the cache. */
- if (PyDict_Size(self->mapping) == self->size) {
+ if (PyDict_GET_SIZE(self->mapping) == self->size) {
if (self->last) {
node = self->last;
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 1d7a935..d621789 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -2048,7 +2048,7 @@
s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
if (s_object != NULL) {
- if (PyDict_Size(cache) >= MAXCACHE)
+ if (PyDict_GET_SIZE(cache) >= MAXCACHE)
PyDict_Clear(cache);
/* Attempt to cache the result */
if (PyDict_SetItem(cache, fmt, s_object) == -1)
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 6bf04cb..7cbee2b 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -4180,7 +4180,7 @@
return NULL;
if (kwds != NULL)
- n_kwds = PyDict_Size(kwds);
+ n_kwds = PyDict_GET_SIZE(kwds);
/* Does user supply times argument? */
if ((PyTuple_Size(args) + n_kwds == 2) && cnt < 0)
cnt = 0;
@@ -4331,9 +4331,9 @@
PyObject *fillvalue = Py_None;
Py_ssize_t tuplesize = PySequence_Length(args);
- if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_Size(kwds) > 0) {
+ if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) {
fillvalue = PyDict_GetItemString(kwds, "fillvalue");
- if (fillvalue == NULL || PyDict_Size(kwds) > 1) {
+ if (fillvalue == NULL || PyDict_GET_SIZE(kwds) > 1) {
PyErr_SetString(PyExc_TypeError,
"zip_longest() got an unexpected keyword argument");
return NULL;
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 79cf199..0206651 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -353,7 +353,7 @@
PyObject *key, *value;
struct pollfd *old_ufds = self->ufds;
- self->ufd_len = PyDict_Size(self->dict);
+ self->ufd_len = PyDict_GET_SIZE(self->dict);
PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
if (self->ufds == NULL) {
self->ufds = old_ufds;