[issue44648] Inspect.getsource raises wrong error on classes in interactive session

Andrei Kulakov report at bugs.python.org
Fri Jul 16 11:38:40 EDT 2021


Andrei Kulakov <andrei.avk at gmail.com> added the comment:

This is kind of interesting:

 - The unit test was wrong, it was catching the wrong OSError. (I was catching regex first but after some tweaking and changes I lost it and forgot to readd)

 - The reason it was passing is exactly what you pointed out -- the __main__.py in `unittest` package. After I change the class' module to __main__ in the test, it starts looking at sys.modules['__main__'] which is the one in unittest, which doesn't have this class, which causes the inspect to throw another OSError.

 - I fixed the unit test by both checking for error regex and patching sys.modules['__main__'] (and of course restoring it later).

Thanks for helping to catch this :-)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44648>
_______________________________________


More information about the Python-bugs-list mailing list