|
100 | 100 | import com.oracle.graal.python.nodes.util.CannotCastException;
|
101 | 101 | import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
|
102 | 102 | import com.oracle.graal.python.nodes.util.SplitArgsNode;
|
| 103 | +import com.oracle.graal.python.runtime.PythonContext; |
103 | 104 | import com.oracle.truffle.api.CompilerDirectives;
|
104 | 105 | import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
105 | 106 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
106 | 107 | import com.oracle.truffle.api.dsl.Cached;
|
| 108 | +import com.oracle.truffle.api.dsl.CachedContext; |
107 | 109 | import com.oracle.truffle.api.dsl.Fallback;
|
108 | 110 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
109 | 111 | import com.oracle.truffle.api.dsl.ImportStatic;
|
@@ -138,38 +140,51 @@ Object getClass(Object self, @SuppressWarnings("unused") PNone value,
|
138 | 140 | return getClass.execute(self);
|
139 | 141 | }
|
140 | 142 |
|
141 |
| - @Specialization |
142 |
| - Object setClass(@SuppressWarnings("unused") Object self, @SuppressWarnings("unused") PythonBuiltinClass klass) { |
143 |
| - throw raise(TypeError, ErrorMessages.CLASS_ASSIGMENT_ONLY_SUPPORTED_FOR_HEAP_TYPES_OR_MODTYPE_SUBCLASSES); |
144 |
| - } |
145 |
| - |
146 | 143 | @Specialization(guards = "isNativeClass(klass)")
|
147 | 144 | Object setClass(@SuppressWarnings("unused") Object self, @SuppressWarnings("unused") Object klass) {
|
148 | 145 | throw raise(TypeError, ErrorMessages.CLASS_ASSIGMENT_ONLY_SUPPORTED_FOR_HEAP_TYPES_OR_MODTYPE_SUBCLASSES);
|
149 | 146 | }
|
150 | 147 |
|
151 |
| - @Specialization(guards = "isPythonClass(value)") |
| 148 | + @Specialization(guards = "isPythonClass(value) || isPythonBuiltinClassType(value)") |
152 | 149 | PNone setClass(VirtualFrame frame, PythonObject self, Object value,
|
153 | 150 | @CachedLibrary(limit = "2") PythonObjectLibrary lib1,
|
154 | 151 | @Cached("create()") BranchProfile errorValueBranch,
|
155 |
| - @Cached("create()") BranchProfile errorSelfBranch) { |
156 |
| - if (value instanceof PythonBuiltinClass || PGuards.isNativeClass(value)) { |
| 152 | + @Cached("create()") BranchProfile errorSelfBranch, |
| 153 | + @CachedContext(PythonLanguage.class) PythonContext ctx) { |
| 154 | + if (isBuiltinClassNotModule(value) || PGuards.isNativeClass(value)) { |
157 | 155 | errorValueBranch.enter();
|
158 | 156 | throw raise(TypeError, ErrorMessages.CLASS_ASSIGMENT_ONLY_SUPPORTED_FOR_HEAP_TYPES_OR_MODTYPE_SUBCLASSES);
|
159 | 157 | }
|
160 | 158 | Object lazyClass = lib1.getLazyPythonClass(self);
|
161 |
| - if (lazyClass instanceof PythonBuiltinClassType || lazyClass instanceof PythonBuiltinClass || PGuards.isNativeClass(lazyClass)) { |
| 159 | + if (isBuiltinClassNotModule(lazyClass) || PGuards.isNativeClass(lazyClass)) { |
162 | 160 | errorSelfBranch.enter();
|
163 | 161 | throw raise(TypeError, ErrorMessages.CLASS_ASSIGMENT_ONLY_SUPPORTED_FOR_HEAP_TYPES_OR_MODTYPE_SUBCLASSES);
|
164 | 162 | }
|
165 | 163 |
|
166 |
| - getCheckCompatibleForAssigmentNode().execute(frame, lazyClass, value); |
| 164 | + setClass(frame, self, lazyClass, value, ctx, lib1); |
| 165 | + return PNone.NONE; |
| 166 | + } |
| 167 | + |
| 168 | + private void setClass(VirtualFrame frame, PythonObject self, Object lazyClass, Object value, PythonContext ctx, PythonObjectLibrary lib1) { |
| 169 | + if (lazyClass instanceof PythonBuiltinClassType) { |
| 170 | + getCheckCompatibleForAssigmentNode().execute(frame, ctx.getCore().lookupType((PythonBuiltinClassType) lazyClass), value); |
| 171 | + } else { |
| 172 | + getCheckCompatibleForAssigmentNode().execute(frame, lazyClass, value); |
| 173 | + } |
167 | 174 |
|
168 | 175 | lib1.setLazyPythonClass(self, value);
|
169 |
| - return PNone.NONE; |
170 | 176 | }
|
171 | 177 |
|
172 |
| - @Specialization(guards = {"isPythonClass(value)", "!isPythonObject(self)"}) |
| 178 | + private static boolean isBuiltinClassNotModule(Object lazyClass) { |
| 179 | + if (lazyClass instanceof PythonBuiltinClass) { |
| 180 | + return ((PythonBuiltinClass) lazyClass).getType() != PythonBuiltinClassType.PythonModule; |
| 181 | + } else if (lazyClass instanceof PythonBuiltinClassType) { |
| 182 | + return ((PythonBuiltinClassType) lazyClass) != PythonBuiltinClassType.PythonModule; |
| 183 | + } |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + @Specialization(guards = {"isPythonClass(value) || isPythonBuiltinClassType(value)", "!isPythonObject(self)"}) |
173 | 188 | Object getClass(@SuppressWarnings("unused") Object self, @SuppressWarnings("unused") Object value) {
|
174 | 189 | throw raise(TypeError, ErrorMessages.CLASS_ASSIGMENT_ONLY_SUPPORTED_FOR_HEAP_TYPES_OR_MODTYPE_SUBCLASSES);
|
175 | 190 | }
|
|
0 commit comments