Skip to content

Commit 193f7e0

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

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

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: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,16 +3075,43 @@ type_getattro(PyTypeObject *type, PyObject *name)
30753075
static int
30763076
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
30773077
{
3078+
int res;
30783079
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
30793080
PyErr_Format(
30803081
PyExc_TypeError,
30813082
"can't set attributes of built-in/extension type '%s'",
30823083
type->tp_name);
30833084
return -1;
30843085
}
3085-
if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
3086-
return -1;
3087-
return update_slot(type, name);
3086+
if (PyUnicode_Check(name)) {
3087+
if (PyUnicode_CheckExact(name)) {
3088+
if (PyUnicode_READY(name) == -1)
3089+
return -1;
3090+
Py_INCREF(name);
3091+
}
3092+
else {
3093+
name = _PyUnicode_Copy(name);
3094+
if (name == NULL)
3095+
return -1;
3096+
}
3097+
PyUnicode_InternInPlace(&name);
3098+
if (!PyUnicode_CHECK_INTERNED(name)) {
3099+
PyErr_SetString(PyExc_MemoryError,
3100+
"Out of memory interning an attribute name");
3101+
Py_DECREF(name);
3102+
return -1;
3103+
}
3104+
}
3105+
else {
3106+
/* Will fail in _PyObject_GenericSetAttrWithDict. */
3107+
Py_INCREF(name);
3108+
}
3109+
res = PyObject_GenericSetAttr((PyObject *)type, name, value);
3110+
if (res == 0) {
3111+
res = update_slot(type, name);
3112+
}
3113+
Py_DECREF(name);
3114+
return res;
30883115
}
30893116

30903117
extern void
@@ -6929,7 +6956,7 @@ init_slotdefs(void)
69296956
/* Slots must be ordered by their offset in the PyHeapTypeObject. */
69306957
assert(!p[1].name || p->offset <= p[1].offset);
69316958
p->name_strobj = PyUnicode_InternFromString(p->name);
6932-
if (!p->name_strobj)
6959+
if (!p->name_strobj || !PyUnicode_CHECK_INTERNED(p->name_strobj))
69336960
Py_FatalError("Out of memory interning slotdef names");
69346961
}
69356962
slotdefs_initialized = 1;
@@ -6954,6 +6981,9 @@ update_slot(PyTypeObject *type, PyObject *name)
69546981
slotdef **pp;
69556982
int offset;
69566983

6984+
assert(PyUnicode_CheckExact(name));
6985+
assert(PyUnicode_CHECK_INTERNED(name));
6986+
69576987
/* Clear the VALID_VERSION flag of 'type' and all its
69586988
subclasses. This could possibly be unified with the
69596989
update_subclasses() recursion below, but carefully:
@@ -6964,7 +6994,6 @@ update_slot(PyTypeObject *type, PyObject *name)
69646994
init_slotdefs();
69656995
pp = ptrs;
69666996
for (p = slotdefs; p->name; p++) {
6967-
/* XXX assume name is interned! */
69686997
if (p->name_strobj == name)
69696998
*pp++ = p;
69706999
}

0 commit comments

Comments
 (0)