Category Archives: Python

jyn: Simple Native Code Access in Jython

I had been wanting to mess around with native code access in Java for awhile. I’ve also been delving fairly deep into Jython, mostly out of necessity for RingMUD. So, I decided to combine these two ideas, and started work on a simple library similar to ctypes. Jython does not have a working ctypes implementation yet, but it is coming along. This was mostly for my own experimentation to learn a bit more Python as well as experiment with JNA (Java Native Access).

JNA is the much simpler alternative to JNI. In normal Java, you define an interface that corresponds to the function prototypes of the native library you want to access. You then create a new “instance” of the library by calling JNA’s special gateway method. From that point on, you can call the defined functions of the library.

The nice thing about Python is that it is a dynamic language. By overriding __getattr__ and friends, and using JNA’s reflection API, it becomes possible to have Jython lazily load the function references and call them in a much more intuitive way. The name of this simple library is jyn (No, no alcohol was involved in the creation of this library).

An example:

import jyn
lib = jyn.libc() #Load libc for Windows or Unix
printf = lib.printf
printf("Hello world! I am printing from Jython!\n")

jyn is not really suitable for production environments yet. The most glaring problem at the moment is that there is no argument checking. This means that passing wrong arguments to a native function will usually crash the JVM. I am definitely open to more experienced Python developers willing to improve the library.

jyn is intended mainly as a demonstration of what is possible with Java and Jython. The Java platform brings with it a large selection of powerful libraries, and Jython brings the dynamic power of Python. Combined, they are an unbeatable team.

Grab jyn from GitHub.

Mutliple Events with Jython Event Properties


At first glance, Jython event properties seem magical–almost too magical. They allow you to bypass the massive Java boilerplate required to set up event listeners. What is normally something along these lines:

public class Test extends JFrame {
    private JButton button;

    public Test() {
         super("A Frame");
         setSize(200, 200);
         setupEvents();
         setVisible(true);
    }

    public void setupEvents() {
          button = new JButton("Click me");
          button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                      System.out.println("Button pressed");
                }
          });

         this.add(button);
    }
}

Instead becomes something like this, when using Jython:

class Test(JFrame):
    def __init__(self):
        JFrame.__init__(self, "A Frame")
        button = JButton("Hello", actionPerformed=self.buttonPress)

        self.add(button)
        self.setSize(200, 200)
        self.visible = true

    def buttonPress(self, event):
        print "Button pressed"

if __name__=="__main__":
    Test()

The Python version of this is far briefer and much more compact than its Java counterpart. That’s all well and good, you say, but what if you want to add multiple event handlers? At first glance, it does not look like Jython’s event properties would allow for such a thing. Luckily, it is possible. However, it is not documented. Indeed, documentation of Jython’s Java API and specialized features is a bit … lacking.

This undocumented feature was discovered after some chatting in the Jython IRC channel, and a bit of experimentation. It turns out that the event properties are actually an object type called CompoundCallable. These objects, as their name implies, store a set of functions that are executed one after another in sequence when called. Observe:

class Test(JFrame):
    def __init__(self):
        JFrame.__init__(self, "Hello Jython")
        button = JButton("Hello")
        button.actionPerformed = self.hello
        button.actionPerformed.append(self.otherEvent)
        self.add(button)

        self.setSize(200, 200)
        self.visible = true

    def hello(self, event):
        print "Hello, world!"

    def otherEvent(self, event):
        print "this is the other event"

if __name__=="__main__":
      Test()

When the button is clicked, both the “hello” and “otherEvent” functions will execute. Any Jython event property (including ones you create yourself) has two useful methods: append() and clear(). They do what their names say. The append method takes a function as a parameter, and adds it to the list of functions to be executed. The clear() methods clears all the functions out of the property.

Unfortunately, there do not appear to be ways to iterate over the functions contained within the CompoundCallable, or access specific event handlers directly. In a similar vein, you cannot delete individual event handlers out of the CompoundCallable. Your only option is the clear() method.

The source code for the PyCompoundCallable object is very simple. It doesn’t look like it would be too hard to add some more methods for getting and deleting specific event handlers. (Scroll way down to the bottom of that page to view the code.)