Issue2566

classification
Title: inspect does not recognize code objects from bytecode files
Type: crash Severity: normal
Components: Library Versions: Jython 2.7, Jython 2.5
Milestone:
process
Status: open Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: fwyzard, jeff.allen, stefan.richthofer
Priority: normal Keywords: patch

Created on 2017-03-07.09:43:41 by fwyzard, last changed 2018-03-23.08:38:46 by jeff.allen.

Files
File name Uploaded Description Edit Remove
2566.patch fwyzard, 2017-03-07.09:51:25 patch for Lib/types.py
Messages
msg11199 (view) Author: Andrea Bocci (fwyzard) Date: 2017-03-07.09:51:25
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.
msg11200 (view) Author: Andrea Bocci (fwyzard) Date: 2017-03-07.09:53:02
After applying the patch and rebuilding jython, this does work

$ java -jar dist/jython-standalone.jar -c 'import pycimport; import test'
Traceback(filename='test.py', lineno=5, function='function', code_context=None, index=None)
msg11201 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-03-07.16:33:26
Maybe a cleaner approach would be to add
@ExposedType(name="code")
to PyBaseCode (or better even to PyCode..?). However that would require to also add @ExposedField and @ExposedMethod tags to everything that is supposed to be visible on Python side, but I think that's how PyCode subclasses should look anyway. (Was actually surprised that PyCode-stuff doesn't already support the expose mechanism).

In case you try this path, don't forget to add the corresponding classes to CoreExposed.includes file. Otherwise ant won't expose them.
msg11848 (view) Author: Jeff Allen (jeff.allen) Date: 2018-03-23.08:38:46
Possibly an easy fix, based on the offered patch, but noting also Stefan's alternative ideas.

However, I suggest we don't rate this as a blocker for 2.7.2.
History
Date User Action Args
2018-03-23 08:38:46jeff.allensetpriority: normal
resolution: accepted
messages: + msg11848
nosy: + jeff.allen
milestone: Jython 2.7.1 ->
2017-03-07 16:33:26stefan.richthofersetnosy: + stefan.richthofer
messages: + msg11201
2017-03-07 09:53:48fwyzardsetmilestone: Jython 2.7.1
2017-03-07 09:53:02fwyzardsetmessages: + msg11200
2017-03-07 09:51:40fwyzardsettype: crash
2017-03-07 09:51:26fwyzardsetfiles: + 2566.patch
keywords: + patch
messages: + msg11199
2017-03-07 09:43:42fwyzardcreate