77
77
import com .oracle .graal .python .builtins .CoreFunctions ;
78
78
import com .oracle .graal .python .builtins .Python3Core ;
79
79
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
80
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .NotImplementedError ;
80
81
import com .oracle .graal .python .builtins .PythonBuiltins ;
81
82
import com .oracle .graal .python .builtins .modules .PythonCextBuiltinsFactory .CreateFunctionNodeGen ;
82
83
import com .oracle .graal .python .builtins .objects .PNone ;
183
184
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .GetItemScalarNode ;
184
185
import com .oracle .graal .python .builtins .objects .dict .DictBuiltins ;
185
186
import com .oracle .graal .python .builtins .objects .dict .PDict ;
187
+ import com .oracle .graal .python .builtins .objects .ellipsis .PEllipsis ;
186
188
import com .oracle .graal .python .builtins .objects .exception .PBaseException ;
187
189
import com .oracle .graal .python .builtins .objects .frame .PFrame ;
188
190
import com .oracle .graal .python .builtins .objects .frame .PFrame .Reference ;
268
270
import com .oracle .graal .python .nodes .function .builtins .PythonQuaternaryBuiltinNode ;
269
271
import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
270
272
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
273
+ import com .oracle .graal .python .nodes .function .builtins .PythonUnaryClinicBuiltinNode ;
271
274
import com .oracle .graal .python .nodes .function .builtins .PythonVarargsBuiltinNode ;
272
275
import com .oracle .graal .python .nodes .function .builtins .clinic .ArgumentClinicProvider ;
273
276
import com .oracle .graal .python .nodes .object .GetClassNode ;
@@ -342,6 +345,8 @@ public class PythonCextBuiltins extends PythonBuiltins {
342
345
private static final String ERROR_HANDLER = "error_handler" ;
343
346
public static final String NATIVE_NULL = "native_null" ;
344
347
348
+ private PythonObject errorHandler ;
349
+
345
350
@ Override
346
351
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
347
352
return PythonCextBuiltinsFactory .getFactories ();
@@ -353,7 +358,8 @@ public void initialize(Python3Core core) {
353
358
PythonClass errorHandlerClass = core .factory ().createPythonClassAndFixupSlots (core .getLanguage (), PythonBuiltinClassType .PythonClass ,
354
359
"CErrorHandler" , new PythonAbstractClass []{core .lookupType (PythonBuiltinClassType .PythonObject )});
355
360
builtinConstants .put ("CErrorHandler" , errorHandlerClass );
356
- builtinConstants .put (ERROR_HANDLER , core .factory ().createPythonObject (errorHandlerClass ));
361
+ errorHandler = core .factory ().createPythonObject (errorHandlerClass );
362
+ builtinConstants .put (ERROR_HANDLER , errorHandler );
357
363
builtinConstants .put (NATIVE_NULL , new PythonNativeNull ());
358
364
builtinConstants .put ("PyEval_SaveThread" , new PyEvalSaveThread ());
359
365
builtinConstants .put ("PyEval_RestoreThread" , new PyEvalRestoreThread ());
@@ -418,6 +424,89 @@ Object run(VirtualFrame frame, Object o) {
418
424
}
419
425
}
420
426
427
+ @ Builtin (name = "Py_ErrorHandler" , minNumOfPositionalArgs = 1 , declaresExplicitSelf = true )
428
+ @ GenerateNodeFactory
429
+ public abstract static class PyErrorHandlerNode extends PythonUnaryBuiltinNode {
430
+ @ Specialization
431
+ Object run (PythonModule cextPython ) {
432
+ return ((PythonCextBuiltins ) cextPython .getBuiltins ()).errorHandler ;
433
+ }
434
+ }
435
+
436
+ @ Builtin (name = "Py_NotImplemented" )
437
+ @ GenerateNodeFactory
438
+ public abstract static class PyNotImplementedNode extends PythonBuiltinNode {
439
+ @ Specialization
440
+ Object run () {
441
+ return NotImplementedError ;
442
+ }
443
+ }
444
+
445
+ @ Builtin (name = "Py_True" )
446
+ @ GenerateNodeFactory
447
+ public abstract static class PyTrueNode extends PythonBuiltinNode {
448
+ @ Specialization
449
+ Object run () {
450
+ return true ;
451
+ }
452
+ }
453
+
454
+ @ Builtin (name = "Py_False" )
455
+ @ GenerateNodeFactory
456
+ public abstract static class PyFalseNode extends PythonBuiltinNode {
457
+ @ Specialization
458
+ Object run () {
459
+ return false ;
460
+ }
461
+ }
462
+
463
+ @ Builtin (name = "Py_Ellipsis" )
464
+ @ GenerateNodeFactory
465
+ public abstract static class PyEllipsisNode extends PythonBuiltinNode {
466
+ @ Specialization
467
+ Object run () {
468
+ return PEllipsis .INSTANCE ;
469
+ }
470
+ }
471
+
472
+ @ Builtin (name = "PyModule_SetDocString" , minNumOfPositionalArgs = 2 )
473
+ @ GenerateNodeFactory
474
+ public abstract static class SetDocStringNode extends PythonBinaryBuiltinNode {
475
+ @ Specialization
476
+ Object run (VirtualFrame frame , PythonModule module , Object doc ,
477
+ @ Cached ObjectBuiltins .SetattrNode setattrNode ) {
478
+ setattrNode .execute (frame , module , __DOC__ , doc );
479
+ return PNone .NONE ;
480
+ }
481
+ }
482
+
483
+ @ Builtin (name = "PyModule_NewObject" , minNumOfPositionalArgs = 1 , parameterNames = {"name" })
484
+ @ ArgumentClinic (name = "name" , conversion = ClinicConversion .String )
485
+ @ GenerateNodeFactory
486
+ public abstract static class PyModuleNewObjectNode extends PythonUnaryClinicBuiltinNode {
487
+
488
+ @ Override
489
+ protected ArgumentClinicProvider getArgumentClinic () {
490
+ return PythonCextBuiltinsClinicProviders .PyModuleNewObjectNodeClinicProviderGen .INSTANCE ;
491
+ }
492
+
493
+ @ Specialization
494
+ Object run (String name ) {
495
+ return newModule (name , getCore (), factory ());
496
+ }
497
+ }
498
+
499
+ private static PythonModule newModule (String name , Python3Core core , PythonObjectFactory factory ) {
500
+ PythonModule sysModule = core .lookupBuiltinModule ("sys" );
501
+ PDict sysModules = (PDict ) sysModule .getAttribute ("modules" );
502
+ PythonModule newModule = (PythonModule ) sysModules .getItem (name );
503
+ if (newModule == null ) {
504
+ newModule = factory .createPythonModule (name );
505
+ sysModules .setItem (name , newModule );
506
+ }
507
+ return newModule ;
508
+ }
509
+
421
510
/**
422
511
* This is used in the ExternalFunctionNode below, so all arguments passed from Python code into
423
512
* a C function are automatically unwrapped if they are wrapped. This function is also called
0 commit comments