55import opcode
66import tempfile
77import unittest
8- from unittest import mock
98
109import _colorize
1110
2928 MockThreadInfo ,
3029)
3130
31+ try :
32+ import _remote_debugging # noqa: F401
33+ except ImportError :
34+ _remote_debugging = None
35+
3236
3337StructseqInterpreterInfo = namedtuple (
3438 "StructseqInterpreterInfo" ,
4549
4650
4751class TestStackDumpFormatting (unittest .TestCase ):
48- @staticmethod
49- def _patch_start_time (value = None ):
50- return mock .patch (
51- "profiling.sampling.dump._target_process_start_time" ,
52- return_value = value ,
53- )
54-
5552 def test_format_stack_dump_single_thread (self ):
5653 frames = [
5754 MockFrameInfo ("leaf.py" , 10 , "leaf" ),
@@ -74,8 +71,7 @@ def test_format_stack_dump_single_thread(self):
7471 )
7572 ]
7673
77- with self ._patch_start_time ():
78- output = format_stack_dump (stack_frames , pid = 42 , colorize = False )
74+ output = format_stack_dump (stack_frames , pid = 42 , colorize = False )
7975
8076 self .assertIn (
8177 "Stack dump for PID 42, thread 123 "
@@ -112,8 +108,7 @@ def test_format_stack_dump_with_structseq_tuples(self):
112108 )
113109 ]
114110
115- with self ._patch_start_time ():
116- output = format_stack_dump (stack_frames , pid = 42 , colorize = False )
111+ output = format_stack_dump (stack_frames , pid = 42 , colorize = False )
117112
118113 self .assertIn (
119114 "Stack dump for PID 42, thread 123 "
@@ -324,6 +319,7 @@ def test_format_stack_dump_filters_internal_frames(self):
324319 self .assertIn (' File "user.py", line 10, in user' , output )
325320 self .assertNotIn ("_sync_coordinator.py" , output )
326321
322+ @unittest .skipIf (_remote_debugging is None , "requires _remote_debugging" )
327323 def test_format_stack_dump_async_task (self ):
328324 task = MockTaskInfo (
329325 task_id = 1 ,
@@ -366,8 +362,7 @@ def test_format_stack_dump_strips_source_like_traceback(self):
366362 )
367363 ]
368364
369- with self ._patch_start_time ():
370- output = format_stack_dump (stack_frames , pid = 42 , colorize = False )
365+ output = format_stack_dump (stack_frames , pid = 42 , colorize = False )
371366
372367 self .assertIn (' File "' , output )
373368 self .assertIn ('", line 1, in <module>' , output )
@@ -395,8 +390,7 @@ def test_format_stack_dump_highlights_source_range(self):
395390 ]
396391 theme = _colorize .get_theme (force_color = True ).profiler_dump
397392
398- with self ._patch_start_time ():
399- output = format_stack_dump (stack_frames , pid = 42 , colorize = True )
393+ output = format_stack_dump (stack_frames , pid = 42 , colorize = True )
400394
401395 self .assertIn (
402396 f"{ theme .source_highlight } \" \" \" { theme .reset } " ,
@@ -429,45 +423,12 @@ def test_format_stack_dump_highlights_source_range_after_trimming(self):
429423 ]
430424 theme = _colorize .get_theme (force_color = True ).profiler_dump
431425
432- with self ._patch_start_time ():
433- output = format_stack_dump (stack_frames , pid = 42 , colorize = True )
426+ output = format_stack_dump (stack_frames , pid = 42 , colorize = True )
434427
435428 self .assertIn (f"{ theme .source_highlight } call{ theme .reset } " , output )
436429 self .assertIn ("\n result = call(arg)\n " , _colorize .decolor (output ))
437430 self .assertNotIn ("\n result = call(arg)\n " , _colorize .decolor (output ))
438431
439- def test_format_stack_dump_warns_about_changed_source (self ):
440- with tempfile .TemporaryDirectory () as tmp_dir :
441- filename = os .path .join (tmp_dir , "target.py" )
442- with open (filename , "w" , encoding = "utf-8" ) as file :
443- file .write ("time.sleep(SLEEP_SECONDS)\n " )
444- os .utime (filename , (200 , 200 ))
445-
446- stack_frames = [
447- MockInterpreterInfo (
448- 0 ,
449- [
450- MockThreadInfo (
451- 123 ,
452- [
453- StructseqFrameInfo (
454- filename ,
455- StructseqLocationInfo (1 , 1 , 0 , 4 ),
456- "<module>" ,
457- None ,
458- )
459- ],
460- )
461- ],
462- )
463- ]
464-
465- with self ._patch_start_time (value = 100 ):
466- output = format_stack_dump (stack_frames , pid = 42 , colorize = False )
467-
468- self .assertIn ("[source file changed after process started]" , output )
469- self .assertNotIn ("time.sleep" , output )
470-
471432 def test_format_stack_dump_empty (self ):
472433 output = format_stack_dump ([], pid = 42 , colorize = False )
473434
0 commit comments