@@ -3059,78 +3059,85 @@ class Keys:
3059
3059
3060
3060
class TestTracing (unittest .TestCase ):
3061
3061
3062
- def _test_trace (self , func , expected_linenos , * f_args ):
3063
- actual_linenos = set ()
3062
+ @staticmethod
3063
+ def _trace (func , * args , ** kwargs ):
3064
+ actual_linenos = []
3065
+
3064
3066
def trace (frame , event , arg ):
3065
- if frame .f_code .co_name == func .__name__ :
3067
+ if event == "line" and frame .f_code .co_name == func .__name__ :
3068
+ assert arg is None
3066
3069
relative_lineno = frame .f_lineno - func .__code__ .co_firstlineno
3067
- actual_linenos .add (relative_lineno )
3070
+ actual_linenos .append (relative_lineno )
3068
3071
return trace
3069
3072
3073
+ old_trace = sys .gettrace ()
3070
3074
sys .settrace (trace )
3071
- func (* f_args )
3072
- sys .settrace (None )
3073
- self .assertSetEqual (actual_linenos , expected_linenos )
3074
-
3075
- def test_default_case_traces_correctly_a (self ):
3076
- def default_no_assign (command ): # 0
3077
- match command .split (): # 1
3078
- case ["go" , direction ] if direction in "nesw" : # 2
3079
- return f"go { direction } " # 3
3080
- case ["go" , _]: # 4
3081
- return "no go" # 5
3082
- case _: # 6
3083
- return "default" # 7
3084
-
3085
- self ._test_trace (default_no_assign , {0 , 1 , 2 , 3 }, "go n" )
3086
- self ._test_trace (default_no_assign , {0 , 1 , 2 , 4 , 5 }, "go x" )
3087
- self ._test_trace (default_no_assign , {0 , 1 , 2 , 4 , 6 , 7 }, "spam" )
3088
-
3089
- def test_default_case_traces_correctly_b (self ):
3090
- def default_wildcard_assign (command ): # 0
3091
- match command .split (): # 1
3092
- case ["go" , direction ] if direction in "nesw" : # 2
3093
- return f"go { direction } " # 3
3094
- case ["go" , _]: # 4
3095
- return "no go" # 5
3096
- case x : # 6
3097
- return x # 7
3098
-
3099
- self ._test_trace (default_wildcard_assign , {0 , 1 , 2 , 3 }, "go n" )
3100
- self ._test_trace (default_wildcard_assign , {0 , 1 , 2 , 4 , 5 }, "go x" )
3101
- self ._test_trace (default_wildcard_assign , {0 , 1 , 2 , 4 , 6 , 7 }, "spam" )
3102
-
3103
- def test_default_case_traces_correctly_c (self ):
3104
- def no_default (command ): # 0
3105
- match command .split (): # 1
3106
- case ["go" , direction ] if direction in "nesw" : # 2
3107
- return f"go { direction } " # 3
3108
- case ["go" , _]: # 4
3109
- return "no go" # 5
3110
-
3111
- self ._test_trace (no_default , {0 , 1 , 2 , 3 }, "go n" )
3112
- self ._test_trace (no_default , {0 , 1 , 2 , 4 , 5 }, "go x" )
3113
- self ._test_trace (no_default , {0 , 1 , 2 , 4 }, "spam" )
3114
-
3115
- def test_default_case_traces_correctly_d (self ):
3116
- def only_default_no_assign (command ): # 0
3117
- match command .split (): # 1
3118
- case _: # 2
3119
- return "default" # 3
3120
-
3121
- self ._test_trace (only_default_no_assign , {0 , 1 , 2 , 3 }, "go n" )
3122
- self ._test_trace (only_default_no_assign , {0 , 1 , 2 , 3 } , "go x" )
3123
- self ._test_trace (only_default_no_assign , {0 , 1 , 2 , 3 }, "spam" )
3124
-
3125
- def test_default_case_traces_correctly_e (self ):
3126
- def only_default_wildcard_assign (command ): # 0
3127
- match command .split (): # 1
3128
- case x : # 2
3129
- return x # 3
3130
-
3131
- self ._test_trace (only_default_wildcard_assign , {0 , 1 , 2 , 3 }, "go n" )
3132
- self ._test_trace (only_default_wildcard_assign , {0 , 1 , 2 , 3 } , "go x" )
3133
- self ._test_trace (only_default_wildcard_assign , {0 , 1 , 2 , 3 }, "spam" )
3075
+ try :
3076
+ func (* args , ** kwargs )
3077
+ finally :
3078
+ sys .settrace (old_trace )
3079
+ return actual_linenos
3080
+
3081
+ def test_default_wildcard (self ):
3082
+ def f (command ): # 0
3083
+ match command .split (): # 1
3084
+ case ["go" , direction ] if direction in "nesw" : # 2
3085
+ return f"go { direction } " # 3
3086
+ case ["go" , _]: # 4
3087
+ return "no go" # 5
3088
+ case _: # 6
3089
+ return "default" # 7
3090
+
3091
+ self .assertListEqual (self ._trace (f , "go n" ), [1 , 2 , 3 ])
3092
+ self .assertListEqual (self ._trace (f , "go x" ), [1 , 2 , 4 , 5 ])
3093
+ self .assertListEqual (self ._trace (f , "spam" ), [1 , 2 , 4 , 6 , 7 ])
3094
+
3095
+ def test_default_capture (self ):
3096
+ def f (command ): # 0
3097
+ match command .split (): # 1
3098
+ case ["go" , direction ] if direction in "nesw" : # 2
3099
+ return f"go { direction } " # 3
3100
+ case ["go" , _]: # 4
3101
+ return "no go" # 5
3102
+ case x : # 6
3103
+ return x # 7
3104
+
3105
+ self .assertListEqual (self ._trace (f , "go n" ), [1 , 2 , 3 ])
3106
+ self .assertListEqual (self ._trace (f , "go x" ), [1 , 2 , 4 , 5 ])
3107
+ self .assertListEqual (self ._trace (f , "spam" ), [1 , 2 , 4 , 6 , 7 ])
3108
+
3109
+ def test_no_default (self ):
3110
+ def f (command ): # 0
3111
+ match command .split (): # 1
3112
+ case ["go" , direction ] if direction in "nesw" : # 2
3113
+ return f"go { direction } " # 3
3114
+ case ["go" , _]: # 4
3115
+ return "no go" # 5
3116
+
3117
+ self .assertListEqual (self ._trace (f , "go n" ), [1 , 2 , 3 ])
3118
+ self .assertListEqual (self ._trace (f , "go x" ), [1 , 2 , 4 , 5 ])
3119
+ self .assertListEqual (self ._trace (f , "spam" ), [1 , 2 , 4 ])
3120
+
3121
+ def test_only_default_wildcard (self ):
3122
+ def f (command ): # 0
3123
+ match command .split (): # 1
3124
+ case _: # 2
3125
+ return "default" # 3
3126
+
3127
+ self .assertListEqual (self ._trace (f , "go n" ), [1 , 2 , 3 ])
3128
+ self .assertListEqual (self ._trace (f , "go x" ), [1 , 2 , 3 ])
3129
+ self .assertListEqual (self ._trace (f , "spam" ), [1 , 2 , 3 ])
3130
+
3131
+ def test_only_default_capture (self ):
3132
+ def f (command ): # 0
3133
+ match command .split (): # 1
3134
+ case x : # 2
3135
+ return x # 3
3136
+
3137
+ self .assertListEqual (self ._trace (f , "go n" ), [1 , 2 , 3 ])
3138
+ self .assertListEqual (self ._trace (f , "go x" ), [1 , 2 , 3 ])
3139
+ self .assertListEqual (self ._trace (f , "spam" ), [1 , 2 , 3 ])
3140
+
3134
3141
3135
3142
if __name__ == "__main__" :
3136
3143
"""
0 commit comments