Skip to content

Commit f37bf79

Browse files
committed
intrinsified PySys_XXX
1 parent 7a77b49 commit f37bf79

File tree

5 files changed

+219
-7
lines changed

5 files changed

+219
-7
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import sys
41+
from . import CPyExtTestCase, CPyExtFunction, unhandled_error_compare, GRAALPYTHON
42+
__dir__ = __file__.rpartition("/")[0]
43+
44+
def _reference_get_object(args):
45+
try:
46+
return getattr(sys, args[0])
47+
except AttributeError:
48+
if sys.version_info.minor >= 6:
49+
raise SystemError
50+
else:
51+
raise KeyError(args[0])
52+
53+
class TestPySys(CPyExtTestCase):
54+
55+
def compile_module(self, name):
56+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
57+
super(TestPySys, self).compile_module(name)
58+
59+
test_PySys_GetObject = CPyExtFunction(
60+
_reference_get_object,
61+
lambda: (
62+
("hello",),
63+
("byteorder",),
64+
),
65+
resultspec="O",
66+
argspec='s',
67+
arguments=["char* name"],
68+
cmpfunc=unhandled_error_compare
69+
)

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.PythonCextAbstractBuiltins;
123123
import com.oracle.graal.python.builtins.modules.cext.PythonCextSetBuiltins;
124124
import com.oracle.graal.python.builtins.modules.cext.PythonCextUnicodeBuiltins;
125+
import com.oracle.graal.python.builtins.modules.cext.PythonCextSysBuiltins;
125126
import com.oracle.graal.python.builtins.modules.csv.CSVDialectBuiltins;
126127
import com.oracle.graal.python.builtins.modules.csv.CSVModuleBuiltins;
127128
import com.oracle.graal.python.builtins.modules.csv.CSVReaderBuiltins;
@@ -492,6 +493,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
492493
new PythonCextLongBuiltins(),
493494
new PythonCextMemoryViewBuiltins(),
494495
new PythonCextSetBuiltins(),
496+
new PythonCextSysBuiltins(),
495497
new PythonCextUnicodeBuiltins(),
496498
new WeakRefModuleBuiltins(),
497499
new ReferenceTypeBuiltins(),

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules.cext;
4242

43+
<<<<<<< HEAD
44+
=======
45+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextAbstractBuiltins.PYTHON_CEXT_ABSTRACT;
46+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextBytesBuiltins.PYTHON_CEXT_BYTES;
47+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextCEvalBuiltins.PYTHON_CEXT_CEVAL;
48+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextCodeBuiltins.PYTHON_CEXT_CODE;
49+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextComplexBuiltins.PYTHON_CEXT_COMPLEX;
50+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextDictBuiltins.PYTHON_CEXT_DICT;
51+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextFileBuiltins.PYTHON_CEXT_FILE;
52+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextFloatBuiltins.PYTHON_CEXT_FLOAT;
53+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextListBuiltins.PYTHON_CEXT_LIST;
54+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextLongBuiltins.PYTHON_CEXT_LONG;
55+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextMemoryViewBuiltins.PYTHON_CEXT_MEMORYVIEW;
56+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextSetBuiltins.PYTHON_CEXT_SET;
57+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextSysBuiltins.PYTHON_CEXT_SYS;
58+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextUnicodeBuiltins.PYTHON_CEXT_UNICODE;
59+
>>>>>>> intrinsified PySys_XXX
4360
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.IndexError;
4461
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
4562
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
@@ -350,6 +367,43 @@ public void initialize(Python3Core core) {
350367
builtinConstants.put("PyGILState_Release", new PyGILStateRelease());
351368
}
352369

