Skip to content

Commit 4a86fe9

Browse files
[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 d896985)
1 parent 9503dd1 commit 4a86fe9

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 @@ Release date: XXXX-XX-XX
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-29935: Fixed error messages in the index() method of tuple, list and deque
1417
when pass indices of wrong type.
1518

Objects/typeobject.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,16 +3020,43 @@ type_getattro(PyTypeObject *type, PyObject *name)
30203020
static int
30213021
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
30223022
{
3023+
int res;
30233024
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
30243025
PyErr_Format(
30253026
PyExc_TypeError,
30263027
"can't set attributes of built-in/extension type '%s'",
30273028
type->tp_name);
30283029
return -1;
30293030
}
3030-
if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
3031-
return -1;
3032-
return update_slot(type, name);
3031+
if (PyUnicode_Check(name)) {
3032+
if (PyUnicode_CheckExact(name)) {
3033+
if (PyUnicode_READY(name) == -1)
3034+
return -1;
3035+
Py_INCREF(name);
3036+
}
3037+
else {
3038+
name = _PyUnicode_Copy(name);
3039+
if (name == NULL)
3040+
return -1;
3041+
}
3042+
PyUnicode_InternInPlace(&name);
3043+
if (!PyUnicode_CHECK_INTERNED(name)) {
3044+
PyErr_SetString(PyExc_MemoryError,
3045+
"Out of memory interning an attribute name");
3046+
Py_DECREF(name);
3047+
return -1;
3048+
}
3049+
}
3050+
else {
3051+
/* Will fail in _PyObject_GenericSetAttrWithDict. */
3052+
Py_INCREF(name);
3053+
}
3054+
res = PyObject_GenericSetAttr((PyObject *)type, name, value);
3055+
if (res == 0) {
3056+
res = update_slot(type, name);
3057+
}
3058+
Py_DECREF(name);
3059+
return res;
30333060
}
30343061

30353062
extern void
@@ -6849,7 +6876,7 @@ init_slotdefs(void)
68496876
/* Slots must be ordered by their offset in the PyHeapTypeObject. */
68506877
assert(!p[1].name || p->offset <= p[1].offset);
68516878
p->name_strobj = PyUnicode_InternFromString(p->name);
6852-
if (!p->name_strobj)
6879+
if (!p->name_strobj || !PyUnicode_CHECK_INTERNED(p->name_strobj))
68536880
Py_FatalError("Out of memory interning slotdef names");
68546881
}
68556882
slotdefs_initialized = 1;
@@ -6874,6 +6901,9 @@ update_slot(PyTypeObject *type, PyObject *name)
68746901
slotdef **pp;
68756902
int offset;
68766903

6904+
assert(PyUnicode_CheckExact(name));
6905+
assert(PyUnicode_CHECK_INTERNED(name));
6906+
68776907
/* Clear the VALID_VERSION flag of 'type' and all its
68786908
subclasses. This could possibly be unified with the
68796909
update_subclasses() recursion below, but carefully:
@@ -6884,7 +6914,6 @@ update_slot(PyTypeObject *type, PyObject *name)
68846914
init_slotdefs();
68856915
pp = ptrs;
68866916
for (p = slotdefs; p->name; p++) {
6887-
/* XXX assume name is interned! */
68886917
if (p->name_strobj == name)
68896918
*pp++ = p;
68906919
}

0 commit comments

Comments
 (0)