Permalink
Browse files

proposed fix for issue 2659

  • Loading branch information...
ohumbel committed Mar 27, 2018
1 parent 997e320 commit 8fdf2c762aa054b7525eaaaf853b4e6a7bd69134
Showing with 60 additions and 27 deletions.
  1. +2 −27 src/org/python/core/PySystemState.java
  2. +58 −0 src/org/python/util/ConsoleEncoding.java
@@ -43,6 +43,7 @@
import org.python.expose.ExposedGet;
import org.python.expose.ExposedType;
import org.python.modules.Setup;
import org.python.util.ConsoleEncoding;
import org.python.util.Generic;
/**
@@ -901,33 +902,7 @@ private static void initRegistry(Properties preProperties, Properties postProper
* @return the encoding of the underlying platform
*/
private static String getPlatformEncoding() {
// first try to grab the Console encoding
String encoding = getConsoleEncoding();
if (encoding == null) {
try {
// Not quite the console encoding (differs on Windows)
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 {
Class<java.io.Console> consoleClass = java.io.Console.class;
Method encodingMethod = consoleClass.getDeclaredMethod("encoding");
encodingMethod.setAccessible(true); // private static method
encoding = (String)encodingMethod.invoke(consoleClass);
} catch (Exception e) {
// ignore any exception
}
return encoding;
return ConsoleEncoding.get();
}
/**
@@ -0,0 +1,58 @@
package org.python.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jnr.posix.util.Platform;
public final class ConsoleEncoding {
private static final Pattern DIGITS_PATTERN = Pattern.compile("[1-9][0-9]+");
/**
* Returns the actual console encoding
* <p>
* On Windows, this is determined with the command {@code chcp}, for all other platforms it is the system property {@code file.encoding}.
*
* @return the console encoding
*/
public static String get() {
if (Platform.IS_WINDOWS) {
return getWindowsConsoleEncoding();
} else {
return System.getProperty("file.encoding");
}
}
private static String getWindowsConsoleEncoding() {
String encoding = "cp1252"; // hopefully a reasonable a priori value
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "chcp");
try {
Process process = pb.start();
process.waitFor();
int maxSize = 120;
byte[] bytes = new byte[maxSize];
try (InputStream inputStream = process.getInputStream()) {
int b;
int i = 0;
while ((b = inputStream.read()) >= 0 && i < maxSize) {
bytes[i] = (byte) b;
i++;
}
}
String output = new String(bytes); // default encoding ok (only digits needed)
Matcher matcher = DIGITS_PATTERN.matcher(output);
if (matcher.find()) {
encoding = "cp".concat(output.substring(matcher.start(), matcher.end()));
}
} catch (IOException e) {
// no action - return the a priori value
} catch (InterruptedException e) {
// no action - return the a priori value
}
return encoding;
}
}

0 comments on commit 8fdf2c7

Please sign in to comment.