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.

classification
Title: inspect.findsource() cannot find source for doctest code
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, benjamin.peterson, brodie, djc, pitrou, python-dev, r.david.murray
Priority: normal Keywords: needs review, patch

Created on 2010-07-17 13:16 by djc, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue9284.diff djc, 2011-06-09 16:30
Messages (13)
msg110551 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2010-07-17 13:16
The fix for issue4050 broke some of my doctests. Minimal test:

import doctest, inspect
def test():
    '''
    >>> def x(): pass
    >>> inspect.getsource(x)
    'def x(): pass\\n'
    '''
doctest.run_docstring_examples(test, globals())

This works in 2.6, but not in 2.7. Reason is that inspect.getsourcefile() finds the fake filename '<docTest NoName[0]>', which it doesn't understand. In 2.6, inspect.getmodule() is also tried, which first looks at obj.__module__, and the filename can be derived from that. I suggest that inspect.getsourcefile() grows some code to use this trick if the filename seems fake (f[0] + f[-1] == '<>').
msg110553 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-07-17 13:43
After chatting with Dirkjan, I misunderstood the impact of the patch. It only occurs when inspect.getsource() is called from a doctest, which isn't a very common situation.
msg110557 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2010-07-17 14:24
Here's a test case that doesn't require doctest trickery:

import inspect, linecache
fn, source = '<test>', 'def x(): pass\n'
getlines = linecache.getlines
def monkey(filename, module_globals=None):
     if filename == fn:
         return source.splitlines(True)
     else:
         return getlines(filename, module_globals)
linecache.getlines = monkey
exec compile(source, fn, 'single') in globals()
inspect.getsource(x)
msg110579 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-07-17 16:32
I don't understand why monkey patching linecache produces a valid test case.  Can you explain?

Since I believe the same code/bugfix is in 3.x, I'm adding those versions.
msg110581 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2010-07-17 16:37
Because doctest also monkeypatches linecache, and without monkeypatching linecache this also fails in 2.6.
msg110584 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-07-17 17:00
Oh, right, I remember that now.  Thanks.
msg137797 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2011-06-07 09:49
Here's an attempted patch against 2.7. It seemed nice to put the test in test_doctest, but maybe it belongs in inspect...
msg137940 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2011-06-09 07:38
Would it still be possible to get this into 2.7.2? It's a 2.6-2.7 regression, would be nice to fix, and it seems fairly low-impact.
msg137980 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-06-09 14:52
- First line should be directly after the docstring
- One import per line
- Shouldn't this test be in test_inspect, since that's what you're changing?
msg137991 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2011-06-09 15:38
I'm fine with moving the test; I put it in doctest because the inspect behavior we're relying upon here seems somewhat doctest-specific, and the test itself is a doctest. Do you want me to move it? I'll fix up the other things as well.
msg137992 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-06-09 15:40
2011/6/9 Dirkjan Ochtman <report@bugs.python.org>:
>
> Dirkjan Ochtman <dirkjan@ochtman.nl> added the comment:
>
> I'm fine with moving the test; I put it in doctest because the inspect behavior we're relying upon here seems somewhat doctest-specific, and the test itself is a doctest. Do you want me to move it? I'll fix up the other things as well.

I would prefer that if the bug is somehow reintroduced, test_inspect
instead of test_doctest fails.
msg138009 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2011-06-09 16:30
Here's a fresh patch.
msg138183 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-06-11 20:56
New changeset 527c40add91d by Benjamin Peterson in branch '2.7':
allow "fake" filenames in findsource (closes #9284)
http://hg.python.org/cpython/rev/527c40add91d

New changeset 6cc4579dca02 by Benjamin Peterson in branch '3.2':
allow "fake" filenames in findsource (closes #9284)
http://hg.python.org/cpython/rev/6cc4579dca02

New changeset f05affb0bb2a by Benjamin Peterson in branch 'default':
merge 3.2 (#9284)
http://hg.python.org/cpython/rev/f05affb0bb2a
History
Date User Action Args
2022-04-11 14:57:03adminsetgithub: 53530
2011-06-11 20:56:16python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg138183

resolution: fixed
stage: test needed -> resolved
2011-06-09 16:31:05djcsetfiles: - issue9284.diff
2011-06-09 16:30:51djcsetfiles: + issue9284.diff

messages: + msg138009
2011-06-09 15:40:51benjamin.petersonsetmessages: + msg137992
2011-06-09 15:38:56djcsetmessages: + msg137991
2011-06-09 14:52:53benjamin.petersonsetmessages: + msg137980
2011-06-09 07:38:08djcsetnosy: + benjamin.peterson
messages: + msg137940
2011-06-07 09:49:16djcsetkeywords: + patch, needs review
files: + issue9284.diff
messages: + msg137797
2010-07-17 17:00:47r.david.murraysetmessages: + msg110584
2010-07-17 16:37:37djcsetmessages: + msg110581
2010-07-17 16:32:46r.david.murraysetstage: needs patch -> test needed
messages: + msg110579
versions: + Python 3.1, Python 3.2
2010-07-17 14:24:45djcsetmessages: + msg110557
2010-07-17 13:43:43pitrousetpriority: critical -> normal

messages: + msg110553
2010-07-17 13:19:31pitrousetpriority: normal -> critical
2010-07-17 13:16:55djccreate