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,50 +260,56 @@ 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
}
267
268
}
268
269
269
- private static final String CAPI_LOAD_ERROR = "Could not load C API from %s.\n " ;
270
-
271
270
@ TruffleBoundary
272
271
private void ensureCapiWasLoaded () {
273
272
PythonContext context = getContext ();
274
273
if (!context .hasCApiContext ()) {
274
+ if (!context .getEnv ().isNativeAccessAllowed ()) {
275
+ throw raise (ImportError , ErrorMessages .NATIVE_ACCESS_NOT_ALLOWED );
276
+ }
277
+
275
278
Env env = context .getEnv ();
276
279
CompilerDirectives .transferToInterpreterAndInvalidate ();
277
280
278
281
String libPythonName = "libpython" + ExtensionSuffixesNode .getSoAbi (context );
279
282
TruffleFile homePath = env .getInternalTruffleFile (context .getCAPIHome ());
280
283
TruffleFile capiFile = homePath .resolve (libPythonName );
281
- Object capi = null ;
284
+ Object capi ;
282
285
try {
283
286
SourceBuilder capiSrcBuilder = Source .newBuilder (LLVM_LANGUAGE , capiFile );
284
287
if (!context .getLanguage ().getEngineOption (PythonOptions .ExposeInternalSources )) {
285
288
capiSrcBuilder .internal (true );
286
289
}
287
290
capi = context .getEnv ().parseInternal (capiSrcBuilder .build ()).call ();
288
291
} catch (IOException | RuntimeException e ) {
289
- LOGGER .severe (() -> String .format (CAPI_LOAD_ERROR , capiFile .getAbsoluteFile ().getPath ()));
290
- LOGGER .severe (() -> "Original error was: " + e );
291
- e .printStackTrace ();
292
- throw raise (PythonErrorType .ImportError , CAPI_LOAD_ERROR , capiFile .getAbsoluteFile ().getPath ());
292
+ logJavaException (e );
293
+ throw raise (ImportError , wrapJavaException (e ), ErrorMessages .CAPI_LOAD_ERROR , capiFile .getAbsoluteFile ().getPath ());
294
+ }
295
+ try {
296
+ // call into Python to initialize python_cext module globals
297
+ ReadAttributeFromObjectNode readNode = ReadAttributeFromObjectNode .getUncached ();
298
+ PythonModule builtinModule = context .getCore ().lookupBuiltinModule (PythonCextBuiltins .PYTHON_CEXT );
299
+
300
+ CallUnaryMethodNode callNode = CallUnaryMethodNode .getUncached ();
301
+ callNode .executeObject (null , readNode .execute (builtinModule , INITIALIZE_CAPI ), capi );
302
+ context .setCapiWasLoaded (capi );
303
+ callNode .executeObject (null , readNode .execute (builtinModule , RUN_CAPI_LOADED_HOOKS ), capi );
304
+
305
+ // initialization needs to be finished already but load memoryview
306
+ // implementation
307
+ // immediately
308
+ callNode .executeObject (null , readNode .execute (builtinModule , IMPORT_NATIVE_MEMORYVIEW ), capi );
309
+ } catch (RuntimeException e ) {
310
+ logJavaException (e );
311
+ throw raise (ImportError , wrapJavaException (e ), ErrorMessages .CAPI_LOAD_ERROR , capiFile .getAbsoluteFile ().getPath ());
293
312
}
294
- // call into Python to initialize python_cext module globals
295
- ReadAttributeFromObjectNode readNode = ReadAttributeFromObjectNode .getUncached ();
296
- PythonModule builtinModule = context .getCore ().lookupBuiltinModule (PythonCextBuiltins .PYTHON_CEXT );
297
-
298
- CallUnaryMethodNode callNode = CallUnaryMethodNode .getUncached ();
299
- callNode .executeObject (null , readNode .execute (builtinModule , INITIALIZE_CAPI ), capi );
300
- context .setCapiWasLoaded (capi );
301
- callNode .executeObject (null , readNode .execute (builtinModule , RUN_CAPI_LOADED_HOOKS ), capi );
302
-
303
- // initialization needs to be finished already but load memoryview implementation
304
- // immediately
305
- callNode .executeObject (null , readNode .execute (builtinModule , IMPORT_NATIVE_MEMORYVIEW ), capi );
306
313
}
307
314
}
308
315
@@ -318,6 +325,12 @@ private static String getJavaStacktrace(Exception e) {
318
325
return sw .toString ();
319
326
}
320
327
328
+ @ TruffleBoundary
329
+ private PBaseException wrapJavaException (Throwable e ) {
330
+ PBaseException excObject = factory ().createBaseException (SystemError , e .getMessage (), new Object [0 ]);
331
+ return ExceptionHandlingStatementNode .wrapJavaException (e , this , excObject ).getEscapedException ();
332
+ }
333
+
321
334
private SetItemNode getSetItemNode () {
322
335
if (setItemNode == null ) {
323
336
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -468,7 +481,7 @@ public Object run(PCode code, String path) {
468
481
}
469
482
}
470
483
471
- @ Builtin (name = "extension_suffixes" , minNumOfPositionalArgs = 0 )
484
+ @ Builtin (name = "extension_suffixes" )
472
485
@ GenerateNodeFactory
473
486
public abstract static class ExtensionSuffixesNode extends PythonBuiltinNode {
474
487
@ Specialization
0 commit comments