Message11322

Author stefan.richthofer
Recipients stefan.richthofer
Date 2017-04-25.13:48:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1493128116.68.0.6416870242.issue2580@psf.upfronthosting.co.za>
In-reply-to
Content
This issue has a somewhat common cause with #2579.
I stumbled over this while working on https://github.com/Stewori/pytypes where I need to find the function for a given code object, so first step is to identify the module. inspect.getmodule takes an optional _filename-parameter that can enhance performance.

Consider the following code in a file - let's call it test_cp.py - located not in . but let's say in ./folder. We have jython-standalone.jar located in .

import inspect, sys, os

def func1(a, b, c):
    pass

# some debug output:
print __name__
print __file__
print func1.__code__
print func1.__code__.co_filename

# the actual issue
print inspect.getmodule(func1.__code__) # works reliably
print inspect.getmodule(func1.__code__, func1.__code__.co_filename) # can break

If I call it using

java -cp .:jython-standalone.jar:folder org.python.util.jython -c "import test_cp"

the output is

__pyclasspath__/test_cp$py.class
<code object func1 at 0x2, file "__pyclasspath__/test_cp$py.class", line 3>
__pyclasspath__/test_cp$py.class
<module 'test_cp' from '__pyclasspath__/test_cp$py.class'>
None

As you can see, inspect.getmodule returns None in this case, if the _filename parameter is given. Note that everything works fine, if I directly call it using

java -cp .:jython-standalone.jar org.python.util.jython folder/test_cp.py
or
java -jar jython-standalone.jar folder/test_cp.py

In the problematic case, one can manually fix the _filename parameter via

cfname = func1.__code__.co_filename.replace('__pyclasspath__', os.path.realpath('')+os.sep+'__pyclasspath__')
cfname = cfname.replace('$py.class', '.py')

Then calling

print inspect.getmodule(func1.__code__, cfname)

will work as expected. However IMO Jython should do this adjustment under the hood. In a similar way, __pyclasspath__ causes #2579. I will reason about a common solution.

Also, I think Jython should feature an easily accessible utility method to resolve a path or filename containing __pyclasspath__ into an actual filename usable with open(), exists() etc. or suitable to pass it to other (non-Jython-aware) scripts or programs.
History
Date User Action Args
2017-04-25 13:48:36stefan.richthofersetrecipients: + stefan.richthofer
2017-04-25 13:48:36stefan.richthofersetmessageid: <1493128116.68.0.6416870242.issue2580@psf.upfronthosting.co.za>
2017-04-25 13:48:36stefan.richthoferlinkissue2580 messages
2017-04-25 13:48:35stefan.richthofercreate