40
40
*/
41
41
package com .oracle .graal .python .builtins .objects .module ;
42
42
43
+ import static com .oracle .graal .python .nodes .SpecialAttributeNames .__DICT__ ;
43
44
import static com .oracle .graal .python .nodes .SpecialAttributeNames .__DOC__ ;
44
45
import static com .oracle .graal .python .nodes .SpecialAttributeNames .__LOADER__ ;
45
46
import static com .oracle .graal .python .nodes .SpecialAttributeNames .__NAME__ ;
57
58
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
58
59
import com .oracle .graal .python .builtins .PythonBuiltins ;
59
60
import com .oracle .graal .python .builtins .objects .PNone ;
61
+ import com .oracle .graal .python .builtins .objects .cext .PythonAbstractNativeObject ;
62
+ import com .oracle .graal .python .builtins .objects .common .PHashingCollection ;
63
+ import com .oracle .graal .python .builtins .objects .dict .PDict ;
60
64
import com .oracle .graal .python .builtins .objects .object .ObjectBuiltins ;
65
+ import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
61
66
import com .oracle .graal .python .nodes .ErrorMessages ;
62
67
import com .oracle .graal .python .nodes .attributes .ReadAttributeFromObjectNode ;
63
68
import com .oracle .graal .python .nodes .attributes .WriteAttributeToObjectNode ;
71
76
import com .oracle .graal .python .nodes .util .CannotCastException ;
72
77
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
73
78
import com .oracle .graal .python .runtime .exception .PException ;
79
+ import com .oracle .truffle .api .CompilerDirectives ;
74
80
import com .oracle .truffle .api .dsl .Cached ;
81
+ import com .oracle .truffle .api .dsl .Fallback ;
75
82
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
76
83
import com .oracle .truffle .api .dsl .NodeFactory ;
77
84
import com .oracle .truffle .api .dsl .Specialization ;
78
85
import com .oracle .truffle .api .dsl .TypeSystemReference ;
79
86
import com .oracle .truffle .api .frame .VirtualFrame ;
87
+ import com .oracle .truffle .api .interop .UnsupportedMessageException ;
88
+ import com .oracle .truffle .api .library .CachedLibrary ;
80
89
import com .oracle .truffle .api .profiles .ConditionProfile ;
81
90
82
91
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PythonModule )
@@ -91,13 +100,25 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
91
100
@ GenerateNodeFactory
92
101
@ TypeSystemReference (PythonArithmeticTypes .class )
93
102
public abstract static class ModuleNode extends PythonBuiltinNode {
94
- @ Specialization
103
+ @ Specialization ( limit = "1" )
95
104
public PNone module (PythonModule self , String name , Object doc ,
96
105
@ Cached WriteAttributeToObjectNode writeName ,
97
106
@ Cached WriteAttributeToObjectNode writeDoc ,
98
107
@ Cached WriteAttributeToObjectNode writePackage ,
99
108
@ Cached WriteAttributeToObjectNode writeLoader ,
100
- @ Cached WriteAttributeToObjectNode writeSpec ) {
109
+ @ Cached WriteAttributeToObjectNode writeSpec ,
110
+ @ CachedLibrary ("self" ) PythonObjectLibrary lib ) {
111
+ // create dict if missing
112
+ if (lib .getDict (self ) == null ) {
113
+ try {
114
+ lib .setDict (self , factory ().createDictFixedStorage (self ));
115
+ } catch (UnsupportedMessageException e ) {
116
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
117
+ throw new IllegalStateException (e );
118
+ }
119
+ }
120
+
121
+ // init
101
122
writeName .execute (self , __NAME__ , name );
102
123
if (doc != PNone .NO_VALUE ) {
103
124
writeDoc .execute (self , __DOC__ , doc );
@@ -111,6 +132,47 @@ public PNone module(PythonModule self, String name, Object doc,
111
132
}
112
133
}
113
134
135
+ @ Builtin (name = __DICT__ , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , isGetter = true , isSetter = true )
136
+ @ GenerateNodeFactory
137
+ public abstract static class ModuleDictNode extends PythonBinaryBuiltinNode {
138
+ @ Specialization (guards = {"isNoValue(none)" }, limit = "1" )
139
+ Object dict (PythonModule self , @ SuppressWarnings ("unused" ) PNone none ,
140
+ @ CachedLibrary ("self" ) PythonObjectLibrary lib ) {
141
+ PHashingCollection dict = lib .getDict (self );
142
+ if (dict == null ) {
143
+ return PNone .NONE ;
144
+ }
145
+ return dict ;
146
+ }
147
+
148
+ @ Specialization (limit = "1" )
149
+ Object dict (PythonModule self , PDict dict ,
150
+ @ CachedLibrary ("self" ) PythonObjectLibrary lib ) {
151
+ try {
152
+ lib .setDict (self , dict );
153
+ } catch (UnsupportedMessageException e ) {
154
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
155
+ throw new IllegalStateException (e );
156
+ }
157
+ return PNone .NONE ;
158
+ }
159
+
160
+ @ Specialization (guards = "isNoValue(none)" , limit = "1" )
161
+ Object dict (PythonAbstractNativeObject self , @ SuppressWarnings ("unused" ) PNone none ,
162
+ @ CachedLibrary ("self" ) PythonObjectLibrary lib ) {
163
+ PHashingCollection dict = lib .getDict (self );
164
+ if (dict == null ) {
165
+ raise (self , none );
166
+ }
167
+ return dict ;
168
+ }
169
+
170
+ @ Fallback
171
+ Object raise (Object self , @ SuppressWarnings ("unused" ) Object dict ) {
172
+ throw raise (PythonBuiltinClassType .TypeError , "descriptor '__dict__' for 'module' objects doesn't apply to a '%p' object" , self );
173
+ }
174
+ }
175
+
114
176
@ Builtin (name = __GETATTRIBUTE__ , minNumOfPositionalArgs = 2 )
115
177
@ GenerateNodeFactory
116
178
public abstract static class ModuleGetattritbuteNode extends PythonBinaryBuiltinNode {
0 commit comments