bpo-42006: Stop using PyDict_GetItem, PyDict_GetItemString and _PyDict_GetItemId. (GH-22648)

These functions are considered not safe because they suppress all internal errors
and can return wrong result.  PyDict_GetItemString and _PyDict_GetItemId can
also silence current exception in rare cases.

Remove no longer used _PyDict_GetItemId.
Add _PyDict_ContainsId and rename _PyDict_Contains into
_PyDict_Contains_KnownHash.
This commit is contained in:
Serhiy Storchaka 2020-10-26 08:43:39 +02:00 committed by GitHub
parent 96a9eed245
commit fb5db7ec58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 259 additions and 142 deletions

View File

@ -46,7 +46,8 @@ PyAPI_FUNC(int) _PyDict_Next(
/* Get the number of items of a dictionary. */ /* Get the number of items of a dictionary. */
#define PyDict_GET_SIZE(mp) (assert(PyDict_Check(mp)),((PyDictObject *)mp)->ma_used) #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_KnownHash(PyObject *, PyObject *, Py_hash_t);
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, struct _Py_Identifier *);
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);
PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp); PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
@ -63,7 +64,6 @@ PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
argument is raised. argument is raised.
*/ */
PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override); PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
PyAPI_FUNC(PyObject *) _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key);
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item); PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item);
PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, struct _Py_Identifier *key); PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, struct _Py_Identifier *key);

View File

@ -3186,6 +3186,31 @@ dotsep_as_utf8(const char *s)
return utf8; return utf8;
} }
static int
dict_get_item_string(PyObject *dict, const char *key, PyObject **valueobj, const char **valuestr)
{
*valueobj = NULL;
PyObject *keyobj = PyUnicode_FromString(key);
if (keyobj == NULL) {
return -1;
}
PyObject *value = PyDict_GetItemWithError(dict, keyobj);
Py_DECREF(keyobj);
if (value == NULL) {
if (PyErr_Occurred()) {
return -1;
}
return 0;
}
value = PyUnicode_AsUTF8String(value);
if (value == NULL) {
return -1;
}
*valueobj = value;
*valuestr = PyBytes_AS_STRING(value);
return 0;
}
/* Formatted representation of a PyDecObject. */ /* Formatted representation of a PyDecObject. */
static PyObject * static PyObject *
dec_format(PyObject *dec, PyObject *args) dec_format(PyObject *dec, PyObject *args)
@ -3256,24 +3281,12 @@ dec_format(PyObject *dec, PyObject *args)
"optional argument must be a dict"); "optional argument must be a dict");
goto finish; goto finish;
} }
if ((dot = PyDict_GetItemString(override, "decimal_point"))) { if (dict_get_item_string(override, "decimal_point", &dot, &spec.dot) ||
if ((dot = PyUnicode_AsUTF8String(dot)) == NULL) { dict_get_item_string(override, "thousands_sep", &sep, &spec.sep) ||
dict_get_item_string(override, "grouping", &grouping, &spec.grouping))
{
goto finish; goto finish;
} }
spec.dot = PyBytes_AS_STRING(dot);
}
if ((sep = PyDict_GetItemString(override, "thousands_sep"))) {
if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) {
goto finish;
}
spec.sep = PyBytes_AS_STRING(sep);
}
if ((grouping = PyDict_GetItemString(override, "grouping"))) {
if ((grouping = PyUnicode_AsUTF8String(grouping)) == NULL) {
goto finish;
}
spec.grouping = PyBytes_AS_STRING(grouping);
}
if (mpd_validate_lconv(&spec) < 0) { if (mpd_validate_lconv(&spec) < 0) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"invalid override dict"); "invalid override dict");

View File

@ -816,10 +816,14 @@ local_clear(localobject *self)
for(tstate = PyInterpreterState_ThreadHead(tstate->interp); for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
tstate; tstate;
tstate = PyThreadState_Next(tstate)) tstate = PyThreadState_Next(tstate))
if (tstate->dict && PyDict_GetItem(tstate->dict, self->key)) { if (tstate->dict) {
if (PyDict_DelItem(tstate->dict, self->key)) { PyObject *v = _PyDict_Pop(tstate->dict, self->key, Py_None);
if (v == NULL) {
PyErr_Clear(); PyErr_Clear();
} }
else {
Py_DECREF(v);
}
} }
} }
return 0; return 0;

