MCPcopy Index your code
hub / github.com/python/cpython / test_dont_merge_constants

Method test_dont_merge_constants

Lib/test/test_compile.py:997–1034  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

995 self.assertNotIn('NOP', output.getvalue())
996
997 def test_dont_merge_constants(self):
998 # Issue #25843: compile() must not merge constants which are equal
999 # but have a different type.
1000
1001 def check_different_constants(const1, const2):
1002 ns = {}
1003 exec("f1, f2 = lambda: %r, lambda: %r" % (const1, const2), ns)
1004 f1 = ns['f1']
1005 f2 = ns['f2']
1006 self.assertIsNot(f1.__code__, f2.__code__)
1007 self.assertNotEqual(f1.__code__, f2.__code__)
1008 self.check_constant(f1, const1)
1009 self.check_constant(f2, const2)
1010 self.assertEqual(repr(f1()), repr(const1))
1011 self.assertEqual(repr(f2()), repr(const2))
1012
1013 check_different_constants(+0.0, -0.0)
1014 check_different_constants((0,), (0.0,))
1015 check_different_constants('a', b'a')
1016 check_different_constants(('a',), (b'a',))
1017
1018 # check_different_constants() cannot be used because repr(-0j) is
1019 # '(-0-0j)', but when '(-0-0j)' is evaluated to 0j: we loose the sign.
1020 f1, f2 = lambda: +0.0j, lambda: -0.0j
1021 self.assertIsNot(f1.__code__, f2.__code__)
1022 self.check_constant(f1, +0.0j)
1023 self.check_constant(f2, -0.0j)
1024 self.assertEqual(repr(f1()), repr(+0.0j))
1025 self.assertEqual(repr(f2()), repr(-0.0j))
1026
1027 # {0} is converted to a constant frozenset({0}) by the peephole
1028 # optimizer
1029 f1, f2 = lambda x: x in {0}, lambda x: x in {0.0}
1030 self.assertIsNot(f1.__code__, f2.__code__)
1031 self.check_constant(f1, frozenset({0}))
1032 self.check_constant(f2, frozenset({0.0}))
1033 self.assertTrue(f1(0))
1034 self.assertTrue(f2(0.0))
1035
1036 def test_path_like_objects(self):
1037 # An implicit test for PyUnicode_FSDecoder().

Callers

nothing calls this directly

Calls 6

check_constantMethod · 0.95
f1Function · 0.85
f2Function · 0.85
assertIsNotMethod · 0.80
assertTrueMethod · 0.80
assertEqualMethod · 0.45

Tested by

no test coverage detected