@@ -71,58 +71,78 @@ public abstract class PConstructAndRaiseNode extends Node {
71
71
private static final ErrorMessageFormatter FORMATTER = new ErrorMessageFormatter ();
72
72
73
73
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 );
75
79
}
76
80
77
81
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 );
79
87
}
80
88
81
89
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 );
83
95
}
84
96
85
97
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 );
87
99
}
88
100
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 );
90
106
91
107
@ CompilerDirectives .TruffleBoundary
92
108
private static String getFormattedMessage (PythonObjectLibrary pol , String format , Object [] formatArgs ) {
93
109
return FORMATTER .format (pol , format , formatArgs );
94
110
}
95
111
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 ,
97
113
CallVarargsMethodNode callNode , PythonLanguage language , PythonCore core ) {
98
114
PBaseException error = (PBaseException ) callNode .execute (frame , core .lookupType (type ), arguments , keywords );
115
+ if (cause != null ) {
116
+ error .setContext (cause );
117
+ error .setCause (cause );
118
+ }
99
119
return PRaiseNode .raise (this , error , PythonOptions .isPExceptionWithJavaStacktrace (language ));
100
120
}
101
121
102
122
@ 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 ,
104
124
Object [] arguments , PKeyword [] keywords ,
105
125
@ Cached .Shared ("callNode" ) @ Cached CallVarargsMethodNode callNode ,
106
126
@ CachedLanguage PythonLanguage language ,
107
127
@ CachedContext (PythonLanguage .class ) PythonContext context ) {
108
128
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 );
110
130
}
111
131
112
132
@ 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 ,
114
134
@ SuppressWarnings ("unused" ) Object [] arguments , PKeyword [] keywords ,
115
135
@ Cached .Shared ("callNode" ) @ Cached CallVarargsMethodNode callNode ,
116
136
@ Cached .Shared ("pol" ) @ CachedLibrary (limit = "3" ) PythonObjectLibrary pol ,
117
137
@ CachedLanguage PythonLanguage language ,
118
138
@ CachedContext (PythonLanguage .class ) PythonContext context ) {
119
139
PythonCore core = context .getCore ();
120
140
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 );
122
142
}
123
143
124
144
@ 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 ,
126
146
Object [] arguments , PKeyword [] keywords ,
127
147
@ Cached .Shared ("callNode" ) @ Cached CallVarargsMethodNode callNode ,
128
148
@ Cached .Shared ("pol" ) @ CachedLibrary (limit = "3" ) PythonObjectLibrary pol ,
@@ -132,10 +152,14 @@ PException constructAndRaise(VirtualFrame frame, PythonBuiltinClassType type, St
132
152
Object [] args = new Object [arguments .length + 1 ];
133
153
args [0 ] = formatArgs != null ? getFormattedMessage (pol , format , formatArgs ) : format ;
134
154
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 );
136
156
}
137
157
138
158
// 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
+
139
163
private PException raiseImportErrorInternal (Frame frame , String format , Object [] formatArgs , PKeyword [] keywords ) {
140
164
return executeWithFmtMessageAndKwargs (frame , PythonBuiltinClassType .ImportError , format , formatArgs , keywords );
141
165
}
@@ -148,6 +172,10 @@ public final PException raiseImportError(Frame frame, String format, Object... f
148
172
return raiseImportErrorInternal (frame , format , formatArgs , PKeyword .EMPTY_KEYWORDS );
149
173
}
150
174
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
+
151
179
// OSError helpers
152
180
@ CompilerDirectives .TruffleBoundary
153
181
private static String getMessage (Exception exception ) {
0 commit comments