gaogaotiantian / objprint

A library that can print Python objects in human readable format
Apache License 2.0
515 stars 43 forks source link

Crash with ipdb #111

Open riyadparvez opened 1 week ago

riyadparvez commented 1 week ago
from objprint import config, op, install

install()
config(
    line_number=True,
    arg_name=True,
)

for i in range(3):
    op(i)
    import ipdb; ipdb.set_trace()

On first hit of the breaskpoint if I continue and on the second hit of the op(i) throws exception.

The traceback:

Traceback (most recent call last):
  File "/Users/-----------/test.py", line 10, in <module>
    op(i)
  File "/opt/homebrew/lib/python3.11/site-packages/objprint/objprint.py", line 95, in __call__
    args = self.frame_analyzer.get_args(call_frame)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/objprint/frame_analyzer.py", line 20, in get_args
    func_call_str = self.get_executing_function_call_str(frame)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/objprint/frame_analyzer.py", line 58, in get_executing_function_call_str
    source = inspect.getsource(module)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 1258, in getsource
    lines, lnum = getsourcelines(object)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 1240, in getsourcelines
    lines, lnum = findsource(object)
                  ^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 1059, in findsource
    file = getsourcefile(object)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 936, in getsourcefile
    filename = getfile(object)
               ^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 897, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__main__'> is a built-in module

If you suspect this is an IPython 8.12.0 bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True
gaogaotiantian commented 1 week ago

arg_name=True requires the source code access but ipython definitely does something weird when it enters the debugger. If you throw out arg_name=True this should work fine.

riyadparvez commented 1 week ago

Thank you for the workaround. That being said I do think an exception shouldn't be thrown since this is primarily a debugging tool. Is it possible to just catch the exception and leave the source line empty?

gaogaotiantian commented 1 week ago

Yes, TypeError should be caught together with OSError if the source is not available. I will fix it later. Thanks for the issue.