48
48
import static com .oracle .graal .python .nodes .SpecialMethodNames .__GETATTRIBUTE__ ;
49
49
import static com .oracle .graal .python .nodes .SpecialMethodNames .__GETATTR__ ;
50
50
import static com .oracle .graal .python .nodes .SpecialMethodNames .__INIT__ ;
51
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
51
52
import static com .oracle .graal .python .runtime .exception .PythonErrorType .AttributeError ;
52
53
53
54
import java .util .List ;
54
55
56
+ import com .oracle .graal .python .PythonLanguage ;
55
57
import com .oracle .graal .python .builtins .Builtin ;
56
58
import com .oracle .graal .python .builtins .CoreFunctions ;
57
59
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
66
68
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
67
69
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
68
70
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
71
+ import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
69
72
import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
70
73
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
71
74
import com .oracle .graal .python .nodes .util .CannotCastException ;
72
75
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
76
+ import com .oracle .graal .python .runtime .PythonContext ;
73
77
import com .oracle .graal .python .runtime .exception .PException ;
74
78
import com .oracle .truffle .api .dsl .Cached ;
79
+ import com .oracle .truffle .api .dsl .CachedContext ;
75
80
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
76
81
import com .oracle .truffle .api .dsl .NodeFactory ;
77
82
import com .oracle .truffle .api .dsl .Specialization ;
78
83
import com .oracle .truffle .api .dsl .TypeSystemReference ;
79
84
import com .oracle .truffle .api .frame .VirtualFrame ;
85
+ import com .oracle .truffle .api .interop .ArityException ;
86
+ import com .oracle .truffle .api .interop .InteropLibrary ;
87
+ import com .oracle .truffle .api .interop .UnknownIdentifierException ;
88
+ import com .oracle .truffle .api .interop .UnsupportedMessageException ;
89
+ import com .oracle .truffle .api .interop .UnsupportedTypeException ;
90
+ import com .oracle .truffle .api .library .CachedLibrary ;
80
91
import com .oracle .truffle .api .profiles .ConditionProfile ;
81
92
82
93
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PythonModule )
@@ -87,6 +98,30 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
87
98
return ModuleBuiltinsFactory .getFactories ();
88
99
}
89
100
101
+ @ Builtin (name = __REPR__ , minNumOfPositionalArgs = 1 , declaresExplicitSelf = true )
102
+ @ GenerateNodeFactory
103
+ public abstract static class ModuleReprNode extends PythonUnaryBuiltinNode {
104
+ @ Specialization
105
+ public Object repr (PythonModule self ,
106
+ @ CachedLibrary (limit = "1" ) InteropLibrary lib ,
107
+ @ CachedContext (PythonLanguage .class ) PythonContext context ) {
108
+ // PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
109
+ PythonModule builtins = context .getCore ().getBuiltins ();
110
+ try {
111
+ Object __import__ = lib .readMember (builtins , "__import__" );
112
+ Object module_repr = importFrom ( __import__ , "importlib._bootstrap" , "_module_repr" , lib );
113
+ return lib .execute (module_repr , self );
114
+ } catch (UnknownIdentifierException | UnsupportedMessageException | UnsupportedTypeException | ArityException e ) {
115
+ return self .toString ();
116
+ }
117
+ }
118
+
119
+ private Object importFrom (Object importBuiltinFunction , String moduleName , String from , InteropLibrary lib ) throws UnsupportedTypeException , ArityException , UnsupportedMessageException , UnknownIdentifierException {
120
+ Object _bootstrap = lib .execute (importBuiltinFunction , moduleName , PNone .NONE , PNone .NONE , factory ().createList (new Object []{from }));
121
+ return lib .readMember (_bootstrap , from );
122
+ }
123
+ }
124
+
90
125
@ Builtin (name = __INIT__ , minNumOfPositionalArgs = 2 , declaresExplicitSelf = true , parameterNames = {"self" , "name" , "doc" })
91
126
@ GenerateNodeFactory
92
127
@ TypeSystemReference (PythonArithmeticTypes .class )
0 commit comments