Skip to content

Commit 1589f93

Browse files
committed
[GR-23542] add __graalpython__.context_config builtin
PullRequest: graalpython/958
2 parents 14389ef + 816d054 commit 1589f93

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

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

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

43+
import static com.oracle.graal.python.nodes.BuiltinNames.__GRAALPYTHON__;
4344
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
4445
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ImportError;
4546
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
@@ -64,7 +65,6 @@
6465
import com.oracle.graal.python.builtins.objects.method.PMethod;
6566
import com.oracle.graal.python.builtins.objects.module.PythonModule;
6667
import com.oracle.graal.python.builtins.objects.object.PythonObject;
67-
import com.oracle.graal.python.nodes.BuiltinNames;
6868
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
6969
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
7070
import com.oracle.graal.python.nodes.call.PythonCallNode;
@@ -105,7 +105,7 @@
105105
import com.oracle.truffle.api.nodes.NodeVisitor;
106106
import com.oracle.truffle.llvm.api.Toolchain;
107107

108-
@CoreFunctions(defineModule = BuiltinNames.__GRAALPYTHON__)
108+
@CoreFunctions(defineModule = __GRAALPYTHON__)
109109
public class GraalPythonModuleBuiltins extends PythonBuiltins {
110110
public static final String LLVM_LANGUAGE = "llvm";
111111

@@ -126,7 +126,7 @@ public void initialize(PythonCore core) {
126126
public void postInitialize(PythonCore core) {
127127
super.postInitialize(core);
128128
PythonContext context = core.getContext();
129-
PythonModule mod = core.lookupBuiltinModule("__graalpython__");
129+
PythonModule mod = core.lookupBuiltinModule(__GRAALPYTHON__);
130130
if (!ImageInfo.inImageBuildtimeCode()) {
131131
mod.setAttribute("home", context.getLanguage().getHome());
132132
}

graalpython/lib-graalpython/__graalpython__.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,44 @@ def __getattr__(attr):
9292
owner_module.__dict__['__getattr__'] = __getattr__
9393

9494

95+
@builtin
96+
def auto_wrap_methods(delegate_name, delegate_attributes, owner_globals):
97+
func_type = type(import_current_as_named_module)
98+
99+
new_globals = dict(**owner_globals)
100+
101+
for attr in owner_globals:
102+
if attr.startswith("__"):
103+
continue
104+
elif not isinstance(owner_globals[attr], func_type):
105+
continue
106+
elif attr not in delegate_attributes:
107+
raise AttributeError("attribute '{}' not allowed in module '{}', permitted values are: '{}'".format(
108+
attr, __name__, delegate_attributes
109+
))
110+
111+
if attr in delegate_attributes:
112+
def make_wrapper(attribute, method):
113+
@__graalpython__.builtin
114+
def wrapper(*args, **kwargs):
115+
try:
116+
return method(*args, **kwargs)
117+
except NotImplementedError:
118+
delegate_module = __import__(delegate_name)
119+
return getattr(delegate_module, attribute)(*args, **kwargs)
120+
return wrapper
121+
122+
new_globals[attr] = make_wrapper(attr, owner_globals[attr])
123+
124+
return new_globals
125+
126+
95127
@builtin
96128
def import_current_as_named_module_with_delegate(module_name, delegate_name, delegate_attributes=None,
97-
owner_globals=None):
129+
owner_globals=None, wrap_methods=True):
98130
owner_module = import_current_as_named_module(module_name, owner_globals=owner_globals)
131+
if wrap_methods and owner_globals:
132+
wrapped_globals = auto_wrap_methods(delegate_name, delegate_attributes, owner_globals)
133+
owner_module.__dict__.update(**wrapped_globals)
99134
if delegate_attributes:
100135
lazy_attributes_from_delegate(delegate_name, delegate_attributes, owner_module)

graalpython/lib-graalpython/_struct.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@
4141
__graalpython__.import_current_as_named_module_with_delegate(
4242
module_name="_struct",
4343
delegate_name="_cpython_struct",
44-
delegate_attributes=['Struct', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack', 'pack_into', 'unpack',
45-
'unpack_from'],
44+
delegate_attributes=['Struct', 'StructError', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack',
45+
'pack_into', 'unpack', 'unpack_from'],
4646
owner_globals=globals())

0 commit comments

Comments
 (0)