View File

@ -722,17 +722,16 @@ zoneinfo__unpickle(PyTypeObject *cls, PyObject *args)
static PyObject * static PyObject *
load_timedelta(long seconds) load_timedelta(long seconds)
{ {
PyObject *rv = NULL; PyObject *rv;
PyObject *pyoffset = PyLong_FromLong(seconds); PyObject *pyoffset = PyLong_FromLong(seconds);
if (pyoffset == NULL) { if (pyoffset == NULL) {
return NULL; return NULL;
} }
int contains = PyDict_Contains(TIMEDELTA_CACHE, pyoffset); rv = PyDict_GetItemWithError(TIMEDELTA_CACHE, pyoffset);
if (contains == -1) { if (rv == NULL) {
if (PyErr_Occurred()) {
goto error; goto error;
} }
if (!contains) {
PyObject *tmp = PyDateTimeAPI->Delta_FromDelta( PyObject *tmp = PyDateTimeAPI->Delta_FromDelta(
0, seconds, 0, 1, PyDateTimeAPI->DeltaType); 0, seconds, 0, 1, PyDateTimeAPI->DeltaType);
@ -743,12 +742,9 @@ load_timedelta(long seconds)
rv = PyDict_SetDefault(TIMEDELTA_CACHE, pyoffset, tmp); rv = PyDict_SetDefault(TIMEDELTA_CACHE, pyoffset, tmp);
Py_DECREF(tmp); Py_DECREF(tmp);
} }
else {
rv = PyDict_GetItem(TIMEDELTA_CACHE, pyoffset);
}
Py_XINCREF(rv);
Py_DECREF(pyoffset); Py_DECREF(pyoffset);
Py_INCREF(rv);
return rv; return rv;
error: error:
Py_DECREF(pyoffset); Py_DECREF(pyoffset);

View File

@ -1427,10 +1427,9 @@ signal_exec(PyObject *m)
return -1; return -1;
#endif #endif
IntHandler = PyDict_GetItemString(d, "default_int_handler"); IntHandler = PyMapping_GetItemString(d, "default_int_handler");
if (!IntHandler) if (!IntHandler)
return -1; return -1;
Py_INCREF(IntHandler);
_Py_atomic_store_relaxed(&Handlers[0].tripped, 0); _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
for (int i = 1; i < NSIG; i++) { for (int i = 1; i < NSIG; i++) {

View File

@ -324,7 +324,7 @@ static FlagRuntimeInfo win_runtime_flags[] = {
{14393, "TCP_FASTOPEN"} {14393, "TCP_FASTOPEN"}
}; };
static void static int
remove_unusable_flags(PyObject *m) remove_unusable_flags(PyObject *m)
{ {
PyObject *dict; PyObject *dict;
@ -333,7 +333,7 @@ remove_unusable_flags(PyObject *m)
dict = PyModule_GetDict(m); dict = PyModule_GetDict(m);
if (dict == NULL) { if (dict == NULL) {
return; return -1;
} }
/* set to Windows 10, except BuildNumber. */ /* set to Windows 10, except BuildNumber. */
@ -359,19 +359,19 @@ remove_unusable_flags(PyObject *m)
break; break;
} }
else { else {
if (PyDict_GetItemString( PyObject *flag_name = PyUnicode_FromString(win_runtime_flags[i].flag_name);
dict, if (flag_name == NULL) {
win_runtime_flags[i].flag_name) != NULL) return -1;
{ }
if (PyDict_DelItemString( PyObject *v = _PyDict_Pop(dict, flag_name, Py_None);
dict, Py_DECREF(flag_name);
win_runtime_flags[i].flag_name)) if (v == NULL) {
{ return -1;
PyErr_Clear(); }
} Py_DECREF(v);
}
} }
} }
return 0;
} }
#endif #endif
@ -8382,7 +8382,10 @@ PyInit__socket(void)
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
/* remove some flags on older version Windows during run-time */ /* remove some flags on older version Windows during run-time */
remove_unusable_flags(m); if (remove_unusable_flags(m) < 0) {
Py_DECREF(m);
return NULL;
}
#endif #endif
return m; return m;

