python / python/cpython

sys.monitoring branches for `match` cases showing incorrectly

Offen
#123,044 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

With the code from PR 122564, the destinations of branches into the blocks of certain cases are mapping to the pattern as well, making them look like they go out of scope:

v = 1
match v:
    case 1:
        x = 1
    case 2:
        x = 2
    case 3:
        x = 3
x += 2

This code yields (branches listing below extracted from co_branches()):

  0          0       RESUME                   0

  1          2       LOAD_CONST               0 (1)
             4       STORE_NAME               0 (v)

  2          6       LOAD_NAME                0 (v)

  3          8       COPY                     1
            10       LOAD_CONST               0 (1)
            12       COMPARE_OP              88 (bool(==))
            16       POP_JUMP_IF_FALSE        5 (to L1)
            20       NOT_TAKEN
            22       POP_TOP

  4         24       LOAD_CONST               0 (1)
            26       STORE_NAME               1 (x)
            28       JUMP_FORWARD            19 (to L3)

  5   L1:   30       COPY                     1
            32       LOAD_CONST               1 (2)
            34       COMPARE_OP              88 (bool(==))
            38       POP_JUMP_IF_FALSE        5 (to L2)
            42       NOT_TAKEN
            44       POP_TOP

  6         46       LOAD_CONST               1 (2)
            48       STORE_NAME               1 (x)
            50       JUMP_FORWARD             8 (to L3)

  7   L2:   52       LOAD_CONST               2 (3)
            54       COMPARE_OP              88 (bool(==))
            58       POP_JUMP_IF_FALSE        3 (to L3)
            62       NOT_TAKEN

  8         64       LOAD_CONST               2 (3)
            66       STORE_NAME               1 (x)

  9   L3:   68       LOAD_NAME                1 (x)
            70       LOAD_CONST               1 (2)
            72       BINARY_OP               13 (+=)
            76       STORE_NAME               1 (x)
            78       RETURN_CONST             3 (None)
'<module>' branches:
    ex8.py 3:9-3:10 "1" (<module>@16) -> 3:9-3:10 "1" (<module>@22) [case -> out]
    ex8.py 3:9-3:10 "1" (<module>@16) -> 5:9-5:10 "2" (<module>@30) [case -> next case]
    ex8.py 5:9-5:10 "2" (<module>@38) -> 5:9-5:10 "2" (<module>@44) [case -> out]
    ex8.py 5:9-5:10 "2" (<module>@38) -> 7:9-7:10 "3" (<module>@52) [case -> next case]
    ex8.py 7:9-7:10 "3" (<module>@58) -> 8:12-8:13 "3" (<module>@64) [case -> body]
    ex8.py 7:9-7:10 "3" (<module>@58) -> 9:0-9:1 "x" (<module>@68) [case -> next]

@markshannon @nedbat

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-130627

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere die Branch-Auflistung mit dem match-case-Beispiel und untersuche den sys.monitoring-Einstiegspunkt co_branches() sowie das durch PR 122564 eingeführte Verhalten. Vergleiche die gemeldeten Ziele für Case-Muster und -Bodies; abgeschlossen ist die Aufgabe, wenn Branches in Case-Blöcke nicht mehr auf die Positionen der Muster abgebildet werden oder fälschlicherweise den Anschein erwecken, den Gültigkeitsbereich zu verlassen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.