Skip to content

Commit 289ec81

Browse files
committed
ModuleBuiltins: add __dict__ builtin
1 parent 5cf929c commit 289ec81

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/ModuleBuiltins.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.module;
4242

43+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__;
4344
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__;
4445
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__LOADER__;
4546
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
@@ -57,7 +58,11 @@
5758
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5859
import com.oracle.graal.python.builtins.PythonBuiltins;
5960
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;
6064
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins;
65+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6166
import com.oracle.graal.python.nodes.ErrorMessages;
6267
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
6368
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
@@ -71,12 +76,16 @@
7176
import com.oracle.graal.python.nodes.util.CannotCastException;
7277
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
7378
import com.oracle.graal.python.runtime.exception.PException;
79+
import com.oracle.truffle.api.CompilerDirectives;
7480
import com.oracle.truffle.api.dsl.Cached;
81+
import com.oracle.truffle.api.dsl.Fallback;
7582
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7683
import com.oracle.truffle.api.dsl.NodeFactory;
7784
import com.oracle.truffle.api.dsl.Specialization;
7885
import com.oracle.truffle.api.dsl.TypeSystemReference;
7986
import com.oracle.truffle.api.frame.VirtualFrame;
87+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
88+
import com.oracle.truffle.api.library.CachedLibrary;
8089
import com.oracle.truffle.api.profiles.ConditionProfile;
8190

8291
@CoreFunctions(extendClasses = PythonBuiltinClassType.PythonModule)
@@ -91,13 +100,25 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
91100
@GenerateNodeFactory
92101
@TypeSystemReference(PythonArithmeticTypes.class)
93102
public abstract static class ModuleNode extends PythonBuiltinNode {
94-
@Specialization
103+
@Specialization(limit = "1")
95104
public PNone module(PythonModule self, String name, Object doc,
96105
@Cached WriteAttributeToObjectNode writeName,
97106
@Cached WriteAttributeToObjectNode writeDoc,
98107
@Cached WriteAttributeToObjectNode writePackage,
99108
@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
101122
writeName.execute(self, __NAME__, name);
102123
if (doc != PNone.NO_VALUE) {
103124
writeDoc.execute(self, __DOC__, doc);
@@ -111,6 +132,47 @@ public PNone module(PythonModule self, String name, Object doc,
111132
}
112133
}
113134

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+
114176
@Builtin(name = __GETATTRIBUTE__, minNumOfPositionalArgs = 2)
115177
@GenerateNodeFactory
116178
public abstract static class ModuleGetattritbuteNode extends PythonBinaryBuiltinNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/PythonModule.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333

3434
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3535
import com.oracle.graal.python.builtins.objects.PNone;
36+
import com.oracle.graal.python.builtins.objects.dict.PDict;
3637
import com.oracle.graal.python.builtins.objects.object.PythonObject;
38+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
39+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
40+
import com.oracle.truffle.api.CompilerDirectives;
41+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
3742
import com.oracle.truffle.api.object.DynamicObject;
3843

3944
public final class PythonModule extends PythonObject {
@@ -54,7 +59,14 @@ private PythonModule(String moduleName) {
5459
* Only to be used during context creation
5560
*/
5661
public static PythonModule createInternal(String moduleName) {
57-
return new PythonModule(moduleName);
62+
PythonModule pythonModule = new PythonModule(moduleName);
63+
PDict dict = PythonObjectFactory.getUncached().createDictFixedStorage(pythonModule);
64+
try {
65+
PythonObjectLibrary.getUncached().setDict(pythonModule, dict);
66+
} catch (UnsupportedMessageException e) {
67+
throw CompilerDirectives.shouldNotReachHere("BuiltinModule: could not set __dict__");
68+
}
69+
return pythonModule;
5870
}
5971

6072
@Override

0 commit comments

Comments
 (0)