View File

@ -3434,7 +3434,7 @@ PyDict_Contains(PyObject *op, PyObject *key)
/* Internal version of PyDict_Contains used when the hash value is already known */ /* Internal version of PyDict_Contains used when the hash value is already known */
int int
_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash) _PyDict_Contains_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
{ {
PyDictObject *mp = (PyDictObject *)op; PyDictObject *mp = (PyDictObject *)op;
PyObject *value; PyObject *value;
@ -3446,6 +3446,16 @@ _PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
return (ix != DKIX_EMPTY && value != NULL); return (ix != DKIX_EMPTY && value != NULL);
} }
int
_PyDict_ContainsId(PyObject *op, struct _Py_Identifier *key)
{
PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
if (kv == NULL) {
return -1;
}
return PyDict_Contains(op, kv);
}
/* Hack to implement "key in dict" */ /* Hack to implement "key in dict" */
static PySequenceMethods dict_as_sequence = { static PySequenceMethods dict_as_sequence = {
0, /* sq_length */ 0, /* sq_length */
@ -3590,18 +3600,6 @@ PyTypeObject PyDict_Type = {
.tp_vectorcall = dict_vectorcall, .tp_vectorcall = dict_vectorcall,
}; };
PyObject *
_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
{
PyObject *kv;
kv = _PyUnicode_FromId(key); /* borrowed */
if (kv == NULL) {
PyErr_Clear();
return NULL;
}
return PyDict_GetItem(dp, kv);
}
/* For backward compatibility with old dictionary interface */ /* For backward compatibility with old dictionary interface */
PyObject * PyObject *

View File

@ -477,10 +477,12 @@ PyModule_GetNameObject(PyObject *m)
} }
d = ((PyModuleObject *)m)->md_dict; d = ((PyModuleObject *)m)->md_dict;
if (d == NULL || if (d == NULL ||
(name = _PyDict_GetItemId(d, &PyId___name__)) == NULL || (name = _PyDict_GetItemIdWithError(d, &PyId___name__)) == NULL ||
!PyUnicode_Check(name)) !PyUnicode_Check(name))
{ {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_SystemError, "nameless module"); PyErr_SetString(PyExc_SystemError, "nameless module");
}
return NULL; return NULL;
} }
Py_INCREF(name); Py_INCREF(name);
@ -509,10 +511,12 @@ PyModule_GetFilenameObject(PyObject *m)
} }
d = ((PyModuleObject *)m)->md_dict; d = ((PyModuleObject *)m)->md_dict;
if (d == NULL || if (d == NULL ||
(fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL || (fileobj = _PyDict_GetItemIdWithError(d, &PyId___file__)) == NULL ||
!PyUnicode_Check(fileobj)) !PyUnicode_Check(fileobj))
{ {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_SystemError, "module filename missing"); PyErr_SetString(PyExc_SystemError, "module filename missing");
}
return NULL; return NULL;
} }
Py_INCREF(fileobj); Py_INCREF(fileobj);
@ -721,14 +725,21 @@ module_getattro(PyModuleObject *m, PyObject *name)
PyErr_Clear(); PyErr_Clear();
if (m->md_dict) { if (m->md_dict) {
_Py_IDENTIFIER(__getattr__); _Py_IDENTIFIER(__getattr__);
getattr = _PyDict_GetItemId(m->md_dict, &PyId___getattr__); getattr = _PyDict_GetItemIdWithError(m->md_dict, &PyId___getattr__);
if (getattr) { if (getattr) {
return PyObject_CallOneArg(getattr, name); return PyObject_CallOneArg(getattr, name);
} }
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__); if (PyErr_Occurred()) {
return NULL;
}
mod_name = _PyDict_GetItemIdWithError(m->md_dict, &PyId___name__);
if (mod_name && PyUnicode_Check(mod_name)) { if (mod_name && PyUnicode_Check(mod_name)) {
Py_INCREF(mod_name); Py_INCREF(mod_name);
PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__); PyObject *spec = _PyDict_GetItemIdWithError(m->md_dict, &PyId___spec__);
if (spec == NULL && PyErr_Occurred()) {
Py_DECREF(mod_name);
return NULL;
}
Py_XINCREF(spec); Py_XINCREF(spec);
if (_PyModuleSpec_IsInitializing(spec)) { if (_PyModuleSpec_IsInitializing(spec)) {
PyErr_Format(PyExc_AttributeError, PyErr_Format(PyExc_AttributeError,
@ -746,6 +757,9 @@ module_getattro(PyModuleObject *m, PyObject *name)
Py_DECREF(mod_name); Py_DECREF(mod_name);
return NULL; return NULL;
} }
else if (PyErr_Occurred()) {
return NULL;
}
} }
PyErr_Format(PyExc_AttributeError, PyErr_Format(PyExc_AttributeError,
"module has no attribute '%U'", name); "module has no attribute '%U'", name);

View File

@ -1498,7 +1498,7 @@ set_difference(PySetObject *so, PyObject *other)
while (set_next(so, &pos, &entry)) { while (set_next(so, &pos, &entry)) {
key = entry->key; key = entry->key;
hash = entry->hash; hash = entry->hash;
rv = _PyDict_Contains(other, key, hash); rv = _PyDict_Contains_KnownHash(other, key, hash);
if (rv < 0) { if (rv < 0) {
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;

View File

@ -19,20 +19,33 @@ static const char unnamed_fields_key[] = "n_unnamed_fields";
/* Fields with this name have only a field index, not a field name. /* Fields with this name have only a field index, not a field name.
They are only allowed for indices < n_visible_fields. */ They are only allowed for indices < n_visible_fields. */
const char * const PyStructSequence_UnnamedField = "unnamed field"; const char * const PyStructSequence_UnnamedField = "unnamed field";
_Py_IDENTIFIER(n_sequence_fields); _Py_IDENTIFIER(n_sequence_fields);
_Py_IDENTIFIER(n_fields); _Py_IDENTIFIER(n_fields);
_Py_IDENTIFIER(n_unnamed_fields); _Py_IDENTIFIER(n_unnamed_fields);
#define VISIBLE_SIZE(op) Py_SIZE(op) static ssize_t
#define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \ get_type_attr_as_size(PyTypeObject *tp, _Py_Identifier *id)
_PyDict_GetItemId((tp)->tp_dict, &PyId_n_sequence_fields)) {
PyObject *name = _PyUnicode_FromId(id);
if (name == NULL) {
return -1;
}
PyObject *v = PyDict_GetItemWithError(tp->tp_dict, name);
if (v == NULL && !PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError,
"Missed attribute '%U' of type %s",
name, tp->tp_name);
}
return PyLong_AsSsize_t(v);
}
#define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \ #define VISIBLE_SIZE(op) Py_SIZE(op)
_PyDict_GetItemId((tp)->tp_dict, &PyId_n_fields)) #define VISIBLE_SIZE_TP(tp) get_type_attr_as_size(tp, &PyId_n_sequence_fields)
#define REAL_SIZE_TP(tp) get_type_attr_as_size(tp, &PyId_n_fields)
#define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op)) #define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
#define UNNAMED_FIELDS_TP(tp) PyLong_AsSsize_t( \ #define UNNAMED_FIELDS_TP(tp) get_type_attr_as_size(tp, &PyId_n_unnamed_fields)
_PyDict_GetItemId((tp)->tp_dict, &PyId_n_unnamed_fields))
#define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op)) #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
@ -41,13 +54,20 @@ PyStructSequence_New(PyTypeObject *type)
{ {
PyStructSequence *obj; PyStructSequence *obj;
Py_ssize_t size = REAL_SIZE_TP(type), i; Py_ssize_t size = REAL_SIZE_TP(type), i;
if (size < 0) {
return NULL;
}
Py_ssize_t vsize = VISIBLE_SIZE_TP(type);
if (vsize < 0) {
return NULL;
}
obj = PyObject_GC_NewVar(PyStructSequence, type, size); obj = PyObject_GC_NewVar(PyStructSequence, type, size);
if (obj == NULL) if (obj == NULL)
return NULL; return NULL;
/* Hack the size of the variable object, so invisible fields don't appear /* Hack the size of the variable object, so invisible fields don't appear
to Python code. */ to Python code. */
Py_SET_SIZE(obj, VISIBLE_SIZE_TP(type)); Py_SET_SIZE(obj, vsize);
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
obj->ob_item[i] = NULL; obj->ob_item[i] = NULL;
@ -121,6 +141,19 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
PyStructSequence *res = NULL; PyStructSequence *res = NULL;
Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
min_len = VISIBLE_SIZE_TP(type);
if (min_len < 0) {
return NULL;
}
max_len = REAL_SIZE_TP(type);
if (max_len < 0) {
return NULL;
}
n_unnamed_fields = UNNAMED_FIELDS_TP(type);
if (n_unnamed_fields < 0) {
return NULL;
}
arg = PySequence_Fast(arg, "constructor requires a sequence"); arg = PySequence_Fast(arg, "constructor requires a sequence");
if (!arg) { if (!arg) {
@ -136,10 +169,6 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
} }
len = PySequence_Fast_GET_SIZE(arg); len = PySequence_Fast_GET_SIZE(arg);
min_len = VISIBLE_SIZE_TP(type);
max_len = REAL_SIZE_TP(type);
n_unnamed_fields = UNNAMED_FIELDS_TP(type);
if (min_len != max_len) { if (min_len != max_len) {
if (len < min_len) { if (len < min_len) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
@ -177,18 +206,26 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
Py_INCREF(v); Py_INCREF(v);
res->ob_item[i] = v; res->ob_item[i] = v;
} }
Py_DECREF(arg);
for (; i < max_len; ++i) { for (; i < max_len; ++i) {
if (dict && (ob = PyDict_GetItemString( if (dict == NULL) {
dict, type->tp_members[i-n_unnamed_fields].name))) { ob = Py_None;
} }
else { else {
ob = _PyDict_GetItemStringWithError(dict,
type->tp_members[i-n_unnamed_fields].name);
if (ob == NULL) {
if (PyErr_Occurred()) {
Py_DECREF(res);
return NULL;
}
ob = Py_None; ob = Py_None;
} }
}
Py_INCREF(ob); Py_INCREF(ob);
res->ob_item[i] = ob; res->ob_item[i] = ob;
} }
Py_DECREF(arg);
_PyObject_GC_TRACK(res); _PyObject_GC_TRACK(res);
return (PyObject*) res; return (PyObject*) res;
} }
@ -288,8 +325,14 @@ structseq_reduce(PyStructSequence* self, PyObject *Py_UNUSED(ignored))
Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i; Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i;
n_fields = REAL_SIZE(self); n_fields = REAL_SIZE(self);
if (n_fields < 0) {
return NULL;
}
n_visible_fields = VISIBLE_SIZE(self); n_visible_fields = VISIBLE_SIZE(self);
n_unnamed_fields = UNNAMED_FIELDS(self); n_unnamed_fields = UNNAMED_FIELDS(self);
if (n_unnamed_fields < 0) {
return NULL;
}
tup = _PyTuple_FromArray(self->ob_item, n_visible_fields); tup = _PyTuple_FromArray(self->ob_item, n_visible_fields);
if (!tup) if (!tup)
goto error; goto error;

View File

@ -4767,8 +4767,11 @@ object___reduce_ex___impl(PyObject *self, int protocol)
_Py_IDENTIFIER(__reduce__); _Py_IDENTIFIER(__reduce__);
if (objreduce == NULL) { if (objreduce == NULL) {
objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, objreduce = _PyDict_GetItemIdWithError(PyBaseObject_Type.tp_dict,
&PyId___reduce__); &PyId___reduce__);
if (objreduce == NULL && PyErr_Occurred()) {
return NULL;
}
} }
if (_PyObject_LookupAttrId(self, &PyId___reduce__, &reduce) < 0) { if (_PyObject_LookupAttrId(self, &PyId___reduce__, &reduce) < 0) {
@ -5181,14 +5184,14 @@ overrides_hash(PyTypeObject *type)
_Py_IDENTIFIER(__eq__); _Py_IDENTIFIER(__eq__);
assert(dict != NULL); assert(dict != NULL);
if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) int r = _PyDict_ContainsId(dict, &PyId___eq__);
return 1; if (r == 0) {
if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) r = _PyDict_ContainsId(dict, &PyId___hash__);
return 1; }
return 0; return r;
} }
static void static int
inherit_slots(PyTypeObject *type, PyTypeObject *base) inherit_slots(PyTypeObject *type, PyTypeObject *base)
{ {
PyTypeObject *basebase; PyTypeObject *basebase;
@ -5331,13 +5334,18 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
/* Copy comparison-related slots only when /* Copy comparison-related slots only when
not overriding them anywhere */ not overriding them anywhere */
if (type->tp_richcompare == NULL && if (type->tp_richcompare == NULL &&
type->tp_hash == NULL && type->tp_hash == NULL)
!overrides_hash(type))
{ {
int r = overrides_hash(type);
if (r < 0) {
return -1;
}
if (!r) {
type->tp_richcompare = base->tp_richcompare; type->tp_richcompare = base->tp_richcompare;
type->tp_hash = base->tp_hash; type->tp_hash = base->tp_hash;
} }
} }
}
{ {
COPYSLOT(tp_iter); COPYSLOT(tp_iter);
COPYSLOT(tp_iternext); COPYSLOT(tp_iternext);
@ -5378,6 +5386,7 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
* obvious to be done -- the type is on its own. * obvious to be done -- the type is on its own.
*/ */
} }
return 0;
} }
static int add_operators(PyTypeObject *); static int add_operators(PyTypeObject *);
@ -5507,8 +5516,11 @@ PyType_Ready(PyTypeObject *type)
n = PyTuple_GET_SIZE(bases); n = PyTuple_GET_SIZE(bases);
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
PyObject *b = PyTuple_GET_ITEM(bases, i); PyObject *b = PyTuple_GET_ITEM(bases, i);
if (PyType_Check(b)) if (PyType_Check(b)) {
inherit_slots(type, (PyTypeObject *)b); if (inherit_slots(type, (PyTypeObject *)b) < 0) {
goto error;
}
}
} }
/* All bases of statically allocated type should be statically allocated */ /* All bases of statically allocated type should be statically allocated */

