SF.net SVN: jython:[7128] trunk/jython

[email protected] Wed, 29 Sep 2010 06:29:59 +0000
Newsgroups gmane.comp.lang.jython.cvs
Message-ID <[email protected]>
Revision: 7128
          http://jython.svn.sourceforge.net/jython/?rev=7128&view=rev
Author:   otmarhumbel
Date:     2010-09-29 06:29:59 +0000 (Wed, 29 Sep 2010)

Log Message:
-----------
try to get the console encoding from java.io.Console using reflection
fixes issue #1568 (given that Jython runs on Java 6)

thanks to Regis Desgroppes for the hints

Modified Paths:
--------------
    trunk/jython/NEWS
    trunk/jython/src/org/python/core/PySystemState.java

Modified: trunk/jython/NEWS
===================================================================
--- trunk/jython/NEWS	2010-09-28 05:35:21 UTC (rev 7127)
+++ trunk/jython/NEWS	2010-09-29 06:29:59 UTC (rev 7128)
@@ -2,6 +2,7 @@
 
 Jython 2.5.2rc1
   Bugs Fixed
+    - [ 1568 ] sys.stdout.encoding returns wrong value in Windows with Jython 2.5.1 (fixed on Java 6 only)
     - [ 1647 ] zxJDBC does not handle NVARCHAR
 
 Jython 2.5.2b2

Modified: trunk/jython/src/org/python/core/PySystemState.java
===================================================================
--- trunk/jython/src/org/python/core/PySystemState.java	2010-09-28 05:35:21 UTC (rev 7127)
+++ trunk/jython/src/org/python/core/PySystemState.java	2010-09-29 06:29:59 UTC (rev 7128)
@@ -10,6 +10,7 @@
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
+import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.nio.charset.Charset;
@@ -652,12 +653,7 @@
             }
         }
         if (!registry.containsKey(PYTHON_CONSOLE_ENCODING)) {
-            String encoding;
-            try {
-                encoding = System.getProperty("file.encoding");
-            } catch (SecurityException se) {
-                encoding = null;
-            }
+            String encoding = getPlatformEncoding();
             if (encoding != null) {
                 registry.put(PYTHON_CONSOLE_ENCODING, encoding);
             }
@@ -665,7 +661,40 @@
         // Set up options from registry
         Options.setFromRegistry();
     }
+    
+    /**
+     * @return the encoding of the underlying platform; can be <code>null</code>
+     */
+    private static String getPlatformEncoding() {
+        // first try to grab the Console encoding
+        String encoding = getConsoleEncoding();
+        if (encoding == null) {
+            try {
+                encoding = System.getProperty("file.encoding");
+            } catch (SecurityException se) {
+                // ignore, can't do anything about it
+            }
+        }
+        return encoding;
+    }
 
+    /**
+     * @return the console encoding; can be <code>null</code>
+     */
+    private static String getConsoleEncoding() {
+        String encoding = null;
+        try {
+            // the Console class is only present in java 6 - have to use reflection
+            Class<?> consoleClass = Class.forName("java.io.Console");
+            Method encodingMethod = consoleClass.getDeclaredMethod("encoding");
+            encodingMethod.setAccessible(true); // private static method
+            encoding = (String)encodingMethod.invoke(consoleClass);
+        } catch (Exception e) {
+            // ignore any exception
+        }
+        return encoding;
+    }
+    
     private static void addRegistryFile(File file) {
         if (file.exists()) {
             if (!file.isDirectory()) {


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev