Skip to content

Commit 3b90879

Browse files
committed
Fix: also pass exc message args
1 parent b54d53d commit 3b90879

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/PBaseException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
5959
import com.oracle.graal.python.runtime.sequence.storage.BasicSequenceStorage;
6060
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
61+
import com.oracle.graal.python.util.PythonUtils;
6162
import com.oracle.truffle.api.CompilerAsserts;
6263
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6364
import com.oracle.truffle.api.dsl.Cached;
@@ -193,7 +194,7 @@ public boolean hasMessageFormat() {
193194

194195
public Object[] getMessageArgs() {
195196
// clone message args to ensure that they stay unmodified
196-
return messageArgs.clone();
197+
return messageArgs != null ? messageArgs.clone() : PythonUtils.EMPTY_OBJECT_ARRAY;
197198
}
198199

199200
@TruffleBoundary

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.oracle.graal.python.PythonFileDetector;
4141
import com.oracle.graal.python.PythonLanguage;
4242
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
43+
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
4344
import com.oracle.graal.python.nodes.ModuleRootNode;
4445
import com.oracle.graal.python.nodes.function.FunctionDefinitionNode;
4546
import com.oracle.graal.python.nodes.function.GeneratorFunctionDefinitionNode;
@@ -61,6 +62,7 @@
6162
import com.oracle.graal.python.runtime.PythonOptions;
6263
import com.oracle.graal.python.runtime.PythonParser;
6364
import com.oracle.graal.python.runtime.exception.PException;
65+
import com.oracle.graal.python.util.PythonUtils;
6466
import com.oracle.truffle.api.CompilerDirectives;
6567
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6668
import com.oracle.truffle.api.TruffleLanguage.Env;
@@ -386,17 +388,35 @@ public Node parseN(ParserMode mode, int optimizeLevel, ParserErrorCallback error
386388
}
387389

388390
private static PException handleParserError(ParserErrorCallback errors, Source source, Exception e) {
389-
try {
390-
if (e instanceof PException && InteropLibrary.getUncached().getExceptionType(e) == ExceptionType.PARSE_ERROR) {
391-
throw (PException) e;
391+
String message = null;
392+
Object[] messageArgs = PythonUtils.EMPTY_OBJECT_ARRAY;
393+
if (e instanceof PException) {
394+
PException pException = (PException) e;
395+
try {
396+
InteropLibrary uncached = InteropLibrary.getUncached(pException);
397+
// If the exception is already a syntax error then just re-throw.
398+
if (uncached.getExceptionType(pException) == ExceptionType.PARSE_ERROR) {
399+
throw pException;
400+
}
401+
} catch (UnsupportedMessageException unsupportedMessageException) {
402+
throw CompilerDirectives.shouldNotReachHere();
392403
}
393-
} catch (UnsupportedMessageException unsupportedMessageException) {
394-
throw CompilerDirectives.shouldNotReachHere();
404+
/*
405+
* If we got a Python exception but it's not a syntax error then we need to pass the
406+
* message format AND the message arguments. Also, there is no need to reify the Python
407+
* exception since we just drop it.
408+
*/
409+
PBaseException unreifiedException = pException.getUnreifiedException();
410+
if (unreifiedException.hasMessageFormat()) {
411+
message = unreifiedException.getMessageFormat();
412+
messageArgs = unreifiedException.getMessageArgs();
413+
}
414+
} else {
415+
// from parser we are getting RuntimeExceptions
416+
message = e instanceof RuntimeException ? e.getMessage() : null;
395417
}
396418
SourceSection section = PythonErrorStrategy.getPosition(source, e);
397-
// from parser we are getting RuntimeExceptions
398-
String message = e instanceof RuntimeException && e.getMessage() != null ? e.getMessage() : "invalid syntax";
399419
ErrorType errorType = PythonErrorStrategy.getErrorType(e, section);
400-
throw errors.raiseInvalidSyntax(errorType, source, section, message);
420+
throw errors.raiseInvalidSyntax(errorType, source, section, message != null ? message : "invalid syntax", messageArgs);
401421
}
402422
}

0 commit comments

Comments
 (0)