40
40
*/
41
41
package com .oracle .graal .python .builtins .modules ;
42
42
43
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .SystemError ;
43
44
import static com .oracle .graal .python .nodes .SpecialAttributeNames .__FILE__ ;
44
45
import static com .oracle .graal .python .runtime .exception .PythonErrorType .ImportError ;
45
46
import static com .oracle .graal .python .runtime .exception .PythonErrorType .NotImplementedError ;
78
79
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
79
80
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
80
81
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
82
+ import com .oracle .graal .python .nodes .statement .ExceptionHandlingStatementNode ;
81
83
import com .oracle .graal .python .parser .sst .SerializationUtils ;
82
84
import com .oracle .graal .python .runtime .ExecutionContext .ForeignCallContext ;
83
85
import com .oracle .graal .python .runtime .PythonContext ;
84
86
import com .oracle .graal .python .runtime .PythonOptions ;
85
87
import com .oracle .graal .python .runtime .exception .PException ;
86
- import com .oracle .graal .python .runtime .exception .PythonErrorType ;
87
88
import com .oracle .truffle .api .CallTarget ;
88
89
import com .oracle .truffle .api .CompilerDirectives ;
89
90
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
@@ -230,17 +231,17 @@ private Object loadDynamicModuleWithSpec(String name, String path, InteropLibrar
230
231
CallTarget callTarget = env .parseInternal (Source .newBuilder (LLVM_LANGUAGE , context .getPublicTruffleFileRelaxed (path , extSuffix )).build ());
231
232
sulongLibrary = (TruffleObject ) callTarget .call ();
232
233
} catch (SecurityException | IOException e ) {
233
- LOGGER .severe (() -> String .format ("cannot load C extension '%s'" , path ));
234
234
logJavaException (e );
235
- throw raise (ImportError , ErrorMessages .CANNOT_LOAD_M , path , e );
235
+ throw raise (ImportError , wrapJavaException ( e ), ErrorMessages .CANNOT_LOAD_M , path , e );
236
236
} catch (RuntimeException e ) {
237
237
throw reportImportError (e , path );
238
238
}
239
239
TruffleObject pyinitFunc ;
240
240
try {
241
241
pyinitFunc = (TruffleObject ) interop .readMember (sulongLibrary , "PyInit_" + basename );
242
- } catch (UnknownIdentifierException | UnsupportedMessageException e1 ) {
243
- throw raise (ImportError , ErrorMessages .NO_FUNCTION_FOUND , "PyInit_" , basename , path );
242
+ } catch (UnknownIdentifierException | UnsupportedMessageException e ) {
243
+ logJavaException (e );
244
+ throw raise (ImportError , wrapJavaException (e ), ErrorMessages .NO_FUNCTION_FOUND , "PyInit_" , basename , path );
244
245
}
245
246
try {
246
247
Object nativeResult = interop .execute (pyinitFunc );
@@ -250,7 +251,7 @@ private Object loadDynamicModuleWithSpec(String name, String path, InteropLibrar
250
251
if (!(result instanceof PythonModule )) {
251
252
// PyModuleDef_Init(pyModuleDef)
252
253
// TODO: PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
253
- throw raise (PythonErrorType . NotImplementedError , "multi-phase init of extension module %s" , name );
254
+ throw raise (NotImplementedError , "multi-phase init of extension module %s" , name );
254
255
} else {
255
256
((PythonObject ) result ).setAttribute (__FILE__ , path );
256
257
// TODO: _PyImport_FixupExtensionObject(result, name, path, sys.modules)
@@ -259,8 +260,8 @@ private Object loadDynamicModuleWithSpec(String name, String path, InteropLibrar
259
260
return result ;
260
261
}
261
262
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e ) {
262
- e . printStackTrace ( );
263
- throw raise (ImportError , ErrorMessages .CANNOT_INITIALIZE_WITH , "PyInit_" , path , basename );
263
+ logJavaException ( e );
264
+ throw raise (ImportError , wrapJavaException ( e ), ErrorMessages .CANNOT_INITIALIZE_WITH , "PyInit_" , path , basename );
264
265
} catch (RuntimeException e ) {
265
266
throw reportImportError (e , path );
266
267
}
@@ -276,31 +277,35 @@ private void ensureCapiWasLoaded() {
276
277
String libPythonName = "libpython" + ExtensionSuffixesNode .getSoAbi (context );
277
278
TruffleFile homePath = env .getInternalTruffleFile (context .getCAPIHome ());
278
279
TruffleFile capiFile = homePath .resolve (libPythonName );
279
- Object capi = null ;
280
+ Object capi ;
280
281
try {
281
282
SourceBuilder capiSrcBuilder = Source .newBuilder (LLVM_LANGUAGE , capiFile );
282
283
if (!context .getLanguage ().getEngineOption (PythonOptions .ExposeInternalSources )) {
283
284
capiSrcBuilder .internal (true );
284
285
}
285
286
capi = context .getEnv ().parseInternal (capiSrcBuilder .build ()).call ();
286
287
} catch (IOException | RuntimeException e ) {
287
- LOGGER .severe (() -> String .format (ErrorMessages .CAPI_LOAD_ERROR , capiFile .getAbsoluteFile ().getPath ()));
288
- LOGGER .severe (() -> "Original error was: " + e );
289
- e .printStackTrace ();
290
- throw raise (PythonErrorType .ImportError , ErrorMessages .CAPI_LOAD_ERROR , capiFile .getAbsoluteFile ().getPath ());
288
+ logJavaException (e );
289
+ throw raise (ImportError , wrapJavaException (e ), ErrorMessages .CAPI_LOAD_ERROR , capiFile .getAbsoluteFile ().getPath ());
290
+ }
291
+ try {
292
+ // call into Python to initialize python_cext module globals
293
+ ReadAttributeFromObjectNode readNode = ReadAttributeFromObjectNode .getUncached ();
294
+ PythonModule builtinModule = context .getCore ().lookupBuiltinModule (PythonCextBuiltins .PYTHON_CEXT );
295
+
296
+ CallUnaryMethodNode callNode = CallUnaryMethodNode .getUncached ();
297
+ callNode .executeObject (null , readNode .execute (builtinModule , INITIALIZE_CAPI ), capi );
298
+ context .setCapiWasLoaded (capi );
299
+ callNode .executeObject (null , readNode .execute (builtinModule , RUN_CAPI_LOADED_HOOKS ), capi );
300
+
301
+ // initialization needs to be finished already but load memoryview
302
+ // implementation
303
+ // immediately
304
+ callNode .executeObject (null , readNode .execute (builtinModule , IMPORT_NATIVE_MEMORYVIEW ), capi );
305
+ } catch (RuntimeException e ) {
306
+ logJavaException (e );
307
+ throw raise (ImportError , wrapJavaException (e ), ErrorMessages .CAPI_LOAD_ERROR , capiFile .getAbsoluteFile ().getPath ());
291
308
}
292
- // call into Python to initialize python_cext module globals
293
- ReadAttributeFromObjectNode readNode = ReadAttributeFromObjectNode .getUncached ();
294
- PythonModule builtinModule = context .getCore ().lookupBuiltinModule (PythonCextBuiltins .PYTHON_CEXT );
295
-
296
- CallUnaryMethodNode callNode = CallUnaryMethodNode .getUncached ();
297
- callNode .executeObject (null , readNode .execute (builtinModule , INITIALIZE_CAPI ), capi );
298
- context .setCapiWasLoaded (capi );
299
- callNode .executeObject (null , readNode .execute (builtinModule , RUN_CAPI_LOADED_HOOKS ), capi );
300
-
301
- // initialization needs to be finished already but load memoryview implementation
302
- // immediately
303
- callNode .executeObject (null , readNode .execute (builtinModule , IMPORT_NATIVE_MEMORYVIEW ), capi );
304
309
}
305
310
}
306
311
@@ -316,6 +321,12 @@ private static String getJavaStacktrace(Exception e) {
316
321
return sw .toString ();
317
322
}
318
323
324
+ @ TruffleBoundary
325
+ private PBaseException wrapJavaException (Throwable e ) {
326
+ PBaseException excObject = factory ().createBaseException (SystemError , e .getMessage (), new Object [0 ]);
327
+ return ExceptionHandlingStatementNode .wrapJavaException (e , this , excObject ).getEscapedException ();
328
+ }
329
+
319
330
private SetItemNode getSetItemNode () {
320
331
if (setItemNode == null ) {
321
332
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -466,7 +477,7 @@ public Object run(PCode code, String path) {
466
477
}
467
478
}
468
479
469
- @ Builtin (name = "extension_suffixes" , minNumOfPositionalArgs = 0 )
480
+ @ Builtin (name = "extension_suffixes" )
470
481
@ GenerateNodeFactory
471
482
public abstract static class ExtensionSuffixesNode extends PythonBuiltinNode {
472
483
@ Specialization
0 commit comments