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 work on functions imported from a zipfile #48473
Comments
It'd be better if it did. |
doesn't it ? it works here |
Well anyway, some sample code: import zipfile
z = zipfile.ZipFile('aaa.zip', mode='w')
z.writestr('aa.py', 'def x(): print "hi there"\n\ndef y(): print "hi"')
z.close() and then: import sys
import inspect
sys.path.append('aaa.zip')
import aa
inspect.getsource(aa.x)
inspect.getsource(aa.y) Doesn't that work for you ? |
It seems to depend on working directory: exarkun@charm:/tmp$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/exarkun/foobar.zip')
>>> import foobar, inspect
>>> inspect.getsource(foobar.foo)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/inspect.py", line 629, in getsource
lines, lnum = getsourcelines(object)
File "/usr/lib/python2.5/inspect.py", line 618, in getsourcelines
lines, lnum = findsource(object)
File "/usr/lib/python2.5/inspect.py", line 468, in findsource
raise IOError('could not get source code')
IOError: could not get source code
>>> versus: exarkun@charm:~$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/exarkun/foobar.zip')
>>> import foobar, inspect
>>> inspect.getsource(foobar.foo)
'def foo():\n pass\n'
>>> |
Hmm, apparently inspect was made to work with zipped modules back in r45248 . I cannot reproduce the problem either. Jean-Paul, can you attach your foobar.zip? What else do you have in your |
In any case, it's clearly not a candidate for 2.5.3, due to the lack of |
Here is a transcript for a complete, self-contained, minimal reproduction: exarkun@charm:~$ cd /tmp
exarkun@charm:/tmp$ mkdir zipimportbugexample
exarkun@charm:/tmp$ cd zipimportbugexample/
exarkun@charm:/tmp/zipimportbugexample$ mkdir foobar
exarkun@charm:/tmp/zipimportbugexample$ echo "def foo(): pass" >
foobar/__init__.py
exarkun@charm:/tmp/zipimportbugexample$ zip foobar.zip foobar
adding: foobar/ (stored 0%)
exarkun@charm:/tmp/zipimportbugexample$ zip foobar.zip foobar/__init__.py
adding: foobar/__init__.py (stored 0%)
exarkun@charm:/tmp/zipimportbugexample$ rm -r foobar
exarkun@charm:/tmp/zipimportbugexample$ mkdir workingdirectory
exarkun@charm:/tmp/zipimportbugexample$ cd workingdirectory/
exarkun@charm:/tmp/zipimportbugexample/workingdirectory$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/tmp/zipimportbugexample/foobar.zip')
>>> import foobar, inspect
>>> inspect.getsource(foobar.foo)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/inspect.py", line 629, in getsource
lines, lnum = getsourcelines(object)
File "/usr/lib/python2.5/inspect.py", line 618, in getsourcelines
lines, lnum = findsource(object)
File "/usr/lib/python2.5/inspect.py", line 468, in findsource
raise IOError('could not get source code')
IOError: could not get source code
>>> |
Thanks, Jean-Paul, I can now reproduce your problem and I think I found I believe the file name check in linecache.updatecache is unnecessary --- Lib/linecache.py (revision 67040)
+++ Lib/linecache.py (working copy)
@@ -88,7 +88,7 @@
get_source = getattr(loader, 'get_source', None)
if name and get_source:
- if basename.startswith(name.split('.')[-1]+'.'):
+# if basename.startswith(name.split('.')[-1]+'.'):
try:
data = get_source(name)
except (ImportError, IOError): and Jean-Paul's foobar: $ PYTHONPATH=/tmp/foobar.zip ./python.exe -c "import inspect,foobar;
print inspect.getsource(foobar)"
def foo(): pass Martin, is there a chance for this issue and bpo-4197, bpo-4201 to make it |
After looking into this, I think Alexander is correct. There is no In this particular case, it was the differences between the way the two |
Fixed for 2.7 in r67750. Will be ported to 2.6, 3.0 and 3.1. |
Final revisions for fix: |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: