Skip to content

Commit 0042e88

Browse files
committed
__graalpython__ automatically wrap native methods if their truffle counterparts exist
1 parent 9455796 commit 0042e88

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

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:
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 & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,9 @@
3838
# SOFTWARE.
3939

4040

41-
if "_truffle_pack" in globals():
42-
@__graalpython__.builtin
43-
def pack(fmt, *values):
44-
try:
45-
return _truffle_pack(fmt, *values)
46-
except:
47-
import _cpython_struct
48-
return _cpython_struct.pack(fmt, *values)
49-
50-
51-
if "_truffle_unpack" in globals():
52-
@__graalpython__.builtin
53-
def unpack(fmt, buffer):
54-
try:
55-
return _truffle_unpack(fmt, buffer)
56-
except:
57-
import _cpython_struct
58-
return _cpython_struct.unpack(fmt, buffer)
59-
60-
6141
__graalpython__.import_current_as_named_module_with_delegate(
6242
module_name="_struct",
6343
delegate_name="_cpython_struct",
64-
delegate_attributes=['Struct', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack', 'pack_into', 'unpack',
65-
'unpack_from'],
44+
delegate_attributes=['Struct', 'StructError', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack',
45+
'pack_into', 'unpack', 'unpack_from'],
6646
owner_globals=globals())

0 commit comments

Comments
 (0)