Issue #28959: Added private macro PyDict_GET_SIZE for retrieving the size of dict.

This commit is contained in:
Serhiy Storchaka 2016-12-16 16:18:57 +02:00
parent 1d59a0aacf
commit 5ab81d787f
25 changed files with 61 additions and 80 deletions

View File

@ -106,6 +106,8 @@ PyAPI_FUNC(Py_ssize_t) PyDict_Size(PyObject *mp);
PyAPI_FUNC(PyObject *) PyDict_Copy(PyObject *mp); PyAPI_FUNC(PyObject *) PyDict_Copy(PyObject *mp);
PyAPI_FUNC(int) PyDict_Contains(PyObject *mp, PyObject *key); PyAPI_FUNC(int) PyDict_Contains(PyObject *mp, PyObject *key);
#ifndef Py_LIMITED_API #ifndef Py_LIMITED_API
/* Get the number of items of a dictionary. */
#define PyDict_GET_SIZE(mp) (assert(PyDict_Check(mp)),((PyDictObject *)mp)->ma_used)
PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash); PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash);
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused); PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp); PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);

View File

@ -21,7 +21,7 @@ PyAPI_DATA(PyTypeObject) PyODictValues_Type;
#define PyODict_Check(op) PyObject_TypeCheck(op, &PyODict_Type) #define PyODict_Check(op) PyObject_TypeCheck(op, &PyODict_Type)
#define PyODict_CheckExact(op) (Py_TYPE(op) == &PyODict_Type) #define PyODict_CheckExact(op) (Py_TYPE(op) == &PyODict_Type)
#define PyODict_SIZE(op) ((PyDictObject *)op)->ma_used #define PyODict_SIZE(op) PyDict_GET_SIZE((op))
#define PyODict_HasKey(od, key) (PyMapping_HasKey(PyObject *)od, key) #define PyODict_HasKey(od, key) (PyMapping_HasKey(PyObject *)od, key)
PyAPI_FUNC(PyObject *) PyODict_New(void); PyAPI_FUNC(PyObject *) PyODict_New(void);

View File

@ -3684,7 +3684,7 @@ _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
must be the same as len(inargs) + len(kwds), otherwise we have must be the same as len(inargs) + len(kwds), otherwise we have
either too much or not enough arguments. */ 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) { if (actual_args != inargs_index) {
/* When we have default values or named parameters, this error /* When we have default values or named parameters, this error
message is misleading. See unittests/test_paramflags.py message is misleading. See unittests/test_paramflags.py

View File

@ -3169,7 +3169,7 @@ tzinfo_reduce(PyObject *self)
PyErr_Clear(); PyErr_Clear();
state = Py_None; state = Py_None;
dictptr = _PyObject_GetDictPtr(self); dictptr = _PyObject_GetDictPtr(self);
if (dictptr && *dictptr && PyDict_Size(*dictptr)) { if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) {
state = *dictptr; state = *dictptr;
} }
Py_INCREF(state); Py_INCREF(state);

View File

@ -428,7 +428,7 @@ dict_as_flags(PyObject *val)
return DEC_INVALID_SIGNALS; return DEC_INVALID_SIGNALS;
} }
if (PyDict_Size(val) != SIGNAL_MAP_LEN) { if (PyDict_GET_SIZE(val) != SIGNAL_MAP_LEN) {
PyErr_SetString(PyExc_KeyError, PyErr_SetString(PyExc_KeyError,
"invalid signal dict"); "invalid signal dict");
return DEC_INVALID_SIGNALS; return DEC_INVALID_SIGNALS;

View File

@ -150,7 +150,7 @@ list_join(PyObject* list)
static int static int
is_empty_dict(PyObject *obj) is_empty_dict(PyObject *obj)
{ {
return PyDict_CheckExact(obj) && PyDict_Size(obj) == 0; return PyDict_CheckExact(obj) && PyDict_GET_SIZE(obj) == 0;
} }

View File

@ -84,7 +84,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
} }
Py_DECREF(nargs); Py_DECREF(nargs);
if (pkw == NULL || PyDict_Size(pkw) == 0) { if (pkw == NULL || PyDict_GET_SIZE(pkw) == 0) {
if (kw == NULL) { if (kw == NULL) {
pto->kw = PyDict_New(); pto->kw = PyDict_New();
} }
@ -155,7 +155,7 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kw)
assert(PyTuple_Check(argappl)); assert(PyTuple_Check(argappl));
} }
if (PyDict_Size(pto->kw) == 0) { if (PyDict_GET_SIZE(pto->kw) == 0) {
kwappl = kw; kwappl = kw;
Py_XINCREF(kwappl); Py_XINCREF(kwappl);
} }
@ -713,7 +713,7 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
return args; return args;
} }
if (kwds && PyDict_Size(kwds) > 0) { if (kwds && PyDict_GET_SIZE(kwds) > 0) {
sorted_items = PyDict_Items(kwds); sorted_items = PyDict_Items(kwds);
if (!sorted_items) if (!sorted_items)
return NULL; return NULL;
@ -933,7 +933,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
} }
lru_cache_append_link(self, link); lru_cache_append_link(self, link);
Py_INCREF(result); /* for return */ Py_INCREF(result); /* for return */
self->full = (PyDict_Size(self->cache) >= self->maxsize); self->full = (PyDict_GET_SIZE(self->cache) >= self->maxsize);
} }
self->misses++; self->misses++;
return result; return result;
@ -1062,7 +1062,7 @@ lru_cache_cache_info(lru_cache_object *self, PyObject *unused)
{ {
return PyObject_CallFunction(self->cache_info_type, "nnOn", return PyObject_CallFunction(self->cache_info_type, "nnOn",
self->hits, self->misses, self->maxsize_O, self->hits, self->misses, self->maxsize_O,
PyDict_Size(self->cache)); PyDict_GET_SIZE(self->cache));
} }
static PyObject * static PyObject *

View File

@ -1016,16 +1016,7 @@ methodcaller_repr(methodcallerobject *mc)
return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name); return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name);
} }
if (mc->kwds != NULL) { numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0;
numkwdargs = PyDict_Size(mc->kwds);
if (numkwdargs < 0) {
Py_ReprLeave((PyObject *)mc);
return NULL;
}
} else {
numkwdargs = 0;
}
numposargs = PyTuple_GET_SIZE(mc->args); numposargs = PyTuple_GET_SIZE(mc->args);
numtotalargs = numposargs + numkwdargs; numtotalargs = numposargs + numkwdargs;
@ -1092,7 +1083,7 @@ static PyObject *
methodcaller_reduce(methodcallerobject *mc) methodcaller_reduce(methodcallerobject *mc)
{ {
PyObject *newargs; 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 i;
Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args); Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args);
newargs = PyTuple_New(1 + callargcount); newargs = PyTuple_New(1 + callargcount);

View File

