Message5989

Author denis.rangel
Recipients denis.rangel
Date 2010-08-18.11:21:25
SpamBayes Score 2.79776e-14
Marked as misclassified No
Message-id <1282130489.65.0.951805557558.issue1646@psf.upfronthosting.co.za>
In-reply-to
Content
When we took the class he should get a proxy object of the python (PyObject), no proxy. When the proxy so we can not turn the proxy object in python. Below is the problem and the solution simple.

Problem:

///////////////////////////// app.java
package test.test;

import java.io.FileReader;
import java.io.Reader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class App {
	public static void main(String[] args) throws Exception {
		// cria a engine para gerenciar os scripts
		ScriptEngineManager factory = new ScriptEngineManager();
		ScriptEngine python = factory.getEngineByName("jython");
		
		final Reader test = new FileReader(App.class.getResource("test.py").getFile());
		python.eval(test);
		Object pyobject = python.eval("Test()");
		
		Test proxy= (Test) ((Invocable)python).getInterface(pyobject, Test.class);
		
		proxy.test(proxy);
		proxy.test(pyobject);
	}
}

################## test.py
class Test:
    def test(self, object):
        print object.__class__.__name__

//////////////// test.java

package test.test;

public interface Test {
	void test(Object object);
}

Output:

$Proxy8
Test

Solution:

--- src/org/python/jsr223/PyScriptEngine.java	(revisão 7093)
+++ src/org/python/jsr223/PyScriptEngine.java	(cópia de trabalho)
@@ -93,7 +93,9 @@
             NoSuchMethodException {
         try {
             interp.setLocals(new PyScriptEngineScope(this, context));
-            if (!(thiz instanceof PyObject)) {
+            if (thiz instanceof Proxy) {
+            	thiz = ((PythonObjectHolderProxy)thiz).__topython__();
+            } else if (!(thiz instanceof PyObject)) {
                 thiz = Py.java2py(thiz);
             }
             PyObject method = ((PyObject) thiz).__findattr__(name);

@@ -136,16 +138,34 @@
         @SuppressWarnings("unchecked")
         T proxy = (T) Proxy.newProxyInstance(
             clazz.getClassLoader(),
-            new Class[] { clazz },
+            new Class[] { clazz, PythonObjectHolderProxy.class },
             new InvocationHandler() {
                 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                     try {
                         interp.setLocals(new PyScriptEngineScope(PyScriptEngine.this, context));
-                        PyObject pyMethod = thiz.__findattr__(method.getName());
+
+                        String methodName = method.getName();
+                        int length = args == null ? 0 : args.length;
+                        // FIXME: wtf is this? Why would these use the class?
+                        if (methodName == "toString" && length == 0) {
+                            return proxy.getClass().getName();
+                        } else if (methodName == "hashCode" && length == 0) {
+                            return Integer.valueOf(proxy.getClass().hashCode());
+                        } else if (methodName == "__topython__" && length == 0) {
+                            return thiz;
+                        }
+
+                        PyObject pyMethod = thiz.__findattr__(methodName);
                         if (pyMethod == null)
-                            throw new NoSuchMethodException(method.getName());
-                        PyObject result = pyMethod.__call__(Py.javas2pys(args));
-                        return result.__tojava__(Object.class);
+                            throw new NoSuchMethodException(methodName);
+
+                        PyObject result;
+                        if(args != null) {
+                            result = pyMethod.__call__(Py.javas2pys(args));
+                        } else {
+                            result = pyMethod.__call__();
+                        }
+                        return result.__tojava__(method.getReturnType());
                     } catch (PyException pye) {
                         throw scriptException(pye);
                     }
History
Date User Action Args
2010-08-18 11:21:30denis.rangelsetrecipients: + denis.rangel
2010-08-18 11:21:29denis.rangelsetmessageid: <1282130489.65.0.951805557558.issue1646@psf.upfronthosting.co.za>
2010-08-18 11:21:28denis.rangellinkissue1646 messages
2010-08-18 11:21:26denis.rangelcreate