[Python-ideas] stdlib with its own release cycle ?

geremy condra debatem1 at gmail.com
Mon Oct 26 18:51:33 CET 2009


On Mon, Oct 26, 2009 at 1:29 PM, Guido van Rossum <guido at python.org> wrote:
> On Mon, Oct 26, 2009 at 10:27 AM, geremy condra <debatem1 at gmail.com> wrote:
>> No need. Java has the Java Native Interface, which is supported in the
>> Android Native Development Kit.
>
> If it's that easy why aren't more people developing Android apps using Jython?
>
> --
> --Guido van Rossum

Because Jython doesn't run on dalvik- dalvik bytecode and normal java
bytecode are quite different, and apparently porting Jython to dalvik
would be quite the task in its own right.

So far, there have been two solutions to this: the first is, as you mention,
ASE, which works by exposing a small handful of convenience functions
to scripting languages via JSON. As you point out, that's quite slow and
limiting.

Secondly, there's the approach that I took- simply wrap the most of
the JNI in the CPython interface. It requires more work to get multiple
language support- which was their goal in pushing ASE- but it's quite
doable, and frankly if I knew enough about the JNI to make it happen
somebody else has almost certainly had better luck.

It took me a while to get as far as I did on the problem at least
partially because of my unfamiliarity with both the JNI and with the
CPython C API, but here's a (working) example of how you use it:

Here’s the Java class:

public class Sample
{
      private boolean booleanMethod(boolean bool) {
           return !bool;
      }
      public int intMethod(int n) {
            return n+n;
      }
 }

And here’s how you map it to Python:

    from java import Java

    @Java
    class Sample:
            '''Sample Java class implementation'''
            def __init__(self):
                    self.java_init()

            def booleanMethod(truth: "Z") -> "Z":
                    '''Returns the inverse of what was passed in.'''
                    return self.java_booleanMethod(truth)

            def intMethod(x: "I") -> "I":
                    '''Returns double the passed-in value'''
                    return self.java_intMethod(x)


And from then on you can use it just like a Python class,
because it is. There are limitations- you can't pass arrays
filled with arbitrary objects ATM- but if there's any interest
in the project I could fill those gaps in the period of a few
weeks.

I can't make any quantitative evaluations of how quickly it
runs- my main goal was not speed but getting full access-
but I can say that feel-wise it was possible for a
nontechnical friend to repeatedly identify whether a given
app was using ASE or Javalin based solely on the speed
at which they responded.

Geremy Condra



More information about the Python-ideas mailing list