Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ab81d7

Browse files
Issue #28959: Added private macro PyDict_GET_SIZE for retrieving the size of dict.
1 parent 1d59a0a commit 5ab81d7

25 files changed

Lines changed: 61 additions & 80 deletions

Include/dictobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ PyAPI_FUNC(Py_ssize_t) PyDict_Size(PyObject *mp);
106106
PyAPI_FUNC(PyObject *) PyDict_Copy(PyObject *mp);
107107
PyAPI_FUNC(int) PyDict_Contains(PyObject *mp, PyObject *key);
108108
#ifndef Py_LIMITED_API
109+
/* Get the number of items of a dictionary. */
110+
#define PyDict_GET_SIZE(mp) (assert(PyDict_Check(mp)),((PyDictObject *)mp)->ma_used)
109111
PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash);
110112
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
111113
PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);

Include/odictobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PyAPI_DATA(PyTypeObject) PyODictValues_Type;
2121

2222
#define PyODict_Check(op) PyObject_TypeCheck(op, &PyODict_Type)
2323
#define PyODict_CheckExact(op) (Py_TYPE(op) == &PyODict_Type)
24-
#define PyODict_SIZE(op) ((PyDictObject *)op)->ma_used
24+
#define PyODict_SIZE(op) PyDict_GET_SIZE((op))
2525
#define PyODict_HasKey(od, key) (PyMapping_HasKey(PyObject *)od, key)
2626

2727
PyAPI_FUNC(PyObject *) PyODict_New(void);

Modules/_ctypes/_ctypes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3684,7 +3684,7 @@ _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
36843684
must be the same as len(inargs) + len(kwds), otherwise we have
36853685
either too much or not enough arguments. */
36863686

3687-
actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0);
3687+
actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_GET_SIZE(kwds) : 0);
36883688
if (actual_args != inargs_index) {
36893689
/* When we have default values or named parameters, this error
36903690
message is misleading. See unittests/test_paramflags.py

Modules/_datetimemodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3169,7 +3169,7 @@ tzinfo_reduce(PyObject *self)
31693169
PyErr_Clear();
31703170
state = Py_None;
31713171
dictptr = _PyObject_GetDictPtr(self);
3172-
if (dictptr && *dictptr && PyDict_Size(*dictptr)) {
3172+
if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) {
31733173
state = *dictptr;
31743174
}
31753175
Py_INCREF(state);

Modules/_decimal/_decimal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ dict_as_flags(PyObject *val)
428428
return DEC_INVALID_SIGNALS;
429429
}
430430

431-
if (PyDict_Size(val) != SIGNAL_MAP_LEN) {
431+
if (PyDict_GET_SIZE(val) != SIGNAL_MAP_LEN) {
432432
PyErr_SetString(PyExc_KeyError,
433433
"invalid signal dict");
434434
return DEC_INVALID_SIGNALS;

Modules/_elementtree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ list_join(PyObject* list)
150150
static int
151151
is_empty_dict(PyObject *obj)
152152
{
153-
return PyDict_CheckExact(obj) && PyDict_Size(obj) == 0;
153+
return PyDict_CheckExact(obj) && PyDict_GET_SIZE(obj) == 0;
154154
}
155155

156156

Modules/_functoolsmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
8484
}
8585
Py_DECREF(nargs);
8686

87-
if (pkw == NULL || PyDict_Size(pkw) == 0) {
87+
if (pkw == NULL || PyDict_GET_SIZE(pkw) == 0) {
8888
if (kw == NULL) {
8989
pto->kw = PyDict_New();
9090
}
@@ -155,7 +155,7 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kw)
155155
assert(PyTuple_Check(argappl));
156156
}
157157

158-
if (PyDict_Size(pto->kw) == 0) {
158+
if (PyDict_GET_SIZE(pto->kw) == 0) {
159159
kwappl = kw;
160160
Py_XINCREF(kwappl);
161161
}
@@ -713,7 +713,7 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
713713
return args;
714714
}
715715

716-
if (kwds && PyDict_Size(kwds) > 0) {
716+
if (kwds && PyDict_GET_SIZE(kwds) > 0) {
717717
sorted_items = PyDict_Items(kwds);
718718
if (!sorted_items)
719719
return NULL;
@@ -933,7 +933,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
933933
}
934934
lru_cache_append_link(self, link);
935935
Py_INCREF(result); /* for return */
936-
self->full = (PyDict_Size(self->cache) >= self->maxsize);
936+
self->full = (PyDict_GET_SIZE(self->cache) >= self->maxsize);
937937
}
938938
self->misses++;
939939
return result;
@@ -1062,7 +1062,7 @@ lru_cache_cache_info(lru_cache_object *self, PyObject *unused)
10621062
{
10631063
return PyObject_CallFunction(self->cache_info_type, "nnOn",
10641064
self->hits, self->misses, self->maxsize_O,
1065-
PyDict_Size(self->cache));
1065+
PyDict_GET_SIZE(self->cache));
10661066
}
10671067

