From: <zy...@us...> - 2010-08-21 03:56:27
|
Revision: 7100 http://jython.svn.sourceforge.net/jython/?rev=7100&view=rev Author: zyasoft Date: 2010-08-21 03:56:21 +0000 (Sat, 21 Aug 2010) Log Message: ----------- Minor changes to PyLong, PyFloat that were missed by NetBean's accessor refactoring support. Added tests of how our subclasses of our types can now be abused to have treatable as the original type, but mutable. Use such dangerous functionality with care! See #1645 for further discussion of this issue, including rationale. Implemented to help support efforts like processing.py. Thanks Jonathan Feinberg for this patch set. Modified Paths: -------------- trunk/jython/ACKNOWLEDGMENTS trunk/jython/src/org/python/core/PyFloat.java trunk/jython/src/org/python/core/PyLong.java Added Paths: ----------- trunk/jython/tests/java/org/python/core/WrappedBooleanTest.java trunk/jython/tests/java/org/python/core/WrappedFloatTest.java trunk/jython/tests/java/org/python/core/WrappedIntegerTest.java trunk/jython/tests/java/org/python/core/WrappedLongTest.java Modified: trunk/jython/ACKNOWLEDGMENTS =================================================================== --- trunk/jython/ACKNOWLEDGMENTS 2010-08-21 03:42:48 UTC (rev 7099) +++ trunk/jython/ACKNOWLEDGMENTS 2010-08-21 03:56:21 UTC (rev 7100) @@ -90,7 +90,8 @@ Jonathan Feinberg Justin Deoliveira Costantino Cerbo - Alex Gr\xF6nholm + Alex Gr\xF6nholm + Jonathan Feinberg Local Variables: mode: indented-text Modified: trunk/jython/src/org/python/core/PyFloat.java =================================================================== --- trunk/jython/src/org/python/core/PyFloat.java 2010-08-21 03:42:48 UTC (rev 7099) +++ trunk/jython/src/org/python/core/PyFloat.java 2010-08-21 03:56:21 UTC (rev 7100) @@ -82,7 +82,7 @@ * Determine if this float is not infinity, nor NaN. */ public boolean isFinite() { - return !Double.isInfinite(value) && !Double.isNaN(value); + return !Double.isInfinite(getValue()) && !Double.isNaN(getValue()); } @Override Modified: trunk/jython/src/org/python/core/PyLong.java =================================================================== --- trunk/jython/src/org/python/core/PyLong.java 2010-08-21 03:42:48 UTC (rev 7099) +++ trunk/jython/src/org/python/core/PyLong.java 2010-08-21 03:56:21 UTC (rev 7100) @@ -151,7 +151,7 @@ @Override public boolean __nonzero__() { - return !value.equals(BigInteger.valueOf(0)); + return !getValue().equals(BigInteger.ZERO); } @ExposedMethod(doc = BuiltinDocs.long___nonzero___doc) Added: trunk/jython/tests/java/org/python/core/WrappedBooleanTest.java =================================================================== --- trunk/jython/tests/java/org/python/core/WrappedBooleanTest.java (rev 0) +++ trunk/jython/tests/java/org/python/core/WrappedBooleanTest.java 2010-08-21 03:56:21 UTC (rev 7100) @@ -0,0 +1,59 @@ +package org.python.core; + +import junit.framework.TestCase; + +import org.python.util.PythonInterpreter; + +public class WrappedBooleanTest extends TestCase { + + // Simulate the use case where you want to expose some (possibly mutable) + // java boolean field to an interpreter without having to set the value to a + // new PyBoolean each time it changes. + @SuppressWarnings("serial") + static class WrappedBoolean extends PyBoolean { + public WrappedBoolean() { + super(true); + } + + private boolean mutableValue; + + @Override + public boolean getBooleanValue() { + return mutableValue; + } + + public void setMutableValue(final boolean newValue) { + mutableValue = newValue; + } + } + + private PythonInterpreter interp; + private WrappedBoolean a, b; + + @Override + protected void setUp() throws Exception { + interp = new PythonInterpreter(new PyStringMap(), new PySystemState()); + a = new WrappedBoolean(); + b = new WrappedBoolean(); + a.setMutableValue(true); + b.setMutableValue(false); + interp.set("a", a); + interp.set("b", b); + } + + public void testAnd() { + interp.exec("c = a and b"); + assertEquals(new PyBoolean(false), interp.get("c")); + b.setMutableValue(true); + interp.exec("c = a and b"); + assertEquals(new PyBoolean(true), interp.get("c")); + } + + public void testOr() { + interp.exec("c = a or b"); + assertEquals(new PyBoolean(true), interp.get("c")); + a.setMutableValue(false); + interp.exec("c = a or b"); + assertEquals(new PyBoolean(false), interp.get("c")); + } +} Added: trunk/jython/tests/java/org/python/core/WrappedFloatTest.java =================================================================== --- trunk/jython/tests/java/org/python/core/WrappedFloatTest.java (rev 0) +++ trunk/jython/tests/java/org/python/core/WrappedFloatTest.java 2010-08-21 03:56:21 UTC (rev 7100) @@ -0,0 +1,61 @@ +package org.python.core; + +import junit.framework.TestCase; + +import org.python.util.PythonInterpreter; + +public class WrappedFloatTest extends TestCase { + + // Simulate the use case where you want to expose some (possibly mutable) + // java float field to an interpreter without having to set the value to a new + // PyFloat each time it changes. + @SuppressWarnings("serial") + static class WrappedFloat extends PyFloat { + public WrappedFloat() { + super(0); + } + + private double mutableValue; + + @Override + public double getValue() { + return mutableValue; + } + + public void setMutableValue(final double newValue) { + mutableValue = newValue; + } + } + + private PythonInterpreter interp; + private WrappedFloat a, b; + + @Override + protected void setUp() throws Exception { + interp = new PythonInterpreter(new PyStringMap(), new PySystemState()); + a = new WrappedFloat(); + b = new WrappedFloat(); + a.setMutableValue(13.0); + b.setMutableValue(17.0); + interp.set("a", a); + interp.set("b", b); + } + + public void testAdd() { + interp.exec("c = a + b"); + assertEquals(new PyFloat(30), interp.get("c")); + b.setMutableValue(18.0); + interp.exec("c = a + b"); + assertEquals(new PyFloat(31), interp.get("c")); + } + + public void testDiv() { + interp.exec("c = a / b"); + assertEquals(new PyFloat(13 / 17.), interp.get("c")); + } + + public void testMod() { + interp.exec("c = b % a"); + assertEquals(new PyFloat(4), interp.get("c")); + } +} Added: trunk/jython/tests/java/org/python/core/WrappedIntegerTest.java =================================================================== --- trunk/jython/tests/java/org/python/core/WrappedIntegerTest.java (rev 0) +++ trunk/jython/tests/java/org/python/core/WrappedIntegerTest.java 2010-08-21 03:56:21 UTC (rev 7100) @@ -0,0 +1,61 @@ +package org.python.core; + +import junit.framework.TestCase; + +import org.python.util.PythonInterpreter; + +public class WrappedIntegerTest extends TestCase { + + // Simulate the use case where you want to expose some (possibly mutable) + // java int field to an interpreter without having to set the value to a new + // PyInteger each time it changes. + @SuppressWarnings("serial") + static class WrappedInteger extends PyInteger { + public WrappedInteger() { + super(0); + } + + private int mutableValue; + + @Override + public int getValue() { + return mutableValue; + } + + public void setMutableValue(final int newValue) { + mutableValue = newValue; + } + } + + private PythonInterpreter interp; + private WrappedInteger a, b; + + @Override + protected void setUp() throws Exception { + interp = new PythonInterpreter(new PyStringMap(), new PySystemState()); + a = new WrappedInteger(); + b = new WrappedInteger(); + a.setMutableValue(13); + b.setMutableValue(17); + interp.set("a", a); + interp.set("b", b); + } + + public void testAdd() { + interp.exec("c = a + b"); + assertEquals(new PyInteger(30), interp.get("c")); + b.setMutableValue(18); + interp.exec("c = a + b"); + assertEquals(new PyInteger(31), interp.get("c")); + } + + public void testDiv() { + interp.exec("c = a / float(b)"); + assertEquals(new PyFloat(13 / 17.), interp.get("c")); + } + + public void testMod() { + interp.exec("c = b % a"); + assertEquals(new PyInteger(4), interp.get("c")); + } +} Added: trunk/jython/tests/java/org/python/core/WrappedLongTest.java =================================================================== --- trunk/jython/tests/java/org/python/core/WrappedLongTest.java (rev 0) +++ trunk/jython/tests/java/org/python/core/WrappedLongTest.java 2010-08-21 03:56:21 UTC (rev 7100) @@ -0,0 +1,58 @@ +package org.python.core; + +import java.math.BigInteger; + +import junit.framework.TestCase; + +import org.python.util.PythonInterpreter; + +public class WrappedLongTest extends TestCase { + + // Simulate the use case where you want to expose some (possibly mutable) + // java long field to an interpreter without having to set the value to a + // new PyLong each time it changes. + @SuppressWarnings("serial") + static class WrappedLong extends PyLong { + public WrappedLong() { + super(0); + } + + private long mutableValue; + + @Override + public BigInteger getValue() { + return BigInteger.valueOf(mutableValue); + } + + public void setMutableValue(final long newValue) { + mutableValue = newValue; + } + } + + private PythonInterpreter interp; + private WrappedLong a, b; + + @Override + protected void setUp() throws Exception { + interp = new PythonInterpreter(new PyStringMap(), new PySystemState()); + a = new WrappedLong(); + b = new WrappedLong(); + a.setMutableValue(13000000000L); + b.setMutableValue(17000000000L); + interp.set("a", a); + interp.set("b", b); + } + + public void testAdd() { + interp.exec("c = a + b"); + assertEquals(new PyLong(30000000000L), interp.get("c")); + b.setMutableValue(18000000000L); + interp.exec("c = a + b"); + assertEquals(new PyLong(31000000000L), interp.get("c")); + } + + public void testMod() { + interp.exec("c = b % a"); + assertEquals(new PyLong(4000000000L), interp.get("c")); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |