Skip to content

Commit 0d318d0

Browse files
committed
Check encoding comment only for files
1 parent 395ba09 commit 0d318d0

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,20 @@ private CacheItem parseWithANTLR(ParserMode mode, ParserErrorCallback errors, Py
265265
FrameDescriptor inlineLocals = mode == ParserMode.InlineEvaluation ? currentFrame.getFrameDescriptor() : null;
266266
String sourceText = source.getCharacters().toString();
267267
// Preprocessing
268-
// Check that declared encoding (if any) is valid. The file detector picks an encoding for
269-
// the file, but it doesn't have a means of communicating that the declared encoding wasn't
270-
// valid or supported, so in that case it defaults to Latin-1 and we have to recheck it here
271-
try {
272-
PythonFileDetector.findEncodingStrict(sourceText);
273-
} catch (PythonFileDetector.InvalidEncodingException e) {
274-
throw errors.raiseInvalidSyntax(source, source.createUnavailableSection(), "encoding problem: %s", e.getEncodingName());
268+
269+
// Check that declared encoding (if any) is valid. The file detector picks an encoding
270+
// for the file, but it doesn't have a means of communicating that the declared encoding
271+
// wasn't valid or supported, so in that case it defaults to Latin-1 and we have to
272+
// recheck it here.
273+
// msimacek: The encoding check should happen only when the source encoding was
274+
// determined by PythonFileDetector. But we currently have no way to tell, so we
275+
// assume that it is the case when it is a file.
276+
if (source.getURI().getScheme().equals("file")) {
277+
try {
278+
PythonFileDetector.findEncodingStrict(sourceText);
279+
} catch (PythonFileDetector.InvalidEncodingException e) {
280+
throw errors.raiseInvalidSyntax(source, source.createUnavailableSection(), "encoding problem: %s", e.getEncodingName());
281+
}
275282
}
276283
// ANTLR parsing
277284
Python3Parser parser = getPython3Parser(source, errors);

0 commit comments

Comments
 (0)