[Python-checkins] bpo-35444: Fix error handling when fail to look up builtin "getattr". (GH-11047) (GH-11107)
Serhiy Storchaka
webhook-mailer at python.org
Tue Dec 11 03:51:33 EST 2018
https://github.com/python/cpython/commit/3cae16d2e98ffaa89ddd311df70a857dfaff4020
commit: 3cae16d2e98ffaa89ddd311df70a857dfaff4020
branch: 3.7
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2018-12-11T10:51:27+02:00
summary:
bpo-35444: Fix error handling when fail to look up builtin "getattr". (GH-11047) (GH-11107)
(cherry picked from commit bb86bf4c4eaa30b1f5192dab9f389ce0bb61114d)
files:
A Misc/NEWS.d/next/Core and Builtins/2018-12-09-13-09-39.bpo-35444.9kYn4V.rst
M Include/ceval.h
M Modules/_pickle.c
M Objects/classobject.c
M Objects/descrobject.c
M Objects/methodobject.c
M Python/ceval.c
diff --git a/Include/ceval.h b/Include/ceval.h
index bce8a0beed85..11283c0a570b 100644
--- a/Include/ceval.h
+++ b/Include/ceval.h
@@ -48,10 +48,12 @@ PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void);
+#ifndef Py_LIMITED_API
+/* Helper to look up a builtin object */
+PyAPI_FUNC(PyObject *) _PyEval_GetBuiltinId(_Py_Identifier *);
/* Look at the current frame's (if any) code's co_flags, and turn on
the corresponding compiler flags in cf->cf_flags. Return 1 if any
flag was set, else return 0. */
-#ifndef Py_LIMITED_API
PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
#endif
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-12-09-13-09-39.bpo-35444.9kYn4V.rst b/Misc/NEWS.d/next/Core and Builtins/2018-12-09-13-09-39.bpo-35444.9kYn4V.rst
new file mode 100644
index 000000000000..22c3969f1276
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-12-09-13-09-39.bpo-35444.9kYn4V.rst
@@ -0,0 +1,2 @@
+Fixed error handling in pickling methods when fail to look up builtin
+"getattr".
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 57bb771addde..60ef921521ef 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -207,19 +207,15 @@ _Pickle_ClearState(PickleState *st)
static int
_Pickle_InitState(PickleState *st)
{
- PyObject *builtins;
PyObject *copyreg = NULL;
PyObject *compat_pickle = NULL;
PyObject *codecs = NULL;
PyObject *functools = NULL;
+ _Py_IDENTIFIER(getattr);
- builtins = PyEval_GetBuiltins();
- if (builtins == NULL)
- goto error;
- st->getattr = PyDict_GetItemString(builtins, "getattr");
+ st->getattr = _PyEval_GetBuiltinId(&PyId_getattr);
if (st->getattr == NULL)
goto error;
- Py_INCREF(st->getattr);
copyreg = PyImport_ImportModule("copyreg");
if (!copyreg)
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 3dc23b796c2c..f45d6ae809bb 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -77,8 +77,6 @@ method_reduce(PyMethodObject *im)
{
PyObject *self = PyMethod_GET_SELF(im);
PyObject *func = PyMethod_GET_FUNCTION(im);
- PyObject *builtins;
- PyObject *getattr;
PyObject *funcname;
_Py_IDENTIFIER(getattr);
@@ -86,9 +84,8 @@ method_reduce(PyMethodObject *im)
if (funcname == NULL) {
return NULL;
}
- builtins = PyEval_GetBuiltins();
- getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
- return Py_BuildValue("O(ON)", getattr, self, funcname);
+ return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
+ self, funcname);
}
static PyMethodDef method_methods[] = {
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index f19d07aa2568..277fed995615 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -450,14 +450,9 @@ descr_get_qualname(PyDescrObject *descr, void *Py_UNUSED(ignored))
static PyObject *
descr_reduce(PyDescrObject *descr)
{
- PyObject *builtins;
- PyObject *getattr;
_Py_IDENTIFIER(getattr);
-
- builtins = PyEval_GetBuiltins();
- getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
- return Py_BuildValue("O(OO)", getattr, PyDescr_TYPE(descr),
- PyDescr_NAME(descr));
+ return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_getattr),
+ PyDescr_TYPE(descr), PyDescr_NAME(descr));
}
static PyMethodDef descr_methods[] = {
@@ -1088,13 +1083,9 @@ wrapper_repr(wrapperobject *wp)
static PyObject *
wrapper_reduce(wrapperobject *wp)
{
- PyObject *builtins;
- PyObject *getattr;
_Py_IDENTIFIER(getattr);
-
- builtins = PyEval_GetBuiltins();
- getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
- return Py_BuildValue("O(OO)", getattr, wp->self, PyDescr_NAME(wp->descr));
+ return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_getattr),
+ wp->self, PyDescr_NAME(wp->descr));
}
static PyMethodDef wrapper_methods[] = {
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
index 2cf314660d00..f9bac1922dc7 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -103,16 +103,13 @@ meth_dealloc(PyCFunctionObject *m)
static PyObject *
meth_reduce(PyCFunctionObject *m)
{
- PyObject *builtins;
- PyObject *getattr;
_Py_IDENTIFIER(getattr);
if (m->m_self == NULL || PyModule_Check(m->m_self))
return PyUnicode_FromString(m->m_ml->ml_name);
- builtins = PyEval_GetBuiltins();
- getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
- return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
+ return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr),
+ m->m_self, m->m_ml->ml_name);
}
static PyMethodDef meth_methods[] = {
diff --git a/Python/ceval.c b/Python/ceval.c
index 00b2e6d85f48..c394b9b4827b 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -4419,6 +4419,20 @@ PyEval_GetBuiltins(void)
return current_frame->f_builtins;
}
+/* Convenience function to get a builtin from its name */
+PyObject *
+_PyEval_GetBuiltinId(_Py_Identifier *name)
+{
+ PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
+ if (attr) {
+ Py_INCREF(attr);
+ }
+ else if (!PyErr_Occurred()) {
+ PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
+ }
+ return attr;
+}
+
PyObject *
PyEval_GetLocals(void)
{
More information about the Python-checkins
mailing list