@@ -432,19 +432,28 @@ def test_autoprofile_script_with_prof_imports():
432
432
433
433
434
434
@pytest .mark .parametrize (
435
- ['use_kernprof_exec' , 'prof_mod' , 'prof_imports' ,
435
+ ['use_kernprof_exec' , 'prof_mod' , 'no_preimports' , ' prof_imports' ,
436
436
'add_one' , 'add_two' , 'add_operator' , 'main' ],
437
- [(False , 'test_mod.submod1' , False , True , False , False , False ),
438
- (False , 'test_mod.submod2' , True , False , True , True , False ),
439
- (False , 'test_mod' , True , True , True , True , True ),
437
+ [(False , 'test_mod.submod1' , False , False ,
438
+ True , False , True , False ),
439
+ # By not using `--no-preimports`, the entirety of `.submod1` is
440
+ # passed to `add_imported_function_or_module()`
441
+ (False , 'test_mod.submod1' , True , False ,
442
+ True , False , False , False ),
443
+ (False , 'test_mod.submod2' , False , True ,
444
+ False , True , True , False ),
445
+ (False , 'test_mod' , False , True ,
446
+ True , True , True , True ),
440
447
# Explicitly add all the modules via multiple `-p` flags, without
441
448
# using the `--prof-imports` flag
442
- (False , ['test_mod' , 'test_mod.submod1,test_mod.submod2' ], False ,
449
+ (False , ['test_mod' , 'test_mod.submod1,test_mod.submod2' ], False , False ,
443
450
True , True , True , True ),
444
- (False , None , True , False , False , False , False ),
445
- (True , None , True , False , False , False , False )])
451
+ (False , None , False , True ,
452
+ False , False , False , False ),
453
+ (True , None , False , True ,
454
+ False , False , False , False )])
446
455
def test_autoprofile_exec_package (
447
- use_kernprof_exec , prof_mod , prof_imports ,
456
+ use_kernprof_exec , prof_mod , no_preimports , prof_imports ,
448
457
add_one , add_two , add_operator , main ):
449
458
"""
450
459
Test the execution of a package.
@@ -461,6 +470,8 @@ def test_autoprofile_exec_package(
461
470
prof_mod = [prof_mod ]
462
471
for pm in prof_mod :
463
472
args .extend (['-p' , pm ])
473
+ if no_preimports :
474
+ args .append ('--no-preimports' )
464
475
if prof_imports :
465
476
args .append ('--prof-imports' )
466
477
args .extend (['-l' , '-m' , 'test_mod' , '1' , '2' , '3' ])
@@ -484,16 +495,28 @@ def test_autoprofile_exec_package(
484
495
485
496
486
497
@pytest .mark .parametrize (
487
- ['use_kernprof_exec' , 'prof_mod' , 'prof_imports' ,
488
- 'add_one' , 'add_two' , 'add_four' , 'add_operator' , 'main' ],
489
- [(False , 'test_mod.submod2' , False , False , True , False , False , False ),
490
- (False , 'test_mod.submod1' , False , True , False , False , True , False ),
491
- (False , 'test_mod.subpkg.submod4' , True , True , True , True , True , True ),
492
- (False , None , True , False , False , False , False , False ),
493
- (True , None , True , False , False , False , False , False )])
498
+ ['use_kernprof_exec' , 'prof_mod' , 'no_preimports' , 'prof_imports' ,
499
+ 'add_one' , 'add_two' , 'add_three' , 'add_four' , 'add_operator' , 'main' ],
500
+ [(False , 'test_mod.submod2,test_mod.subpkg.submod3.add_three' , True , False ,
501
+ False , True , False , False , False , False ),
502
+ # By not using `--no-preimports`:
503
+ # - The entirety of `.submod2` is passed to
504
+ # `add_imported_function_or_module()`
505
+ # - Despite not having been imported anywhere, `add_three()` is
506
+ # still profiled
507
+ (False , 'test_mod.submod2,test_mod.subpkg.submod3.add_three' , False , False ,
508
+ False , True , True , False , True , False ),
509
+ (False , 'test_mod.submod1' , False , False ,
510
+ True , False , False , False , True , False ),
511
+ (False , 'test_mod.subpkg.submod4' , False , True ,
512
+ True , True , False , True , True , True ),
513
+ (False , None , False , True ,
514
+ False , False , False , False , False , False ),
515
+ (True , None , False , True ,
516
+ False , False , False , False , False , False )])
494
517
def test_autoprofile_exec_module (
495
- use_kernprof_exec , prof_mod , prof_imports ,
496
- add_one , add_two , add_four , add_operator , main ):
518
+ use_kernprof_exec , prof_mod , no_preimports , prof_imports ,
519
+ add_one , add_two , add_three , add_four , add_operator , main ):
497
520
"""
498
521
Test the execution of a module.
499
522
"""
@@ -506,6 +529,8 @@ def test_autoprofile_exec_module(
506
529
args = [sys .executable , '-m' , 'kernprof' ]
507
530
if prof_mod is not None :
508
531
args .extend (['-p' , prof_mod ])
532
+ if no_preimports :
533
+ args .append ('--no-preimports' )
509
534
if prof_imports :
510
535
args .append ('--prof-imports' )
511
536
args .extend (['-l' , '-m' , 'test_mod.subpkg.submod4' , '1' , '2' , '3' ])
@@ -524,6 +549,7 @@ def test_autoprofile_exec_module(
524
549
525
550
assert ('Function: add_one' in raw_output ) == add_one
526
551
assert ('Function: add_two' in raw_output ) == add_two
552
+ assert ('Function: add_three' in raw_output ) == add_three
527
553
assert ('Function: add_four' in raw_output ) == add_four
528
554
assert ('Function: add_operator' in raw_output ) == add_operator
529
555
assert ('Function: _main' in raw_output ) == main
@@ -625,78 +651,14 @@ def test_autoprofile_from_inlined_script(outfile, expected_outfile) -> None:
625
651
626
652
627
653
@pytest .mark .parametrize (
628
- ['prof_mod' , 'eager_preimports' ,
629
- 'add_one' , 'add_two' , 'add_three' , 'add_four' , 'add_operator' , 'main' ],
630
- # Test that `--eager-preimports` know to exclude the script run
631
- # (so as not to inadvertantly run it twice)
632
- [('script.py' , None , False , False , False , False , False , True ),
633
- (None , 'script.py' , False , False , False , False , False , True ),
634
- # Test explicitly passing targets to `--eager-preimports`
635
- (['test_mod.submod1,test_mod.submod2' , 'test_mod.subpkg.submod4' ], None ,
636
- True , True , False , False , True , False ),
637
- (['test_mod.submod1,test_mod.submod2' ], ['test_mod.subpkg.submod4' ],
638
- True , True , False , True , True , False ),
639
- (None , ['test_mod.submod1,test_mod.submod2' , 'test_mod.subpkg.submod4' ],
640
- True , True , False , True , True , False ),
641
- # Test implicitly passing targets to `--eager-preimports`
642
- (['test_mod.submod1,test_mod.submod2' , 'test_mod.subpkg.submod4' ], True ,
643
- True , True , False , True , True , False )])
644
- def test_autoprofile_eager_preimports (
645
- prof_mod , eager_preimports ,
646
- add_one , add_two , add_three , add_four , add_operator , main ):
647
- """
648
- Test eager imports with the `-e`/`--eager-preimports` flag.
649
- """
650
- with tempfile .TemporaryDirectory () as tmpdir :
651
- temp_dpath = ub .Path (tmpdir )
652
- _write_demo_module (temp_dpath )
653
-
654
- args = [sys .executable , '-m' , 'kernprof' ]
655
- if prof_mod is not None :
656
- if isinstance (prof_mod , str ):
657
- prof_mod = [prof_mod ]
658
- for target in prof_mod :
659
- args .extend (['-p' , target ])
660
- if eager_preimports in (True ,):
661
- args .append ('-e' )
662
- elif eager_preimports is not None :
663
- if isinstance (eager_preimports , str ):
664
- eager_preimports = [eager_preimports ]
665
- for target in eager_preimports :
666
- args .extend (['-e' , target ])
667
- args .extend (['-l' , 'script.py' ])
668
- proc = ub .cmd (args , cwd = temp_dpath , verbose = 2 )
669
- # Check that pre-imports don't accidentally run the code twice
670
- assert proc .stdout .count ('7.9' ) == 1
671
- print (proc .stdout )
672
- print (proc .stderr )
673
- proc .check_returncode ()
674
-
675
- prof = temp_dpath / 'script.py.lprof'
676
-
677
- args = [sys .executable , '-m' , 'line_profiler' , os .fspath (prof )]
678
- proc = ub .cmd (args , cwd = temp_dpath )
679
- raw_output = proc .stdout
680
- print (raw_output )
681
- proc .check_returncode ()
682
-
683
- assert ('Function: add_one' in raw_output ) == add_one
684
- assert ('Function: add_two' in raw_output ) == add_two
685
- assert ('Function: add_three' in raw_output ) == add_three
686
- assert ('Function: add_four' in raw_output ) == add_four
687
- assert ('Function: add_operator' in raw_output ) == add_operator
688
- assert ('Function: main' in raw_output ) == main
689
-
690
-
691
- @pytest .mark .parametrize (
692
- ('eager_preimports, function, method, class_method, static_method, '
654
+ ('prof_mod, function, method, class_method, static_method, '
693
655
'descriptor' ),
694
656
[('my_module' , True , True , True , True , True ),
695
657
# `function()` included in profiling via `Class.partial_method()`
696
658
('my_module.Class' , True , True , True , True , True ),
697
659
('my_module.Class.descriptor' , False , False , False , False , True )])
698
660
def test_autoprofile_callable_wrapper_objects (
699
- eager_preimports , function , method , class_method , static_method ,
661
+ prof_mod , function , method , class_method , static_method ,
700
662
descriptor ):
701
663
"""
702
664
Test that on-import profiling catches various callable-wrapper
@@ -747,7 +709,7 @@ def descriptor(self):
747
709
748
710
with ub .ChDir (temp_dpath ):
749
711
args = [sys .executable , '-m' , 'kernprof' ,
750
- '-e ' , eager_preimports , '-lv' , 'script.py' ]
712
+ '-p ' , prof_mod , '-lv' , 'script.py' ]
751
713
python_path = os .environ .get ('PYTHONPATH' )
752
714
if python_path :
753
715
python_path = '{}:{}' .format (path , python_path )
0 commit comments