Skip to content

Commit 28b1391

Browse files
committed
moved PythonCextBuiltins.PyModule nodes to PythonCextModuleBuiltins
1 parent 16273d0 commit 28b1391

File tree

3 files changed

+202
-96
lines changed

3 files changed

+202
-96
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
import com.oracle.graal.python.builtins.modules.cext.PythonCextMemoryViewBuiltins;
123123
import com.oracle.graal.python.builtins.modules.cext.PythonCextAbstractBuiltins;
124124
import com.oracle.graal.python.builtins.modules.cext.PythonCextIterBuiltins;
125+
import com.oracle.graal.python.builtins.modules.cext.PythonCextModuleBuiltins;
125126
import com.oracle.graal.python.builtins.modules.cext.PythonCextNamespaceBuiltins;
126127
import com.oracle.graal.python.builtins.modules.cext.PythonCextPythonRunBuiltins;
127128
import com.oracle.graal.python.builtins.modules.cext.PythonCextSetBuiltins;
@@ -500,6 +501,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
500501
new PythonCextListBuiltins(),
501502
new PythonCextLongBuiltins(),
502503
new PythonCextMemoryViewBuiltins(),
504+
new PythonCextModuleBuiltins(),
503505
new PythonCextPythonRunBuiltins(),
504506
new PythonCextNamespaceBuiltins(),
505507
new PythonCextSetBuiltins(),

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltins.java

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__;
4949
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__MODULE__;
5050
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
51-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__PACKAGE__;
5251
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
5352
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
5453
import static com.oracle.graal.python.util.PythonUtils.EMPTY_BYTE_ARRAY;
@@ -210,7 +209,6 @@
210209
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
211210
import com.oracle.graal.python.lib.PyMemoryViewFromObject;
212211
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
213-
import com.oracle.graal.python.lib.PyObjectLookupAttr;
214212
import com.oracle.graal.python.nodes.BuiltinNames;
215213
import com.oracle.graal.python.nodes.ErrorMessages;
216214
import com.oracle.graal.python.nodes.PGuards;
@@ -246,7 +244,6 @@
246244
import com.oracle.graal.python.nodes.function.builtins.PythonQuaternaryBuiltinNode;
247245
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
248246
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
249-
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
250247
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
251248
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
252249
import com.oracle.graal.python.nodes.object.GetClassNode;
@@ -439,88 +436,6 @@ Object run() {
439436
}
440437
}
441438

442-
@Builtin(name = "PyModule_SetDocString", minNumOfPositionalArgs = 2)
443-
@GenerateNodeFactory
444-
public abstract static class SetDocStringNode extends PythonBinaryBuiltinNode {
445-
@Specialization
446-
Object run(VirtualFrame frame, PythonModule module, Object doc,
447-
@Cached ObjectBuiltins.SetattrNode setattrNode) {
448-
setattrNode.execute(frame, module, __DOC__, doc);
449-
return PNone.NONE;
450-
}
451-
}
452-
453-
@Builtin(name = "PyModule_NewObject", minNumOfPositionalArgs = 1, parameterNames = {"name"})
454-
@ArgumentClinic(name = "name", conversion = ClinicConversion.String)
455-
@GenerateNodeFactory
456-
public abstract static class PyModuleNewObjectNode extends PythonUnaryClinicBuiltinNode {
457-
458-
@Override
459-
protected ArgumentClinicProvider getArgumentClinic() {
460-
return PythonCextBuiltinsClinicProviders.PyModuleNewObjectNodeClinicProviderGen.INSTANCE;
461-
}
462-
463-
@Specialization
464-
Object run(VirtualFrame frame, String name,
465-
@Cached CallNode callNode) {
466-
return callNode.execute(frame, PythonBuiltinClassType.PythonModule, new Object[]{name});
467-
}
468-
}
469-
470-
@Builtin(name = "_PyModule_CreateInitialized_PyModule_New", parameterNames = {"name"})
471-
@ArgumentClinic(name = "name", conversion = ClinicConversion.String)
472-
@GenerateNodeFactory
473-
public abstract static class PyModuleCreateInitializedNewNode extends PythonUnaryClinicBuiltinNode {
474-
475-
@Override
476-
protected ArgumentClinicProvider getArgumentClinic() {
477-
return PythonCextBuiltinsClinicProviders.PyModuleCreateInitializedNewNodeClinicProviderGen.INSTANCE;
478-
}
479-
480-
@Specialization
481-
Object run(VirtualFrame frame, String name,
482-
@Cached CallNode callNode,
483-
@Cached ObjectBuiltins.SetattrNode setattrNode) {
484-
// see CPython's Objects/moduleobject.c - _PyModule_CreateInitialized for
485-
// comparison how they handle _Py_PackageContext
486-
String newModuleName = name;
487-
PythonContext ctx = getContext();
488-
String pyPackageContext = ctx.getPyPackageContext();
489-
if (pyPackageContext != null && pyPackageContext.endsWith(newModuleName)) {
490-
newModuleName = pyPackageContext;
491-
ctx.setPyPackageContext(null);
492-
}
493-
Object newModule = callNode.execute(frame, PythonBuiltinClassType.PythonModule, new Object[]{newModuleName});
494-
// TODO: (tfel) I don't think this is the right place to set it, but somehow
495-
// at least in the import of sklearn.neighbors.dist_metrics through
496-
// sklearn.neighbors.ball_tree the __package__ attribute seems to be already
497-
// set in CPython. To not produce a warning, I'm setting it here, although I
498-
// could not find what CPython really does
499-
int idx = newModuleName.lastIndexOf(".");
500-
if (idx > -1) {
501-
setattrNode.execute(frame, newModule, __PACKAGE__, newModuleName.substring(0, idx));
502-
}
503-
return newModule;
504-
}
505-
}
506-
507-
@Builtin(name = "PyModule_GetNameObject", minNumOfPositionalArgs = 1)
508-
@GenerateNodeFactory
509-
public abstract static class PyModule_GetNameObjectNode extends PythonUnaryBuiltinNode {
510-
@Specialization
511-
Object run(VirtualFrame frame, Object o,
512-
@Cached PyObjectLookupAttr lookupAttrNode,
513-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
514-
@Cached GetNativeNullNode getNativeNullNode) {
515-
try {
516-
return lookupAttrNode.execute(frame, o, __NAME__);
517-
} catch (PException e) {
518-
transformExceptionToNativeNode.execute(e);
519-
return getNativeNullNode.execute();
520-
}
521-
}
522-
}
523-
524439
///////////// mappingproxy /////////////
525440

526441
@Builtin(name = "PyDictProxy_New", minNumOfPositionalArgs = 1)
@@ -2794,17 +2709,6 @@ static Object doGeneric(Object object) {
27942709
}
27952710
}
27962711

2797-
@Builtin(name = "_PyModule_GetAndIncMaxModuleNumber")
2798-
@GenerateNodeFactory
2799-
abstract static class PyModuleGetAndIncMaxModuleNumber extends PythonBuiltinNode {
2800-
2801-
@Specialization
2802-
long doIt() {
2803-
CApiContext nativeContext = getContext().getCApiContext();
2804-
return nativeContext.getAndIncMaxModuleNumber();
2805-
}
2806-
}
2807-
28082712
@ImportStatic(CExtContext.class)
28092713
abstract static class NewClassMethodNode extends Node {
28102714

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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

Comments
 (0)