Handling file encoding with JSR 223 Scripting API

Svata Dedic <[email protected]> Sun, 15 Sep 2019 19:27:57 +0200
Newsgroups gmane.comp.lang.jython.user
Message-ID <[email protected]>
Hi,

A project I am using utilizes Jython for scripting; invokes Jython using 
JSR 223 Stripting API:

ScriptEngine engine = ... ; // obtain ScriptingEngine instance

try (FileReader fr = new FileReader(file)) {
    return engine.eval(fr, context);
}

The file can script UI, so localized Strings (literals) may appear in 
the script file. Unless the entire application's default encoding 
*happens* to be correct (so that FileReader uses the correct one) + 
*all* the scripts use the encoding, the script text is naturally misread 
and national characters are garbled.

When executing the file through PythonInterpreter.execfile(), one can 
declare encoding in the file explicitly:

# coding=iso-8859-2

but that's not an option with ScriptEngine - the following error is thrown:

javax.script.ScriptException: org.python.antlr.ParseException: 
org.python.antlr.ParseException: encoding declaration in Unicode string

I sort of understand that, since the passed Reader should have already 
decoded the representation.

Sadly if I have scripts that can be invoked (also) by other scripts, 
those scripts HAVE to contain #coding directive for execfile() call in 
Python script. Given the exception above, they cannot be invoked 
directly from Java using JSR223 APIs.

How to solve that situation ? I can naturally provide a clever Reader 
that reads top of the file according to PEP-263 and then passes a Reader 
that decodes the source correctly. But that would have to be replicated 
in other apps using JSR-223 API _and_ entangles the project with 
Python-specific processing, which does not seem "right".

Is there a more appropriate solution for this issue ?

Thanks for the help,
-Svata