Skip to content

Commit 7342263

Browse files
committed
Inline _coconut_import_module and simplify internal names in _coconut_lazy_module
Remove the separate _coconut_import_module function by inlining the import logic directly into the load closure. Drop _coconut_ prefixes from names that are already hidden inside the _coconut_lazy_module function scope. https://claude.ai/code/session_01KBLQLYTdfjLPaR2UJhgnkq
1 parent 5a35cab commit 7342263

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

coconut/compiler/header.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ def process_header_args(which, use_hash, target, no_tco, strict, no_wrap):
322322
(2, 7),
323323
if_lt='''
324324
import imp
325-
return imp.load_module(name, *imp.find_module(name))
325+
mod = imp.load_module(name, *imp.find_module(name))
326326
''',
327327
if_ge='''
328328
import importlib
329-
return importlib.import_module(name)
329+
mod = importlib.import_module(name)
330330
''',
331-
indent=1,
331+
indent=4,
332332
),
333333
import_OrderedDict=prepare(
334334
r'''

coconut/compiler/templates/header.py_template

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
def _coconut_import_module(name):
2-
{lazy_module_import}
31
def _coconut_lazy_module(name, on_import=None, attr=None):
4-
_coconut_lazy_state = [None, None]
5-
def _coconut_lazy_load():
6-
if _coconut_lazy_state[0] is None and _coconut_lazy_state[1] is None:
2+
state = [None, None]
3+
def load():
4+
if state[0] is None and state[1] is None:
75
try:
8-
_coconut_lazy_state[0] = _coconut_import_module(name)
6+
{lazy_module_import}
97
except _coconut.ImportError as err:
10-
_coconut_lazy_state[1] = err
8+
state[1] = err
119
else:
10+
state[0] = mod
1211
if on_import is not None:
13-
on_import(_coconut_lazy_state[0])
14-
if _coconut_lazy_state[1] is not None:
15-
raise _coconut_lazy_state[1]
12+
on_import(mod)
13+
if state[1] is not None:
14+
raise state[1]
1615
if attr is not None:
17-
return _coconut.getattr(_coconut_lazy_state[0], attr)
18-
return _coconut_lazy_state[0]
19-
class _coconut_lazy_meta(type):
16+
return _coconut.getattr(state[0], attr)
17+
return state[0]
18+
class meta(type):
2019
def __getattr__(cls, name):
21-
return _coconut.getattr(_coconut_lazy_load(), name)
20+
return _coconut.getattr(load(), name)
2221
def __setattr__(cls, name, value):
2322
if name.startswith("__") and name.endswith("__"):
2423
type.__setattr__(cls, name, value)
2524
else:
26-
_coconut.setattr(_coconut_lazy_load(), name, value)
25+
_coconut.setattr(load(), name, value)
2726
def __dir__(cls):
2827
try:
29-
return _coconut.dir(_coconut_lazy_load())
28+
return _coconut.dir(load())
3029
except _coconut.ImportError:
3130
return []
3231
def __bool__(cls):
3332
return True
3433
__nonzero__ = __bool__
35-
return _coconut_lazy_meta("_coconut_lazy_module", (), {{}})
34+
return meta("_coconut_lazy_module", (), {{}})
3635
@_coconut_wraps(_coconut_py_super)
3736
def _coconut_super(type=None, object_or_type=None):
3837
if type is None:

0 commit comments

Comments
 (0)