Skip to content

Commit ce89422

Browse files
committed
Handle new opcode RETURN_CONST from python3.13.
1 parent ef1ee66 commit ce89422

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

docs/cookbook.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ If you can't simply review all the sourcecode then runtime analysis is one way t
255255
elif event.depth < self.depth and event.kind == 'return': # stop if function returned
256256
op = event.instruction
257257
op = op if isinstance(op, int) else ord(op)
258-
if op == RETURN_VALUE:
258+
if op in RETURN_OPCODES:
259259
self.output("{BRIGHT}{fore(BLUE)}{} tracing {} on {}{RESET}\n",
260260
">" * 46, event.function, self.exc)
261261
for event in self.events:
@@ -285,7 +285,7 @@ The most basic implementation that only measures timings looks like this:
285285
.. code-block:: python
286286
287287
from hunter.actions import Action
288-
from hunter.actions import RETURN_VALUE
288+
from hunter.actions import RETURN_OPCODES
289289
290290
class ProfileAction(Action):
291291
def __init__(self):
@@ -321,7 +321,7 @@ This means that we have to store the exception for a little while, and do the ch
321321
.. code-block:: python
322322
323323
from hunter.actions import Action
324-
from hunter.actions import RETURN_VALUE
324+
from hunter.actions import RETURN_OPCODES
325325
326326
class ProfileAction(Action):
327327
def __init__(self):
@@ -342,7 +342,7 @@ This means that we have to store the exception for a little while, and do the ch
342342
self.timings[frame_id] = start_time, event.arg
343343
elif event.kind == 'return':
344344
delta = current_time - start_time
345-
if event.instruction == RETURN_VALUE:
345+
if event.instruction in RETURN_OPCODES:
346346
# exception was discarded
347347
print(f'{event.function} returned: {event.arg}. Duration: {delta:.4f}s\n')
348348
else:
@@ -362,7 +362,7 @@ Behold, a `ProfileAction` that works in any mode:
362362
.. code-block:: python
363363
364364
from hunter.actions import ColorStreamAction
365-
from hunter.actions import RETURN_VALUE
365+
from hunter.actions import RETURN_OPCODES
366366
367367
class ProfileAction(ColorStreamAction):
368368
# using ColorStreamAction brings this more in line with the other actions
@@ -405,7 +405,7 @@ Behold, a `ProfileAction` that works in any mode:
405405
self.timings[frame_id] = start_time, event.arg
406406
elif event.kind == 'return':
407407
delta = current_time - start_time
408-
if event.instruction == RETURN_VALUE:
408+
if event.instruction in RETURN_OPCODES:
409409
# exception was discarded
410410
self.output(
411411
'{fore(BLUE)}{} returned: {}. Duration: {:.4f}s{RESET}\n',

src/hunter/actions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,9 @@ def __call__(self, event):
721721
del self.stored_reprs[scope_key]
722722

723723

724-
RETURN_VALUE = opcode.opmap['RETURN_VALUE']
724+
RETURN_OPCODES = (opcode.opmap['RETURN_VALUE'],)
725+
if 'RETURN_CONST' in opcode.opmap:
726+
RETURN_OPCODES += (opcode.opmap['RETURN_CONST'],)
725727

726728

727729
class ErrorSnooper(CodePrinter):
@@ -790,7 +792,7 @@ def __call__(self, event):
790792
detached_event = event.detach(self.try_repr)
791793
self.events.append(detached_event)
792794
if event.depth == self.origin.depth - 1: # stop if the same function returned (depth is -1)
793-
if event.instruction == RETURN_VALUE:
795+
if event.instruction in RETURN_OPCODES:
794796
self.dump_events()
795797
self.output(
796798
'{BRIGHT}{fore(BLACK)}{} function exit{RESET}\n',

tests/test_cookbook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
import hunter
12-
from hunter.actions import RETURN_VALUE
12+
from hunter.actions import RETURN_OPCODES
1313
from hunter.actions import ColorStreamAction
1414
from hunter.util import safe_repr
1515

@@ -117,7 +117,7 @@ def __call__(self, event):
117117
self.timings[frame_id] = start_time, event.arg
118118
elif event.kind == 'return':
119119
delta = current_time - start_time
120-
if event.instruction == RETURN_VALUE:
120+
if event.instruction in RETURN_OPCODES:
121121
# exception was discarded
122122
self.output(
123123
'{fore(BLUE)}{} returned: {}. Duration: {:.4f}s{RESET}\n',

0 commit comments

Comments
 (0)