ninia / jep

Embed Python in Java
Other
1.3k stars 147 forks source link

Python isinstance does not work with imported Java classes #438

Closed mike-hunhoff closed 1 year ago

mike-hunhoff commented 1 year ago

e.g.

>>> from ghidra.program.database.data import *
...
>>> type(dt)
<class 'ghidra.program.database.data.FunctionDefinitionDB'>
>>> print(FunctionDefinitionDB)
class ghidra.program.database.data.FunctionDefinitionDB
>>> type(FunctionDefinitionDB)
<class 'jep.PyJClass'>
>>> isinstance(dt, FunctionDefinitionDB)
Traceback (most recent call last):
  File "/Users/pieceofsummer/.ghidra/.ghidra_10.1.5_PUBLIC/Extensions/Ghidrathon-1.0.0/data/python/jepeval.py", line 66, in jepeval
    more_input_needed = _jepeval(line)
  File "/Users/pieceofsummer/.ghidra/.ghidra_10.1.5_PUBLIC/Extensions/Ghidrathon-1.0.0/data/python/jepeval.py", line 49, in _jepeval
    exec(compile(line, "<string>", "single"), globals(), globals())
  File "<string>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

I see a workaround based on jep code found here that requires users to pass __pytype__ e.g. FunctionDefinitionDB.__pytype__. Is there a way to use isinstance without a workaround?

See https://github.com/mandiant/Ghidrathon/issues/13 for more details.

bsteffensmeier commented 1 year ago

PEP-3119 makes it easy to override this behavior. I have submitted #440 with a proposed fix for this. Unfortunately since we just released 4.1 it will be awhile before this makes it into a release.

mike-hunhoff commented 1 year ago

440 looks great @bsteffensmeier . Would love to see this in a point release to help improve Jep's usability but understand you just made a release. In the mean time I think we can implement a workaround but we will keep our eyes open for the next Jep release, thank you!

ndjensen commented 1 year ago

Fixed in 4.1.1.