我正在使用 org.python.util.PythonInterpreter 将Jython嵌入到Java应用程序中。我使用的是Jython版 2.5.2 。
我解释的python脚本包含UTF-8文字,所以我按照PEP-0263在脚本顶部添加了Python源代码编码:# -*- coding: utf-8 -*-
我的脚本编译如下:
String script = // load from file as a single String ...
PythonInterpreter pi = new PythonInterpreter();
PyCode code = pi.compile(script);
result = pi.eval(code);
然而,Jython抱怨以下痕迹:
[java] ... : encoding declaration in Unicode string
[java] at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:277)
[java] at org.python.core.ParserFacade.parseExpressionOrModule(ParserFacade.java:119)
[java] at org.python.util.PythonInterpreter.compile(PythonInterpreter.java:259)
[java] at org.python.util.PythonInterpreter.compile(PythonInterpreter.java:256)
[java] at org.python.util.PythonInterpreter.compile(PythonInterpreter.java:250)
Jython是否支持脚本文件中的UTF-8?
答案 0 :(得分:0)
这是一个轶事答案:如Homebrew在macOS上安装的,Jython 2.7.1 确实在非嵌入式命令行解释器中支持Python源代码编码。希望它也能得到支持。