Message11199
jython does support the inspect module in a normal .py file:
$ cat test.py
import inspect
def function():
frame = inspect.currentframe()
print inspect.getframeinfo(frame)
function()
$java -jar dist/jython-standalone.jar -c 'import test'
Traceback(filename='test.py', lineno=5, function='function', code_context=[' print inspect.getframeinfo(frame)\n'], index=0)
However if the python module is compiled into bytecode this does not work any more
$ python -m py_compile test.py
$ rm test.py
$ java -jar dist/jython-standalone.jar -c 'import pycimport; import test'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/fwyzard/src/jython/src/dist/Lib/pycimport.py", line 72, in load_module
return __makeModule( fullname, code, filename )
File "/home/fwyzard/src/jython/src/dist/Lib/pycimport.py", line 25, in __makeModule
exec code in module.__dict__
File "test.py", line 7, in <module>
File "test.py", line 7, in <module>
File "test.py", line 5, in function
File "test.py", line 5, in function
File "/home/fwyzard/src/jython/src/dist/Lib/inspect.py", line 1015, in getframeinfo
filename = getsourcefile(frame) or getfile(frame)
File "/home/fwyzard/src/jython/src/dist/Lib/inspect.py", line 448, in getsourcefile
filename = getfile(object)
File "/home/fwyzard/src/jython/src/dist/Lib/inspect.py", line 423, in getfile
raise TypeError('{!r} is not a module, class, method, '
TypeError: <code object function at 0x2, file "test.py", line 3> is not a module, class, method, function, traceback, frame, or code object
while it still works as expected with the C python interpreter:
$ python2.7 -c 'import test'
Traceback(filename='test.py', lineno=5, function='function', code_context=None, index=None)
The problem seems to be that code objects from a bytecode file are not of type types.CodeType, but of type org.python.core.PyBaseCode (which is its base class).
A possible fix is to change Lib/types.py from
CodeType = type(_f.func_code)
to
CodeType = org.python.core.PyBaseCode
as in the attached patch.
An other possibility would be to change only Lib/inspect.py to explicitly check for org.python.core.PyBaseCode in addition to types.CodeType. The change to Lib/types.py seems more general, though. |
|
Date |
User |
Action |
Args |
2017-03-07 09:51:26 | fwyzard | set | messageid: <1488880286.86.0.797886125984.issue2566@psf.upfronthosting.co.za> |
2017-03-07 09:51:26 | fwyzard | set | recipients:
+ fwyzard |
2017-03-07 09:51:26 | fwyzard | link | issue2566 messages |
2017-03-07 09:51:26 | fwyzard | create | |
|