Skip to content

Commit ac926dc

Browse files
author
condut
committed
Update merge
1 parent 0419965 commit ac926dc

2 files changed

Lines changed: 22 additions & 31 deletions

File tree

goto.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _parse_instructions(code, yield_nones_at_end=0):
104104
extended_arg = 0
105105
extended_arg_offset = None
106106
yield (dis.opname[opcode], oparg, offset)
107-
107+
108108
for _ in range(yield_nones_at_end):
109109
yield (None, None, None)
110110

@@ -129,15 +129,6 @@ def _get_instructions_size(ops):
129129
size += _get_instruction_size(op)
130130
else:
131131
size += _get_instruction_size(*op)
132-
return size
133-
134-
def _get_instructions_size(ops):
135-
size = 0
136-
for op in ops:
137-
if isinstance(op, str):
138-
size += _get_instruction_size(op)
139-
else:
140-
size += _get_instruction_size(*op)
141132
return size
142133

143134
def _write_instruction(buf, pos, opname, oparg=0):
@@ -182,28 +173,28 @@ def _find_labels_and_gotos(code):
182173
opname1 = oparg1 = offset1 = None # the main one we're looking at each loop iteration
183174
opname2 = oparg2 = offset2 = None
184175
opname3 = oparg3 = offset3 = None
185-
176+
186177
def replace_block_in_stack(stack, old_block, new_block):
187178
for i, block in enumerate(stack):
188179
if block == old_block:
189180
stack[i] = new_block
190-
181+
191182
def replace_block(old_block, new_block):
192183
replace_block_in_stack(block_stack, old_block, new_block)
193184
for label in labels:
194185
replace_block_in_stack(labels[label][2], old_block, new_block)
195186
for goto in gotos:
196187
replace_block_in_stack(goto[3], old_block, new_block)
197-
188+
198189
def pop_block():
199190
if block_stack:
200191
return block_stack.pop()
201192
else:
202193
_warn_bug("can't pop block")
203-
194+
204195
def pop_block_of_type(type):
205196
if block_stack and block_stack[-1][0] != type:
206-
# in 3.8, only finally blocks are supported, so we must determine the except/finally nature ourselves, and replace the block afterwards
197+
# in 3.8, only finally blocks are supported, so we must determine the except/finally nature ourselves, and replace the block afterwards
207198
if not _BYTECODE.has_setup_except and type == "<EXCEPT>" and block_stack[-1][0] == '<FINALLY>':
208199
replace_block(block_stack[-1], (type, block_stack[-1][1]))
209200
else:
@@ -212,7 +203,7 @@ def pop_block_of_type(type):
212203

213204
for opname4, oparg4, offset4 in _parse_instructions(code.co_code, 3):
214205
endoffset1 = offset2
215-
206+
216207
# check for special offsets
217208
if for_exits and offset1 == for_exits[-1]:
218209
last_block = pop_block()
@@ -225,7 +216,7 @@ def pop_block_of_type(type):
225216
block_counter += 1
226217
block_stack.append(('<FINALLY>', block_counter))
227218
finallies.pop()
228-
219+
229220
# check for special opcodes
230221
if opname1 in ('LOAD_GLOBAL', 'LOAD_NAME'):
231222
if opname2 == 'LOAD_ATTR' and opname3 == 'POP_TOP':
@@ -288,7 +279,7 @@ def _patch_code(code):
288279
new_code = _patched_code_cache.get(code)
289280
if new_code is not None:
290281
return new_code
291-
282+
292283
labels, gotos = _find_labels_and_gotos(code)
293284
buf = array.array('B', code.co_code)
294285

@@ -343,7 +334,7 @@ def _patch_code(code):
343334
_inject_nop_sled(buf, pos, end)
344335

345336
new_code = _make_code(code, _array_to_bytes(buf))
346-
337+
347338
_patched_code_cache[code] = new_code
348339
return new_code
349340

test_goto.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ def test_large_jumps_in_diff_orders():
131131
@with_goto
132132
def func():
133133
goto .start
134-
134+
135135
if NonConstFalse:
136136
label .finalle
137137
return (i, j, k, m, n, i1, j1, k1, m1, n1, i2, j2, k2, m2, n2)
138-
138+
139139
label .start
140140
for i in range(2):
141141
for j in range(2):
@@ -159,7 +159,7 @@ def func():
159159
goto .finalle
160160

161161
assert func() == (0,) * 15
162-
162+
163163
def test_jump_out_of_nested_11_loops():
164164
@with_goto
165165
def func():
@@ -223,7 +223,7 @@ def func():
223223
goto .out
224224
label .out
225225
return c.data()
226-
226+
227227
assert func()== (1, 0)
228228

229229
def test_jump_out_of_with_block_and_survive():
@@ -235,9 +235,9 @@ def func():
235235
goto .out
236236
label .out
237237
return (i, c.data())
238-
238+
239239
assert func() == (2, (3, 0))
240-
240+
241241
def test_jump_out_of_with_block_and_live():
242242
@with_goto
243243
def func():
@@ -248,9 +248,9 @@ def func():
248248
goto .out
249249
label .out
250250
return (i, j, c.data())
251-
251+
252252
assert func() == (2, 0, (3, 0))
253-
253+
254254
def test_jump_into_with_block():
255255
def func():
256256
with Context() as c:
@@ -270,7 +270,7 @@ def func():
270270
label .x
271271
yield 4
272272
yield 5
273-
273+
274274
assert tuple(func()) == (0, 1, 4, 5)
275275

276276
def test_jump_out_of_try_block():
@@ -457,15 +457,15 @@ def func():
457457

458458
assert newfunc is not func
459459
assert newfunc.foo == 'bar'
460-
460+
461461
def test_code_is_not_copy():
462462
def outer_func():
463463
@with_goto
464464
def inner_func():
465465
goto .test
466466
label .test
467467
return inner_func
468-
468+
469469
assert outer_func() is not outer_func()
470470
assert outer_func().__code__ is outer_func().__code__
471-
471+

0 commit comments

Comments
 (0)