Permalink
Browse files

Static mode sketches weren't working at all. Now they're working. At …

…all.
  • Loading branch information...
1 parent f3b64f8 commit 0cca947d1000ca7fa0bee58f4f5d265e072ab2fd @google-feinberg google-feinberg committed Dec 15, 2011
@@ -102,6 +102,7 @@ public PAppletJythonDriver(final InteractiveConsole interp,
this.pySketchPath = sketchPath;
this.sketchPath = new File(sketchPath).getParent();
this.isStaticMode = !ACTIVE_METHOD_DEF.matcher(programText).find();
+ Runner.log("Mode: ", (isStaticMode ? "static" : "active"));
this.builtins = (PyStringMap)interp.getSystemState().getBuiltins();
this.interp = interp;
initializeStatics(builtins);
@@ -114,7 +115,6 @@ public PAppletJythonDriver(final InteractiveConsole interp,
@Override
public PyObject __call__(final PyObject[] args, final String[] kws) {
finishedLatch.countDown();
- PAppletJythonDriver.super.exit();
return Py.None;
}
});
@@ -269,6 +269,7 @@ public void setup() {
try {
if (isStaticMode) {
// A static sketch gets called once, from this spot.
+ Runner.log("Interpreting static-mode sketch.");
interpretSketch();
} else if (setupMeth != null) {
// Call the Python sketch's setup()
@@ -283,8 +284,8 @@ public void setup() {
@Override
public void draw() {
if (drawMeth == null) {
+ Runner.log("Calling super.draw() in what I assume is a static-mode sketch.");
super.draw();
- finishedLatch.countDown();
} else {
// Put all of PApplet's globals into the Python context
setFields();
@@ -15,10 +15,18 @@
*/
package jycessing;
+import org.python.core.Py;
+import org.python.core.PyString;
+import org.python.util.InteractiveConsole;
+import org.python.util.PythonInterpreter;
+
+import processing.core.PApplet;
+
import java.awt.Window;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
+import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
@@ -37,16 +45,19 @@
import javax.swing.SwingUtilities;
-import org.python.core.Py;
-import org.python.core.PyString;
-import org.python.util.InteractiveConsole;
-import org.python.util.PythonInterpreter;
-
-import processing.core.PApplet;
-
public class Runner {
static boolean VERBOSE = false;
+ static void log(final Object... objs) {
+ if (!VERBOSE) {
+ return;
+ }
+ for (final Object o : objs) {
+ System.err.print(String.valueOf(o));
+ }
+ System.err.println();
+ }
+
// Slurp the given Reader into a String.
private static String read(final Reader r) throws IOException {
final BufferedReader reader = new BufferedReader(r);
@@ -80,20 +91,18 @@ private static String read(final InputStream in) throws IOException {
*
*/
private static void addJar(final URL url) throws Exception {
- final URLClassLoader classLoader = (URLClassLoader)ClassLoader
- .getSystemClassLoader();
+ final URLClassLoader classLoader =
+ (URLClassLoader)ClassLoader.getSystemClassLoader();
for (final URL u : classLoader.getURLs()) {
if (u.equals(url)) {
return;
}
}
- final Method method = URLClassLoader.class.getDeclaredMethod("addURL",
- URL.class);
+ final Method method =
+ URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, new Object[] { url });
- if (VERBOSE) {
- System.err.println("Added " + url + " to classpath.");
- }
+ log("Added ", url, " to classpath.");
}
/**
@@ -119,9 +128,7 @@ private static void addLibraryPath(final String newPath) throws Exception {
System.arraycopy(paths, 0, tmp, 0, paths.length);
tmp[paths.length] = newPath;
field.set(null, tmp);
- if (VERBOSE) {
- System.err.println("Added " + newPath + " to java.library.path.");
- }
+ log("Added ", newPath, " to java.library.path.");
}
/**
@@ -134,9 +141,7 @@ private static void searchForExtraStuff(final File dir) throws Exception {
throw new IllegalArgumentException("null dir");
}
- if (VERBOSE) {
- System.err.println("Searching: " + dir);
- }
+ log("Searching: ", dir);
final File[] dlls = dir.listFiles(new FilenameFilter() {
public boolean accept(final File dir, final String name) {
@@ -145,8 +150,8 @@ public boolean accept(final File dir, final String name) {
});
if (dlls != null && dlls.length > 0) {
addLibraryPath(dir.getAbsolutePath());
- } else if (VERBOSE) {
- System.err.println("No DLLs in " + dir);
+ } else {
+ log("No DLLs in ", dir);
}
final File[] jars = dir.listFiles(new FilenameFilter() {
@@ -158,31 +163,41 @@ public boolean accept(final File dir, final String name) {
for (final File jar : jars) {
addJar(jar.toURI().toURL());
}
- } else if (VERBOSE) {
- System.err.println("No JARs in " + dir);
+ } else {
+ log("No JARs in ", dir);
}
final File[] dirs = dir.listFiles(new FileFilter() {
public boolean accept(final File f) {
- if (VERBOSE) {
- System.err.println("Looking at " + f);
- }
+ log("Looking at ", f);
return f.isDirectory() && f.getName().charAt(0) != '.';
}
});
if (!(dirs == null || dirs.length == 0)) {
for (final File d : dirs) {
searchForExtraStuff(d);
}
- } else if (VERBOSE) {
- System.err.println("No dirs in " + dir);
+ } else {
+ log("No dirs in ", dir);
}
}
public static void main(final String[] args) throws Exception {
+ runFromCommandLineArguments(args);
+ System.exit(0);
+ }
+
+ /**
+ * @param args
+ * @throws IOException
+ * @throws FileNotFoundException
+ * @throws Exception
+ */
+ public static void
+ runFromCommandLineArguments(final String[] args) throws Exception {
if (args.length < 1) {
- System.err
- .println("I need the path of your Python script as an argument.");
+ throw new RuntimeException(
+ "I need the path of your Python script as an argument.");
}
// -Dverbose=true for some logging
@@ -191,10 +206,7 @@ public static void main(final String[] args) throws Exception {
final Properties buildnum = new Properties();
buildnum.load(Runner.class
.getResourceAsStream("buildnumber.properties"));
- if (VERBOSE) {
- System.err.println("processing.py build "
- + buildnum.getProperty("buildnumber"));
- }
+ log("processing.py build ", buildnum.getProperty("buildnumber"));
// The last argument is the path to the Python sketch
final String sketchPath = args[args.length - 1];
@@ -206,35 +218,32 @@ public static void main(final String[] args) throws Exception {
runSketch(args, sketchPath, sketchSource);
}
- private static final Pattern JAR_RESOURCE = Pattern
- .compile("jar:file:(.+?)/processing-py.jar!/jycessing/buildnumber.properties");
+ private static final Pattern JAR_RESOURCE =
+ Pattern.compile("jar:file:(.+?)/processing-py.jar!/jycessing/buildnumber.properties");
private static final Pattern FILE_RESOURCE = Pattern
.compile("file:(.+?)/bin/jycessing/buildnumber.properties");
private static File getLibrariesDir() {
String propsResource;
try {
- propsResource = URLDecoder.decode(
- Runner.class.getResource("buildnumber.properties")
- .toString(), "UTF-8");
+ propsResource =
+ URLDecoder.decode(
+ Runner.class.getResource("buildnumber.properties")
+ .toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Impossible: " + e);
}
{
final Matcher m = JAR_RESOURCE.matcher(propsResource);
if (m.matches()) {
- if (VERBOSE) {
- System.err.println("We're running from a JAR file.");
- }
+ log("We're running from a JAR file.");
return new File(m.group(1), "libraries");
}
}
{
final Matcher m = FILE_RESOURCE.matcher(propsResource);
if (m.matches()) {
- if (VERBOSE) {
- System.err.println("We're running from class files.");
- }
+ log("We're running from class files.");
return new File(m.group(1), "libraries");
}
}
@@ -244,7 +253,7 @@ private static File getLibrariesDir() {
}
public static void runSketch(final String[] args, final String sketchPath,
- final String sketchSource) throws Exception {
+ final String sketchSource) throws Exception {
// Recursively search the "libraries" directory for jar files and
// directories containing dynamic libraries, adding them to the
// classpath and the library path respectively.
@@ -257,8 +266,8 @@ public static void runSketch(final String[] args, final String sketchPath,
searchForExtraStuff(libraries);
// Where is the sketch located?
- final String sketchDir = new File(sketchPath).getCanonicalFile()
- .getParent();
+ final String sketchDir =
+ new File(sketchPath).getCanonicalFile().getParent();
final Properties props = new Properties();
props.setProperty("python.path", libraries.getAbsolutePath()
@@ -284,22 +293,18 @@ public static void runSketch(final String[] args, final String sketchPath,
interp.exec(read(Runner.class.getResourceAsStream("core.py")));
// Bind the sketch to a PApplet
- final PAppletJythonDriver applet = new DriverImpl(interp, sketchPath,
- sketchSource);
+ final PAppletJythonDriver applet =
+ new DriverImpl(interp, sketchPath, sketchSource);
try {
PApplet.runSketch(args, applet);
applet.blockUntilFinished();
- if (VERBOSE) {
- System.err.println("Applet is finished. Disposing window.");
- }
+ log("Applet is finished. Disposing window.");
((Window)SwingUtilities.getRoot(applet)).dispose();
} catch (final Throwable t) {
Py.printException(t);
} finally {
- if (VERBOSE) {
- System.err.println("Cleaning up interpreter.");
- }
+ log("Cleaning up interpreter.");
interp.cleanup();
}
}
@@ -1,2 +1,2 @@
-#Tue Feb 08 21:39:06 EST 2011
-buildnumber=0016
+#Thu Dec 15 14:24:16 EST 2011
+buildnumber=0018
@@ -9,4 +9,4 @@ def __repr__(self):
print foo
print str(12)
print str([12,13])
-noLoop()
+exit()
@@ -2,4 +2,5 @@
loadPixels()
pixels[10] = color(10,0,255)
updatePixels()
-print 'OK'
+print 'OK'
+exit()
@@ -8,4 +8,5 @@
def setup():
size(10, 10, P3D)
noLoop();
- print 'OK'
+ print 'OK'
+ exit()
@@ -6,4 +6,5 @@
# Python builtin
print map(lambda x: x + 1, (12, 16, 22))[0]
-#expect 13
+#expect 13
+exit()
@@ -2,3 +2,4 @@
print md5.new("Nobody inspects the spammish repetition").hexdigest()
+exit()
@@ -1,2 +1,3 @@
print PVector(1, 2, 3)
-print PFont
+print PFont
+exit()
@@ -41,3 +41,4 @@
print 'OK'
+exit()
@@ -17,3 +17,4 @@ def __init__(self):
foo = MySet()
foo.add('baz')
print foo
+exit()
@@ -1,2 +1,3 @@
size(10, 10)
print 'OK'
+exit()
@@ -1,2 +1,3 @@
text(u"ö", 0, 20)
-print(u'OK')
+print(u'OK')
+exit()
@@ -1,2 +1,3 @@
import urllib
print "OK"
+exit()
@@ -1,2 +1,3 @@
import urllib2
-print "OK"
+print "OK"
+exit()
@@ -17,7 +17,7 @@ private static String run(final String testResource) throws Exception {
final PrintStream saved = System.out;
try {
System.setOut(new PrintStream(baos, true));
- Runner.main(new String[] { "testing/test_resources/test_"
+ Runner.runFromCommandLineArguments(new String[] { "testing/test_resources/test_"
+ testResource + ".py" });
return new String(baos.toByteArray()).replaceAll("\r\n", "\n")
.replaceAll("\r", "\n");
@@ -33,7 +33,7 @@ private static void testImport(final String module) throws Exception {
System.setOut(new PrintStream(baos, true));
final String testClass = module + "_test";
final String bogusFileName = "<test " + module + ">";
- final String testText = "import " + module + "\nprint 'OK'";
+ final String testText = "import " + module + "\nprint 'OK'\nexit()";
Runner.runSketch(new String[] { testClass }, bogusFileName,
testText);
assertEquals("OK\n",

0 comments on commit 0cca947

Please sign in to comment.