View File

@ -1446,11 +1446,18 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
} }
#ifdef LLTRACE #ifdef LLTRACE
lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; {
int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
if (r < 0) {
goto exit_eval_frame;
}
lltrace = r;
}
#endif #endif
if (throwflag) /* support for generator.throw() */ if (throwflag) { /* support for generator.throw() */
goto error; goto error;
}
#ifdef Py_DEBUG #ifdef Py_DEBUG
/* _PyEval_EvalFrameDefault() must not be called with an exception set, /* _PyEval_EvalFrameDefault() must not be called with an exception set,

View File

@ -489,8 +489,8 @@ dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
/* XXX this should probably be a macro in symtable.h */ /* XXX this should probably be a macro in symtable.h */
long vi; long vi;
k = PyList_GET_ITEM(sorted_keys, key_i); k = PyList_GET_ITEM(sorted_keys, key_i);
v = PyDict_GetItem(src, k); v = PyDict_GetItemWithError(src, k);
assert(PyLong_Check(v)); assert(v && PyLong_Check(v));
vi = PyLong_AS_LONG(v); vi = PyLong_AS_LONG(v);
scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
@ -1889,7 +1889,7 @@ static int
compiler_lookup_arg(PyObject *dict, PyObject *name) compiler_lookup_arg(PyObject *dict, PyObject *name)
{ {
PyObject *v; PyObject *v;
v = PyDict_GetItem(dict, name); v = PyDict_GetItemWithError(dict, name);
if (v == NULL) if (v == NULL)
return -1; return -1;
return PyLong_AS_LONG(v); return PyLong_AS_LONG(v);

View File

@ -160,7 +160,7 @@ init_importlib(PyThreadState *tstate, PyObject *sysmod)
interp->importlib = importlib; interp->importlib = importlib;
Py_INCREF(interp->importlib); Py_INCREF(interp->importlib);
interp->import_func = PyDict_GetItemString(interp->builtins, "__import__"); interp->import_func = _PyDict_GetItemStringWithError(interp->builtins, "__import__");
if (interp->import_func == NULL) if (interp->import_func == NULL)
return _PyStatus_ERR("__import__ not found"); return _PyStatus_ERR("__import__ not found");
Py_INCREF(interp->import_func); Py_INCREF(interp->import_func);
@ -1683,7 +1683,10 @@ add_main_module(PyInterpreterState *interp)
} }
Py_DECREF(ann_dict); Py_DECREF(ann_dict);
if (PyDict_GetItemString(d, "__builtins__") == NULL) { if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) {
if (PyErr_Occurred()) {
return _PyStatus_ERR("Failed to test __main__.__builtins__");
}
PyObject *bimod = PyImport_ImportModule("builtins"); PyObject *bimod = PyImport_ImportModule("builtins");
if (bimod == NULL) { if (bimod == NULL) {
return _PyStatus_ERR("Failed to retrieve builtins module"); return _PyStatus_ERR("Failed to retrieve builtins module");
@ -1700,8 +1703,11 @@ add_main_module(PyInterpreterState *interp)
* be set if __main__ gets further initialized later in the startup * be set if __main__ gets further initialized later in the startup
* process. * process.
*/ */
loader = PyDict_GetItemString(d, "__loader__"); loader = _PyDict_GetItemStringWithError(d, "__loader__");
if (loader == NULL || loader == Py_None) { if (loader == NULL || loader == Py_None) {
if (PyErr_Occurred()) {
return _PyStatus_ERR("Failed to test __main__.__loader__");
}
PyObject *loader = PyObject_GetAttrString(interp->importlib, PyObject *loader = PyObject_GetAttrString(interp->importlib,
"BuiltinImporter"); "BuiltinImporter");
if (loader == NULL) { if (loader == NULL) {

View File

@ -351,7 +351,10 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
return -1; return -1;
Py_INCREF(m); Py_INCREF(m);
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
if (PyDict_GetItemString(d, "__file__") == NULL) { if (_PyDict_GetItemStringWithError(d, "__file__") == NULL) {
if (PyErr_Occurred()) {
goto done;
}
PyObject *f; PyObject *f;
f = PyUnicode_DecodeFSDefault(filename); f = PyUnicode_DecodeFSDefault(filename);
if (f == NULL) if (f == NULL)
@ -1116,9 +1119,11 @@ run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, Py
_Py_UnhandledKeyboardInterrupt = 0; _Py_UnhandledKeyboardInterrupt = 0;
/* Set globals['__builtins__'] if it doesn't exist */ /* Set globals['__builtins__'] if it doesn't exist */
if (globals != NULL && PyDict_GetItemString(globals, "__builtins__") == NULL) { if (globals != NULL && _PyDict_GetItemStringWithError(globals, "__builtins__") == NULL) {
if (PyDict_SetItemString(globals, "__builtins__", if (PyErr_Occurred() ||
tstate->interp->builtins) < 0) { PyDict_SetItemString(globals, "__builtins__",
tstate->interp->builtins) < 0)
{
return NULL; return NULL;
} }
} }

View File

@ -392,7 +392,7 @@ PySymtable_Lookup(struct symtable *st, void *key)
static long static long
_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name) _PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
{ {
PyObject *v = PyDict_GetItem(ste->ste_symbols, name); PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
if (!v) if (!v)
return 0; return 0;
assert(PyLong_Check(v)); assert(PyLong_Check(v));
@ -634,7 +634,7 @@ update_symbols(PyObject *symbols, PyObject *scopes,
long scope, flags; long scope, flags;
assert(PyLong_Check(v)); assert(PyLong_Check(v));
flags = PyLong_AS_LONG(v); flags = PyLong_AS_LONG(v);
v_scope = PyDict_GetItem(scopes, name); v_scope = PyDict_GetItemWithError(scopes, name);
assert(v_scope && PyLong_Check(v_scope)); assert(v_scope && PyLong_Check(v_scope));
scope = PyLong_AS_LONG(v_scope); scope = PyLong_AS_LONG(v_scope);
flags |= (scope << SCOPE_OFFSET); flags |= (scope << SCOPE_OFFSET);
@ -1071,9 +1071,12 @@ symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _s
/* XXX need to update DEF_GLOBAL for other flags too; /* XXX need to update DEF_GLOBAL for other flags too;
perhaps only DEF_FREE_GLOBAL */ perhaps only DEF_FREE_GLOBAL */
val = flag; val = flag;
if ((o = PyDict_GetItem(st->st_global, mangled))) { if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
val |= PyLong_AS_LONG(o); val |= PyLong_AS_LONG(o);
} }
else if (PyErr_Occurred()) {
goto error;
}
o = PyLong_FromLong(val); o = PyLong_FromLong(val);
if (o == NULL) if (o == NULL)
goto error; goto error;

View File

@ -68,7 +68,13 @@ sys_get_object_id(PyThreadState *tstate, _Py_Identifier *key)
if (sd == NULL) { if (sd == NULL) {
return NULL; return NULL;
} }
return _PyDict_GetItemId(sd, key); PyObject *exc_type, *exc_value, *exc_tb;
_PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
PyObject *value = _PyDict_GetItemIdWithError(sd, key);
/* XXX Suppress a new exception if it was raised and restore
* the old one. */
_PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
return value;
} }
PyObject * PyObject *
@ -86,24 +92,39 @@ PySys_GetObject(const char *name)
if (sd == NULL) { if (sd == NULL) {
return NULL; return NULL;
} }
return PyDict_GetItemString(sd, name); PyObject *exc_type, *exc_value, *exc_tb;
_PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
PyObject *value = _PyDict_GetItemStringWithError(sd, name);
/* XXX Suppress a new exception if it was raised and restore
* the old one. */
_PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
return value;
}
static int
sys_set_object(PyThreadState *tstate, PyObject *key, PyObject *v)
{
if (key == NULL) {
return -1;
}
PyObject *sd = tstate->interp->sysdict;
if (v == NULL) {
v = _PyDict_Pop(sd, key, Py_None);
if (v == NULL) {
return -1;
}
Py_DECREF(v);
return 0;
}
else {
return PyDict_SetItem(sd, key, v);
}
} }
static int static int
sys_set_object_id(PyThreadState *tstate, _Py_Identifier *key, PyObject *v) sys_set_object_id(PyThreadState *tstate, _Py_Identifier *key, PyObject *v)
{ {
PyObject *sd = tstate->interp->sysdict; return sys_set_object(tstate, _PyUnicode_FromId(key), v);
if (v == NULL) {
if (_PyDict_GetItemId(sd, key) == NULL) {
return 0;
}
else {
return _PyDict_DelItemId(sd, key);
}
}
else {
return _PyDict_SetItemId(sd, key, v);
}
} }
int int
@ -114,27 +135,20 @@ _PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
} }
static int static int
sys_set_object(PyThreadState *tstate, const char *name, PyObject *v) sys_set_object_str(PyThreadState *tstate, const char *name, PyObject *v)
{ {
PyObject *sd = tstate->interp->sysdict; PyObject *key = v ? PyUnicode_InternFromString(name)
if (v == NULL) { : PyUnicode_FromString(name);
if (PyDict_GetItemString(sd, name) == NULL) { int r = sys_set_object(tstate, key, v);
return 0; Py_XDECREF(key);
} return r;
else {
return PyDict_DelItemString(sd, name);
}
}
else {
return PyDict_SetItemString(sd, name, v);
}
} }
int int
PySys_SetObject(const char *name, PyObject *v) PySys_SetObject(const char *name, PyObject *v)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
return sys_set_object(tstate, name, v); return sys_set_object_str(tstate, name, v);
} }
@ -3083,7 +3097,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
if (av == NULL) { if (av == NULL) {
Py_FatalError("no mem for sys.argv"); Py_FatalError("no mem for sys.argv");
} }
if (sys_set_object(tstate, "argv", av) != 0) { if (sys_set_object_str(tstate, "argv", av) != 0) {
Py_DECREF(av); Py_DECREF(av);
Py_FatalError("can't assign sys.argv"); Py_FatalError("can't assign sys.argv");
} }