5

I am trying out to invoke python function passing it HashMap from within java (groovy). The python function squares every value in input map and returns Dictionary of squares with same keys.

JythonTest.groovy

import org.python.util.PythonInterpreter
import org.python.core.*;

class JythonTest 
{
    static main(def args) 
    {
        PythonInterpreter pi;

        pi = new PythonInterpreter()
        pi.exec("from py1 import square3")
        PyFunction pf = (PyFunction)pi.get("square3")

        def map = ["value1":1,"value2":2,"value3":3]     //groovy map   
        PyDictionary pyDict = new PyDictionary(map)
        pf.__call__(pyDict)         //this is line 16 at which according to stack trace the exception is originated (as python function call occurs here)
    }
}

py1.py

def square3(map):
    squareMap = {}
    for k,v in map.items():        #this is line 3 where according to stack trace exception is occurring
        squareMap[k] = v*v
    return squareMap

But I am getting following error:

Exception in thread "main" Traceback (most recent call last):
  File "__pyclasspath__/py1.py", line 3, in square3
java.lang.ClassCastException: java.lang.String cannot be cast to org.python.core.PyObject

    at org.python.core.PyDictionary.dict_items(PyDictionary.java:659)

    at org.python.core.PyDictionary$dict_items_exposer.__call__(Unknown Source)

    at org.python.core.PyObject.__call__(PyObject.java:449)

    at py1$py.square3$1(__pyclasspath__/py1.py:5)

    at py1$py.call_function(__pyclasspath__/py1.py)

    at org.python.core.PyTableCode.call(PyTableCode.java:167)

    at org.python.core.PyBaseCode.call(PyBaseCode.java:138)

    at org.python.core.PyFunction.__call__(PyFunction.java:413)

    at org.python.core.PyFunction.__call__(PyFunction.java:408)

    at org.python.core.PyFunction$__call__.call(Unknown Source)

    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)

    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)

    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122)

    at JythonTest.main(JythonTest.groovy:16)


java.lang.ClassCastException: java.lang.ClassCastException: java.lang.String cannot be cast to org.python.core.PyObject

In exception stack trace it says the exception is in python file line 3. But when I invoke the python function from python itself, say by attaching below line at the end of py1.py:

a = square3({"val1":1,"val2":2})
print(a) 

I get following output:

{'val2': 4, 'val1': 1}

So why invoking python function from java fails? I am not getting whats going wrong here. Why call pf.__call__(pyDict) throwing such exception?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189
  • those line numbers help in locating code lines where stack trace says exception is occurred. Please think of undoing edit. – Mahesha999 Sep 08 '16 at 14:36
  • I'm sure people can count 3 lines and find the call to the python interpreter. Anyone copying the code would have to remove those line numbers in order to tests with it, which is annoying. – Bob Brinks Sep 08 '16 at 14:38
  • I understand your concern. Ok let me do some changes – Mahesha999 Sep 08 '16 at 14:42

0 Answers0