Skip to content

Commit ddf5923

Browse files
committed
[GR-22297] make PNodeWithState a public utility node
PullRequest: graalpython/940
2 parents fa2fbf2 + ba363f5 commit ddf5923

File tree

10 files changed

+222
-29
lines changed

10 files changed

+222
-29
lines changed

graalpython/com.oracle.graal.python.cext/modules/_struct.c renamed to graalpython/com.oracle.graal.python.cext/modules/_cpython_struct.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,7 @@ static struct PyModuleDef _structmodule = {
23272327
};
23282328

23292329
PyMODINIT_FUNC
2330-
PyInit__struct(void)
2330+
PyInit__cpython_struct(void)
23312331
{
23322332
PyObject *m;
23332333

graalpython/com.oracle.graal.python.cext/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def __call__(self):
344344
NativeBuiltinModule("_cpython_unicodedata"),
345345
NativeBuiltinModule("_memoryview"),
346346
NativeBuiltinModule("_mmap"),
347-
NativeBuiltinModule("_struct"),
347+
NativeBuiltinModule("_cpython_struct"),
348348
# the above modules are more core, we need them first to deal with later, more complex modules with dependencies
349349
NativeBuiltinModule("_bz2", deps=[Bzip2Depedency("bz2", "bzip2==1.0.8", "BZIP2")], extra_link_args=["-Wl,-rpath,%s/../lib/%s/" % (relative_rpath, SOABI)]),
350350
)

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
@@ -224,6 +224,7 @@ private static final String[] initializeCoreFiles() {
224224
"_io",
225225
"_frozen_importlib",
226226
"classes",
227+
"__graalpython__",
227228
"_weakref",
228229
"set",
229230
"itertools",
@@ -258,6 +259,7 @@ private static final String[] initializeCoreFiles() {
258259
"pip_hook",
259260
"_lsprof",
260261
"marshal",
262+
"_struct",
261263
"_lzma"));
262264
// add service loader defined python file extensions
263265
if (!ImageInfo.inImageRuntimeCode()) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public enum PythonBuiltinClassType implements LazyPythonClass {
166166
ZipImportError("ZipImportError", "zipimport"),
167167
ZLibError("error", "zlib"),
168168
LZMAError("LZMAError", "_lzma"),
169+
StructError("StructError", "_struct"),
169170

170171
// todo: all OS errors
171172

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MarshalModuleBuiltins.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,13 @@
6868
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6969
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
7070
import com.oracle.graal.python.nodes.IndirectCallNode;
71-
import com.oracle.graal.python.nodes.PNodeWithContext;
72-
import com.oracle.graal.python.nodes.PRaiseNode;
71+
import com.oracle.graal.python.nodes.PNodeWithState;
7372
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7473
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7574
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
7675
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
7776
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
7877
import com.oracle.graal.python.runtime.PythonContext;
79-
import com.oracle.graal.python.runtime.exception.PException;
80-
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
8178
import com.oracle.truffle.api.Assumption;
8279
import com.oracle.truffle.api.CompilerAsserts;
8380
import com.oracle.truffle.api.CompilerDirectives;
@@ -224,27 +221,6 @@ private InternedString(String string) {
224221
}
225222
}
226223

227-
private abstract static class PNodeWithState extends PNodeWithContext {
228-
@Child private PythonObjectFactory objectFactory;
229-
@Child private PRaiseNode raiseNode;
230-
231-
protected final PException raise(PythonBuiltinClassType type, String format, Object... arguments) {
232-
if (raiseNode == null) {
233-
CompilerDirectives.transferToInterpreterAndInvalidate();
234-
raiseNode = insert(PRaiseNode.create());
235-
}
236-
return raiseNode.raise(type, format, arguments);
237-
}
238-
239-
protected final PythonObjectFactory factory() {
240-
if (objectFactory == null) {
241-
CompilerDirectives.transferToInterpreterAndInvalidate();
242-
objectFactory = insert(PythonObjectFactory.create());
243-
}
244-
return objectFactory;
245-
}
246-
}
247-
248224
abstract static class MarshallerNode extends PNodeWithState {
249225

250226
public abstract void execute(VirtualFrame frame, Object x, int version, DataOutputStream buffer);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2020, 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.nodes;
42+
43+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
44+
import com.oracle.graal.python.runtime.exception.PException;
45+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
46+
import com.oracle.truffle.api.CompilerDirectives;
47+
48+
public abstract class PNodeWithState extends PNodeWithContext {
49+
@Child private PythonObjectFactory objectFactory;
50+
@Child private PRaiseNode raiseNode;
51+
52+
protected final PException raise(PythonBuiltinClassType type, String format, Object... arguments) {
53+
if (raiseNode == null) {
54+
CompilerDirectives.transferToInterpreterAndInvalidate();
55+
raiseNode = insert(PRaiseNode.create());
56+
}
57+
return raiseNode.raise(type, format, arguments);
58+
}
59+
60+
protected final PythonObjectFactory factory() {
61+
if (objectFactory == null) {
62+
CompilerDirectives.transferToInterpreterAndInvalidate();
63+
objectFactory = insert(PythonObjectFactory.create());
64+
}
65+
return objectFactory;
66+
}
67+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/exception/PythonErrorType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -72,4 +72,5 @@ public abstract class PythonErrorType {
7272
public static final PythonBuiltinClassType ZipImportError = PythonBuiltinClassType.ZipImportError;
7373
public static final PythonBuiltinClassType ZLibError = PythonBuiltinClassType.ZLibError;
7474
public static final PythonBuiltinClassType LZMAError = PythonBuiltinClassType.LZMAError;
75+
public static final PythonBuiltinClassType StructError = PythonBuiltinClassType.StructError;
7576
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright (c) 2020, 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+
42+
43+
@builtin
44+
def import_current_as_named_module(name, owner_globals=None):
45+
"""
46+
load a builtin anonymous module which does not have a Truffle land builtin module counter part
47+
48+
:param name: the module name to load as
49+
:param owner_globals: the module globals (to update)
50+
:return: the loaded module
51+
"""
52+
if owner_globals is None:
53+
owner_globals = {}
54+
55+
module = sys.modules.get(name, None)
56+
if not module:
57+
module = type(sys)(name)
58+
sys.modules[name] = module
59+
new_globals = dict(**owner_globals)
60+
new_globals.update(**module.__dict__)
61+
module.__dict__.update(**new_globals)
62+
return module
63+
64+
65+
@builtin
66+
def lazy_attributes_from_delegate(delegate_name, attributes, owner_module):
67+
"""
68+
used to lazily load attributes defined in another module via the __getattr__ mechanism.
69+
This will only cache the attributes in the caller module.
70+
71+
:param delegate_name: the delegate module
72+
:param attributes: a list of attributes names to be loaded lazily from the delagate module
73+
:param owner_module: the owner module (where this is called from)
74+
:return:
75+
"""
76+
attributes.append('__all__')
77+
78+
def __getattr__(attr):
79+
if attr in attributes:
80+
delegate_module = __import__(delegate_name)
81+
82+
new_globals = dict(**delegate_module.__dict__)
83+
new_globals.update(**owner_module.__dict__)
84+
owner_module.__dict__.update(**new_globals)
85+
86+
if '__getattr__' in owner_module.__dict__:
87+
del owner_module.__dict__['__getattr__']
88+
89+
return getattr(delegate_module, attr)
90+
raise AttributeError("module '{}' does not have '{}' attribute".format(delegate_name, attr))
91+
92+
owner_module.__dict__['__getattr__'] = __getattr__
93+
94+
95+
@builtin
96+
def import_current_as_named_module_with_delegate(module_name, delegate_name, delegate_attributes=None,
97+
owner_globals=None):
98+
owner_module = import_current_as_named_module(module_name, owner_globals=owner_globals)
99+
if delegate_attributes:
100+
lazy_attributes_from_delegate(delegate_name, delegate_attributes, owner_module)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) 2020, 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+
41+
__graalpython__.import_current_as_named_module_with_delegate(
42+
module_name="_struct",
43+
delegate_name="_cpython_struct",
44+
delegate_attributes=['Struct', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack', 'pack_into', 'unpack',
45+
'unpack_from'],
46+
owner_globals=globals())

mx.graalpython/copyrights/overrides

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ graalpython/com.oracle.graal.python.cext/modules/_cpython_sre.c,python.copyright
128128
graalpython/com.oracle.graal.python.cext/modules/_cpython_unicodedata.c,python.copyright
129129
graalpython/com.oracle.graal.python.cext/modules/_memoryview.c,python.copyright
130130
graalpython/com.oracle.graal.python.cext/modules/_mmap.c,python.copyright
131-
graalpython/com.oracle.graal.python.cext/modules/_struct.c,python.copyright
131+
graalpython/com.oracle.graal.python.cext/modules/_cpython_struct.c,python.copyright
132132
graalpython/com.oracle.graal.python.cext/modules/clinic/_bz2module.c.h,python.copyright
133133
graalpython/com.oracle.graal.python.cext/modules/clinic/_sre.c.h,python.copyright
134134
graalpython/com.oracle.graal.python.cext/modules/clinic/_struct.c.h,python.copyright

0 commit comments

Comments
 (0)