This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: undo findsource regression/change
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: r.david.murray Nosy List: Trundle, benjamin.peterson, hpk, r.david.murray
Priority: normal Keywords: patch

Created on 2010-05-14 22:59 by hpk, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
findsource.patch hpk, 2010-05-15 07:26 patch to Lib/, relative to r311
getsourcfile.patch r.david.murray, 2010-05-21 00:59
Messages (9)
msg105776 - (view) Author: holger krekel (hpk) Date: 2010-05-14 22:59
Somewhere between python 3.0.1 and 3.1.2 the behaviour of inspect.findsource changed: it does not find the source code anymore for a code object whose filename/source is in the linecache.cache because instead of the previous::

    file = getsourcefile(object) or getfile(object)

it only does now::

    file = getsourcefile(object) 

IMO it is enough if findsource() raises if it can't eventually discover the source code - i don't see the need to do this ahead.

In any case, the r312 findsource version requires an ugly work around within py.test in order for it to continue to be able to use the inspect module with dynamically compiled code.  The underlying issue is that at code-compile time no sensible (PEP302 compliantly loaded) module can be constructed because this is only known at exec-time but this is not part of the test tool API and is done somewhere in user code. 

Proposed fix: revert to r301 behaviour and add back "or getfile(object)" to the first line of inspect.findsource()
msg105778 - (view) Author: Andreas Stührk (Trundle) * Date: 2010-05-14 23:47
This was changed due to issue #4050.
msg105780 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-05-15 01:36
As I remember it, #4050 was in turn caused by a change elsewhere in the codebase to using linecache where it wasn't used before.

Regardless, though, the problem is that findsource doesn't produce an error when called on a binary module. It thinks it found the source code, but what it found was binary data.

@holger: can you suggest an alternate fix to #4050?
msg105794 - (view) Author: holger krekel (hpk) Date: 2010-05-15 07:26
Thanks for helping with this!  Attached is a patch that adds a keyword "check=True" to getsourcefile so that findsource can defer existence-checking until after cache lookup.  Eventually, findsource will still raise an IOError if it can't find the source file and cannot recover the source from cache.  The patch mainly consists of adding a number of tests to specify the (refined) behaviour of findsource and getsourcefile.  

best, holger
msg106209 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-05-21 00:59
Holger, what do you think of this alternate patch that instead makes getsourcefile smarter?  It's a simpler patch and doesn't change the API, and your tests (suitably modified) pass.
msg106218 - (view) Author: holger krekel (hpk) Date: 2010-05-21 09:04
David, your getsourcefile.patch looks fine (and better than mine) to me.
msg106224 - (view) Author: holger krekel (hpk) Date: 2010-05-21 10:06
Well, maybe we could introduce a "linecache.setlines" function to give
the linecache module control over its internal caching and data
handling. What do you think?
msg107913 - (view) Author: holger krekel (hpk) Date: 2010-06-16 11:38
Seems the inspect.getsourcefile regression now is in the RC1 of Python2.7 as well.  I suggest to apply the "getsourcefile.patch" patch which was attached from David.  I tested it and it works fine for Python2.7 and Python3.1.
msg107986 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-06-17 02:07
I've applied the patch to trunk in r82039, to py3k in r82041, and 3.1 in r82042.

Holger, I'm not really familiar with the linecache module and its uses, so I'm not sure what your proposal is aimed at.  Perhaps you could flesh out your proposal in a new ticket?
Date User Action Args
2022-04-11 14:57:01adminsetgithub: 52966
2010-06-17 02:07:22r.david.murraysetstatus: pending -> closed
versions: - Python 3.3
messages: + msg107986

assignee: r.david.murray
resolution: fixed
stage: patch review -> resolved
2010-06-16 11:38:47hpksetstatus: open -> pending

messages: + msg107913
versions: + Python 2.7, Python 3.3
2010-05-21 10:06:27hpksetmessages: + msg106224
2010-05-21 09:04:47hpksetmessages: + msg106218
2010-05-21 00:59:14r.david.murraysetfiles: + getsourcfile.patch

messages: + msg106209
stage: patch review
2010-05-15 07:26:18hpksetfiles: + findsource.patch
keywords: + patch
messages: + msg105794
2010-05-15 01:36:28r.david.murraysetmessages: + msg105780
2010-05-15 00:47:16pitrousetnosy: + r.david.murray

versions: + Python 3.2
2010-05-14 23:47:13Trundlesetnosy: + Trundle
messages: + msg105778
2010-05-14 22:59:53hpkcreate