msg110551 - (view) |
Author: Dirkjan Ochtman (djc) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
Date: 2010-07-17 17:00 |
Oh, right, I remember that now. Thanks. |
|
|
msg137797 - (view) |
Author: Dirkjan Ochtman (djc) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
Date: 2011-06-09 16:30 |
Here's a fresh patch. |
|
|
msg138183 - (view) |
Author: Roundup Robot (python-dev)  |
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 |
|
|