
-----------------------------------
DemonWasp
Sun Dec 19, 2010 6:44 pm

Jython execFile SyntaxError issue
-----------------------------------
I wasn't sure whether this belongs in Java Help or Python Help, but figured it's mostly Java.

I'm using Jython (2.5.2rc2), trying to execute code stored in a file, extend.py. The contents of extend.py are pretty trivial:

print "extend.py works!"


The code I'm using to invoke that looks like the following:

Initialization:

PythonInterpreter.initialize (
	System.getProperties(),
	System.getProperties(),
	new String

Approach 1 (does NOT work):
This approach invokes execfile() on the source file's InputStream

System.out.println ( "Approach 1: " );
try {
	InputStream in = new FileInputStream( srcFile );
	interpreter.execfile ( in );
	in.close();
} catch ( Throwable t ) {
	t.printStackTrace();
}


Results:
Approach 2 (does work):
This approach reads the code from the file into a StringBuilder, then invokes exec() on the assembled String.
		
System.out.println ( "Approach 2: " );
StringBuilder code = new StringBuilder();
Scanner in = new Scanner ( new FileInputStream( srcFile ) );
while ( in.hasNextLine() ) {
	code.append ( in.nextLine() ).append( "\n" );
}
in.close();

System.out.println ( "--- CODE --- " );
System.out.println ( code.toString() );
System.out.println ( "--- END --- " );
interpreter.exec ( code.toString() );


Results:
[code]
Approach 2: 
--- CODE --- 
print "extend.py works!"

--- END --- 
extend.py works!
[/code]



I've tried googling the error message with a few different combinations of keywords, all to no avail. I don't know if I'm doing something stupid here, or if I'm running into some sort of bizarre bug. Has anyone run into similar issues with Jython before?

-----------------------------------
DemonWasp
Tue Dec 21, 2010 1:01 am

RE:Jython execFile SyntaxError issue
-----------------------------------
So...I'm not sure what I did, but something fixed it. Maybe it was just a random glitch...?
