``bninspect`` prints documentation about a command that is about to be run The interpreter will invoke this function if you input a line ending in `?` e.g. `bv?` :param str code_: Python code to be evaluated :param dict globals_: globals() from callsite :param dict locals_: locals() from call
(code_, globals_, locals_)
| 613 | |
| 614 | |
| 615 | def bninspect(code_, globals_, locals_): |
| 616 | """ |
| 617 | ``bninspect`` prints documentation about a command that is about to be run |
| 618 | The interpreter will invoke this function if you input a line ending in `?` e.g. `bv?` |
| 619 | |
| 620 | :param str code_: Python code to be evaluated |
| 621 | :param dict globals_: globals() from callsite |
| 622 | :param dict locals_: locals() from callsite |
| 623 | """ |
| 624 | try: |
| 625 | import inspect |
| 626 | value = eval(code_, globals_, locals_) |
| 627 | |
| 628 | try: |
| 629 | if not (inspect.ismethod(value) or inspect.isclass(value)): |
| 630 | if isinstance(code_, bytes): |
| 631 | code_ = code_.decode("utf-8") |
| 632 | class_type_str = code_.split(".")[:-1] |
| 633 | class_value = eval("type(" + ".".join(class_type_str) + ")." + code_.split(".")[-1], globals_, locals_) |
| 634 | doc = inspect.getdoc(class_value) |
| 635 | if doc is None: |
| 636 | comments = inspect.getcomments(class_value) |
| 637 | if comments is None: |
| 638 | pass |
| 639 | else: |
| 640 | print(comments) |
| 641 | return |
| 642 | else: |
| 643 | print(doc) |
| 644 | return |
| 645 | except: |
| 646 | pass |
| 647 | |
| 648 | doc = inspect.getdoc(value) |
| 649 | if doc is None: |
| 650 | comments = inspect.getcomments(value) |
| 651 | if comments is None: |
| 652 | pass |
| 653 | else: |
| 654 | print(comments) |
| 655 | return |
| 656 | else: |
| 657 | print(doc) |
| 658 | return |
| 659 | |
| 660 | print(f"No documentation found for {code_}") |
| 661 | except: |
| 662 | # Hide exceptions so the normal execution can report them |
| 663 | pass |
| 664 | |
| 665 | |
| 666 | class PythonScriptingInstance(ScriptingInstance): |