From: <cg...@us...> - 2007-10-09 07:50:22
|
Revision: 3588 http://jython.svn.sourceforge.net/jython/?rev=3588&view=rev Author: cgroves Date: 2007-10-09 00:50:21 -0700 (Tue, 09 Oct 2007) Log Message: ----------- Simple default handling for Py.None and null Modified Paths: -------------- branches/exposed_annotation/src/org/python/expose/ExposedMethod.java branches/exposed_annotation/src/org/python/expose/MethodExposer.java branches/exposed_annotation/tests/java/org/python/expose/MethodExposerTest.java branches/exposed_annotation/tests/java/org/python/expose/SimpleExposed.java branches/exposed_annotation/tests/java/org/python/expose/TypeExposerTest.java Modified: branches/exposed_annotation/src/org/python/expose/ExposedMethod.java =================================================================== --- branches/exposed_annotation/src/org/python/expose/ExposedMethod.java 2007-10-09 03:36:42 UTC (rev 3587) +++ branches/exposed_annotation/src/org/python/expose/ExposedMethod.java 2007-10-09 07:50:21 UTC (rev 3588) @@ -21,7 +21,7 @@ /** * @return default arguments. Starts at the number of arguments - - * defaults.length + * defaults.length. */ String[] defaults() default {}; Modified: branches/exposed_annotation/src/org/python/expose/MethodExposer.java =================================================================== --- branches/exposed_annotation/src/org/python/expose/MethodExposer.java 2007-10-09 03:36:42 UTC (rev 3587) +++ branches/exposed_annotation/src/org/python/expose/MethodExposer.java 2007-10-09 07:50:21 UTC (rev 3588) @@ -54,7 +54,9 @@ generateNamedConstructor(); generateFullConstructor(); generateBind(); - generateCall(); + for(int i = 0; i < exp.defaults().length + 1; i++) { + generateCall(i); + } } private void generateFullConstructor() { @@ -70,8 +72,8 @@ startConstructor(STRING); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); + mv.visitLdcInsn(method.getParameterTypes().length + 1 - exp.defaults().length); mv.visitLdcInsn(method.getParameterTypes().length + 1); - mv.visitLdcInsn(method.getParameterTypes().length + 1); superConstructor(STRING, INT, INT); endConstructor(); } @@ -87,9 +89,9 @@ endMethod(); } - private void generateCall() { + private void generateCall(int numDefaults) { int usedLocals = 1;// We always have one used local for this - Type[] args = new Type[method.getParameterTypes().length]; + Type[] args = new Type[method.getParameterTypes().length - numDefaults]; for(int i = 0; i < args.length; i++) { args[i] = PYOBJ; } @@ -100,6 +102,14 @@ for(int i = 0; i < args.length; i++) { mv.visitVarInsn(ALOAD, usedLocals++); } + for(int i = 0; i < numDefaults; i++) { + String def = exp.defaults()[i]; + if(def.equals("Py.None")) { + pushNone(); + }else if(def.equals("null")) { + mv.visitInsn(ACONST_NULL); + } + } mv.visitMethodInsn(INVOKEVIRTUAL, methType.getInternalName(), method.getName(), @@ -154,7 +164,7 @@ } Class ret = method.getReturnType(); if(ret == Void.TYPE) { - mv.visitFieldInsn(GETSTATIC, PY.getInternalName(), "None", PYOBJ.getDescriptor()); + pushNone(); } else if(ret == String.class) { mv.visitMethodInsn(INVOKESTATIC, PY.getInternalName(), "newString", methodDesc(PYSTR, STRING)); @@ -173,6 +183,10 @@ endMethod(); } + private void pushNone() { + mv.visitFieldInsn(GETSTATIC, PY.getInternalName(), "None", PYOBJ.getDescriptor()); + } + private ExposedMethod exp; private Method method; Modified: branches/exposed_annotation/tests/java/org/python/expose/MethodExposerTest.java =================================================================== --- branches/exposed_annotation/tests/java/org/python/expose/MethodExposerTest.java 2007-10-09 03:36:42 UTC (rev 3587) +++ branches/exposed_annotation/tests/java/org/python/expose/MethodExposerTest.java 2007-10-09 07:50:21 UTC (rev 3588) @@ -79,7 +79,7 @@ public void testCmp() throws Exception { MethodExposer me = new MethodExposer(SimpleExposed.class.getMethod("__cmp__", PyObject.class), - "simpleexpose"); + "simpleexpose"); PyBuiltinFunction bound = createBound(me); try { bound.__call__(Py.None); @@ -91,4 +91,20 @@ } assertEquals(Py.One, bound.__call__(Py.False)); } + + public void testNoneDefault() throws Exception { + MethodExposer me = new MethodExposer(SimpleExposed.class.getMethod("defaultToNone", + PyObject.class)); + PyBuiltinFunction bound = createBound(me); + assertEquals(Py.One, bound.__call__(Py.One)); + assertEquals(Py.None, bound.__call__()); + } + + public void testNullDefault() throws Exception { + MethodExposer me = new MethodExposer(SimpleExposed.class.getMethod("defaultToNull", + PyObject.class)); + PyBuiltinFunction bound = createBound(me); + assertEquals(Py.One, bound.__call__(Py.One)); + assertEquals(null, bound.__call__()); + } } Modified: branches/exposed_annotation/tests/java/org/python/expose/SimpleExposed.java =================================================================== --- branches/exposed_annotation/tests/java/org/python/expose/SimpleExposed.java 2007-10-09 03:36:42 UTC (rev 3587) +++ branches/exposed_annotation/tests/java/org/python/expose/SimpleExposed.java 2007-10-09 07:50:21 UTC (rev 3588) @@ -48,6 +48,16 @@ } return -2; } + + @ExposedMethod(defaults = {"Py.None"}) + public PyObject defaultToNone(PyObject arg) { + return arg; + } + + @ExposedMethod(defaults = {"null"}) + public PyObject defaultToNull(PyObject arg) { + return arg; + } public static final String TO_STRING_RETURN = "A simple test class"; } \ No newline at end of file Modified: branches/exposed_annotation/tests/java/org/python/expose/TypeExposerTest.java =================================================================== --- branches/exposed_annotation/tests/java/org/python/expose/TypeExposerTest.java 2007-10-09 03:36:42 UTC (rev 3587) +++ branches/exposed_annotation/tests/java/org/python/expose/TypeExposerTest.java 2007-10-09 07:50:21 UTC (rev 3588) @@ -21,7 +21,7 @@ public void testFindMethods() { TypeExposer ecp = new TypeExposer(SimpleExposed.class); List<Method> methods = ecp.findMethods(); - assertEquals(7, methods.size()); + assertEquals(9, methods.size()); } public void testGetName() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |