Issue 9284: inspect.findsource() cannot find source for doctest code (original) (raw)

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 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 = '', '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:03 admin set github: 53530
2011-06-11 20:56:16 python-dev set status: open -> closednosy: + python-devmessages: + resolution: fixedstage: test needed -> resolved
2011-06-09 16:31:05 djc set files: - issue9284.diff
2011-06-09 16:30:51 djc set files: + issue9284.diffmessages: +
2011-06-09 15:40:51 benjamin.peterson set messages: +
2011-06-09 15:38:56 djc set messages: +
2011-06-09 14:52:53 benjamin.peterson set messages: +
2011-06-09 07:38:08 djc set nosy: + benjamin.petersonmessages: +
2011-06-07 09:49:16 djc set keywords: + patch, needs reviewfiles: + issue9284.diffmessages: +
2010-07-17 17:00:47 r.david.murray set messages: +
2010-07-17 16:37:37 djc set messages: +
2010-07-17 16:32:46 r.david.murray set stage: needs patch -> test neededmessages: + versions: + Python 3.1, Python 3.2
2010-07-17 14:24:45 djc set messages: +
2010-07-17 13:43:43 pitrou set priority: critical -> normalmessages: +
2010-07-17 13:19:31 pitrou set priority: normal -> critical
2010-07-17 13:16:55 djc create