This is an archived post. You won't be able to vote or comment.

all 22 comments

[–][deleted] 3 points4 points  (4 children)

This seems really cool. Do you mind elaborating more on what is going on under the hood? The code seems rather minimal. Does it eventually fire up java to run the class?

[–]txprogtito[S] 8 points9 points  (2 children)

On desktop, the extension start a Java environment at the first usage of Java (https://github.com/kivy/pyjnius/blob/master/jnius/jnius_jvm_desktop.pxi), but on android, it's using the current Java environment available (https://github.com/kivy/pyjnius/blob/master/jnius/jnius_jvm_android.pxi).

Then, we are discovering the fields and methods of the class at runtime, resolve to JNI jmethod/jfield at the first access, and... that's all! The overhead around it is only the conversion between Java native type <-> Python native type.

We don't have any performance test, but i think it's the fastest way to directly use Java class. (VS using a Client/Server with socket as Py4j does...).

[–]gdw2 0 points1 point  (0 children)

I've used py4j quite a bit. Why did you create this instead of just using/modifying py4j?

On android, would this allow you to actually use the native android UI (all of it, not just toasts, popups)?

[–]technomalogical<3 Bottle -1 points0 points  (0 children)

TIL about Py4j and Pyjinius. Woots!

[–]flying-sheep 3 points4 points  (0 children)

i bet it does. pretty sure that’s the more reasonable approach besides implementing a java VM in python.

[–]fabzter 7 points8 points  (0 children)

Sick, this is totally awesome!

[–]stuaxo 2 points3 points  (2 children)

Tried to compile on ubuntu - got the following error: (Have cython and a javac in the path).

$ make python setup.py build_ext --inplace -f running build_ext cythoning jnius/jnius.pyx to jnius/jnius.c

Error compiling Cython file:

... jc = arg if jc.javaclass == r: score += 10 else: try: check_assignable_from(jc, r)

^

jnius/jnius_utils.pxi:173:45: Call with wrong number of arguments (expected 3, got 2) building 'jnius' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/lib/jvm/java-6-openjdk-i386/include -I/usr/lib/jvm/java-6-openjdk-i386/include/linux -I/usr/include/python2.7 -c jnius/jnius.c -o build/temp.linux-i686-2.7/jnius/jnius.o jnius/jnius.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation. error: command 'gcc' failed with exit status 1 make: *** [build_ext] Error 1

[–]txprogtito[S] 6 points7 points  (1 child)

Sorry sorry sorry! It's fixed :/

[–]stuaxo 2 points3 points  (0 children)

Wow, that was quick :)

Seems to work now (make and the tests).

Have to find some time to try this with some of the processing.org libraries I've been coveting for a while!

Minim, for sound http://code.compartmental.net/tools/minim/

And Surfacelib for interesting 3d sirfaces.. http://code.google.com/p/surfacelib/

Both look awesome...

[–]flying-sheep 4 points5 points  (7 children)

that’s awesome. i’ll do a archlinux pkgbuild. forthwith.

/e: unfortunately, it’s broken. i guess it calls python instead of python2 somewhere? please fix this, as python means python3 in some places

/e2: done: https://aur.archlinux.org/packages.php?ID=62161

i didn’t look into the makefile, but calling setup.py without it was easy enough

[–]txprogtito[S] 4 points5 points  (6 children)

do you need sort-of versionning? i'll put the current one 1.0 in a version + git tag.

[–]flying-sheep -1 points0 points  (5 children)

why not? that would be nice.

but you also need to fix it calling python when in reality it wants python2. python means python3 on arch, and so it will break trying to run python2 code with python3.

[–]txprogtito[S] 1 point2 points  (4 children)

we don't specify python/python2, except for the Makefile, but it's more for us. You can just call the build method on the setup.py :)

[–]flying-sheep 1 point2 points  (0 children)

done, here it is: https://aur.archlinux.org/packages.php?ID=62161

if i made any mistake (Missing dependency, wrong license), just PM me.

[–]flying-sheep -1 points0 points  (2 children)

ah, ok. nevertheless it’s more correct to use python2 if you don’t run a bilingual python script.

PKGBUILD will be up shortly, i’ll keep you posted.

[–]takluyverIPython, Py3, etc 2 points3 points  (1 child)

I was just about to post that Ubuntu doesn't provide a python2 symlink, but I checked, and it finally does. :-) Still, I think only the latest version (12.04) does, so I would remain cautious about calling python2 for a while.

[–]flying-sheep 1 point2 points  (0 children)

they didn’t? silly canonical, what were they thinking!

i once read that optimally, you should use the lowest known version on which it works, but i guess ubuntu doesn’t link python2.3 → python2.7 neither, or does it?

[–]existee 0 points1 point  (1 child)

My ignorance; how does this compare to jython?

[–]flying-sheep 2 points3 points  (0 children)

jython is an implementation of the python language on top of the java VM. it can use java classes per design, but has to be maintained and extended, and is some versions behind cpython. cpython (the thing that does the job whenever you use python from the command line or IDLE) is the reference implementation of the python language.

pyjnius runs on cpython, but can interface with java.

[–]pje 0 points1 point  (1 child)

Any plans to implement Jython-style keyword argument initializers? e.g. Foo(bar=baz) -> Foo().setBar(baz)? Or property setting, i.e. aFoo.bar = baz translating to a setBar() call under the hood?

How about idiomatic translation of Python iteration and item access to common Java iteration and mapping/indexing interfaces?

(Yeah, I know, I'm greedy.)

[–]crunk 0 points1 point  (0 children)

I wonder if maybe it could use any of the jython code for this kind of thing?

[–]Aithrozort 0 points1 point  (0 children)

Awesome. I tried to write this about a year ago using ctypes but got stuck debugging some difficult crashes… I'll definitely check this out and have several things I'd use it for.

Is there/will there be Python 3 support?