Skip to content

Commit 9448b19

Browse files
committed
add lazy_attribute_loading_from_module builtin
- used to lazily load attributes defined in another module via the __getattr__ mechanism. This will only cache the attributes in the module where this is called.
1 parent ba31c43 commit 9448b19

File tree

2 files changed

+32
-55
lines changed

2 files changed

+32
-55
lines changed

graalpython/lib-graalpython/__graalpython__.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,44 @@
3939

4040

4141
@builtin
42-
def import_current_as_named_module(module_name):
42+
def import_current_as_named_module(module_name, globals):
4343
"""
4444
load a builtin anonymous module which does not have a Truffle land builtin module counter part
4545
4646
:param module_name: the module name to load as
47+
:param globals: the module globals (to update)
4748
:return: None
4849
"""
4950
import sys
5051
module = sys.modules.get(module_name, None)
5152
if not module:
5253
module = type(sys)(module_name)
5354
sys.modules[module_name] = module
54-
new_globals = dict(**globals())
55+
new_globals = dict(**globals)
5556
new_globals.update(**module.__dict__)
5657
module.__dict__.update(**new_globals)
58+
59+
60+
@builtin
61+
def lazy_attribute_loading_from_module(attributes, module_name, globals):
62+
"""
63+
used to lazily load attributes defined in another module via the __getattr__ mechanism.
64+
This will only cache the attributes in the caller module.
65+
66+
:param attributes: a list of attributes names to be loaded lazily from the delagate module
67+
:param module_name: the delegate module
68+
:param globals: the module globals (to update)
69+
:return:
70+
"""
71+
def __getattr__(name):
72+
if name in attributes:
73+
delegate_module = __import__(module_name)
74+
globals.update(delegate_module.__dict__)
75+
globals['__all__'] = attributes
76+
if '__getattr__' in globals:
77+
del globals['__getattr__']
78+
return getattr(delegate_module, name)
79+
raise AttributeError("module '{}' does not have '{}' attribute".format(module_name, name))
80+
81+
globals['__getattr__'] = __getattr__
82+
globals['__all__'] = ['__getattr__']

graalpython/lib-graalpython/_struct.py

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,57 +37,8 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
# __all__ = ['Struct', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack', 'pack_into', 'unpack', 'unpack_from']
4140

42-
43-
@__graalpython__.builtin
44-
def _clearcache(*args):
45-
import _cpython_struct
46-
return _cpython_struct.clearcache(*args)
47-
48-
49-
@__graalpython__.builtin
50-
def calcsize(fmt):
51-
import _cpython_struct
52-
return _cpython_struct.calcsize(fmt)
53-
54-
55-
@__graalpython__.builtin
56-
def iter_unpack(fmt, buffer):
57-
import _cpython_struct
58-
return _cpython_struct.calcsize(fmt, buffer)
59-
60-
61-
@__graalpython__.builtin
62-
def pack(fmt, *vals):
63-
import _cpython_struct
64-
return _cpython_struct.pack(fmt, *vals)
65-
66-
67-
@__graalpython__.builtin
68-
def pack_into(fmt, buffer, offset, *vals):
69-
import _cpython_struct
70-
return _cpython_struct.pack_into(fmt, buffer, offset, *vals)
71-
72-
73-
@__graalpython__.builtin
74-
def unpack(fmt, *vals):
75-
import _cpython_struct
76-
return _cpython_struct.unpack(fmt, *vals)
77-
78-
79-
@__graalpython__.builtin
80-
def unpack_from(fmt, buffer, offset=0):
81-
import _cpython_struct
82-
return _cpython_struct.unpack_from(fmt, buffer, offset=offset)
83-
84-
85-
# error and Struct
86-
def __getattr__(name):
87-
if name in ['error', 'Struct']:
88-
import _cpython_struct
89-
return __getattr__(_cpython_struct, name)
90-
raise AttributeError("module {} has no attribute {}".format(__name__, name))
91-
92-
93-
__graalpython__.import_current_as_named_module("_struct")
41+
__graalpython__.lazy_attribute_loading_from_module(
42+
['Struct', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack', 'pack_into', 'unpack', 'unpack_from'],
43+
'_cpython_struct', globals())
44+
__graalpython__.import_current_as_named_module("_struct", globals())

0 commit comments

Comments
 (0)