Skip to content

Commit 4a96bfe

Browse files
committed
[GR-23269] Make test_utf8source pass
PullRequest: graalpython/1266
2 parents 95f2714 + 37799fb commit 4a96bfe

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*graalpython.lib-python.3.test.test_utf8source.BuiltinCompileTests.test_latin1
2+
*graalpython.lib-python.3.test.test_utf8source.PEP3120Test.test_badsyntax
23
*graalpython.lib-python.3.test.test_utf8source.PEP3120Test.test_pep3120

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
import java.math.BigInteger;
7474
import java.nio.charset.Charset;
75+
import java.nio.charset.CodingErrorAction;
7576
import java.util.List;
7677

7778
import com.oracle.graal.python.PythonFileDetector;
@@ -159,6 +160,7 @@
159160
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
160161
import com.oracle.graal.python.runtime.exception.PException;
161162
import com.oracle.graal.python.runtime.exception.PythonErrorType;
163+
import com.oracle.graal.python.util.CharsetMapping;
162164
import com.oracle.graal.python.util.OverflowException;
163165
import com.oracle.graal.python.util.PythonUtils;
164166
import com.oracle.graal.python.util.Supplier;
@@ -800,8 +802,9 @@ PCode compile(String expression, String filename, String mode, Object kwFlags, O
800802
@Specialization(limit = "3")
801803
PCode generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMode, Object kwFlags, Object kwDontInherit, Object kwOptimize,
802804
@Cached CastToJavaStringNode castStr,
805+
@Cached CodecsModuleBuiltins.HandleDecodingErrorNode handleDecodingErrorNode,
803806
@CachedLibrary("wSource") InteropLibrary interopLib,
804-
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
807+
@CachedLibrary(limit = "4") PythonObjectLibrary lib) {
805808
if (wSource instanceof PCode) {
806809
return (PCode) wSource;
807810
}
@@ -812,12 +815,12 @@ PCode generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMode
812815
} catch (CannotCastException e) {
813816
throw raise(TypeError, ErrorMessages.ARG_S_MUST_BE_S_NOT_P, "compile()", "mode", "str", wMode);
814817
}
815-
String source = sourceAsString(wSource, filename, interopLib, lib);
818+
String source = sourceAsString(wSource, filename, interopLib, lib, handleDecodingErrorNode);
816819
return compile(source, filename, mode, kwFlags, kwDontInherit, kwOptimize);
817820
}
818821

819822
// modeled after _Py_SourceAsString
820-
String sourceAsString(Object source, String filename, InteropLibrary interopLib, PythonObjectLibrary pyLib) {
823+
String sourceAsString(Object source, String filename, InteropLibrary interopLib, PythonObjectLibrary pyLib, CodecsModuleBuiltins.HandleDecodingErrorNode handleDecodingErrorNode) {
821824
if (interopLib.isString(source)) {
822825
try {
823826
return interopLib.asString(source);
@@ -835,27 +838,31 @@ String sourceAsString(Object source, String filename, InteropLibrary interopLib,
835838
throw CompilerDirectives.shouldNotReachHere(e);
836839
}
837840
Charset charset = PythonFileDetector.findEncodingStrict(bytes);
838-
return createString(bytes, charset);
841+
String pythonEncodingNameFromJavaName = CharsetMapping.getPythonEncodingNameFromJavaName(charset.name());
842+
CodecsModuleBuiltins.TruffleDecoder decoder = new CodecsModuleBuiltins.TruffleDecoder(pythonEncodingNameFromJavaName, charset, bytes, CodingErrorAction.REPORT);
843+
if (!decoder.decodingStep(true)) {
844+
try {
845+
handleDecodingErrorNode.execute(decoder, "strict", source);
846+
throw CompilerDirectives.shouldNotReachHere();
847+
} catch (PException e) {
848+
throw raiseInvalidSyntax(filename, "(unicode error) %s", pyLib.asPString(e.getEscapedException()));
849+
}
850+
}
851+
return decoder.getString();
839852
} catch (PythonFileDetector.InvalidEncodingException e) {
840-
throw handleInvalidEncoding(filename, e);
853+
throw raiseInvalidSyntax(filename, "encoding problem: %s", e.getEncodingName());
841854
}
842855
} else {
843856
throw raise(TypeError, ErrorMessages.ARG_D_MUST_BE_S, "compile()", 1, "string, bytes or AST object");
844857
}
845858
}
846859

847860
@TruffleBoundary
848-
private RuntimeException handleInvalidEncoding(String filename, PythonFileDetector.InvalidEncodingException e) {
861+
private RuntimeException raiseInvalidSyntax(String filename, String format, Object... args) {
849862
PythonContext context = getContext();
850863
// Create non-empty source to avoid overwriting the message with "unexpected EOF"
851864
Source source = PythonLanguage.newSource(context, " ", filename, mayBeFromFile);
852-
throw getCore().raiseInvalidSyntax(source, source.createUnavailableSection(), "encoding problem: %s", e.getEncodingName());
853-
}
854-
855-
@TruffleBoundary
856-
private static String createString(byte[] bytes, Charset charset) {
857-
return new String(bytes, charset);
858-
865+
throw getCore().raiseInvalidSyntax(source, source.createUnavailableSection(), format, args);
859866
}
860867

861868
public static CompileNode create(boolean mapFilenameToUri) {

0 commit comments

Comments
 (0)