Skip to content

Commit 5d24261

Browse files
[3.6] bpo-25794: Fix type.__setattr__() for non-interned attribute names. (pythonGH-1652)
Based on patch by Eryk Sun.. (cherry picked from commit d896985)
1 parent 2773add commit 5d24261

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

Lib/test/test_class.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,5 +568,32 @@ class B(A):
568568
a = A(hash(A.f)^(-1))
569569
hash(a.f)
570570

571+
def testSetattrWrapperNameIntern(self):
572+
# Issue #25794: __setattr__ should intern the attribute name
573+
class A:
574+
pass
575+
576+
def add(self, other):
577+
return 'summa'
578+
579+
name = str(b'__add__', 'ascii') # shouldn't be optimized
580+
self.assertIsNot(name, '__add__') # not interned
581+
type.__setattr__(A, name, add)
582+
self.assertEqual(A() + 1, 'summa')
583+
584+
name2 = str(b'__add__', 'ascii')
585+
self.assertIsNot(name2, '__add__')
586+
self.assertIsNot(name2, name)
587+
type.__delattr__(A, name2)
588+
with self.assertRaises(TypeError):
589+
A() + 1
590+
591+
def testSetattrNonStringName(self):
592+
class A:
593+
pass
594+
595+
with self.assertRaises(TypeError):
596+
type.__setattr__(A, b'x', None)
597+
571598
if __name__ == '__main__':
572599
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.6.2 release candidate 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-25794: Fixed type.__setattr__() and type.__delattr__() for
14+
non-interned attribute names. Based on patch by Eryk Sun.
15+
1316
- bpo-12414: sys.getsizeof() on a code object now returns the sizes
1417
which includes the code struct and sizes of objects which it references.
1518
Patch by Dong-hee Na.

Objects/typeobject.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3082,9 +3082,35 @@ type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
30823082
type->tp_name);
30833083
return -1;
30843084
}
3085-
if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
3086-
return -1;
3087-
return update_slot(type, name);
3085+
if (PyUnicode_Check(name)) {
3086+
if (PyUnicode_CheckExact(name)) {
3087+
if (PyUnicode_READY(name) == -1)
3088+
return -1;
3089+
Py_INCREF(name);
3090+
}
3091+
else {
3092+
name = _PyUnicode_Copy(name);
3093+
if (name == NULL)
3094+
return -1;
3095+
}
3096+
PyUnicode_InternInPlace(&name);
3097+
if (!PyUnicode_CHECK_INTERNED(name)) {
3098+
PyErr_SetString(PyExc_MemoryError,
3099+
"Out of memory interning an attribute name");
3100+
Py_DECREF(name);
3101+
return -1;
3102+
}
3103+
}
3104+
else {
3105+
/* Will fail in _PyObject_GenericSetAttrWithDict. */
3106+
Py_INCREF(name);
3107+
}
3108+
res = PyObject_GenericSetAttr((PyObject *)type, name, value);
3109+
if (res == 0) {
3110+
res = update_slot(type, name);
3111+
}
3112+
Py_DECREF(name);
3113+
return res;
30883114
}
30893115

30903116
extern void
@@ -6929,7 +6955,7 @@ init_slotdefs(void)
69296955
/* Slots must be ordered by their offset in the PyHeapTypeObject. */
69306956
assert(!p[1].name || p->offset <= p[1].offset);
69316957
p->name_strobj = PyUnicode_InternFromString(p->name);
6932-
if (!p->name_strobj)
6958+
if (!p->name_strobj || !PyUnicode_CHECK_INTERNED(p->name_strobj))
69336959
Py_FatalError("Out of memory interning slotdef names");
69346960
}
69356961
slotdefs_initialized = 1;
@@ -6954,6 +6980,9 @@ update_slot(PyTypeObject *type, PyObject *name)
69546980
slotdef **pp;
69556981
int offset;
69566982

6983+
assert(PyUnicode_CheckExact(name));
6984+
assert(PyUnicode_CHECK_INTERNED(name));
6985+
69576986
/* Clear the VALID_VERSION flag of 'type' and all its
69586987
subclasses. This could possibly be unified with the
69596988
update_subclasses() recursion below, but carefully:
@@ -6964,7 +6993,6 @@ update_slot(PyTypeObject *type, PyObject *name)
69646993
init_slotdefs();
69656994
pp = ptrs;
69666995
for (p = slotdefs; p->name; p++) {
6967-
/* XXX assume name is interned! */
69686996
if (p->name_strobj == name)
69696997
*pp++ = p;
69706998
}

0 commit comments

Comments
 (0)