From: <pj...@us...> - 2009-05-10 00:01:11
|
Revision: 6321 http://jython.svn.sourceforge.net/jython/?rev=6321&view=rev Author: pjenvey Date: 2009-05-10 00:00:52 +0000 (Sun, 10 May 2009) Log Message: ----------- o set std{in,out,err}.encoding to the file.encoding property (which is usually accurate), and only when those are ttys fixes #1314 o force file.encoding to utf-8 on darwin instead of MacRoman. JRuby also does this, CPython uses utf-8 there too Modified Paths: -------------- trunk/jython/src/org/python/core/PySystemState.java trunk/jython/src/shell/jython Modified: trunk/jython/src/org/python/core/PySystemState.java =================================================================== --- trunk/jython/src/org/python/core/PySystemState.java 2009-05-09 22:30:55 UTC (rev 6320) +++ trunk/jython/src/org/python/core/PySystemState.java 2009-05-10 00:00:52 UTC (rev 6321) @@ -164,20 +164,20 @@ // Set up the initial standard ins and outs String mode = Options.unbuffered ? "b" : ""; int buffering = Options.unbuffered ? 0 : 1; - stdout = new PyFile(System.out, "<stdout>", "w" + mode, buffering, false); - stderr = new PyFile(System.err, "<stderr>", "w" + mode, 0, false); - stdin = new PyFile(System.in, "<stdin>", "r" + mode, buffering, false); + stdin = __stdin__ = new PyFile(System.in, "<stdin>", "r" + mode, buffering, false); + stdout = __stdout__ = new PyFile(System.out, "<stdout>", "w" + mode, buffering, false); + stderr = __stderr__ = new PyFile(System.err, "<stderr>", "w" + mode, 0, false); + if (Py.getSystemState() != null) { + // XXX: initEncoding fails without an existing sys module as it can't import + // os (for os.isatty). In that case PySystemState.doInitialize calls it for + // us. The correct fix for this is rewriting the posix/nt module portions of + // os in Java + initEncoding(); + } + __displayhook__ = new PySystemStateFunctions("displayhook", 10, 1, 1); __excepthook__ = new PySystemStateFunctions("excepthook", 30, 3, 3); - String encoding = registry.getProperty("python.console.encoding", "US-ASCII"); - ((PyFile)stdout).encoding = encoding; - ((PyFile)stderr).encoding = encoding; - ((PyFile)stdin).encoding = encoding; - __stdout__ = stdout; - __stderr__ = stderr; - __stdin__ = stdin; - if (builtins == null) { builtins = getDefaultBuiltins(); } @@ -215,6 +215,25 @@ } } + private void initEncoding() { + String encoding; + try { + encoding = System.getProperty("file.encoding"); + } catch (SecurityException se) { + encoding = null; + } + if (encoding == null) { + return; + } + + for (PyFile stdStream : new PyFile[] {(PyFile)this.stdin, (PyFile)this.stdout, + (PyFile)this.stderr}) { + if (stdStream.isatty()) { + stdStream.encoding = encoding; + } + } + } + // might be nice to have something general here, but for now these // seem to be the only values that need to be explicitly shadowed private Shadow shadowing; @@ -815,6 +834,8 @@ Py.defaultSystemState.setClassLoader(classLoader); } Py.initClassExceptions(getDefaultBuiltins()); + // defaultSystemState can't init its own encoding, see its constructor + Py.defaultSystemState.initEncoding(); // Make sure that Exception classes have been loaded new PySyntaxError("", 1, 1, "", ""); return Py.defaultSystemState; Modified: trunk/jython/src/shell/jython =================================================================== --- trunk/jython/src/shell/jython 2009-05-09 22:30:55 UTC (rev 6320) +++ trunk/jython/src/shell/jython 2009-05-10 00:00:52 UTC (rev 6321) @@ -16,7 +16,8 @@ # ----- Identify OS we are running under -------------------------------------- case "`uname`" in - CYGWIN*) cygwin=true + CYGWIN*) cygwin=true;; + Darwin) darwin=true;; esac # ----- Verify and set required environment variables ------------------------- @@ -107,6 +108,8 @@ JAVA_STACK=-Xss1024k fi +JAVA_ENCODING="" + # Split out any -J argument for passing to the JVM. # Scanning for args is aborted by '--'. while [ $# -gt 0 ] ; do @@ -127,7 +130,10 @@ echo "(Prepend -J in front of these options when using 'jython' command)" exit else - java_args=("${java_args[@]}" "${1:2}") + if [ "${val:0:16}" = "-Dfile.encoding=" ]; then + JAVA_ENCODING=$val + fi + java_args=("${java_args[@]}" "${1:2}") fi ;; # Match switches that take an argument @@ -195,6 +201,11 @@ shift done +# Force file.encoding to UTF-8 when on Mac, since Apple JDK defaults to MacRoman (JRUBY-3576) +if [[ $darwin && -z "$JAVA_ENCODING" ]]; then + java_args=("${java_args[@]}" "-Dfile.encoding=UTF-8") +fi + # Append the rest of the arguments python_args=("${python_args[@]}" "$@") This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |