|
40 | 40 | import com.oracle.graal.python.PythonFileDetector; |
41 | 41 | import com.oracle.graal.python.PythonLanguage; |
42 | 42 | import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 43 | +import com.oracle.graal.python.builtins.objects.exception.PBaseException; |
43 | 44 | import com.oracle.graal.python.nodes.ModuleRootNode; |
44 | 45 | import com.oracle.graal.python.nodes.function.FunctionDefinitionNode; |
45 | 46 | import com.oracle.graal.python.nodes.function.GeneratorFunctionDefinitionNode; |
|
61 | 62 | import com.oracle.graal.python.runtime.PythonOptions; |
62 | 63 | import com.oracle.graal.python.runtime.PythonParser; |
63 | 64 | import com.oracle.graal.python.runtime.exception.PException; |
| 65 | +import com.oracle.graal.python.util.PythonUtils; |
64 | 66 | import com.oracle.truffle.api.CompilerDirectives; |
65 | 67 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
66 | 68 | import com.oracle.truffle.api.TruffleLanguage.Env; |
@@ -386,17 +388,35 @@ public Node parseN(ParserMode mode, int optimizeLevel, ParserErrorCallback error |
386 | 388 | } |
387 | 389 |
|
388 | 390 | 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(); |
392 | 403 | } |
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; |
395 | 417 | } |
396 | 418 | 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"; |
399 | 419 | 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); |
401 | 421 | } |
402 | 422 | } |
0 commit comments