|
41 | 41 | package com.oracle.graal.python.builtins.modules.cext;
|
42 | 42 |
|
43 | 43 | import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
|
44 |
| -import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError; |
45 | 44 | import static com.oracle.graal.python.nodes.ErrorMessages.MODULE_S_HAS_NO;
|
46 |
| -import static com.oracle.graal.python.nodes.ErrorMessages.S_NEEDS_NON_NULL_VALUE; |
47 | 45 | import static com.oracle.graal.python.nodes.ErrorMessages.S_NEEDS_S_AS_FIRST_ARG;
|
48 |
| -import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__; |
49 | 46 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__;
|
50 | 47 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
|
51 | 48 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.__PACKAGE__;
|
| 49 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__; |
52 | 50 |
|
53 | 51 | import com.oracle.graal.python.annotations.ArgumentClinic;
|
54 | 52 | import com.oracle.graal.python.annotations.ArgumentClinic.ClinicConversion;
|
@@ -197,4 +195,58 @@ Object getName(VirtualFrame frame, Object o,
|
197 | 195 | }
|
198 | 196 | }
|
199 | 197 |
|
| 198 | + @Builtin(name = "PyModule_AddObject", minNumOfPositionalArgs = 3) |
| 199 | + @GenerateNodeFactory |
| 200 | + public abstract static class PyModuleAddObjectNode extends PythonTernaryBuiltinNode { |
| 201 | + @Specialization(guards = "isModuleSubtype(frame, m, getClassNode, isSubtypeNode)") |
| 202 | + Object addObject(VirtualFrame frame, Object m, String k, Object o, |
| 203 | + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
| 204 | + @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
| 205 | + @Cached PyObjectLookupAttr lookupAttrNode, |
| 206 | + @Cached GetDictIfExistsNode getDictNode, |
| 207 | + @Cached BranchProfile noDictProfile, |
| 208 | + @CachedLibrary(limit = "3") HashingStorageLibrary lib, |
| 209 | + @Cached PRaiseNativeNode raiseNativeNode, |
| 210 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) { |
| 211 | + try { |
| 212 | + PDict dict = getDictNode.execute(m); |
| 213 | + if (dict == null) { |
| 214 | + noDictProfile.enter(); |
| 215 | + return raiseNativeNode.raiseInt(frame, -1, SystemError, MODULE_S_HAS_NO, lookupAttrNode.execute(frame, m, __NAME__), __DICT__); |
| 216 | + } |
| 217 | + lib.setItem(dict.getDictStorage(), k, o); |
| 218 | + return 0; |
| 219 | + } catch (PException e) { |
| 220 | + transformExceptionToNativeNode.execute(e); |
| 221 | + return -1; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + @Specialization(guards = "isModuleSubtype(frame, m, getClassNode, isSubtypeNode)") |
| 226 | + Object addObject(VirtualFrame frame, Object m, PString k, Object o, |
| 227 | + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
| 228 | + @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
| 229 | + @Cached PyObjectLookupAttr lookupAttrNode, |
| 230 | + @Cached GetDictIfExistsNode getDictNode, |
| 231 | + @Cached BranchProfile noDictProfile, |
| 232 | + @CachedLibrary(limit = "3") HashingStorageLibrary lib, |
| 233 | + @Cached PRaiseNativeNode raiseNativeNode, |
| 234 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) { |
| 235 | + return addObject(frame, m, k.getValue(), o, getClassNode, isSubtypeNode, lookupAttrNode, getDictNode, noDictProfile, lib, raiseNativeNode, transformExceptionToNativeNode); |
| 236 | + } |
| 237 | + |
| 238 | + @SuppressWarnings("unused") |
| 239 | + @Specialization(guards = "!isModuleSubtype(frame, m, getClassNode, isSubtypeNode)") |
| 240 | + public Object pop(VirtualFrame frame, Object m, Object key, Object defaultValue, |
| 241 | + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
| 242 | + @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
| 243 | + @Cached PRaiseNativeNode raiseNativeNode) { |
| 244 | + return raiseNativeNode.raiseInt(frame, -1, SystemError, S_NEEDS_S_AS_FIRST_ARG, "PyModule_AddObject", "module"); |
| 245 | + } |
| 246 | + |
| 247 | + protected boolean isModuleSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) { |
| 248 | + return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PythonModule); |
| 249 | + } |
| 250 | + } |
| 251 | + |
200 | 252 | }
|
0 commit comments