diff options
author | Brandt Bucher <brandt@python.org> | 2021-05-02 20:02:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-02 20:02:10 (GMT) |
commit | 0ad1e0384c8afc5259a6d03363491d89500a5d03 (patch) | |
tree | 66debec62434d9503dd8c3b60c22dc99dcd15f95 /Lib/test/test_patma.py | |
parent | 7d2b83e9f092a2ea1f715fe028f7c48324bee756 (diff) | |
download | cpython-0ad1e0384c8afc5259a6d03363491d89500a5d03.zip cpython-0ad1e0384c8afc5259a6d03363491d89500a5d03.tar.gz cpython-0ad1e0384c8afc5259a6d03363491d89500a5d03.tar.bz2 |
bpo-43754: Eliminate bindings for partial pattern matches (GH-25229)
Diffstat (limited to 'Lib/test/test_patma.py')
-rw-r--r-- | Lib/test/test_patma.py | 80 |
1 files changed, 77 insertions, 3 deletions
diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index 1f80fb4..8a273be 100644 --- a/Lib/test/test_patma.py +++ b/Lib/test/test_patma.py @@ -2905,6 +2905,79 @@ class TestPatma(unittest.TestCase): pass """) + def test_patma_289(self): + x = {"y": 1} + match x: + case {"y": (0 as y) | (1 as y)}: + z = 0 + self.assertEqual(x, {"y": 1}) + self.assertEqual(y, 1) + self.assertEqual(z, 0) + + @no_perf + def test_patma_290(self): + self.assert_syntax_error(""" + match ...: + case [a, [b] | [c] | [d]]: + pass + """) + + @no_perf + def test_patma_291(self): + # Hunting for leaks using -R doesn't catch leaks in the compiler itself, + # just the code under test. This test ensures that if there are leaks in + # the pattern compiler, those runs will fail: + with open(__file__) as file: + compile(file.read(), __file__, "exec") + + def test_patma_292(self): + def f(x): + match x: + case ((a, b, c, d, e, f, g, h, i, 9) | + (h, g, i, a, b, d, e, c, f, 10) | + (g, b, a, c, d, -5, e, h, i, f) | + (-1, d, f, b, g, e, i, a, h, c)): + w = 0 + out = locals() + del out["x"] + return out + alts = [ + dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, w=0), + dict(h=1, g=2, i=3, a=4, b=5, d=6, e=7, c=8, f=9, w=0), + dict(g=0, b=-1, a=-2, c=-3, d=-4, e=-6, h=-7, i=-8, f=-9, w=0), + dict(d=-2, f=-3, b=-4, g=-5, e=-6, i=-7, a=-8, h=-9, c=-10, w=0), + dict(), + ] + self.assertEqual(f(range(10)), alts[0]) + self.assertEqual(f(range(1, 11)), alts[1]) + self.assertEqual(f(range(0, -10, -1)), alts[2]) + self.assertEqual(f(range(-1, -11, -1)), alts[3]) + self.assertEqual(f(range(10, 20)), alts[4]) + + def test_patma_293(self): + def f(x): + match x: + case [y, (a, b, c, d, e, f, g, h, i, 9) | + (h, g, i, a, b, d, e, c, f, 10) | + (g, b, a, c, d, -5, e, h, i, f) | + (-1, d, f, b, g, e, i, a, h, c), z]: + w = 0 + out = locals() + del out["x"] + return out + alts = [ + dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, w=0, y=False, z=True), + dict(h=1, g=2, i=3, a=4, b=5, d=6, e=7, c=8, f=9, w=0, y=False, z=True), + dict(g=0, b=-1, a=-2, c=-3, d=-4, e=-6, h=-7, i=-8, f=-9, w=0, y=False, z=True), + dict(d=-2, f=-3, b=-4, g=-5, e=-6, i=-7, a=-8, h=-9, c=-10, w=0, y=False, z=True), + dict(), + ] + self.assertEqual(f((False, range(10), True)), alts[0]) + self.assertEqual(f((False, range(1, 11), True)), alts[1]) + self.assertEqual(f((False, range(0, -10, -1), True)), alts[2]) + self.assertEqual(f((False, range(-1, -11, -1), True)), alts[3]) + self.assertEqual(f((False, range(10, 20), True)), alts[4]) + class PerfPatma(TestPatma): @@ -2936,7 +3009,8 @@ class PerfPatma(TestPatma): """ -sudo ./python -m pyperf system tune && \ - ./python -m pyperf timeit --rigorous --setup "from test.test_patma import PerfPatma; p = PerfPatma()" "p.run_perf()"; \ -sudo ./python -m pyperf system reset +# From inside venv pointing to this Python, with pyperf installed: +sudo $(which python) -m pyperf system tune && \ + $(which python) -m pyperf timeit --rigorous --setup "from test.test_patma import PerfPatma; p = PerfPatma()" "p.run_perf()"; \ +sudo $(which python) -m pyperf system reset """ |