summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_sys_settrace.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-05-13 10:52:54 (GMT)
committerGitHub <noreply@github.com>2022-05-13 10:52:54 (GMT)
commit2e8b2d0ee2f8d0c53d2747d400b83c171732d3a9 (patch)
tree4fa4792578899ed499bbef3291edf2bf0cf0616d /Lib/test/test_sys_settrace.py
parentc41667e71b6f1b0c9581160ccd82026738c3a736 (diff)
downloadcpython-2e8b2d0ee2f8d0c53d2747d400b83c171732d3a9.zip
cpython-2e8b2d0ee2f8d0c53d2747d400b83c171732d3a9.tar.gz
cpython-2e8b2d0ee2f8d0c53d2747d400b83c171732d3a9.tar.bz2
GH-92236: Remove spurious "line" event when starting coroutine or generator. (GH-92722) (GH-92772)
(cherry picked from commit 22a1db378c5c381272362c5b2f68ac78a368e136)
Diffstat (limited to 'Lib/test/test_sys_settrace.py')
-rw-r--r--Lib/test/test_sys_settrace.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py
index b1c8f6f..1a67fa6 100644
--- a/Lib/test/test_sys_settrace.py
+++ b/Lib/test/test_sys_settrace.py
@@ -7,6 +7,7 @@ import difflib
import gc
from functools import wraps
import asyncio
+from test.support import import_helper
support.requires_working_socket(module=True)
@@ -1473,6 +1474,58 @@ class TraceTestCase(unittest.TestCase):
(3, 'return'),
(1, 'return')])
+ @support.cpython_only
+ def test_no_line_event_after_creating_generator(self):
+ # Spurious line events before call events only show up with C tracer
+
+ # Skip this test if the _testcapi module isn't available.
+ _testcapi = import_helper.import_module('_testcapi')
+
+ def gen():
+ yield 1
+
+ def func():
+ for _ in (
+ gen()
+ ):
+ pass
+
+ EXPECTED_EVENTS = [
+ (0, 'call'),
+ (2, 'line'),
+ (1, 'line'),
+ (-3, 'call'),
+ (-2, 'line'),
+ (-2, 'return'),
+ (4, 'line'),
+ (1, 'line'),
+ (-2, 'call'),
+ (-2, 'return'),
+ (1, 'return'),
+ ]
+
+ # C level events should be the same as expected and the same as Python level.
+
+ events = []
+ # Turning on and off tracing must be on same line to avoid unwanted LINE events.
+ _testcapi.settrace_to_record(events); func(); sys.settrace(None)
+ start_line = func.__code__.co_firstlineno
+ events = [
+ (line-start_line, EVENT_NAMES[what])
+ for (what, line, arg) in events
+ ]
+ self.assertEqual(events, EXPECTED_EVENTS)
+
+ self.run_and_compare(func, EXPECTED_EVENTS)
+
+
+EVENT_NAMES = [
+ 'call',
+ 'exception',
+ 'line',
+ 'return'
+]
+
class SkipLineEventsTraceTestCase(TraceTestCase):
"""Repeat the trace tests, but with per-line events skipped"""