summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-05-20 07:23:58 (GMT)
committerGitHub <noreply@github.com>2017-05-20 07:23:58 (GMT)
commit4a86fe9d3f5e7af6f019ae22536eec228f04e22e (patch)
treeb72c781d6e73c2ef4c445dd067dbb92776b857d3 /Objects
parent9503dd1e1865bb873a1f72f63ae384bba8462c5e (diff)
downloadcpython-4a86fe9d3f5e7af6f019ae22536eec228f04e22e.zip
cpython-4a86fe9d3f5e7af6f019ae22536eec228f04e22e.tar.gz
cpython-4a86fe9d3f5e7af6f019ae22536eec228f04e22e.tar.bz2
[3.5] bpo-25794: Fix `type.__setattr__()` for non-interned attribute names. (GH-1652) (#1674)
Based on patch by Eryk Sun. (cherry picked from commit d896985bb2de49046f9b6879e906d1e4db255e23)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c39
1 files changed, 34 insertions, 5 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 7b76e5c..3f526b4 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3020,6 +3020,7 @@ type_getattro(PyTypeObject *type, PyObject *name)
static int
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
{
+ int res;
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
PyErr_Format(
PyExc_TypeError,
@@ -3027,9 +3028,35 @@ type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
type->tp_name);
return -1;
}
- if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
- return -1;
- return update_slot(type, name);
+ if (PyUnicode_Check(name)) {
+ if (PyUnicode_CheckExact(name)) {
+ if (PyUnicode_READY(name) == -1)
+ return -1;
+ Py_INCREF(name);
+ }
+ else {
+ name = _PyUnicode_Copy(name);
+ if (name == NULL)
+ return -1;
+ }
+ PyUnicode_InternInPlace(&name);
+ if (!PyUnicode_CHECK_INTERNED(name)) {
+ PyErr_SetString(PyExc_MemoryError,
+ "Out of memory interning an attribute name");
+ Py_DECREF(name);
+ return -1;
+ }
+ }
+ else {
+ /* Will fail in _PyObject_GenericSetAttrWithDict. */
+ Py_INCREF(name);
+ }
+ res = PyObject_GenericSetAttr((PyObject *)type, name, value);
+ if (res == 0) {
+ res = update_slot(type, name);
+ }
+ Py_DECREF(name);
+ return res;
}
extern void
@@ -6849,7 +6876,7 @@ init_slotdefs(void)
/* Slots must be ordered by their offset in the PyHeapTypeObject. */
assert(!p[1].name || p->offset <= p[1].offset);
p->name_strobj = PyUnicode_InternFromString(p->name);
- if (!p->name_strobj)
+ if (!p->name_strobj || !PyUnicode_CHECK_INTERNED(p->name_strobj))
Py_FatalError("Out of memory interning slotdef names");
}
slotdefs_initialized = 1;
@@ -6874,6 +6901,9 @@ update_slot(PyTypeObject *type, PyObject *name)
slotdef **pp;
int offset;
+ assert(PyUnicode_CheckExact(name));
+ assert(PyUnicode_CHECK_INTERNED(name));
+
/* Clear the VALID_VERSION flag of 'type' and all its
subclasses. This could possibly be unified with the
update_subclasses() recursion below, but carefully:
@@ -6884,7 +6914,6 @@ update_slot(PyTypeObject *type, PyObject *name)
init_slotdefs();
pp = ptrs;
for (p = slotdefs; p->name; p++) {
- /* XXX assume name is interned! */
if (p->name_strobj == name)
*pp++ = p;
}