Jython class exception exception with PyObject

I try to create and cast an object in Jython and I get the following error:

Exception in thread "MainThread" java.lang.ClassCastException: org.python.core.PySingleton cannot be cast to resources.ixia.IxNetType at resources.ixia.IxNetFactory.create(IxNetFactory.java:34) at resources.ixia.IxiaTest.run(IxiaTest.java:34) at resources.ixia.IxiaTest.<init>(IxiaTest.java:14) at resources.ixia.IxiaTest.main(IxiaTest.java:42) 

Here is the code:

 import org.python.core.PyObject; import org.python.util.PythonInterpreter; public class IxNetFactory { private PyObject ixNetClass; private PythonInterpreter interpreter; public IxNetFactory(String script_dir) { script_dir=script_dir.replace("\\", "/"); interpreter = new PythonInterpreter(); interpreter.exec("import sys"); interpreter.exec("sys.path.append('"+script_dir+"')"); interpreter.exec("import time"); interpreter.exec("import os"); interpreter.exec("from ixnetworks import IxNet"); //interpreter.exec("from utils import sm"); //interpreter.exec("from utils import cpf"); ixNetClass = interpreter.get("IxNet"); } /* * Create an IxNet object * * Usage: ixNet.create(); */ public IxNetType create() { PyObject ixNetObject = ixNetClass.__call__(); return (IxNetType)ixNetObject.__tojava__(IxNetType.class); } public void close() { interpreter.close(); } } 

In life, I cannot understand what I am doing wrong. Of all the things I read, I seem to be doing it right, but I can't get it to work.

If anyone who has experience with Jython can tell me what I'm doing wrong, that would be very appreciated.

+5
source share
1 answer

This is a very late answer, but for other people who may run into the same problem: I just had what I consider the same error and fixed it. I assume that declaring your Python class is not inheriting from your interface.

For example, ixnet.py:

 import IxNetType class IxNet(IxNetType): ... 

That's what you need. Instead, you probably just declared IxNet as:

 class IxNet: ... 

which will result in an error: "java.lang.ClassCastException: org.python.core.PySingleton cannot be passed to .ixia.IxNetType resources"

0
source

Source: https://habr.com/ru/post/1243341/


All Articles