Skip to content

Commit 5ed5199

Browse files
committed
Raise SyntaxError for illegal jumps
1 parent a46dbe5 commit 5ed5199

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

goto.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,16 @@ def _patch_code(code):
8989
try:
9090
label_pos, label_stack = labels[arg]
9191
except KeyError:
92-
continue
92+
raise SyntaxError('Unknown label %r' % code.co_names[arg])
9393

9494
label_depth = len(label_stack)
9595
if goto_stack[:label_depth] != label_stack:
96-
continue
96+
raise SyntaxError('Jumps into different blocks are not allowed')
9797

9898
depth_delta = len(goto_stack) - label_depth
99-
if depth_delta > _STRUCT_ATTR_LOOKUP.size - _STRUCT_OP_WITH_ARG.size:
100-
continue
99+
max_depth_delta = _STRUCT_ATTR_LOOKUP.size - _STRUCT_OP_WITH_ARG.size
100+
if depth_delta > max_depth_delta:
101+
raise SyntaxError('Jumps out of more than %d nested blocks are not allowed' % max_depth_delta)
101102

102103
_inject_ops(buf, goto_pos, 'NOP', _STRUCT_ATTR_LOOKUP.size)
103104
_inject_ops(buf, goto_pos, 'POP_BLOCK', depth_delta)

test_goto.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ def func():
3939
assert func() == 0
4040

4141
def test_jump_into_loop():
42-
@with_goto
4342
def func():
4443
for i in range(10):
4544
label .loop
4645
goto .loop
4746

48-
pytest.raises(NameError, func)
47+
pytest.raises(SyntaxError, with_goto, func)
4948

5049
def test_jump_out_of_nested_4_loops():
5150
@with_goto
@@ -61,7 +60,6 @@ def func():
6160
assert func() == (0, 0, 0, 0)
6261

6362
def test_jump_out_of_nested_5_loops():
64-
@with_goto
6563
def func():
6664
for i in range(2):
6765
for j in range(2):
@@ -72,18 +70,17 @@ def func():
7270
label .end
7371
return (i, j, k, m, n)
7472

75-
pytest.raises(NameError, func)
73+
pytest.raises(SyntaxError, with_goto, func)
7674

7775
def test_jump_across_loops():
78-
@with_goto
7976
def func():
8077
for i in range(10):
8178
goto .other_loop
8279

8380
for i in range(10):
8481
label .other_loop
8582

86-
pytest.raises(NameError, func)
83+
pytest.raises(SyntaxError, with_goto, func)
8784

8885
def test_jump_out_of_try_block():
8986
@with_goto
@@ -101,12 +98,17 @@ def func():
10198
assert func() == None
10299

103100
def test_jump_into_try_block():
104-
@with_goto
105101
def func():
106102
try:
107103
label .block
108104
except:
109105
pass
110106
goto .block
111107

112-
pytest.raises(NameError, func)
108+
pytest.raises(SyntaxError, with_goto, func)
109+
110+
def test_jump_to_unkown_label():
111+
def func():
112+
goto .unknown
113+
114+
pytest.raises(SyntaxError, with_goto, func)

0 commit comments

Comments
 (0)