|
8 | 8 | import ubelt as ub
|
9 | 9 |
|
10 | 10 |
|
| 11 | +@contextlib.contextmanager |
| 12 | +def enter_tmpdir(): |
| 13 | + with contextlib.ExitStack() as stack: |
| 14 | + enter = stack.enter_context |
| 15 | + tmpdir = os.path.abspath(enter(tempfile.TemporaryDirectory())) |
| 16 | + enter(ub.ChDir(tmpdir)) |
| 17 | + yield ub.Path(tmpdir) |
| 18 | + |
| 19 | + |
11 | 20 | def test_simple_explicit_nonglobal_usage():
|
12 | 21 | """
|
13 | 22 | python -c "from test_explicit_profile import *; test_simple_explicit_nonglobal_usage()"
|
@@ -458,10 +467,7 @@ def write(path, code):
|
458 | 467 | '' if wrap_class is None else f', wrap={wrap_class}',
|
459 | 468 | reset_enable_count))
|
460 | 469 |
|
461 |
| - with contextlib.ExitStack() as stack: |
462 |
| - enter = stack.enter_context |
463 |
| - enter(ub.ChDir(enter(tempfile.TemporaryDirectory()))) |
464 |
| - curdir = ub.Path.cwd() |
| 470 | + with enter_tmpdir() as curdir: |
465 | 471 | write(curdir / 'script.py', script)
|
466 | 472 | write(curdir / 'my_module_1.py',
|
467 | 473 | '''
|
@@ -507,49 +513,180 @@ def test_profiler_add_class_recursion_guard():
|
507 | 513 | has a reference to the other in its namespace, we don't end up in
|
508 | 514 | infinite recursion.
|
509 | 515 | """
|
510 |
| - with contextlib.ExitStack() as stack: |
511 |
| - enter = stack.enter_context |
512 |
| - enter(ub.ChDir(enter(tempfile.TemporaryDirectory()))) |
513 |
| - curdir = ub.Path.cwd() |
514 |
| - (curdir / 'script.py').write_text(ub.codeblock(""" |
515 |
| - from line_profiler import LineProfiler |
| 516 | + from line_profiler import LineProfiler |
| 517 | + |
| 518 | + class Class1: |
| 519 | + def method1(self): |
| 520 | + pass |
| 521 | + |
| 522 | + class ChildClass1: |
| 523 | + def child_method_1(self): |
| 524 | + pass |
| 525 | + |
| 526 | + class Class2: |
| 527 | + def method2(self): |
| 528 | + pass |
| 529 | + |
| 530 | + class ChildClass2: |
| 531 | + def child_method_2(self): |
| 532 | + pass |
| 533 | + |
| 534 | + OtherClass = Class1 |
| 535 | + # A duplicate reference shouldn't affect profiling either |
| 536 | + YetAnotherClass = Class1 |
| 537 | + |
| 538 | + # Add self/mutual references |
| 539 | + Class1.ThisClass = Class1 |
| 540 | + Class1.OtherClass = Class2 |
516 | 541 |
|
| 542 | + profile = LineProfiler() |
| 543 | + profile.add_class(Class1) |
| 544 | + assert len(profile.functions) == 4 |
| 545 | + assert Class1.method1 in profile.functions |
| 546 | + assert Class2.method2 in profile.functions |
| 547 | + assert Class1.ChildClass1.child_method_1 in profile.functions |
| 548 | + assert Class2.ChildClass2.child_method_2 in profile.functions |
517 | 549 |
|
| 550 | + |
| 551 | +def test_profiler_warn_unwrappable(): |
| 552 | + """ |
| 553 | + Test for warnings when using `LineProfiler.add_*(wrap=True)` with a |
| 554 | + namespace which doesn't allow attribute assignment. |
| 555 | + """ |
| 556 | + from line_profiler import LineProfiler |
| 557 | + |
| 558 | + class ProblamticMeta(type): |
| 559 | + def __init__(cls, *args, **kwargs): |
| 560 | + super(ProblamticMeta, cls).__init__(*args, **kwargs) |
| 561 | + cls._initialized = True |
| 562 | + |
| 563 | + def __setattr__(cls, attr, value): |
| 564 | + if not getattr(cls, '_initialized', None): |
| 565 | + return super(ProblamticMeta, cls).__setattr__(attr, value) |
| 566 | + raise AttributeError( |
| 567 | + f'cannot set attribute on {type(cls)} instance') |
| 568 | + |
| 569 | + class ProblematicClass(metaclass=ProblamticMeta): |
| 570 | + def method(self): |
| 571 | + pass |
| 572 | + |
| 573 | + profile = LineProfiler() |
| 574 | + vanilla_method = ProblematicClass.method |
| 575 | + |
| 576 | + with pytest.warns(match=r"cannot wrap 1 attribute\(s\) of " |
| 577 | + r"<class '.*\.ProblematicClass'> \(`\{attr: value\}`\): " |
| 578 | + r"\{'method': <function .*\.method at 0x.*>\}"): |
| 579 | + # The method is added to the profiler, but we can't assign its |
| 580 | + # wrapper back into the class namespace |
| 581 | + assert profile.add_class(ProblematicClass, wrap=True) == 1 |
| 582 | + |
| 583 | + assert ProblematicClass.method is vanilla_method |
| 584 | + |
| 585 | + |
| 586 | +@pytest.mark.parametrize( |
| 587 | + ('match_scope', 'add_module_targets', 'add_class_targets'), |
| 588 | + [('exact', |
| 589 | + {'class2_method', 'child_class2_method'}, |
| 590 | + {'class3_method', 'child_class3_method'}), |
| 591 | + ('descendants', |
| 592 | + {'class2_method', 'child_class2_method', |
| 593 | + 'class3_method', 'child_class3_method'}, |
| 594 | + {'class3_method', 'child_class3_method'}), |
| 595 | + ('siblings', |
| 596 | + {'class1_method', 'child_class1_method', |
| 597 | + 'class2_method', 'child_class2_method', |
| 598 | + 'class3_method', 'child_class3_method', 'other_class3_method'}, |
| 599 | + {'class3_method', 'child_class3_method', 'other_class3_method'}), |
| 600 | + ('none', |
| 601 | + {'class1_method', 'child_class1_method', |
| 602 | + 'class2_method', 'child_class2_method', |
| 603 | + 'class3_method', 'child_class3_method', 'other_class3_method'}, |
| 604 | + {'child_class1_method', |
| 605 | + 'class3_method', 'child_class3_method', 'other_class3_method'})]) |
| 606 | +def test_profiler_scope_matching(monkeypatch, |
| 607 | + match_scope, |
| 608 | + add_module_targets, |
| 609 | + add_class_targets): |
| 610 | + """ |
| 611 | + Test for the scope-matching strategies of the `LineProfiler.add_*()` |
| 612 | + methods. |
| 613 | + """ |
| 614 | + def write(path, code=None): |
| 615 | + path.parent.mkdir(exist_ok=True, parents=True) |
| 616 | + if code is None: |
| 617 | + path.touch() |
| 618 | + else: |
| 619 | + path.write_text(ub.codeblock(code)) |
| 620 | + |
| 621 | + with enter_tmpdir() as curdir: |
| 622 | + pkg_dir = curdir / 'packages' / 'my_pkg' |
| 623 | + write(pkg_dir / '__init__.py') |
| 624 | + write(pkg_dir / 'submod1.py', |
| 625 | + """ |
518 | 626 | class Class1:
|
519 |
| - def method1(self): |
| 627 | + def class1_method(self): |
520 | 628 | pass
|
521 | 629 |
|
522 | 630 | class ChildClass1:
|
523 |
| - def child_method_1(self): |
| 631 | + def child_class1_method(self): |
524 | 632 | pass
|
| 633 | + """) |
| 634 | + write(pkg_dir / 'subpkg2' / '__init__.py', |
| 635 | + """ |
| 636 | + from ..submod1 import Class1 # Import from a sibling |
| 637 | + from .submod3 import Class3 # Import from a descendant |
525 | 638 |
|
526 | 639 |
|
527 | 640 | class Class2:
|
528 |
| - def method2(self): |
| 641 | + def class2_method(self): |
529 | 642 | pass
|
530 | 643 |
|
531 | 644 | class ChildClass2:
|
532 |
| - def child_method_2(self): |
| 645 | + def child_class2_method(self): |
533 | 646 | pass
|
534 | 647 |
|
535 |
| - OtherClass = Class1 |
536 |
| - # A duplicate reference shouldn't affect profiling either |
537 |
| - YetAnotherClass = Class1 |
| 648 | + BorrowedChildClass = Class1.ChildClass1 # Non-sibling class |
| 649 | + """) |
| 650 | + write(pkg_dir / 'subpkg2' / 'submod3.py', |
| 651 | + """ |
| 652 | + from ..submod1 import Class1 |
538 | 653 |
|
539 | 654 |
|
540 |
| - # Add self/mutual references |
541 |
| - Class1.ThisClass = Class1 |
542 |
| - Class1.OtherClass = Class2 |
| 655 | + class Class3: |
| 656 | + def class3_method(self): |
| 657 | + pass |
543 | 658 |
|
| 659 | + class OtherChildClass3: |
| 660 | + def child_class3_method(self): |
| 661 | + pass |
| 662 | +
|
| 663 | + # Unrelated class |
| 664 | + BorrowedChildClass1 = Class1.ChildClass1 |
| 665 | +
|
| 666 | + class OtherClass3: |
| 667 | + def other_class3_method(self): |
| 668 | + pass |
| 669 | +
|
| 670 | + # Sibling class |
| 671 | + Class3.BorrowedChildClass3 = OtherClass3 |
| 672 | + """) |
| 673 | + monkeypatch.syspath_prepend(pkg_dir.parent) |
| 674 | + |
| 675 | + from my_pkg import subpkg2 |
| 676 | + from line_profiler import LineProfiler |
| 677 | + |
| 678 | + # Add a module |
| 679 | + profile = LineProfiler() |
| 680 | + profile.add_module(subpkg2, match_scope=match_scope) |
| 681 | + assert len(profile.functions) == len(add_module_targets) |
| 682 | + added = {func.__name__ for func in profile.functions} |
| 683 | + assert added == set(add_module_targets) |
| 684 | + # Add a class |
544 | 685 | profile = LineProfiler()
|
545 |
| - profile.add_class(Class1) |
546 |
| - assert len(profile.functions) == 4 |
547 |
| - assert Class1.method1 in profile.functions |
548 |
| - assert Class2.method2 in profile.functions |
549 |
| - assert Class1.ChildClass1.child_method_1 in profile.functions |
550 |
| - assert Class2.ChildClass2.child_method_2 in profile.functions |
551 |
| - """)) |
552 |
| - ub.cmd([sys.executable, 'script.py'], verbose=2).check_returncode() |
| 686 | + profile.add_class(subpkg2.Class3, match_scope=match_scope) |
| 687 | + assert len(profile.functions) == len(add_class_targets) |
| 688 | + added = {func.__name__ for func in profile.functions} |
| 689 | + assert added == set(add_class_targets) |
553 | 690 |
|
554 | 691 |
|
555 | 692 | if __name__ == '__main__':
|
|
0 commit comments