Jython execFile SyntaxError issue
Author |
Message |
DemonWasp
|
Posted: Sun Dec 19, 2010 6:44 pm Post subject: 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:
Python: |
print "extend.py works!"
|
The code I'm using to invoke that looks like the following:
Initialization:
Java: |
PythonInterpreter.initialize (
System.getProperties(),
System.getProperties(),
new String[0]
);
PythonInterpreter interpreter = new PythonInterpreter();
File srcFile = new File ( "extend.py" );
|
Approach 1 (does NOT work):
This approach invokes execfile() on the source file's InputStream
Java: |
System.out.println ( "Approach 1: " );
try {
InputStream in = new FileInputStream( srcFile );
interpreter.execfile ( in );
in.close();
} catch ( Throwable t ) {
t.printStackTrace();
}
|
Results:
code: |
Approach 1:
File "<iostream>", line 0
SyntaxError: encoding declaration in Unicode string
|
Approach 2 (does work):
This approach reads the code from the file into a StringBuilder, then invokes exec() on the assembled String.
Java: |
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!
|
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? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DemonWasp
|
Posted: Tue Dec 21, 2010 1:01 am Post subject: RE:Jython execFile SyntaxError issue |
|
|
So...I'm not sure what I did, but something fixed it. Maybe it was just a random glitch...? |
|
|
|
|
 |
|
|