From: <cg...@us...> - 2008-01-02 00:18:17
|
Revision: 3944 http://jython.svn.sourceforge.net/jython/?rev=3944&view=rev Author: cgroves Date: 2008-01-01 16:18:13 -0800 (Tue, 01 Jan 2008) Log Message: ----------- Allow any primitive type to be returned by exposed methods Modified Paths: -------------- trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/expose/generate/MethodExposer.java trunk/jython/src/org/python/expose/generate/PyTypes.java trunk/jython/tests/java/org/python/expose/generate/ExposedTypeProcessorTest.java trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java trunk/jython/tests/java/org/python/expose/generate/SimpleExposed.java Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2008-01-01 23:56:13 UTC (rev 3943) +++ trunk/jython/src/org/python/core/Py.java 2008-01-02 00:18:13 UTC (rev 3944) @@ -538,6 +538,10 @@ return new PyLong(i); } + public static PyLong newLong(long l) { + return new PyLong(l); + } + public static PyComplex newImaginary(double v) { return new PyComplex(0, v); } @@ -1537,7 +1541,7 @@ return makeCharacter(o.charValue()); } - static final PyString makeCharacter(char c) { + public static final PyString makeCharacter(char c) { return makeCharacter(c, false); } static final PyString makeCharacter(char c, boolean explicitUnicode) { Modified: trunk/jython/src/org/python/expose/generate/MethodExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/MethodExposer.java 2008-01-01 23:56:13 UTC (rev 3943) +++ trunk/jython/src/org/python/expose/generate/MethodExposer.java 2008-01-02 00:18:13 UTC (rev 3944) @@ -163,8 +163,16 @@ callStatic(PY, "newString", PYSTR, STRING); } else if(returnType.equals(BOOLEAN)) { callStatic(PY, "newBoolean", PYBOOLEAN, BOOLEAN); - } else if(returnType.equals(INT)) { + } else if(returnType.equals(INT) || returnType.equals(BYTE) || returnType.equals(SHORT)) { callStatic(PY, "newInteger", PYINTEGER, INT); + } else if(returnType.equals(CHAR)) { + callStatic(PY, "makeCharacter", PYSTR, CHAR); + } else if(returnType.equals(Type.DOUBLE_TYPE)) { + callStatic(PY, "newFloat", PYFLOAT , Type.DOUBLE_TYPE); + } else if(returnType.equals(Type.FLOAT_TYPE)) { + callStatic(PY, "newFloat", PYFLOAT, Type.FLOAT_TYPE); + } else if(returnType.equals(Type.LONG_TYPE)) { + callStatic(PY, "newLong", PYLONG, Type.LONG_TYPE); } endMethod(ARETURN); } @@ -216,7 +224,7 @@ /** Throw NotImplemented if a binary method returned null. */ private void checkBinaryResult() { - // If this is a binary method, + // If this is a binary method, mv.visitInsn(DUP); Label regularReturn = new Label(); mv.visitJumpInsn(IFNONNULL, regularReturn); @@ -235,8 +243,7 @@ instantiate(STRING_BUILDER, new Instantiator(STRING) { public void pushArgs() { - mv.visitLdcInsn(prefix + ".__cmp__(x,y) requires y to be '" + prefix - + "', not a '"); + mv.visitLdcInsn(prefix + ".__cmp__(x,y) requires y to be '" + prefix + "', not a '"); } }); mv.visitVarInsn(ALOAD, 1); Modified: trunk/jython/src/org/python/expose/generate/PyTypes.java =================================================================== --- trunk/jython/src/org/python/expose/generate/PyTypes.java 2008-01-01 23:56:13 UTC (rev 3943) +++ trunk/jython/src/org/python/expose/generate/PyTypes.java 2008-01-02 00:18:13 UTC (rev 3944) @@ -8,7 +8,9 @@ import org.python.core.PyBuiltinMethodNarrow; import org.python.core.PyDataDescr; import org.python.core.PyException; +import org.python.core.PyFloat; import org.python.core.PyInteger; +import org.python.core.PyLong; import org.python.core.PyNewWrapper; import org.python.core.PyObject; import org.python.core.PyString; @@ -43,6 +45,10 @@ public static final Type PYINTEGER = Type.getType(PyInteger.class); + public static final Type PYLONG = Type.getType(PyLong.class); + + public static final Type PYFLOAT = Type.getType(PyFloat.class); + public static final Type PYNEWWRAPPER = Type.getType(PyNewWrapper.class); public static final Type BUILTIN_METHOD = Type.getType(PyBuiltinMethod.class); Modified: trunk/jython/tests/java/org/python/expose/generate/ExposedTypeProcessorTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/ExposedTypeProcessorTest.java 2008-01-01 23:56:13 UTC (rev 3943) +++ trunk/jython/tests/java/org/python/expose/generate/ExposedTypeProcessorTest.java 2008-01-02 00:18:13 UTC (rev 3944) @@ -18,7 +18,7 @@ .getResourceAsStream("org/python/expose/generate/SimpleExposed.class"); ExposedTypeProcessor ice = new ExposedTypeProcessor(in); assertEquals("simpleexposed", ice.getName()); - assertEquals(13, ice.getMethodExposers().size()); + assertEquals(16, ice.getMethodExposers().size()); assertNotNull(ice.getNewExposer()); assertEquals(1, ice.getDescriptorExposers().size()); assertEquals("simpleexposed", ice.getTypeExposer().getName()); Modified: trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java 2008-01-01 23:56:13 UTC (rev 3943) +++ trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java 2008-01-02 00:18:13 UTC (rev 3944) @@ -64,8 +64,8 @@ } public void testArgumentPassing() throws Exception { - PyBuiltinFunction bound = createBound("takesArgument", VOID, PYOBJ); - bound.__call__(Py.None); + PyBuiltinFunction bound = createBound("takesArgument", Type.DOUBLE_TYPE, PYOBJ); + assertEquals(1.0, Py.py2double(bound.__call__(Py.One))); try { bound.__call__(); fail("Need to pass an argument to takesArgument"); @@ -126,7 +126,7 @@ public void testPrimitiveDefaults() throws Exception { MethodExposer exp = createExposer("manyPrimitives", - PYOBJ, + STRING, CHAR, SHORT, Type.DOUBLE_TYPE, @@ -146,7 +146,7 @@ MethodExposer exp = new MethodExposer(Type.getType(SimpleExposed.class), Opcodes.ACC_PUBLIC, "fullArgs", - Type.getMethodDescriptor(PYOBJ, new Type[] {APYOBJ, + Type.getMethodDescriptor(Type.LONG_TYPE, new Type[] {APYOBJ, ASTRING}), "simpleexposed"); PyBuiltinFunction bound = createBound(exp); @@ -175,6 +175,12 @@ fail("Shouldn't be able to create an exposer on a static method"); } catch(InvalidExposingException ite) {} } + + public void testPrimitiveReturns() throws Exception{ + assertEquals(12, Py.py2int(createBound("shortReturn", SHORT).__call__())); + assertEquals(0, Py.py2int(createBound("byteReturn", BYTE).__call__())); + assertEquals("a", createBound("charReturn", CHAR).__call__().toString()); + } public void test__new__() throws Exception { try { Modified: trunk/jython/tests/java/org/python/expose/generate/SimpleExposed.java =================================================================== --- trunk/jython/tests/java/org/python/expose/generate/SimpleExposed.java 2008-01-01 23:56:13 UTC (rev 3943) +++ trunk/jython/tests/java/org/python/expose/generate/SimpleExposed.java 2008-01-02 00:18:13 UTC (rev 3944) @@ -62,8 +62,8 @@ } @ExposedMethod - public void takesArgument(PyObject arg) { - assert arg == Py.None; + public double takesArgument(PyObject arg) { + return Py.py2double(arg); } @ExposedMethod(type = MethodType.BINARY) @@ -98,15 +98,30 @@ } @ExposedMethod(defaults = {"a", "1", "2", "3"}) - public PyObject manyPrimitives(char c, short s, double d, byte b) { - return new PyString("" + c + s + d + b); + public String manyPrimitives(char c, short s, double d, byte b) { + return "" + c + s + d + b; } @ExposedMethod - public PyObject fullArgs(PyObject[] args, String[] kws) { - return new PyInteger(args.length + kws.length); + public long fullArgs(PyObject[] args, String[] kws) { + return args.length + kws.length; } + @ExposedMethod + public short shortReturn() { + return 12; + } + + @ExposedMethod + public byte byteReturn() { + return 0; + } + + @ExposedMethod + public char charReturn() { + return 'a'; + } + @ExposedGet(name = "tostring") public String toStringVal = TO_STRING_RETURN; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |