File: InterpreterTest.java

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (66 lines) | stat: -rw-r--r-- 2,484 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.python.util;

import java.util.concurrent.CountDownLatch;

import junit.framework.TestCase;

import org.python.core.Py;
import org.python.core.PyDictionary;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyUnicode;

public class InterpreterTest extends TestCase {

    /**
     * Motivated by a NPE reported on http://bugs.jython.org/issue1174.
     */
    public void testBasicEval() throws Exception {
        PyDictionary test = new PyDictionary();
        test.__setitem__(new PyUnicode("one"), new PyUnicode("two"));
        PythonInterpreter.initialize(System.getProperties(), null, new String[] {});
        PythonInterpreter interp = new PythonInterpreter();
        PyObject pyo = interp.eval("{u'one': u'two'}");
        assertEquals(test, pyo);
    }

    public void testMultipleThreads() {
        final CountDownLatch doneSignal = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            new Thread() {
                @Override
                public void run() {
                    PythonInterpreter interp = new PythonInterpreter();
                    interp.exec("import sys");
                    interp.set("a", new PyInteger(41));
                    int set = Py.tojava(interp.get("a"), Integer.class);
                    assertEquals(41, set);
                    interp.exec("x = 'hello ' + 'goodbye'");
                    assertEquals("hello goodbye", Py.tojava(interp.get("x"), String.class));
                    doneSignal.countDown();
                }
            }.start();
        }
        try {
            doneSignal.await();
        } catch (InterruptedException e) {
            System.err.println("Interpreters in multiple threads test interrupted, bailing");
        }
    }

    public void testCallInstancesFromJava() {
        PythonInterpreter interp = new PythonInterpreter();
        interp.exec("class Blah(object):\n" +
                    "    def __init__(self, val):\n" +
                    "        self.val = val\n" +
                    "    def incval(self):\n" +
                    "        self.val += 1\n" +
                    "        return self.val");
        PyObject blahClass = interp.get("Blah");
        int base = 42;
        PyObject blahInstance = blahClass.__call__(new PyInteger(base));
        for (int i = 0; i < 4; i++) {
            assertEquals(++base, blahInstance.invoke("incval").__tojava__(Integer.class));
        }
    }
}