10681068
static PyObject *

Modules/_operator.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,16 +1016,7 @@ methodcaller_repr(methodcallerobject *mc)
10161016
return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name);
10171017
}
10181018

1019-
if (mc->kwds != NULL) {
1020-
numkwdargs = PyDict_Size(mc->kwds);
1021-
if (numkwdargs < 0) {
1022-
Py_ReprLeave((PyObject *)mc);
1023-
return NULL;
1024-
}
1025-
} else {
1026-
numkwdargs = 0;
1027-
}
1028-
1019+
numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0;
10291020
numposargs = PyTuple_GET_SIZE(mc->args);
10301021
numtotalargs = numposargs + numkwdargs;
10311022

@@ -1092,7 +1083,7 @@ static PyObject *
10921083
methodcaller_reduce(methodcallerobject *mc)
10931084
{
10941085
PyObject *newargs;
1095-
if (!mc->kwds || PyDict_Size(mc->kwds) == 0) {
1086+
if (!mc->kwds || PyDict_GET_SIZE(mc->kwds) == 0) {
10961087
Py_ssize_t i;
10971088
Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args);
10981089
newargs = PyTuple_New(1 + callargcount);

Modules/_pickle.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,10 +2787,10 @@ batch_dict_exact(PicklerObject *self, PyObject *obj)
27872787
const char setitem_op = SETITEM;
27882788
const char setitems_op = SETITEMS;
27892789

2790-
assert(obj != NULL);
2790+
assert(obj != NULL && PyDict_CheckExact(obj));
27912791
assert(self->proto > 0);
27922792

2793-
dict_size = PyDict_Size(obj);
2793+
dict_size = PyDict_GET_SIZE(obj);
27942794

27952795
/* Special-case len(d) == 1 to save space. */
27962796
if (dict_size == 1) {
@@ -2819,7 +2819,7 @@ batch_dict_exact(PicklerObject *self, PyObject *obj)
28192819
}
28202820
if (_Pickler_Write(self, &setitems_op, 1) < 0)
28212821
return -1;
2822-
if (PyDict_Size(obj) != dict_size) {
2822+
if (PyDict_GET_SIZE(obj) != dict_size) {
28232823
PyErr_Format(
28242824
PyExc_RuntimeError,
28252825
"dictionary changed size during iteration");
@@ -2837,6 +2837,7 @@ save_dict(PicklerObject *self, PyObject *obj)
28372837
char header[3];
28382838
Py_ssize_t len;
28392839
int status = 0;
2840+
assert(PyDict_Check(obj));
28402841

28412842
if (self->fast && !fast_save_enter(self, obj))
28422843
goto error;
@@ -2855,14 +2856,10 @@ save_dict(PicklerObject *self, PyObject *obj)
28552856
if (_Pickler_Write(self, header, len) < 0)
28562857
goto error;
28572858

2858-
/* Get dict size, and bow out early if empty. */
2859-
if ((len = PyDict_Size(obj)) < 0)
2860-
goto error;
2861-
28622859
if (memo_put(self, obj) < 0)
28632860
goto error;
28642861

2865-
if (len != 0) {
2862+
if (PyDict_GET_SIZE(obj)) {
28662863
/* Save the dict items. */
28672864
if (PyDict_CheckExact(obj) && self->proto > 0) {
28682865
/* We can take certain shortcuts if we know this is a dict and
@@ -6878,7 +6875,7 @@ Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
68786875
Py_ssize_t i = 0;
68796876
PyObject *key, *value;
68806877

6881-
new_memo_size = PyDict_Size(obj);
6878+
new_memo_size = PyDict_GET_SIZE(obj);
68826879
new_memo = _Unpickler_NewMemo(new_memo_size);
68836880
if (new_memo == NULL)
68846881
return -1;

Modules/_sqlite/cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args)
162162
* entry in the cache, and make space if necessary by throwing the
163163
* least used item out of the cache. */
164164

165-
if (PyDict_Size(self->mapping) == self->size) {
165+
if (PyDict_GET_SIZE(self->mapping) == self->size) {
166166
if (self->last) {
167167
node = self->last;
168168

0 commit comments

Comments
 (0)