mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
bpo-25794: Fix type.__setattr__() for non-interned attribute names. (#1652)
Based on patch by Eryk Sun.
This commit is contained in:
parent
3378b2062c
commit
d896985bb2
3 changed files with 63 additions and 6 deletions
|
|
@ -568,5 +568,32 @@ class ClassTests(unittest.TestCase):
|
|||
a = A(hash(A.f)^(-1))
|
||||
hash(a.f)
|
||||
|
||||
def testSetattrWrapperNameIntern(self):
|
||||
# Issue #25794: __setattr__ should intern the attribute name
|
||||
class A:
|
||||
pass
|
||||
|
||||
def add(self, other):
|
||||
return 'summa'
|
||||
|
||||
name = str(b'__add__', 'ascii') # shouldn't be optimized
|
||||
self.assertIsNot(name, '__add__') # not interned
|
||||
type.__setattr__(A, name, add)
|
||||
self.assertEqual(A() + 1, 'summa')
|
||||
|
||||
name2 = str(b'__add__', 'ascii')
|
||||
self.assertIsNot(name2, '__add__')
|
||||
self.assertIsNot(name2, name)
|
||||
type.__delattr__(A, name2)
|
||||
with self.assertRaises(TypeError):
|
||||
A() + 1
|
||||
|
||||
def testSetattrNonStringName(self):
|
||||
class A:
|
||||
pass
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
type.__setattr__(A, b'x', None)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue