Skip to content
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

Use of wrapper classes in ipython causes Java Exceptions #72

Closed
jbcoe opened this issue Feb 19, 2016 · 4 comments
Closed

Use of wrapper classes in ipython causes Java Exceptions #72

jbcoe opened this issue Feb 19, 2016 · 4 comments

Comments

@jbcoe
Copy link
Contributor

jbcoe commented Feb 19, 2016

Running the following code in ipython leads to a java exception

import javabridge
javabridge.start_vm(run_headless=True)
a = javabridge.JWrapper(javabridge.make_instance("java/util/ArrayList","()V"))
a

I would hope that a on its own would call the repr method and show the array but instead it give a java exception:

Exception in thread "Thread-1" java.lang.NoSuchFieldException: _ipython_display_
        at java.lang.Class.getField(Class.java:1690)
Out[4]: Instance of java.util.ArrayList: []

A user-defined class without ipython_display defined works as expected. I'm not sure where the problem lies but the ability to wrap Java classes in python in an interactive session is awesome and it would be worth getting it working nicely.

I've posted a parallel issue on ipython as I'm not sure where changes need making: ipython/ipython#9253

@LeeKamentsky
Copy link
Owner

Cool idea. I am betting that the wrapper class should be translating the Java exception in this case into an AttributeError exception or similar and I'd bet that IPython is expecting to catch that and then do the repr. Also, repr should be calling a.toString() - right? A pull request for the change would be much appreciated.

BTW. Maybe you want to do this instead of using make_instance 😉 :

a = javabridge.JClassWrapper("java.util.ArrayList")()

@takluyver
Copy link

Spot on - IPython is checking for a method that may exist, and is ready to catch AttributeError and fall back to other methods if it doesn't.

@LeeKamentsky
Copy link
Owner

Oh, regarding the __repr__ stuff, __str__ calls o.toString(), __repr__ does a little more: it tells you the class and then appends the output of o.toString(). Try this:

> a = javabridge.JClassWrapper('java.util.ArrayList')()
> a.add("Foo")
> a
Instance of java.util.ArrayList: [foo]

I think I'd like to keep the current repr behavior.

@takluyver
Copy link

I think it's a good idea to keep the extra info in the repr; without it you could easily think that was a native Python list.

@jbcoe jbcoe closed this as completed Feb 19, 2016
LeeKamentsky pushed a commit that referenced this issue Feb 22, 2016
Fixes #72 prevent call to getField for random attributes
jbcoe pushed a commit to jbcoe/python-javabridge that referenced this issue Jun 22, 2016
jbcoe pushed a commit to jbcoe/python-javabridge that referenced this issue Jun 23, 2016
jbcoe pushed a commit to jbcoe/python-javabridge that referenced this issue Jun 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants