Skip to content

Commit 16e3e31

Browse files
committed
fix(dataset/mesh): import gmsh normally instead of a LazyLoader sys.modules stub
`meshgen.py` planted an importlib LazyLoader stub in sys.modules["gmsh"] at import time. That stub shadowed the real module for every plain `import gmsh` (rectangle/circle/cube/sphere/L), and its attribute resolution is fragile across versions: on Python 3.12 + gmsh 4.15 it raised "'_LazyModule' object has no attribute 'initialize'", breaking Mesh.gen_rectangle and verify_install on Colab. Replace it with a small _LazyImport proxy that does a normal importlib.import_module on first attribute access and never touches sys.modules, preserving laziness while letting plain `import gmsh` resolve the real module everywhere. Bump version to 0.1.1.
1 parent b34fd33 commit 16e3e31

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

tensormesh/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.0'
1+
__version__ = '0.1.1'

tensormesh/dataset/mesh/meshgen.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1-
import importlib.util
1+
import importlib
22
import os
3-
import sys
43
from warnings import warn
54
from ...mesh import Mesh
65
from ...element import element_type2dimension, element_type2order
76

87

9-
def _lazy_import(name):
10-
"""Defer loading a native module until first attribute access.
8+
class _LazyImport:
9+
"""Defer ``import name`` until the module is first used.
10+
11+
Using ``importlib.util.LazyLoader`` to register a stub in
12+
``sys.modules`` is fragile: on some Python/gmsh combinations
13+
(e.g. Python 3.12 + gmsh 4.15) attribute access on the stub raises
14+
``'_LazyModule' object has no attribute ...``. Worse, the stub
15+
shadows the real module, so a plain ``import gmsh`` elsewhere picks
16+
up the broken stub too. This proxy instead does a normal ``import``
17+
on first attribute access and never touches ``sys.modules``.
1118
"""
12-
spec = importlib.util.find_spec(name)
13-
if spec is None:
14-
class _Missing:
15-
def __getattr__(self, attr):
19+
20+
def __init__(self, name):
21+
self._name = name
22+
self._module = None
23+
24+
def __getattr__(self, attr):
25+
if self._module is None:
26+
try:
27+
self._module = importlib.import_module(self._name)
28+
except ImportError as exc:
1629
raise ImportError(
17-
f"{name!r} is required for this functionality. "
18-
f"Install it with: pip install {name}"
19-
)
20-
return _Missing()
21-
loader = importlib.util.LazyLoader(spec.loader)
22-
spec.loader = loader
23-
module = importlib.util.module_from_spec(spec)
24-
sys.modules[name] = module
25-
loader.exec_module(module)
26-
return module
27-
28-
29-
gmsh = _lazy_import("gmsh")
30+
f"{self._name!r} is required for this functionality. "
31+
f"Install it with: pip install {self._name}"
32+
) from exc
33+
return getattr(self._module, attr)
34+
35+
36+
gmsh = _LazyImport("gmsh")
3037

3138

3239
abbr2element_type = {

0 commit comments

Comments
 (0)