370+
<<<<<<< HEAD
371+
=======
372+
@Override
373+
public void postInitialize(Python3Core core) {
374+
PythonModule cext = core.lookupBuiltinModule(PYTHON_CEXT);
375+
addModuleDict(cext, PYTHON_CEXT_ABSTRACT, core);
376+
addModuleDict(cext, PYTHON_CEXT_BYTES, core);
377+
addModuleDict(cext, PYTHON_CEXT_CEVAL, core);
378+
addModuleDict(cext, PYTHON_CEXT_CODE, core);
379+
addModuleDict(cext, PYTHON_CEXT_COMPLEX, core);
380+
addModuleDict(cext, PYTHON_CEXT_DICT, core);
381+
addModuleDict(cext, PYTHON_CEXT_FILE, core);
382+
addModuleDict(cext, PYTHON_CEXT_FLOAT, core);
383+
addModuleDict(cext, PYTHON_CEXT_LONG, core);
384+
addModuleDict(cext, PYTHON_CEXT_LIST, core);
385+
addModuleDict(cext, PYTHON_CEXT_MEMORYVIEW, core);
386+
addModuleDict(cext, PYTHON_CEXT_SET, core);
387+
addModuleDict(cext, PYTHON_CEXT_SYS, core);
388+
addModuleDict(cext, PYTHON_CEXT_UNICODE, core);
389+
}
390+
391+
private void addModuleDict(PythonModule cext, String module, Python3Core core) {
392+
PythonModule cext_module = core.lookupBuiltinModule(module);
393+
PDict dict = GetDictIfExistsNodeGen.getUncached().execute(cext_module);
394+
HashingStorageIterable<Object> keys = dict.keys();
395+
HashingStorageIterator<Object> it = keys.iterator();
396+
while (it.hasNext()) {
397+
Object key = it.next();
398+
Object value = dict.getItem(key);
399+
if (value instanceof PythonBuiltinObject) {
400+
assert cext.getAttribute(key) == PNone.NO_VALUE || cext.getAttribute(key) == null : "python_cext dict already contains value " + cext.getAttribute(key) + " for key " + key;
401+
cext.setAttribute(key, value);
402+
}
403+
}
404+
}
405+
406+
>>>>>>> intrinsified PySys_XXX
353407
@FunctionalInterface
354408
public interface TernaryFunction<T1, T2, T3, R> {
355409
R apply(T1 arg0, T2 arg1, T3 arg2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 com.oracle.graal.python.builtins.Builtin;
44+
import java.util.List;
45+
import com.oracle.graal.python.builtins.CoreFunctions;
46+
import com.oracle.graal.python.builtins.Python3Core;
47+
import com.oracle.graal.python.builtins.PythonBuiltins;
48+
import com.oracle.graal.python.builtins.objects.PNone;
49+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetNativeNullNode;
50+
import com.oracle.graal.python.lib.PyObjectLookupAttr;
51+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
52+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
53+
import com.oracle.graal.python.runtime.exception.PException;
54+
import com.oracle.truffle.api.dsl.Cached;
55+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
56+
import com.oracle.truffle.api.dsl.NodeFactory;
57+
import com.oracle.truffle.api.dsl.Specialization;
58+
import com.oracle.truffle.api.frame.VirtualFrame;
59+
60+
@CoreFunctions(extendsModule = PythonCextBuiltins.PYTHON_CEXT)
61+
@GenerateNodeFactory
62+
public class PythonCextSysBuiltins extends PythonBuiltins {
63+
64+
@Override
65+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
66+
return PythonCextSysBuiltinsFactory.getFactories();
67+
}
68+
69+
@Override
70+
public void initialize(Python3Core core) {
71+
super.initialize(core);
72+
}
73+
74+
@Builtin(name = "PySys_GetObject", minNumOfPositionalArgs = 1)
75+
@GenerateNodeFactory
76+
public abstract static class PySysGetObjectNode extends PythonUnaryBuiltinNode {
77+
@Specialization
78+
public Object writeStr(VirtualFrame frame, String name,
79+
@Cached PyObjectLookupAttr lookupNode,
80+
@Cached GetNativeNullNode getNativeNull) {
81+
try {
82+
Object value = lookupNode.execute(frame, getContext().getCore().lookupBuiltinModule("sys"), name);
83+
if (value == PNone.NO_VALUE) {
84+
return getNativeNull.execute();
85+
}
86+
return value;
87+
} catch (PException e) {
88+
// PySys_GetObject delegates to PyDict_GetItem
89+
// which suppresses all exceptions for historical reasons
90+
return getNativeNull.execute();
91+
}
92+
}
93+
}
94+
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,3 @@ def _PyNamespace_New(kwds):
510510
from types import SimpleNamespace as namespace_type
511511
return namespace_type(**kwds)
512512

513-
514-
@may_raise
515-
def PySys_GetObject(name):
516-
try:
517-
return getattr(sys, name)
518-
except AttributeError:
519-
raise KeyError(name)

0 commit comments

Comments
 (0)