Skip to content
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

Closed
sonderblade mannequin opened this issue Jun 10, 2005 · 16 comments
Closed

inspect.getsource doesn't update when a module is reloaded #42071

sonderblade mannequin opened this issue Jun 10, 2005 · 16 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sonderblade
Copy link
Mannequin

sonderblade mannequin commented Jun 10, 2005

BPO 1218234
Nosy @birkenfeld, @jaraco, @devdanzin, @bitdancer, @berkerpeksag, @1st1
Files
  • inspect.py.diff
  • test_inspect-trunk.diff: Test source reloading with inspect.getsource
  • test_inspect-py3k2.diff: Test source reloading with inspect.getsource in py3k
  • issue1218234.diff
  • 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:

    assignee = None
    closed_at = <Date 2014-12-08.21:07:36.879>
    created_at = <Date 2005-06-10.13:49:10.000>
    labels = ['type-bug', 'library']
    title = "inspect.getsource doesn't update when a module is reloaded"
    updated_at = <Date 2014-12-08.23:10:15.628>
    user = 'https://bugs.python.org/sonderblade'

    bugs.python.org fields:

    activity = <Date 2014-12-08.23:10:15.628>
    actor = 'yselivanov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-12-08.21:07:36.879>
    closer = 'yselivanov'
    components = ['Library (Lib)']
    creation = <Date 2005-06-10.13:49:10.000>
    creator = 'sonderblade'
    dependencies = []
    files = ['9382', '13775', '13778', '28272']
    hgrepos = []
    issue_num = 1218234
    keywords = ['patch']
    message_count = 16.0
    messages = ['60756', '60757', '62174', '86540', '86544', '110435', '110436', '110438', '217951', '217990', '217991', '232328', '232329', '232332', '232335', '232336']
    nosy_count = 9.0
    nosy_names = ['georg.brandl', 'jaraco', 'sonderblade', 'ajaksu2', 'gpolo', 'r.david.murray', 'python-dev', 'berker.peksag', 'yselivanov']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1218234'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

    @sonderblade
    Copy link
    Mannequin Author

    sonderblade mannequin commented Jun 10, 2005

    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
    session):

    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.

    @sonderblade sonderblade mannequin added stdlib Python modules in the Lib dir labels Jun 10, 2005
    @birkenfeld
    Copy link
    Member

    Logged In: YES
    user_id=1188172

    This is the fault of the linecache module which inspect
    uses. It caches the file contents and does not reload it
    accordingly. However, I assume it's pretty hard finding a
    good solution to this.

    @gpolo
    Copy link
    Mannequin

    gpolo mannequin commented Feb 7, 2008

    I'm attaching a patch. Is there some hidden problem that it may cause ?
    By the way, this issue is a duplicate of http://bugs.python.org/issue993580

    @devdanzin
    Copy link
    Mannequin

    devdanzin mannequin commented Apr 25, 2009

    Fix was committed to py3k but with no tests AFAIK. Here's a (failing)
    test for trunk that runs OK after Guilherme's patch.

    We might want to keep this behavior restricted for when the module was
    reloaded.

    @devdanzin devdanzin mannequin added type-bug An unexpected behavior, bug, or error labels Apr 25, 2009
    @devdanzin
    Copy link
    Mannequin

    devdanzin mannequin commented Apr 25, 2009

    Er, no, it wasn't committed to py3k, it was just me getting my patches
    mixed. Attaching the py3k test with relative import fixed.

    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jul 16, 2010

    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?

    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Jul 16, 2010

    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.

    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Jul 16, 2010

    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.

    @jaraco
    Copy link
    Member

    jaraco commented May 5, 2014

    This issue appears implicated in https://bitbucket.org/pypa/setuptools/issue/201

    @1st1
    Copy link
    Member

    1st1 commented May 6, 2014

    Thanks, I'll take a look at the patch.

    @bitdancer
    Copy link
    Member

    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.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 8, 2014

    New changeset 8e2505d535c8 by Yury Selivanov in branch 'default':
    inspect: Fix getsource() to load updated source of reloaded module
    https://hg.python.org/cpython/rev/8e2505d535c8

    @1st1
    Copy link
    Member

    1st1 commented Dec 8, 2014

    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!

    @1st1 1st1 closed this as completed Dec 8, 2014
    @1st1 1st1 closed this as completed Dec 8, 2014
    @jaraco
    Copy link
    Member

    jaraco commented Dec 8, 2014

    FWIW, I'd appreciate a backport to 3.4, given that 3.5 is scheduled for release in Sep 2015.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 8, 2014

    New changeset e52d8e888df1 by Yury Selivanov in branch '3.4':
    inspect: Fix getsource() to load updated source of reloaded module
    https://hg.python.org/cpython/rev/e52d8e888df1

    @1st1
    Copy link
    Member

    1st1 commented Dec 8, 2014

    @jason: done ;)

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants