Skip to content

Commit

Permalink
Insert synthetic settings() function at the end of the module body, r…
Browse files Browse the repository at this point in the history
…ather than at the beginning.

This avoids inserting before import __future__.

Fixes jdf/Processing.py-Bugs#148
  • Loading branch information
Jonathan Feinberg committed Apr 3, 2016
1 parent 7d4c853 commit e4a4352
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions runtime/src/jycessing/pyde_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def pyde_preprocessor(module):
settingsArgs = ast.arguments(args=[], vararg=None, kwarg=None, defaults=[])
settingsFunc = ast.FunctionDef("settings", settingsArgs, [], [])
info.insert(settingsFunc.body)
# Place the newly defined settings() function within the module body.
# Place the newly defined settings() function at the end of the module body.
# It's like it's been there the whole time...
module.body.insert(0, settingsFunc)
module.body.insert(len(module.body), settingsFunc)

module = ast.parse(__processing_source__ + "\n\n", filename=__file__)
pyde_preprocessor(module)
Expand Down
8 changes: 8 additions & 0 deletions testing/resources/test_from_future_with_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import division

def setup():
size(20, 20)

def draw():
print 'OK'
exit()
10 changes: 8 additions & 2 deletions testing/src/test/jycessing/JycessingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import org.junit.Test;

import jycessing.MixedModeError;
import jycessing.PAppletJythonDriver;
import jycessing.Printer;
import jycessing.PythonSketchError;
import jycessing.Runner;
import jycessing.StreamPrinter;

import org.junit.Test;

public class JycessingTests {
private static class CapturingPrinter implements Printer {
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down Expand Up @@ -270,4 +270,10 @@ public void randintDomainRegression() throws Exception {
public void syspathappend() throws Exception {
expectOK("syspathappend");
}

// https://github.com/jdf/Processing.py-Bugs/issues/148
@Test
public void from_future_with_settings() throws Exception {
expectOK("from_future_with_settings");
}
}

0 comments on commit e4a4352

Please sign in to comment.