Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix issue 4, subclassing builtin set class

  • Loading branch information...
commit 6a270441cd0b926eaa7e528006f53cfadf2fd4c7 1 parent 3fb764a
@jdf authored
View
2  .classpath
@@ -5,7 +5,7 @@
<classpathentry kind="src" path="runtime/generated"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
- <classpathentry kind="lib" path="buildtime/lib/jython/jython.jar"/>
+ <classpathentry kind="lib" path="buildtime/lib/jython/jython.jar" sourcepath="/jython-trunk"/>
<classpathentry kind="lib" path="buildtime/lib/processing/core.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
2  buildtime/py/processing_parser.py
@@ -23,7 +23,7 @@
|(get|set|remove)Cache|update|destroy|main|flush|addListeners|dataFile
|die|setup|mouseE(ntered|xited)|paint|sketch[A-Z].*|stop|save(File|Path)
|displayable|method|runSketch|start|focus(Lost|Gained)|(data|create)Path
- |round|abs|max|min|open|append|splice|expand|contract
+ |round|abs|max|min|open|append|splice|expand|contract|set
)$
''', re.X)
View
54 runtime/src/jycessing/PAppletJythonDriver.java
@@ -18,18 +18,24 @@
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.util.HashSet;
import java.util.regex.Pattern;
import org.python.core.CompileMode;
import org.python.core.CompilerFlags;
import org.python.core.Py;
import org.python.core.PyException;
+import org.python.core.PyInteger;
+import org.python.core.PyJavaType;
import org.python.core.PyObject;
+import org.python.core.PySet;
import org.python.core.PyStringMap;
+import org.python.core.PyType;
import org.python.util.InteractiveConsole;
import processing.core.PApplet;
import processing.core.PConstants;
+import processing.core.PImage;
/**
*
@@ -92,6 +98,7 @@ public PAppletJythonDriver(final InteractiveConsole interp,
this.builtins = (PyStringMap) interp.getSystemState().getBuiltins();
this.interp = interp;
initializeStatics(builtins);
+ setSet();
populateBuiltins();
setFields();
builtins.__setitem__("this", Py.java2py(this));
@@ -117,6 +124,53 @@ public PAppletJythonDriver(final InteractiveConsole interp,
}
/**
+ * Permit the punning use of set() by mucking with the builtin "set" Type.
+ * If you call it with 3 arguments, it acts like the Processing set(x, y,
+ * whatever) method. If you call it with 0 or 1 args, it constructs a Python
+ * set.
+ */
+ private void setSet() {
+ builtins.__setitem__("set", new PyJavaType() {
+ {
+ builtin = true;
+ init(PySet.class, new HashSet<PyJavaType>());
+ invalidateMethodCache();
+ }
+
+ @Override
+ public PyObject __call__(PyObject[] args, String[] kws) {
+ switch (args.length) {
+ default:
+ return super.__call__(args, kws);
+ case 3: {
+ final PyObject x = args[0];
+ final PyType tx = x.getType();
+ final PyObject y = args[1];
+ final PyType ty = y.getType();
+ final PyObject c = args[2];
+ final PyType tc = c.getType();
+ if (tx == PyInteger.TYPE && ty == PyInteger.TYPE
+ && tc == PyInteger.TYPE) {
+ set(x.asInt(), y.asInt(), c.asInt());
+ return Py.None;
+ } else if (tx == PyInteger.TYPE && ty == PyInteger.TYPE
+ && tc.getProxyType() != null
+ && tc.getProxyType() == PImage.class) {
+ set(x.asInt(),
+ y.asInt(),
+ (processing.core.PImage) c
+ .__tojava__(processing.core.PImage.class));
+ return Py.None;
+ } else {
+ return super.__call__(args, kws);
+ }
+ }
+ }
+ }
+ });
+ }
+
+ /**
* Populate the Python builtins namespace with PConstants.
*/
public static void initializeStatics(final PyStringMap builtins) {
View
10 testing/test_resources/test_set.py
@@ -8,4 +8,12 @@
s = set()
s.add('banana')
print s
-#expect 13
+
+# subclass
+class MySet(set):
+ def __init__(self):
+ set.__init__(self)
+print"issubclass: %s" % issubclass(MySet, set)
+foo = MySet()
+foo.add('baz')
+print foo
View
2  testing/tests/test/jycessing/JycessingTests.java
@@ -56,7 +56,7 @@ public void static_size() throws Exception {
@Test
public void set_builtins() throws Exception {
- assertEquals("128\nset(['banana'])\n", run("set"));
+ assertEquals("128\nset(['banana'])\nissubclass: True\nset(['baz'])\n", run("set"));
}
@Test
Please sign in to comment.
Something went wrong with that request. Please try again.