Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 397466d

Browse files
bpo-30953: Improve error messages and add tests for jumping (GH-6196)
into/out of an except block.
1 parent 702f8f3 commit 397466d

2 files changed

Lines changed: 70 additions & 8 deletions

File tree

Lib/test/test_sys_settrace.py

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,8 +1201,16 @@ def test_no_jump_between_except_blocks_2(output):
12011201
output.append(7)
12021202
output.append(8)
12031203

1204-
@jump_test(3, 6, [2, 5, 6], (ValueError, 'finally'))
1204+
@jump_test(1, 5, [], (ValueError, "into a 'finally'"))
12051205
def test_no_jump_into_finally_block(output):
1206+
output.append(1)
1207+
try:
1208+
output.append(3)
1209+
finally:
1210+
output.append(5)
1211+
1212+
@jump_test(3, 6, [2, 5, 6], (ValueError, "into a 'finally'"))
1213+
def test_no_jump_into_finally_block_from_try_block(output):
12061214
try:
12071215
output.append(2)
12081216
output.append(3)
@@ -1211,21 +1219,71 @@ def test_no_jump_into_finally_block(output):
12111219
output.append(6)
12121220
output.append(7)
12131221

1214-
@jump_test(1, 5, [], (ValueError, 'finally'))
1215-
def test_no_jump_into_finally_block_2(output):
1222+
@jump_test(5, 1, [1, 3], (ValueError, "out of a 'finally'"))
1223+
def test_no_jump_out_of_finally_block(output):
12161224
output.append(1)
12171225
try:
12181226
output.append(3)
12191227
finally:
12201228
output.append(5)
12211229

1222-
@jump_test(5, 1, [1, 3], (ValueError, 'finally'))
1223-
def test_no_jump_out_of_finally_block(output):
1230+
@jump_test(1, 5, [], (ValueError, "into an 'except'"))
1231+
def test_no_jump_into_bare_except_block(output):
12241232
output.append(1)
12251233
try:
12261234
output.append(3)
1227-
finally:
1235+
except:
1236+
output.append(5)
1237+
1238+
@jump_test(1, 5, [], (ValueError, "into an 'except'"))
1239+
def test_no_jump_into_qualified_except_block(output):
1240+
output.append(1)
1241+
try:
1242+
output.append(3)
1243+
except Exception:
1244+
output.append(5)
1245+
1246+
@jump_test(3, 6, [2, 5, 6], (ValueError, "into an 'except'"))
1247+
def test_no_jump_into_bare_except_block_from_try_block(output):
1248+
try:
1249+
output.append(2)
1250+
output.append(3)
1251+
except: # executed if the jump is failed
12281252
output.append(5)
1253+
output.append(6)
1254+
raise
1255+
output.append(8)
1256+
1257+
@jump_test(3, 6, [2], (ValueError, "into an 'except'"))
1258+
def test_no_jump_into_qualified_except_block_from_try_block(output):
1259+
try:
1260+
output.append(2)
1261+
output.append(3)
1262+
except ZeroDivisionError:
1263+
output.append(5)
1264+
output.append(6)
1265+
raise
1266+
output.append(8)
1267+
1268+
@jump_test(7, 1, [1, 3, 6], (ValueError, "out of an 'except'"))
1269+
def test_no_jump_out_of_bare_except_block(output):
1270+
output.append(1)
1271+
try:
1272+
output.append(3)
1273+
1/0
1274+
except:
1275+
output.append(6)
1276+
output.append(7)
1277+
1278+
@jump_test(7, 1, [1, 3, 6], (ValueError, "out of an 'except'"))
1279+
def test_no_jump_out_of_qualified_except_block(output):
1280+
output.append(1)
1281+
try:
1282+
output.append(3)
1283+
1/0
1284+
except Exception:
1285+
output.append(6)
1286+
output.append(7)
12291287

12301288
@jump_test(3, 5, [1, 2, -2], (ValueError, 'into'))
12311289
def test_no_jump_between_with_blocks(output):

Objects/frameobject.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,12 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
277277
int first_in = target_addr <= f->f_lasti && f->f_lasti <= addr;
278278
int second_in = target_addr <= new_lasti && new_lasti <= addr;
279279
if (first_in != second_in) {
280-
PyErr_SetString(PyExc_ValueError,
281-
"can't jump into or out of a 'finally' block");
280+
op = code[target_addr];
281+
PyErr_Format(PyExc_ValueError,
282+
"can't jump %s %s block",
283+
second_in ? "into" : "out of",
284+
(op == DUP_TOP || op == POP_TOP) ?
285+
"an 'except'" : "a 'finally'");
282286
return -1;
283287
}
284288
break;

0 commit comments

Comments
 (0)