Skip to content

Commit 41e23c9

Browse files
committed
PConstructAndRaiseNode: add PBaseException cause parameter
1 parent 43ecfe3 commit 41e23c9

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PConstructAndRaiseNode.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,58 +71,78 @@ public abstract class PConstructAndRaiseNode extends Node {
7171
private static final ErrorMessageFormatter FORMATTER = new ErrorMessageFormatter();
7272

7373
public final PException executeWithArgsOnly(Frame frame, PythonBuiltinClassType type, Object[] arguments) {
74-
return execute(frame, type, null, null, arguments, PKeyword.EMPTY_KEYWORDS);
74+
return execute(frame, type, null, null, null, arguments, PKeyword.EMPTY_KEYWORDS);
75+
}
76+
77+
public final PException executeWithArgsOnly(Frame frame, PythonBuiltinClassType type, PBaseException cause, Object[] arguments) {
78+
return execute(frame, type, cause, null, null, arguments, PKeyword.EMPTY_KEYWORDS);
7579
}
7680

7781
public final PException executeWithArgsAndKwargs(Frame frame, PythonBuiltinClassType type, Object[] arguments, PKeyword[] keywords) {
78-
return execute(frame, type, null, null, arguments, keywords);
82+
return execute(frame, type, null, null, null, arguments, keywords);
83+
}
84+
85+
public final PException executeWithArgsAndKwargs(Frame frame, PythonBuiltinClassType type, PBaseException cause, Object[] arguments, PKeyword[] keywords) {
86+
return execute(frame, type, cause, null, null, arguments, keywords);
7987
}
8088

8189
public final PException executeWithFmtMessageAndKwargs(Frame frame, PythonBuiltinClassType type, String format, Object[] formatArgs, PKeyword[] keywords) {
82-
return execute(frame, type, format, formatArgs, null, keywords);
90+
return execute(frame, type, null, format, formatArgs, null, keywords);
91+
}
92+
93+
public final PException executeWithFmtMessageAndKwargs(Frame frame, PythonBuiltinClassType type, PBaseException cause, String format, Object[] formatArgs, PKeyword[] keywords) {
94+
return execute(frame, type, cause, format, formatArgs, null, keywords);
8395
}
8496

8597
public final PException executeWithFmtMessageAndArgs(Frame frame, PythonBuiltinClassType type, String format, Object[] formatArgs, Object[] arguments) {
86-
return execute(frame, type, format, formatArgs, arguments, PKeyword.EMPTY_KEYWORDS);
98+
return execute(frame, type, null, format, formatArgs, arguments, PKeyword.EMPTY_KEYWORDS);
8799
}
88100

89-
public abstract PException execute(Frame frame, PythonBuiltinClassType type, String format, Object[] formatArgs, Object[] arguments, PKeyword[] keywords);
101+
public final PException executeWithFmtMessageAndArgs(Frame frame, PythonBuiltinClassType type, PBaseException cause, String format, Object[] formatArgs, Object[] arguments) {
102+
return execute(frame, type, cause, format, formatArgs, arguments, PKeyword.EMPTY_KEYWORDS);
103+
}
104+
105+
public abstract PException execute(Frame frame, PythonBuiltinClassType type, PBaseException cause, String format, Object[] formatArgs, Object[] arguments, PKeyword[] keywords);
90106

91107
@CompilerDirectives.TruffleBoundary
92108
private static String getFormattedMessage(PythonObjectLibrary pol, String format, Object[] formatArgs) {
93109
return FORMATTER.format(pol, format, formatArgs);
94110
}
95111

96-
private PException raiseInternal(VirtualFrame frame, PythonBuiltinClassType type, Object[] arguments, PKeyword[] keywords,
112+
private PException raiseInternal(VirtualFrame frame, PythonBuiltinClassType type, PBaseException cause, Object[] arguments, PKeyword[] keywords,
97113
CallVarargsMethodNode callNode, PythonLanguage language, PythonCore core) {
98114
PBaseException error = (PBaseException) callNode.execute(frame, core.lookupType(type), arguments, keywords);
115+
if (cause != null) {
116+
error.setContext(cause);
117+
error.setCause(cause);
118+
}
99119
return PRaiseNode.raise(this, error, PythonOptions.isPExceptionWithJavaStacktrace(language));
100120
}
101121

102122
@Specialization(guards = {"format == null", "formatArgs == null"})
103-
PException constructAndRaiseNoFormatString(VirtualFrame frame, PythonBuiltinClassType type, @SuppressWarnings("unused") String format, @SuppressWarnings("unused") Object[] formatArgs,
123+
PException constructAndRaiseNoFormatString(VirtualFrame frame, PythonBuiltinClassType type, PBaseException cause, @SuppressWarnings("unused") String format, @SuppressWarnings("unused") Object[] formatArgs,
104124
Object[] arguments, PKeyword[] keywords,
105125
@Cached.Shared("callNode") @Cached CallVarargsMethodNode callNode,
106126
@CachedLanguage PythonLanguage language,
107127
@CachedContext(PythonLanguage.class) PythonContext context) {
108128
PythonCore core = context.getCore();
109-
return raiseInternal(frame, type, arguments, keywords, callNode, language, core);
129+
return raiseInternal(frame, type, cause, arguments, keywords, callNode, language, core);
110130
}
111131

112132
@Specialization(guards = {"format != null", "arguments == null"})
113-
PException constructAndRaiseNoArgs(VirtualFrame frame, PythonBuiltinClassType type, String format, Object[] formatArgs,
133+
PException constructAndRaiseNoArgs(VirtualFrame frame, PythonBuiltinClassType type, PBaseException cause, String format, Object[] formatArgs,
114134
@SuppressWarnings("unused") Object[] arguments, PKeyword[] keywords,
115135
@Cached.Shared("callNode") @Cached CallVarargsMethodNode callNode,
116136
@Cached.Shared("pol") @CachedLibrary(limit = "3") PythonObjectLibrary pol,
117137
@CachedLanguage PythonLanguage language,
118138
@CachedContext(PythonLanguage.class) PythonContext context) {
119139
PythonCore core = context.getCore();
120140
Object[] args = new Object[]{formatArgs != null ? getFormattedMessage(pol, format, formatArgs) : format};
121-
return raiseInternal(frame, type, args, keywords, callNode, language, core);
141+
return raiseInternal(frame, type, cause, args, keywords, callNode, language, core);
122142
}
123143

124144
@Specialization(guards = {"format != null", "arguments != null"})
125-
PException constructAndRaise(VirtualFrame frame, PythonBuiltinClassType type, String format, Object[] formatArgs,
145+
PException constructAndRaise(VirtualFrame frame, PythonBuiltinClassType type, PBaseException cause, String format, Object[] formatArgs,
126146
Object[] arguments, PKeyword[] keywords,
127147
@Cached.Shared("callNode") @Cached CallVarargsMethodNode callNode,
128148
@Cached.Shared("pol") @CachedLibrary(limit = "3") PythonObjectLibrary pol,
@@ -132,10 +152,14 @@ PException constructAndRaise(VirtualFrame frame, PythonBuiltinClassType type, St
132152
Object[] args = new Object[arguments.length + 1];
133153
args[0] = formatArgs != null ? getFormattedMessage(pol, format, formatArgs) : format;
134154
System.arraycopy(arguments, 0, args, 1, arguments.length);
135-
return raiseInternal(frame, type, args, keywords, callNode, language, core);
155+
return raiseInternal(frame, type,cause, args, keywords, callNode, language, core);
136156
}
137157

138158
// ImportError helpers
159+
private PException raiseImportErrorInternal(Frame frame, PBaseException cause, String format, Object[] formatArgs, PKeyword[] keywords) {
160+
return executeWithFmtMessageAndKwargs(frame, PythonBuiltinClassType.ImportError, cause, format, formatArgs, keywords);
161+
}
162+
139163
private PException raiseImportErrorInternal(Frame frame, String format, Object[] formatArgs, PKeyword[] keywords) {
140164
return executeWithFmtMessageAndKwargs(frame, PythonBuiltinClassType.ImportError, format, formatArgs, keywords);
141165
}
@@ -148,6 +172,10 @@ public final PException raiseImportError(Frame frame, String format, Object... f
148172
return raiseImportErrorInternal(frame, format, formatArgs, PKeyword.EMPTY_KEYWORDS);
149173
}
150174

175+
public final PException raiseImportError(Frame frame, PBaseException cause, Object name, Object path, String format, Object... formatArgs) {
176+
return raiseImportErrorInternal(frame, cause, format, formatArgs, new PKeyword[]{new PKeyword("name", name), new PKeyword("path", path)});
177+
}
178+
151179
// OSError helpers
152180
@CompilerDirectives.TruffleBoundary
153181
private static String getMessage(Exception exception) {

0 commit comments

Comments
 (0)