|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.modules.cext; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError; |
| 44 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError; |
| 45 | +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 | +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 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__; |
| 50 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__; |
| 51 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__PACKAGE__; |
| 52 | + |
| 53 | +import com.oracle.graal.python.annotations.ArgumentClinic; |
| 54 | +import com.oracle.graal.python.annotations.ArgumentClinic.ClinicConversion; |
| 55 | +import com.oracle.graal.python.builtins.Builtin; |
| 56 | +import java.util.List; |
| 57 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 58 | +import com.oracle.graal.python.builtins.Python3Core; |
| 59 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 60 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 61 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 62 | +import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext; |
| 63 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetNativeNullNode; |
| 64 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PRaiseNativeNode; |
| 65 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.TransformExceptionToNativeNode; |
| 66 | +import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary; |
| 67 | +import com.oracle.graal.python.builtins.objects.dict.PDict; |
| 68 | +import com.oracle.graal.python.builtins.objects.module.PythonModule; |
| 69 | +import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins; |
| 70 | +import com.oracle.graal.python.builtins.objects.str.PString; |
| 71 | +import com.oracle.graal.python.lib.PyObjectLookupAttr; |
| 72 | +import com.oracle.graal.python.nodes.call.CallNode; |
| 73 | +import com.oracle.graal.python.nodes.classes.IsSubtypeNode; |
| 74 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 75 | +import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
| 76 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 77 | +import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode; |
| 78 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 79 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode; |
| 80 | +import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; |
| 81 | +import com.oracle.graal.python.nodes.object.GetClassNode; |
| 82 | +import com.oracle.graal.python.nodes.object.GetDictIfExistsNode; |
| 83 | +import com.oracle.graal.python.runtime.PythonContext; |
| 84 | +import com.oracle.graal.python.runtime.exception.PException; |
| 85 | +import com.oracle.truffle.api.dsl.Cached; |
| 86 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 87 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 88 | +import com.oracle.truffle.api.dsl.Specialization; |
| 89 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 90 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 91 | +import com.oracle.truffle.api.profiles.BranchProfile; |
| 92 | + |
| 93 | +@CoreFunctions(extendsModule = PythonCextBuiltins.PYTHON_CEXT) |
| 94 | +@GenerateNodeFactory |
| 95 | +public class PythonCextModuleBuiltins extends PythonBuiltins { |
| 96 | + |
| 97 | + @Override |
| 98 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 99 | + return PythonCextModuleBuiltinsFactory.getFactories(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void initialize(Python3Core core) { |
| 104 | + super.initialize(core); |
| 105 | + } |
| 106 | + |
| 107 | + @Builtin(name = "_PyModule_GetAndIncMaxModuleNumber") |
| 108 | + @GenerateNodeFactory |
| 109 | + abstract static class PyModuleGetAndIncMaxModuleNumber extends PythonBuiltinNode { |
| 110 | + |
| 111 | + @Specialization |
| 112 | + long doIt() { |
| 113 | + CApiContext nativeContext = getContext().getCApiContext(); |
| 114 | + return nativeContext.getAndIncMaxModuleNumber(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Builtin(name = "PyModule_SetDocString", minNumOfPositionalArgs = 2) |
| 119 | + @GenerateNodeFactory |
| 120 | + public abstract static class SetDocStringNode extends PythonBinaryBuiltinNode { |
| 121 | + @Specialization |
| 122 | + Object run(VirtualFrame frame, PythonModule module, Object doc, |
| 123 | + @Cached ObjectBuiltins.SetattrNode setattrNode) { |
| 124 | + setattrNode.execute(frame, module, __DOC__, doc); |
| 125 | + return PNone.NONE; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Builtin(name = "PyModule_NewObject", minNumOfPositionalArgs = 1, parameterNames = {"name"}) |
| 130 | + @ArgumentClinic(name = "name", conversion = ClinicConversion.String) |
| 131 | + @GenerateNodeFactory |
| 132 | + public abstract static class PyModuleNewObjectNode extends PythonUnaryClinicBuiltinNode { |
| 133 | + |
| 134 | + @Override |
| 135 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 136 | + return PythonCextModuleBuiltinsClinicProviders.PyModuleNewObjectNodeClinicProviderGen.INSTANCE; |
| 137 | + } |
| 138 | + |
| 139 | + @Specialization |
| 140 | + Object run(VirtualFrame frame, String name, |
| 141 | + @Cached CallNode callNode) { |
| 142 | + return callNode.execute(frame, PythonBuiltinClassType.PythonModule, new Object[]{name}); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Builtin(name = "_PyModule_CreateInitialized_PyModule_New", parameterNames = {"name"}) |
| 147 | + @ArgumentClinic(name = "name", conversion = ClinicConversion.String) |
| 148 | + @GenerateNodeFactory |
| 149 | + public abstract static class PyModuleCreateInitializedNewNode extends PythonUnaryClinicBuiltinNode { |
| 150 | + |
| 151 | + @Override |
| 152 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 153 | + return PythonCextModuleBuiltinsClinicProviders.PyModuleCreateInitializedNewNodeClinicProviderGen.INSTANCE; |
| 154 | + } |
| 155 | + |
| 156 | + @Specialization |
| 157 | + Object run(VirtualFrame frame, String name, |
| 158 | + @Cached CallNode callNode, |
| 159 | + @Cached ObjectBuiltins.SetattrNode setattrNode) { |
| 160 | + // see CPython's Objects/moduleobject.c - _PyModule_CreateInitialized for |
| 161 | + // comparison how they handle _Py_PackageContext |
| 162 | + String newModuleName = name; |
| 163 | + PythonContext ctx = getContext(); |
| 164 | + String pyPackageContext = ctx.getPyPackageContext(); |
| 165 | + if (pyPackageContext != null && pyPackageContext.endsWith(newModuleName)) { |
| 166 | + newModuleName = pyPackageContext; |
| 167 | + ctx.setPyPackageContext(null); |
| 168 | + } |
| 169 | + Object newModule = callNode.execute(frame, PythonBuiltinClassType.PythonModule, new Object[]{newModuleName}); |
| 170 | + // TODO: (tfel) I don't think this is the right place to set it, but somehow |
| 171 | + // at least in the import of sklearn.neighbors.dist_metrics through |
| 172 | + // sklearn.neighbors.ball_tree the __package__ attribute seems to be already |
| 173 | + // set in CPython. To not produce a warning, I'm setting it here, although I |
| 174 | + // could not find what CPython really does |
| 175 | + int idx = newModuleName.lastIndexOf("."); |
| 176 | + if (idx > -1) { |
| 177 | + setattrNode.execute(frame, newModule, __PACKAGE__, newModuleName.substring(0, idx)); |
| 178 | + } |
| 179 | + return newModule; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Builtin(name = "PyModule_GetNameObject", minNumOfPositionalArgs = 1) |
| 184 | + @GenerateNodeFactory |
| 185 | + public abstract static class PyModule_GetNameObjectNode extends PythonUnaryBuiltinNode { |
| 186 | + @Specialization |
| 187 | + Object getName(VirtualFrame frame, Object o, |
| 188 | + @Cached PyObjectLookupAttr lookupAttrNode, |
| 189 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 190 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 191 | + try { |
| 192 | + return lookupAttrNode.execute(frame, o, __NAME__); |
| 193 | + } catch (PException e) { |
| 194 | + transformExceptionToNativeNode.execute(e); |
| 195 | + return getNativeNullNode.execute(); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments