1
1
import os
2
2
from pickle import dump
3
3
import sys
4
- from test .support import captured_stdout
4
+ from test .support import captured_stdout , requires_resource , requires_gil_enabled
5
5
from test .support .os_helper import (TESTFN , rmtree , unlink )
6
6
from test .support .script_helper import assert_python_ok , assert_python_failure
7
7
import textwrap
8
8
import unittest
9
+ from types import FunctionType
9
10
10
11
import trace
11
12
from trace import Trace
@@ -197,9 +198,7 @@ def test_trace_list_comprehension(self):
197
198
firstlineno_called = get_firstlineno (traced_doubler )
198
199
expected = {
199
200
(self .my_py_filename , firstlineno_calling + 1 ): 1 ,
200
- # List comprehensions work differently in 3.x, so the count
201
- # below changed compared to 2.x.
202
- (self .my_py_filename , firstlineno_calling + 2 ): 12 ,
201
+ (self .my_py_filename , firstlineno_calling + 2 ): 11 ,
203
202
(self .my_py_filename , firstlineno_calling + 3 ): 1 ,
204
203
(self .my_py_filename , firstlineno_called + 1 ): 10 ,
205
204
}
@@ -251,7 +250,7 @@ def setUp(self):
251
250
self .my_py_filename = fix_ext_py (__file__ )
252
251
self .addCleanup (sys .settrace , sys .gettrace ())
253
252
254
- # TODO: RUSTPYTHON, KeyError: ('Lib/test/test_trace.py', 43)
253
+ # TODO: RUSTPYTHON
255
254
@unittest .expectedFailure
256
255
def test_exec_counts (self ):
257
256
self .tracer = Trace (count = 1 , trace = 0 , countfuncs = 0 , countcallers = 0 )
@@ -287,7 +286,7 @@ def tearDown(self):
287
286
if self ._saved_tracefunc is not None :
288
287
sys .settrace (self ._saved_tracefunc )
289
288
290
- # TODO: RUSTPYTHON, gc
289
+ # TODO: RUSTPYTHON
291
290
@unittest .expectedFailure
292
291
def test_simple_caller (self ):
293
292
self .tracer .runfunc (traced_func_simple_caller , 1 )
@@ -306,7 +305,7 @@ def test_arg_errors(self):
306
305
with self .assertRaises (TypeError ):
307
306
self .tracer .runfunc ()
308
307
309
- # TODO: RUSTPYTHON, gc
308
+ # TODO: RUSTPYTHON
310
309
@unittest .expectedFailure
311
310
def test_loop_caller_importing (self ):
312
311
self .tracer .runfunc (traced_func_importing_caller , 1 )
@@ -320,10 +319,11 @@ def test_loop_caller_importing(self):
320
319
}
321
320
self .assertEqual (self .tracer .results ().calledfuncs , expected )
322
321
323
- # TODO: RUSTPYTHON, gc
322
+ # TODO: RUSTPYTHON
324
323
@unittest .expectedFailure
325
324
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
326
325
'pre-existing trace function throws off measurements' )
326
+ @requires_gil_enabled ("gh-117783: immortalization of types affects traced method names" )
327
327
def test_inst_method_calling (self ):
328
328
obj = TracedClass (20 )
329
329
self .tracer .runfunc (obj .inst_method_calling , 1 )
@@ -335,7 +335,7 @@ def test_inst_method_calling(self):
335
335
}
336
336
self .assertEqual (self .tracer .results ().calledfuncs , expected )
337
337
338
- # TODO: RUSTPYTHON, gc
338
+ # TODO: RUSTPYTHON
339
339
@unittest .expectedFailure
340
340
def test_traced_decorated_function (self ):
341
341
self .tracer .runfunc (traced_decorated_function )
@@ -357,9 +357,11 @@ def setUp(self):
357
357
self .tracer = Trace (count = 0 , trace = 0 , countcallers = 1 )
358
358
self .filemod = my_file_and_modname ()
359
359
360
+ # TODO: RUSTPYTHON
361
+ @unittest .expectedFailure
360
362
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
361
363
'pre-existing trace function throws off measurements' )
362
- @unittest . skip ( "TODO: RUSTPYTHON, Error in atexit._run_exitfuncs " )
364
+ @requires_gil_enabled ( "gh-117783: immortalization of types affects traced method names " )
363
365
def test_loop_caller_importing (self ):
364
366
self .tracer .runfunc (traced_func_importing_caller , 1 )
365
367
@@ -387,15 +389,21 @@ def tearDown(self):
387
389
rmtree (TESTFN )
388
390
unlink (TESTFN )
389
391
390
- def _coverage (self , tracer ,
391
- cmd = 'import test.support, test.test_pprint;'
392
- 'test.support.run_unittest(test.test_pprint.QueryTestCase)' ):
392
+ DEFAULT_SCRIPT = '''if True:
393
+ import unittest
394
+ from test.test_pprint import QueryTestCase
395
+ loader = unittest.TestLoader()
396
+ tests = loader.loadTestsFromTestCase(QueryTestCase)
397
+ tests(unittest.TestResult())
398
+ '''
399
+ def _coverage (self , tracer , cmd = DEFAULT_SCRIPT ):
393
400
tracer .run (cmd )
394
401
r = tracer .results ()
395
402
r .write_results (show_missing = True , summary = True , coverdir = TESTFN )
396
403
397
404
# TODO: RUSTPYTHON
398
405
@unittest .expectedFailure
406
+ @requires_resource ('cpu' )
399
407
def test_coverage (self ):
400
408
tracer = trace .Trace (trace = 0 , count = 1 )
401
409
with captured_stdout () as stdout :
@@ -412,7 +420,7 @@ def test_coverage_ignore(self):
412
420
libpath = os .path .normpath (os .path .dirname (os .path .dirname (__file__ )))
413
421
# sys.prefix does not work when running from a checkout
414
422
tracer = trace .Trace (ignoredirs = [sys .base_prefix , sys .base_exec_prefix ,
415
- libpath ], trace = 0 , count = 1 )
423
+ libpath ] + sys . path , trace = 0 , count = 1 )
416
424
with captured_stdout () as stdout :
417
425
self ._coverage (tracer )
418
426
if os .path .exists (TESTFN ):
@@ -592,5 +600,31 @@ def test_run_as_module(self):
592
600
assert_python_failure ('-m' , 'trace' , '-l' , '--module' , 'not_a_module_zzz' )
593
601
594
602
603
+ class TestTrace (unittest .TestCase ):
604
+ def setUp (self ):
605
+ self .addCleanup (sys .settrace , sys .gettrace ())
606
+ self .tracer = Trace (count = 0 , trace = 1 )
607
+ self .filemod = my_file_and_modname ()
608
+
609
+ # TODO: RUSTPYTHON
610
+ @unittest .expectedFailure
611
+ def test_no_source_file (self ):
612
+ filename = "<unknown>"
613
+ co = traced_func_linear .__code__
614
+ co = co .replace (co_filename = filename )
615
+ f = FunctionType (co , globals ())
616
+
617
+ with captured_stdout () as out :
618
+ self .tracer .runfunc (f , 2 , 3 )
619
+
620
+ out = out .getvalue ().splitlines ()
621
+ firstlineno = get_firstlineno (f )
622
+ self .assertIn (f" --- modulename: { self .filemod [1 ]} , funcname: { f .__code__ .co_name } " , out [0 ])
623
+ self .assertIn (f"{ filename } ({ firstlineno + 1 } )" , out [1 ])
624
+ self .assertIn (f"{ filename } ({ firstlineno + 2 } )" , out [2 ])
625
+ self .assertIn (f"{ filename } ({ firstlineno + 3 } )" , out [3 ])
626
+ self .assertIn (f"{ filename } ({ firstlineno + 4 } )" , out [4 ])
627
+
628
+
595
629
if __name__ == '__main__' :
596
630
unittest .main ()
0 commit comments