summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_class.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-05-20 05:48:06 (GMT)
committerGitHub <noreply@github.com>2017-05-20 05:48:06 (GMT)
commitd896985bb2de49046f9b6879e906d1e4db255e23 (patch)
treeb66eda420f110f93fa33b0e6454b822b23e4a8f7 /Lib/test/test_class.py
parent3378b2062c7fbb77a9b5e6d315d6b94160fac69a (diff)
downloadcpython-d896985bb2de49046f9b6879e906d1e4db255e23.zip
cpython-d896985bb2de49046f9b6879e906d1e4db255e23.tar.gz
cpython-d896985bb2de49046f9b6879e906d1e4db255e23.tar.bz2
bpo-25794: Fix `type.__setattr__()` for non-interned attribute names. (#1652)
Based on patch by Eryk Sun.
Diffstat (limited to 'Lib/test/test_class.py')
-rw-r--r--Lib/test/test_class.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py
index 4d554a3..ecc01f2 100644
--- a/Lib/test/test_class.py
+++ b/Lib/test/test_class.py
@@ -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()