summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_code.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-09-30 07:07:26 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-09-30 07:07:26 (GMT)
commit00a0fc1144d928515ff8abd0de7bb6ad072fcbdb (patch)
treec556e0e9d15dea2648f7b62dcea717cb67653a40 /Lib/test/test_code.py
parent55f3ae68bb5b3d17719f9c2b85913c9a3f38adbb (diff)
downloadcpython-00a0fc1144d928515ff8abd0de7bb6ad072fcbdb.zip
cpython-00a0fc1144d928515ff8abd0de7bb6ad072fcbdb.tar.gz
cpython-00a0fc1144d928515ff8abd0de7bb6ad072fcbdb.tar.bz2
Issue #27942: String constants now interned recursively in tuples and frozensets.
Diffstat (limited to 'Lib/test/test_code.py')
-rw-r--r--Lib/test/test_code.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py
index 21b12a5..2be17a2 100644
--- a/Lib/test/test_code.py
+++ b/Lib/test/test_code.py
@@ -102,6 +102,7 @@ consts: ('None',)
"""
+import sys
import unittest
import weakref
from test.support import run_doctest, run_unittest, cpython_only
@@ -134,6 +135,43 @@ class CodeTest(unittest.TestCase):
self.assertEqual(co.co_name, "funcname")
self.assertEqual(co.co_firstlineno, 15)
+class CodeConstsTest(unittest.TestCase):
+
+ def find_const(self, consts, value):
+ for v in consts:
+ if v == value:
+ return v
+ self.assertIn(value, consts) # rises an exception
+ self.fail('Should be never reached')
+
+ def assertIsInterned(self, s):
+ if s is not sys.intern(s):
+ self.fail('String %r is not interned' % (s,))
+
+ @cpython_only
+ def test_interned_string(self):
+ co = compile('res = "str_value"', '?', 'exec')
+ v = self.find_const(co.co_consts, 'str_value')
+ self.assertIsInterned(v)
+
+ @cpython_only
+ def test_interned_string_in_tuple(self):
+ co = compile('res = ("str_value",)', '?', 'exec')
+ v = self.find_const(co.co_consts, ('str_value',))
+ self.assertIsInterned(v[0])
+
+ @cpython_only
+ def test_interned_string_in_frozenset(self):
+ co = compile('res = a in {"str_value"}', '?', 'exec')
+ v = self.find_const(co.co_consts, frozenset(('str_value',)))
+ self.assertIsInterned(tuple(v)[0])
+
+ @cpython_only
+ def test_interned_string_default(self):
+ def f(a='str_value'):
+ return a
+ self.assertIsInterned(f())
+
class CodeWeakRefTest(unittest.TestCase):
@@ -163,7 +201,7 @@ class CodeWeakRefTest(unittest.TestCase):
def test_main(verbose=None):
from test import test_code
run_doctest(test_code, verbose)
- run_unittest(CodeTest, CodeWeakRefTest)
+ run_unittest(CodeTest, CodeConstsTest, CodeWeakRefTest)
if __name__ == "__main__":