@ -2787,10 +2787,10 @@ batch_dict_exact(PicklerObject *self, PyObject *obj)
const char setitem_op = SETITEM; const char setitem_op = SETITEM;
const char setitems_op = SETITEMS; const char setitems_op = SETITEMS;
assert(obj != NULL); assert(obj != NULL && PyDict_CheckExact(obj));
assert(self->proto > 0); assert(self->proto > 0);
dict_size = PyDict_Size(obj); dict_size = PyDict_GET_SIZE(obj);
/* Special-case len(d) == 1 to save space. */ /* Special-case len(d) == 1 to save space. */
if (dict_size == 1) { if (dict_size == 1) {
@ -2819,7 +2819,7 @@ batch_dict_exact(PicklerObject *self, PyObject *obj)
} }
if (_Pickler_Write(self, &setitems_op, 1) < 0) if (_Pickler_Write(self, &setitems_op, 1) < 0)
return -1; return -1;
if (PyDict_Size(obj) != dict_size) { if (PyDict_GET_SIZE(obj) != dict_size) {
PyErr_Format( PyErr_Format(
PyExc_RuntimeError, PyExc_RuntimeError,
"dictionary changed size during iteration"); "dictionary changed size during iteration");
@ -2837,6 +2837,7 @@ save_dict(PicklerObject *self, PyObject *obj)
char header[3]; char header[3];
Py_ssize_t len; Py_ssize_t len;
int status = 0; int status = 0;
assert(PyDict_Check(obj));
if (self->fast && !fast_save_enter(self, obj)) if (self->fast && !fast_save_enter(self, obj))
goto error; goto error;
@ -2855,14 +2856,10 @@ save_dict(PicklerObject *self, PyObject *obj)
if (_Pickler_Write(self, header, len) < 0) if (_Pickler_Write(self, header, len) < 0)
goto error; 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) if (memo_put(self, obj) < 0)
goto error; goto error;
if (len != 0) { if (PyDict_GET_SIZE(obj)) {
/* Save the dict items. */ /* Save the dict items. */
if (PyDict_CheckExact(obj) && self->proto > 0) { if (PyDict_CheckExact(obj) && self->proto > 0) {
/* We can take certain shortcuts if we know this is a dict and /* We can take certain shortcuts if we know this is a dict and
@ -6878,7 +6875,7 @@ Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Py_ssize_t i = 0; Py_ssize_t i = 0;
PyObject *key, *value; PyObject *key, *value;
new_memo_size = PyDict_Size(obj); new_memo_size = PyDict_GET_SIZE(obj);
new_memo = _Unpickler_NewMemo(new_memo_size); new_memo = _Unpickler_NewMemo(new_memo_size);
if (new_memo == NULL) if (new_memo == NULL)
return -1; return -1;

View File

@ -162,7 +162,7 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args)
* entry in the cache, and make space if necessary by throwing the * entry in the cache, and make space if necessary by throwing the
* least used item out of the cache. */ * 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) { if (self->last) {
node = self->last; node = self->last;

View File

@ -2048,7 +2048,7 @@ cache_struct(PyObject *fmt)
s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL); s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
if (s_object != NULL) { if (s_object != NULL) {
if (PyDict_Size(cache) >= MAXCACHE) if (PyDict_GET_SIZE(cache) >= MAXCACHE)
PyDict_Clear(cache); PyDict_Clear(cache);
/* Attempt to cache the result */ /* Attempt to cache the result */
if (PyDict_SetItem(cache, fmt, s_object) == -1) if (PyDict_SetItem(cache, fmt, s_object) == -1)

View File

@ -4180,7 +4180,7 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
if (kwds != NULL) if (kwds != NULL)
n_kwds = PyDict_Size(kwds); n_kwds = PyDict_GET_SIZE(kwds);
/* Does user supply times argument? */ /* Does user supply times argument? */
if ((PyTuple_Size(args) + n_kwds == 2) && cnt < 0) if ((PyTuple_Size(args) + n_kwds == 2) && cnt < 0)
cnt = 0; cnt = 0;
@ -4331,9 +4331,9 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *fillvalue = Py_None; PyObject *fillvalue = Py_None;
Py_ssize_t tuplesize = PySequence_Length(args); 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"); fillvalue = PyDict_GetItemString(kwds, "fillvalue");
if (fillvalue == NULL || PyDict_Size(kwds) > 1) { if (fillvalue == NULL || PyDict_GET_SIZE(kwds) > 1) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"zip_longest() got an unexpected keyword argument"); "zip_longest() got an unexpected keyword argument");
return NULL; return NULL;

View File

@ -353,7 +353,7 @@ update_ufd_array(pollObject *self)
PyObject *key, *value; PyObject *key, *value;
struct pollfd *old_ufds = self->ufds; 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); PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
if (self->ufds == NULL) { if (self->ufds == NULL) {
self->ufds = old_ufds; self->ufds = old_ufds;

View File

@ -2409,8 +2409,7 @@ _PyStack_UnpackDict(PyObject **args, Py_ssize_t nargs, PyObject *kwargs,
assert(nargs >= 0); assert(nargs >= 0);
assert(kwargs == NULL || PyDict_CheckExact(kwargs)); assert(kwargs == NULL || PyDict_CheckExact(kwargs));
nkwargs = (kwargs != NULL) ? PyDict_Size(kwargs) : 0; if (kwargs == NULL || (nkwargs = PyDict_GET_SIZE(kwargs)) == 0) {
if (!nkwargs) {
*p_kwnames = NULL; *p_kwnames = NULL;
return args; return args;
} }

View File

@ -1173,7 +1173,7 @@ wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
return (*wk)(self, args, wp->descr->d_wrapped, kwds); return (*wk)(self, args, wp->descr->d_wrapped, kwds);
} }
if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) { if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_GET_SIZE(kwds) != 0)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"wrapper %s doesn't take keyword arguments", "wrapper %s doesn't take keyword arguments",
wp->descr->d_base->name); wp->descr->d_base->name);

View File

@ -583,7 +583,7 @@ function_call(PyObject *func, PyObject *arg, PyObject *kw)
if (kw != NULL && PyDict_Check(kw)) { if (kw != NULL && PyDict_Check(kw)) {
Py_ssize_t pos, i; Py_ssize_t pos, i;
nk = PyDict_Size(kw); nk = PyDict_GET_SIZE(kw);
kwtuple = PyTuple_New(2*nk); kwtuple = PyTuple_New(2*nk);
if (kwtuple == NULL) if (kwtuple == NULL)
return NULL; return NULL;

View File

@ -87,6 +87,7 @@ PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwds)
Py_ssize_t size; Py_ssize_t size;
int flags; int flags;
assert(kwds == NULL || PyDict_Check(kwds));
/* PyCFunction_Call() must not be called with an exception set, /* PyCFunction_Call() must not be called with an exception set,
because it may clear it (directly or indirectly) and so the because it may clear it (directly or indirectly) and so the
caller loses its exception */ caller loses its exception */
@ -103,7 +104,7 @@ PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwds)
res = _PyCFunction_FastCallDict(func, stack, nargs, kwds); res = _PyCFunction_FastCallDict(func, stack, nargs, kwds);
} }
else { else {
if (kwds != NULL && PyDict_Size(kwds) != 0) { if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
f->m_ml->ml_name); f->m_ml->ml_name);
return NULL; return NULL;
@ -176,7 +177,7 @@ _PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
switch (flags) switch (flags)
{ {
case METH_NOARGS: case METH_NOARGS:
if (kwargs != NULL && PyDict_Size(kwargs) != 0) { if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
func->m_ml->ml_name); func->m_ml->ml_name);
return NULL; return NULL;
@ -193,7 +194,7 @@ _PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
break; break;
case METH_O: case METH_O:
if (kwargs != NULL && PyDict_Size(kwargs) != 0) { if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
func->m_ml->ml_name); func->m_ml->ml_name);
return NULL; return NULL;
@ -215,7 +216,7 @@ _PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
/* Slow-path: create a temporary tuple */ /* Slow-path: create a temporary tuple */
PyObject *tuple; PyObject *tuple;
if (!(flags & METH_KEYWORDS) && kwargs != NULL && PyDict_Size(kwargs) != 0) { if (!(flags & METH_KEYWORDS) && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s() takes no keyword arguments", "%.200s() takes no keyword arguments",
func->m_ml->ml_name); func->m_ml->ml_name);

View File

@ -1454,7 +1454,7 @@ none_dealloc(PyObject* ignore)
static PyObject * static PyObject *
none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{ {
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments"); PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
return NULL; return NULL;
} }
@ -1573,7 +1573,7 @@ static PyMethodDef notimplemented_methods[] = {
static PyObject * static PyObject *
notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{ {
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments"); PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
return NULL; return NULL;
} }

View File

@ -2423,8 +2423,7 @@ mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
/* now handle kwargs */ /* now handle kwargs */
assert(kwargs == NULL || PyDict_Check(kwargs)); assert(kwargs == NULL || PyDict_Check(kwargs));
len = (kwargs != NULL) ? PyDict_Size(kwargs) : 0; if (kwargs != NULL && PyDict_GET_SIZE(kwargs)) {
if (len > 0) {
PyObject *items = PyDict_Items(kwargs); PyObject *items = PyDict_Items(kwargs);
if (items == NULL) if (items == NULL)
return NULL; return NULL;

View File

@ -981,7 +981,7 @@ set_update_internal(PySetObject *so, PyObject *other)
PyObject *value; PyObject *value;
Py_ssize_t pos = 0; Py_ssize_t pos = 0;
Py_hash_t hash; Py_hash_t hash;
Py_ssize_t dictsize = PyDict_Size(other); Py_ssize_t dictsize = PyDict_GET_SIZE(other);
/* Do one big resize at the start, rather than /* Do one big resize at the start, rather than
* incrementally resizing as we insert new keys. Expect * incrementally resizing as we insert new keys. Expect

View File

@ -19,7 +19,7 @@ this type and there is exactly one in existence.
static PyObject * static PyObject *
ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{ {
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments"); PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments");
return NULL; return NULL;
} }

View File

@ -902,7 +902,7 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (type == &PyType_Type && if (type == &PyType_Type &&
PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
(kwds == NULL || (kwds == NULL ||
(PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) (PyDict_Check(kwds) && PyDict_GET_SIZE(kwds) == 0)))
return obj; return obj;
/* If the returned object is not an instance of type, /* If the returned object is not an instance of type,
@ -1585,7 +1585,7 @@ set_mro_error(PyObject *to_merge, int *remain)
} }
} }
} }
n = PyDict_Size(set); n = PyDict_GET_SIZE(set);
off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
consistent method resolution\norder (MRO) for bases"); consistent method resolution\norder (MRO) for bases");
@ -2187,7 +2187,7 @@ type_init(PyObject *cls, PyObject *args, PyObject *kwds)
assert(kwds == NULL || PyDict_Check(kwds)); assert(kwds == NULL || PyDict_Check(kwds));
if (kwds != NULL && PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && if (kwds != NULL && PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
PyDict_Check(kwds) && PyDict_Size(kwds) != 0) { PyDict_Check(kwds) && PyDict_GET_SIZE(kwds) != 0) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"type.__init__() takes no keyword arguments"); "type.__init__() takes no keyword arguments");
return -1; return -1;
@ -2272,7 +2272,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
Note: We don't call PyType_CheckExact as that also allows subclasses */ Note: We don't call PyType_CheckExact as that also allows subclasses */
if (metatype == &PyType_Type) { if (metatype == &PyType_Type) {
const Py_ssize_t nargs = PyTuple_GET_SIZE(args); const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_GET_SIZE(kwds);
if (nargs == 1 && nkwds == 0) { if (nargs == 1 && nkwds == 0) {
PyObject *x = PyTuple_GET_ITEM(args, 0); PyObject *x = PyTuple_GET_ITEM(args, 0);
@ -3416,7 +3416,7 @@ static int
excess_args(PyObject *args, PyObject *kwds) excess_args(PyObject *args, PyObject *kwds)
{ {
return PyTuple_GET_SIZE(args) || return PyTuple_GET_SIZE(args) ||
(kwds && PyDict_Check(kwds) && PyDict_Size(kwds)); (kwds && PyDict_Check(kwds) && PyDict_GET_SIZE(kwds));
} }
static int static int
@ -3894,7 +3894,7 @@ _PyObject_GetState(PyObject *obj, int required)
We also return None if the dict is empty to make the behavior We also return None if the dict is empty to make the behavior
consistent regardless whether the dict was initialized or not. consistent regardless whether the dict was initialized or not.
This make unit testing easier. */ This make unit testing easier. */
if (dict != NULL && *dict != NULL && PyDict_Size(*dict) > 0) { if (dict != NULL && *dict != NULL && PyDict_GET_SIZE(*dict)) {
state = *dict; state = *dict;
} }
else { else {
@ -3983,7 +3983,7 @@ _PyObject_GetState(PyObject *obj, int required)
/* If we found some slot attributes, pack them in a tuple along /* If we found some slot attributes, pack them in a tuple along
the original attribute dictionary. */ the original attribute dictionary. */
if (PyDict_Size(slots) > 0) { if (PyDict_GET_SIZE(slots) > 0) {
PyObject *state2; PyObject *state2;
state2 = PyTuple_Pack(2, state, slots); state2 = PyTuple_Pack(2, state, slots);
@ -4175,7 +4175,7 @@ reduce_newobj(PyObject *obj)
return NULL; return NULL;
} }
hasargs = (args != NULL); hasargs = (args != NULL);
if (kwargs == NULL || PyDict_Size(kwargs) == 0) { if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
_Py_IDENTIFIER(__newobj__); _Py_IDENTIFIER(__newobj__);
PyObject *cls; PyObject *cls;
Py_ssize_t i, n; Py_ssize_t i, n;

View File

@ -5010,7 +5010,7 @@ _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
assert(kwargs == NULL || PyDict_Check(kwargs)); assert(kwargs == NULL || PyDict_Check(kwargs));
if (co->co_kwonlyargcount == 0 && if (co->co_kwonlyargcount == 0 &&
(kwargs == NULL || PyDict_Size(kwargs) == 0) && (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) &&
co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
{ {
/* Fast paths */ /* Fast paths */
@ -5028,7 +5028,7 @@ _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
if (kwargs != NULL) { if (kwargs != NULL) {
Py_ssize_t pos, i; Py_ssize_t pos, i;
nk = PyDict_Size(kwargs); nk = PyDict_GET_SIZE(kwargs);
kwtuple = PyTuple_New(2 * nk); kwtuple = PyTuple_New(2 * nk);
if (kwtuple == NULL) { if (kwtuple == NULL) {

View File

@ -564,7 +564,7 @@ compiler_enter_scope(struct compiler *c, identifier name,
PyObject *tuple, *name, *zero; PyObject *tuple, *name, *zero;
int res; int res;
assert(u->u_scope_type == COMPILER_SCOPE_CLASS); assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
assert(PyDict_Size(u->u_cellvars) == 0); assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
name = _PyUnicode_FromId(&PyId___class__); name = _PyUnicode_FromId(&PyId___class__);
if (!name) { if (!name) {
compiler_unit_free(u); compiler_unit_free(u);
@ -591,7 +591,7 @@ compiler_enter_scope(struct compiler *c, identifier name,
} }
u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
PyDict_Size(u->u_cellvars)); PyDict_GET_SIZE(u->u_cellvars));
if (!u->u_freevars) { if (!u->u_freevars) {
compiler_unit_free(u); compiler_unit_free(u);
return 0; return 0;
@ -1128,7 +1128,7 @@ compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
Py_DECREF(t); Py_DECREF(t);
return -1; return -1;
} }
arg = PyDict_Size(dict); arg = PyDict_GET_SIZE(dict);
v = PyLong_FromSsize_t(arg); v = PyLong_FromSsize_t(arg);
if (!v) { if (!v) {
Py_DECREF(t); Py_DECREF(t);
@ -1999,7 +1999,7 @@ compiler_class(struct compiler *c, stmt_ty s)
} }
else { else {
/* No methods referenced __class__, so just return None */ /* No methods referenced __class__, so just return None */
assert(PyDict_Size(c->u->u_cellvars) == 0); assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
ADDOP_O(c, LOAD_CONST, Py_None, consts); ADDOP_O(c, LOAD_CONST, Py_None, consts);
} }
ADDOP_IN_SCOPE(c, RETURN_VALUE); ADDOP_IN_SCOPE(c, RETURN_VALUE);
@ -5179,7 +5179,7 @@ static PyObject *
dict_keys_inorder(PyObject *dict, Py_ssize_t offset) dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
{ {
PyObject *tuple, *k, *v; PyObject *tuple, *k, *v;
Py_ssize_t i, pos = 0, size = PyDict_Size(dict); Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
tuple = PyTuple_New(size); tuple = PyTuple_New(size);
if (tuple == NULL) if (tuple == NULL)
@ -5203,7 +5203,6 @@ compute_code_flags(struct compiler *c)
{ {
PySTEntryObject *ste = c->u->u_ste; PySTEntryObject *ste = c->u->u_ste;
int flags = 0; int flags = 0;
Py_ssize_t n;
if (ste->ste_type == FunctionBlock) { if (ste->ste_type == FunctionBlock) {
flags |= CO_NEWLOCALS | CO_OPTIMIZED; flags |= CO_NEWLOCALS | CO_OPTIMIZED;
if (ste->ste_nested) if (ste->ste_nested)
@ -5223,16 +5222,9 @@ compute_code_flags(struct compiler *c)
/* (Only) inherit compilerflags in PyCF_MASK */ /* (Only) inherit compilerflags in PyCF_MASK */
flags |= (c->c_flags->cf_flags & PyCF_MASK); flags |= (c->c_flags->cf_flags & PyCF_MASK);
n = PyDict_Size(c->u->u_freevars); if (!PyDict_GET_SIZE(c->u->u_freevars) &&
if (n < 0) !PyDict_GET_SIZE(c->u->u_cellvars)) {
return -1; flags |= CO_NOFREE;
if (n == 0) {
n = PyDict_Size(c->u->u_cellvars);
if (n < 0)
return -1;
if (n == 0) {
flags |= CO_NOFREE;
}
} }
return flags; return flags;
@ -5273,7 +5265,7 @@ makecode(struct compiler *c, struct assembler *a)
if (!freevars) if (!freevars)
goto error; goto error;
nlocals = PyDict_Size(c->u->u_varnames); nlocals = PyDict_GET_SIZE(c->u->u_varnames);
assert(nlocals < INT_MAX); assert(nlocals < INT_MAX);
nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);

View File

@ -1650,7 +1650,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
} }
nargs = PyTuple_GET_SIZE(args); nargs = PyTuple_GET_SIZE(args);
nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords); nkeywords = (keywords == NULL) ? 0 : PyDict_GET_SIZE(keywords);
if (nargs + nkeywords > len) { if (nargs + nkeywords > len) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%s%s takes at most %d argument%s (%zd given)", "%s%s takes at most %d argument%s (%zd given)",
@ -2034,7 +2034,7 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
} }
if (keywords != NULL) { if (keywords != NULL) {
nkeywords = PyDict_Size(keywords); nkeywords = PyDict_GET_SIZE(keywords);
} }
else if (kwnames != NULL) { else if (kwnames != NULL) {
nkeywords = PyTuple_GET_SIZE(kwnames); nkeywords = PyTuple_GET_SIZE(kwnames);
@ -2421,7 +2421,7 @@ _PyArg_NoKeywords(const char *funcname, PyObject *kw)
PyErr_BadInternalCall(); PyErr_BadInternalCall();
return 0; return 0;
} }
if (PyDict_Size(kw) == 0) if (PyDict_GET_SIZE(kw) == 0)
return 1; return 1;
PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments", PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",