mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
gh-103615: Use local events for opcode tracing (GH-109472)
* Use local monitoring for opcode trace * Remove f_opcode_trace_set * Add test for setting f_trace_opcodes after settrace
This commit is contained in:
parent
2bc01cc0c7
commit
e0afed7e27
9 changed files with 114 additions and 8 deletions
|
@ -9,6 +9,10 @@ from functools import wraps
|
|||
import asyncio
|
||||
from test.support import import_helper
|
||||
import contextlib
|
||||
import os
|
||||
import tempfile
|
||||
import textwrap
|
||||
import subprocess
|
||||
import warnings
|
||||
|
||||
support.requires_working_socket(module=True)
|
||||
|
@ -1802,6 +1806,39 @@ class TraceOpcodesTestCase(TraceTestCase):
|
|||
def make_tracer():
|
||||
return Tracer(trace_opcode_events=True)
|
||||
|
||||
def test_trace_opcodes_after_settrace(self):
|
||||
"""Make sure setting f_trace_opcodes after starting trace works even
|
||||
if it's the first time f_trace_opcodes is being set. GH-103615"""
|
||||
|
||||
code = textwrap.dedent("""
|
||||
import sys
|
||||
|
||||
def opcode_trace_func(frame, event, arg):
|
||||
if event == "opcode":
|
||||
print("opcode trace triggered")
|
||||
return opcode_trace_func
|
||||
|
||||
sys.settrace(opcode_trace_func)
|
||||
sys._getframe().f_trace = opcode_trace_func
|
||||
sys._getframe().f_trace_opcodes = True
|
||||
a = 1
|
||||
""")
|
||||
|
||||
# We can't use context manager because Windows can't execute a file while
|
||||
# it's being written
|
||||
tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.py')
|
||||
tmp.write(code.encode('utf-8'))
|
||||
tmp.close()
|
||||
try:
|
||||
p = subprocess.Popen([sys.executable, tmp.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
p.wait()
|
||||
out = p.stdout.read()
|
||||
finally:
|
||||
os.remove(tmp.name)
|
||||
p.stdout.close()
|
||||
p.stderr.close()
|
||||
self.assertIn(b"opcode trace triggered", out)
|
||||
|
||||
|
||||
class RaisingTraceFuncTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue