Skip to content

Commit e082d8c

Browse files
authored
Merge pull request #4586 from youknowone/trace
Update trace from CPython 3.11.2
2 parents 94237df + 9377bef commit e082d8c

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

Lib/test/test_trace.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
2+
from pickle import dump
23
import sys
34
from test.support import captured_stdout
4-
from test.support.os_helper import TESTFN, TESTFN_UNICODE, FS_NONASCII, rmtree, unlink
5+
from test.support.os_helper import (TESTFN, rmtree, unlink)
56
from test.support.script_helper import assert_python_ok, assert_python_failure
67
import textwrap
78
import unittest
@@ -11,6 +12,11 @@
1112

1213
from test.tracedmodules import testmod
1314

15+
##
16+
## See also test_sys_settrace.py, which contains tests that cover
17+
## tracing of many more code blocks.
18+
##
19+
1420
#------------------------------- Utilities -----------------------------------#
1521

1622
def fix_ext_py(filename):
@@ -191,7 +197,7 @@ def test_trace_list_comprehension(self):
191197
firstlineno_called = get_firstlineno(traced_doubler)
192198
expected = {
193199
(self.my_py_filename, firstlineno_calling + 1): 1,
194-
# List compehentions work differently in 3.x, so the count
200+
# List comprehensions work differently in 3.x, so the count
195201
# below changed compared to 2.x.
196202
(self.my_py_filename, firstlineno_calling + 2): 12,
197203
(self.my_py_filename, firstlineno_calling + 3): 1,
@@ -212,9 +218,9 @@ def test_traced_decorated_function(self):
212218
(self.my_py_filename, firstlineno + 4): 1,
213219
(self.my_py_filename, firstlineno + 5): 1,
214220
(self.my_py_filename, firstlineno + 6): 1,
215-
(self.my_py_filename, firstlineno + 7): 1,
216-
(self.my_py_filename, firstlineno + 8): 1,
217-
(self.my_py_filename, firstlineno + 9): 1,
221+
(self.my_py_filename, firstlineno + 7): 2,
222+
(self.my_py_filename, firstlineno + 8): 2,
223+
(self.my_py_filename, firstlineno + 9): 2,
218224
(self.my_py_filename, firstlineno + 10): 1,
219225
(self.my_py_filename, firstlineno + 11): 1,
220226
}
@@ -297,9 +303,8 @@ def test_simple_caller(self):
297303
def test_arg_errors(self):
298304
res = self.tracer.runfunc(traced_capturer, 1, 2, self=3, func=4)
299305
self.assertEqual(res, ((1, 2), {'self': 3, 'func': 4}))
300-
with self.assertWarns(DeprecationWarning):
301-
res = self.tracer.runfunc(func=traced_capturer, arg=1)
302-
self.assertEqual(res, ((), {'arg': 1}))
306+
with self.assertRaises(TypeError):
307+
self.tracer.runfunc(func=traced_capturer, arg=1)
303308
with self.assertRaises(TypeError):
304309
self.tracer.runfunc()
305310

@@ -406,7 +411,7 @@ def test_coverage(self):
406411

407412
def test_coverage_ignore(self):
408413
# Ignore all files, nothing should be traced nor printed
409-
libpath = os.path.normpath(os.path.dirname(os.__file__))
414+
libpath = os.path.normpath(os.path.dirname(os.path.dirname(__file__)))
410415
# sys.prefix does not work when running from a checkout
411416
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,
412417
libpath], trace=0, count=1)
@@ -439,6 +444,15 @@ def test_issue9936(self):
439444
self.assertIn(modname, coverage)
440445
self.assertEqual(coverage[modname], (5, 100))
441446

447+
def test_coverageresults_update(self):
448+
# Update empty CoverageResults with a non-empty infile.
449+
infile = TESTFN + '-infile'
450+
with open(infile, 'wb') as f:
451+
dump(({}, {}, {'caller': 1}), f, protocol=1)
452+
self.addCleanup(unlink, infile)
453+
results = trace.CoverageResults({}, {}, infile, {})
454+
self.assertEqual(results.callers, {'caller': 1})
455+
442456
### Tests that don't mess with sys.settrace and can be traced
443457
### themselves TODO: Skip tests that do mess with sys.settrace when
444458
### regrtest is invoked with -T option.
@@ -547,7 +561,8 @@ def test_sys_argv_list(self):
547561
fd.write("print(type(sys.argv))\n")
548562

549563
status, direct_stdout, stderr = assert_python_ok(TESTFN)
550-
status, trace_stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN)
564+
status, trace_stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN,
565+
PYTHONIOENCODING='utf-8')
551566
self.assertIn(direct_stdout.strip(), trace_stdout)
552567

553568
# TODO: RUSTPYTHON
@@ -569,7 +584,8 @@ def f():
569584
for i in range(10):
570585
f()
571586
"""))
572-
status, stdout, _ = assert_python_ok('-m', 'trace', '-cs', filename)
587+
status, stdout, _ = assert_python_ok('-m', 'trace', '-cs', filename,
588+
PYTHONIOENCODING='utf-8')
573589
stdout = stdout.decode()
574590
self.assertEqual(status, 0)
575591
self.assertIn('lines cov% module (path)', stdout)

Lib/trace.py

100644100755
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def names(self, filename, modulename):
116116
return 0
117117

118118
def _modname(path):
119-
"""Return a plausible module name for the patch."""
119+
"""Return a plausible module name for the path."""
120120

121121
base = os.path.basename(path)
122122
filename, ext = os.path.splitext(base)
@@ -172,7 +172,7 @@ def __init__(self, counts=None, calledfuncs=None, infile=None,
172172
try:
173173
with open(self.infile, 'rb') as f:
174174
counts, calledfuncs, callers = pickle.load(f)
175-
self.update(self.__class__(counts, calledfuncs, callers))
175+
self.update(self.__class__(counts, calledfuncs, callers=callers))
176176
except (OSError, EOFError, ValueError) as err:
177177
print(("Skipping counts file %r: %s"
178178
% (self.infile, err)), file=sys.stderr)
@@ -453,22 +453,7 @@ def runctx(self, cmd, globals=None, locals=None):
453453
sys.settrace(None)
454454
threading.settrace(None)
455455

456-
def runfunc(*args, **kw):
457-
if len(args) >= 2:
458-
self, func, *args = args
459-
elif not args:
460-
raise TypeError("descriptor 'runfunc' of 'Trace' object "
461-
"needs an argument")
462-
elif 'func' in kw:
463-
func = kw.pop('func')
464-
self, *args = args
465-
import warnings
466-
warnings.warn("Passing 'func' as keyword argument is deprecated",
467-
DeprecationWarning, stacklevel=2)
468-
else:
469-
raise TypeError('runfunc expected at least 1 positional argument, '
470-
'got %d' % (len(args)-1))
471-
456+
def runfunc(self, func, /, *args, **kw):
472457
result = None
473458
if not self.donothing:
474459
sys.settrace(self.globaltrace)
@@ -478,7 +463,6 @@ def runfunc(*args, **kw):
478463
if not self.donothing:
479464
sys.settrace(None)
480465
return result
481-
runfunc.__text_signature__ = '($self, func, /, *args, **kw)'
482466

483467
def file_module_function_of(self, frame):
484468
code = frame.f_code

0 commit comments

Comments
 (0)