Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Jython execFile SyntaxError issue
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DemonWasp




PostPosted: 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
Sponsor
sponsor
DemonWasp




PostPosted: 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...?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: