MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_merge_constants

Method test_merge_constants

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

Source from the content-addressed store, hash-verified

791 # semantics, it's a more an implementation detail.
792 @support.cpython_only
793 def test_merge_constants(self):
794 # Issue #25843: compile() must merge constants which are equal
795 # and have the same type.
796
797 def check_same_constant(const):
798 ns = {}
799 code = "f1, f2 = lambda: %r, lambda: %r" % (const, const)
800 exec(code, ns)
801 f1 = ns['f1']
802 f2 = ns['f2']
803 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
804 self.check_constant(f1, const)
805 self.assertEqual(repr(f1()), repr(const))
806
807 check_same_constant(None)
808 check_same_constant(0.0)
809 check_same_constant(b'abc')
810 check_same_constant('abc')
811
812 # Note: "lambda: ..." emits "LOAD_CONST Ellipsis",
813 # whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis"
814 f1, f2 = lambda: ..., lambda: ...
815 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
816 self.check_constant(f1, Ellipsis)
817 self.assertEqual(repr(f1()), repr(Ellipsis))
818
819 # Merge constants in tuple or frozenset
820 f1, f2 = lambda: "not a name", lambda: ("not a name",)
821 f3 = lambda x: x in {("not a name",)}
822 self.assertIs(f1.__code__.co_consts[0],
823 f2.__code__.co_consts[1][0])
824 self.assertIs(next(iter(f3.__code__.co_consts[1])),
825 f2.__code__.co_consts[1])
826
827 # {0} is converted to a constant frozenset({0}) by the peephole
828 # optimizer
829 f1, f2 = lambda x: x in {0}, lambda x: x in {0}
830 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
831 self.check_constant(f1, frozenset({0}))
832 self.assertTrue(f1(0))
833
834 # Merging equal co_linetable is not a strict requirement
835 # for the Python semantics, it's a more an implementation detail.

Callers

nothing calls this directly

Calls 8

check_constantMethod · 0.95
reprFunction · 0.85
nextFunction · 0.85
iterFunction · 0.85
assertTrueMethod · 0.80
f1Function · 0.70
assertIsMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected