New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inspect.getsource doesn't update when a module is reloaded #42071
Comments
This is test.py: def foo():
print "Bla" And in an interactive session: >>> import inspect, test
>>> test.foo()
Bla
>>> inspect.getsource(test.foo)
'def foo():\n print "Bla"\n' Now I edit test.py to (without exiting the interactive def foo():
print "Oh no!" >>> reload(test)
<module 'test' from 'test.py'>
>>> test.foo()
Oh no!
>>> inspect.getsource(test.foo)
'def foo():\n print "Bla"\n' inspect should output the new source. |
Logged In: YES This is the fault of the linecache module which inspect |
I'm attaching a patch. Is there some hidden problem that it may cause ? |
Fix was committed to py3k but with no tests AFAIK. Here's a (failing) We might want to keep this behavior restricted for when the module was |
Er, no, it wasn't committed to py3k, it was just me getting my patches |
The change to inspect.py is two lines. Given that unit test patches are also supplied could someone with the knowledge give yes or no to accepting this patch? |
Calling linecache.checkcache for every inspect.getsource call sounds like a fairly bad idea to me. linecache.checkcache does a stat() of every single cached file. |
Ah, sorry. I didn't read carefully enough. I see that the patch passes in the filename and checkcache restricts the work it does in that case. Something else to consider, though, is that this change means you'll also get the new source if you *don't* reload the module, too. So, what exactly is inspect.getsource() supposed to be doing? Giving you the current on-disk contents of the relevant source file? Or giving you the actual source corresponding to the object passed to it? If the latter, then this actually needs to be more tightly integrated with module reloading somehow. |
This issue appears implicated in https://bitbucket.org/pypa/setuptools/issue/201 |
Thanks, I'll take a look at the patch. |
Note that there has never been a strict correspondence between linecache/getsource, the module in memory, and the code on disk. If the file is changed before a traceback is generated, for example, you will get the new source. That doesn't mean fixing the reload case is wrong, it's just something to keep in mind. |
New changeset 8e2505d535c8 by Yury Selivanov in branch 'default': |
Fixed in 3.5. Not sure if we need to backport this to 3.4 and 2.7. Closing this issue. Thanks to Björn Lindqvist and Berker Peksag! |
FWIW, I'd appreciate a backport to 3.4, given that 3.5 is scheduled for release in Sep 2015. |
New changeset e52d8e888df1 by Yury Selivanov in branch '3.4': |
@jason: done ;) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: