Some simple match/case statements show incorrect tracing. Below is the code to run, as well as the output. Output lines with initial stars are incorrect: they incorrectly indicate that case bodies are executing when they are not. Sorry for the bulk here, I wanted to give you all the cases I had.
-- 8< -------------------------------------
import linecache, sys
def trace(frame, event, arg):
# The weird globals here is to avoid a NameError on shutdown...
if frame.f_code.co_filename == globals().get("__file__"):
lineno = frame.f_lineno
print("{} {}: {}".format(event[:4], lineno, linecache.getline(__file__, lineno).rstrip()))
return trace
def match_with_default():
for command in ["huh", "go home", "go n"]:
print(command)
match command.split():
case ["go", direction] if direction in "nesw":
match = f"go: {direction}"
case ["go", _]:
match = "no go"
case _:
match = "default"
print(match)
def match_with_wildcard():
for command in ["huh", "go home", "go n"]:
print(command)
match command.split():
case ["go", direction] if direction in "nesw":
match = f"go: {direction}"
case ["go", _]:
match = "no go"
case x:
match = f"default: {x}"
print(match)
def match_without_wildcard():
match = None
for command in ["huh", "go home", "go n"]:
print(command)
match command.split():
case ["go", direction] if direction in "nesw":
match = f"go: {direction}"
case ["go", _]:
match = "no go"
print(match)
print(sys.version)
sys.settrace(trace)
match_with_default()
match_with_wildcard()
match_without_wildcard()
-- 8< -------------------------------------
Output:
3.10.0b4 (default, Jul 11 2021, 13:51:53) [Clang 12.0.0 (clang-1200.0.32.29)]
call 10: def match_with_default():
line 11: for command in ["huh", "go home", "go n"]:
line 12: print(command)
huh
line 13: match command.split():
line 14: case ["go", direction] if direction in "nesw":
line 15: match = f"go: {direction}"
line 16: case ["go", _]:
* line 17: match = "no go"
line 19: match = "default"
line 20: print(match)
default
line 11: for command in ["huh", "go home", "go n"]:
line 12: print(command)
go home
line 13: match command.split():
line 14: case ["go", direction] if direction in "nesw":
line 16: case ["go", _]:
line 17: match = "no go"
line 20: print(match)
no go
line 11: for command in ["huh", "go home", "go n"]:
line 12: print(command)
go n
line 13: match command.split():
line 14: case ["go", direction] if direction in "nesw":
line 15: match = f"go: {direction}"
line 20: print(match)
go: n
line 11: for command in ["huh", "go home", "go n"]:
retu 11: for command in ["huh", "go home", "go n"]:
call 22: def match_with_wildcard():
line 23: for command in ["huh", "go home", "go n"]:
line 24: print(command)
huh
line 25: match command.split():
line 26: case ["go", direction] if direction in "nesw":
* line 27: match = f"go: {direction}"
line 28: case ["go", _]:
* line 29: match = "no go"
line 30: case x:
line 31: match = f"default: {x}"
line 32: print(match)
default: ['huh']
line 23: for command in ["huh", "go home", "go n"]:
line 24: print(command)
go home
line 25: match command.split():
line 26: case ["go", direction] if direction in "nesw":
line 28: case ["go", _]:
line 29: match = "no go"
line 32: print(match)
no go
line 23: for command in ["huh", "go home", "go n"]:
line 24: print(command)
go n
line 25: match command.split():
line 26: case ["go", direction] if direction in "nesw":
line 27: match = f"go: {direction}"
line 32: print(match)
go: n
line 23: for command in ["huh", "go home", "go n"]:
retu 23: for command in ["huh", "go home", "go n"]:
call 34: def match_without_wildcard():
line 35: match = None
line 36: for command in ["huh", "go home", "go n"]:
line 37: print(command)
huh
line 38: match command.split():
line 39: case ["go", direction] if direction in "nesw":
* line 40: match = f"go: {direction}"
line 41: case ["go", _]:
* line 42: match = "no go"
line 43: print(match)
None
line 36: for command in ["huh", "go home", "go n"]:
line 37: print(command)
go home
line 38: match command.split():
line 39: case ["go", direction] if direction in "nesw":
line 41: case ["go", _]:
line 42: match = "no go"
line 43: print(match)
no go
line 36: for command in ["huh", "go home", "go n"]:
line 37: print(command)
go n
line 38: match command.split():
line 39: case ["go", direction] if direction in "nesw":
line 40: match = f"go: {direction}"
line 43: print(match)
go: n
line 36: for command in ["huh", "go home", "go n"]:
retu 36: for command in ["huh", "go home", "go n"]:
|