Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ACKNOWLEDGMENTS
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ In no particular order they are:
lifebarier (GitHub: lifebarier) C, 2024-03-06
Adrian Ross (GitHub: R077A6r1an) C, 2024-04-09
John Hubbard (GitHub: jhubbardbnso) C, 2024-06-06

Werner Fouché (GitHub: wfouche) C, 2025-05-27

Since about 2012 we have been asking contributors to sign the Python
Contributor Agreement. The part following names above records evidence
Expand Down
3 changes: 2 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ New Features
deprecated for removal in Java 26. See https://github.com/jnr/jffi/issues/165

Jython 2.7.5a1 Bugs fixed
- [ GH-404 ] Windows code page 65001 (UTF-8) not supported by Jython
- [ GH-384 ] Console encoding inferred incorrectly on Java 21
- [ GH-382 ] Java EE Servlet Namespace Has Been Changed From javax.servlet to jakarta.servlet
- [ GH-84 ] PyServlet Will Need To Use The jakarta.servlet Namespace #84


==============================================================================
Jython 2.7.4
==============================================================================
Expand Down
31 changes: 25 additions & 6 deletions src/org/python/core/PySystemState.java
Original file line number Diff line number Diff line change
Expand Up @@ -1022,14 +1022,29 @@ private static void initRegistry(Properties preProperties, Properties postProper
*/
private static String getConsoleEncoding(Properties props) {

// From Java 8 onwards, the answer may already be to hand in the registry:
String encoding = props.getProperty("sun.stdout.encoding");
String os = props.getProperty("os.name");
// Java 19+
Comment thread
jeff5 marked this conversation as resolved.
String encoding = props.getProperty("stdout.encoding");
if (encoding != null) {
// Windows: cp65001 is automatically mapped to UTF-8
// https://github.com/openjdk/jdk25u/blob/master/src/java.base/windows/native/libjava/java_props_md.c#L131
// C function: getConsoleEncoding()
encoding = encoding.toLowerCase();
return encoding;
}

// Java 8 to 18
Comment thread
wfouche marked this conversation as resolved.
encoding = props.getProperty("sun.stdout.encoding");
if (encoding != null) {
// Windows: some of the older versions of Java return "cp65001" for UTF-8
if (encoding.equals("cp65001")) {
encoding = "utf-8";
}
return encoding;
}

} else if (os != null && os.startsWith("Windows")) {
String os = props.getProperty("os.name");
boolean isWindows = os != null && os.startsWith("Windows");
if (isWindows) {
// Go via the Windows code page built-in command "chcp".
String output = Py.getCommandResultWindows("chcp");
/*
Expand All @@ -1039,10 +1054,14 @@ private static String getConsoleEncoding(Properties props) {
final Pattern DIGITS_PATTERN = Pattern.compile("[1-9]\\d+");
Matcher matcher = DIGITS_PATTERN.matcher(output);
if (matcher.find()) {
return "cp".concat(output.substring(matcher.start(), matcher.end()));
encoding = "cp".concat(output.substring(matcher.start(), matcher.end()));
if (encoding.equals("cp65001")) {
encoding = "utf-8";
}
return encoding;
}

} else {
// On Linux and macOS, sun.stdout.encoding and stdout.encoding are undefined for Java 8 and 11
// Try a Unix-like "locale charmap".
String output = Py.getCommandResult("locale", "charmap");
// The result of "locale charmap" is just the charmap name ~ Charset or codec name.
Expand Down
Loading