Skip to content
Open
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
23 changes: 12 additions & 11 deletions src/org/python/core/ParserFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -335,19 +336,19 @@ private static ExpectedEncodingBufferedReader prepBufReader(InputStream input,
}

Charset cs;
try {
// Use ascii for the raw bytes when no encoding was specified
if (encoding == null) {
if (fromString) {
cs = Charset.forName("ISO-8859-1");
} else {
cs = Charset.forName("ascii");
}
// Use ascii for the raw bytes when no encoding was specified
if (encoding == null) {
if (fromString) {
cs = StandardCharsets.ISO_8859_1;
} else {
cs = StandardCharsets.US_ASCII;
}
} else {
try {
cs = Charset.forName(encoding);
} catch (UnsupportedCharsetException exc) {
throw new PySyntaxError("Unknown encoding: " + encoding, 1, 0, "", filename);
}
} catch (UnsupportedCharsetException exc) {
throw new PySyntaxError("Unknown encoding: " + encoding, 1, 0, "", filename);
}
CharsetDecoder dec = cs.newDecoder();
dec.onMalformedInput(CodingErrorAction.REPORT);
Expand Down Expand Up @@ -398,7 +399,7 @@ private static boolean adjustForBOM(InputStream stream) throws IOException {
private static String readEncoding(InputStream stream) throws IOException {
stream.mark(MARK_LIMIT);
String encoding = null;
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "ISO-8859-1"), 512);
BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.ISO_8859_1), 512);
encoding = findEncoding(br);
// XXX: reset() can still raise an IOException if a line exceeds our large mark
// limit
Expand Down
4 changes: 2 additions & 2 deletions src/org/python/core/PySystemState.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -1196,7 +1196,7 @@ private static boolean initialize(Properties pre, Properties post, String[] argv
"'" + INITIALIZER_SERVICE + "' not found on " + initializerClassLoader);
return false;
}
BufferedReader r = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
String className;
try {
className = r.readLine();
Expand Down
4 changes: 2 additions & 2 deletions src/org/python/core/codecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package org.python.core;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;

Expand Down Expand Up @@ -1136,7 +1136,7 @@ public static String PyUnicode_DecodeUTF8Stateful(String str, String errors, int
}

public static String PyUnicode_EncodeUTF8(String str, String errors) {
return StringUtil.fromBytes(Charset.forName("UTF-8").encode(str));
return StringUtil.fromBytes(StandardCharsets.UTF_8.encode(str));
}

/* --- ASCII and Latin-1 Codecs --------------------------------------- */
Expand Down
8 changes: 2 additions & 6 deletions src/org/python/core/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import org.python.core.BaseBytes;
import org.python.core.Py;
Expand All @@ -22,12 +23,7 @@ public class StringUtil {
* @return a byte array with one byte for each char in string
*/
public static byte[] toBytes(String string) {
try {
return string.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException uee) {
// This JVM is whacked, it doesn't even have iso-8859-1
throw Py.SystemError("Java couldn't find the ISO-8859-1 encoding");
}
return string.getBytes(StandardCharsets.ISO_8859_1);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/org/python/indexer/AstCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -117,7 +118,7 @@ public NModule getAST(String path, String contents) throws Exception {
try {
mod = parse(path, contents);
if (mod != null) {
mod.setFileAndMD5(path, Util.getMD5(contents.getBytes("UTF-8")));
mod.setFileAndMD5(path, Util.getMD5(contents.getBytes(StandardCharsets.UTF_8)));
}
} finally {
cache.put(path, mod); // may be null
Expand Down
3 changes: 2 additions & 1 deletion src/org/python/indexer/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Collection;
import java.util.Set;
Expand Down Expand Up @@ -141,7 +142,7 @@ public static String readFile(String filename) throws Exception {
public static String readFile(File path) throws Exception {
// Don't use line-oriented file read -- need to retain CRLF if present
// so the style-run and link offsets are correct.
return new String(getBytesFromFile(path), UTF_8);
return new String(getBytesFromFile(path), StandardCharsets.UTF_8);
}

public static byte[] getBytesFromFile(File file) throws IOException {
Expand Down
7 changes: 4 additions & 3 deletions src/org/python/modules/_codecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

import org.python.core.Py;
Expand Down Expand Up @@ -679,11 +680,11 @@ public static PyTuple utf_16_be_encode(String str, String errors) {
public static String encode_UTF16(String str, String errors, int byteorder) {
final Charset utf16;
if (byteorder == 0) {
utf16 = Charset.forName("UTF-16");
utf16 = StandardCharsets.UTF_16;
} else if (byteorder == -1) {
utf16 = Charset.forName("UTF-16LE");
utf16 = StandardCharsets.UTF_16LE;
} else {
utf16 = Charset.forName("UTF-16BE");
utf16 = StandardCharsets.UTF_16BE;
}

// XXX errors argument ignored: Java's codecs implement "replace"
Expand Down
8 changes: 2 additions & 6 deletions tests/java/javatests/Issue1972.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -459,12 +460,7 @@ private void checkFromProc(String message, boolean addSeparator, LineQueue queue

if (addSeparator) {
// Each expected string must be treated as if the lineSeparator were appended
String escapedSeparator = "";
try {
escapedSeparator = escape(lineSeparator.getBytes("US-ASCII"));
} catch (UnsupportedEncodingException e) {
fail("Could not encode line separator as ASCII"); // Never happens
}
String escapedSeparator = escape(lineSeparator.getBytes(StandardCharsets.US_ASCII));
// ... so append one
for (int i = 0; i < expected.length; i++) {
expected[i] += escapedSeparator;
Expand Down