Skip to content

Commit 2aeeee0

Browse files
committed
Handle all possible ImportErrors for lazy modules
1 parent 711c5e0 commit 2aeeee0

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src_py/__init__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,11 @@ def PixelArray(surface): # pylint: disable=unused-argument
306306

307307

308308
def lazy_import(name):
309-
"""See https://docs.python.org/3/library/importlib.html#implementing-lazy-imports
309+
"""Lazily import a pygame module.
310+
311+
See https://docs.python.org/3/library/importlib.html#implementing-lazy-imports
312+
Only load the module upon its first attribute access.
310313
311-
Only load modules upon their first attribute access.
312314
Lazily imported modules are directly referenced in packager_imports function.
313315
"""
314316
spec = find_spec("pygame." + name)
@@ -320,15 +322,22 @@ def lazy_import(name):
320322
return module
321323

322324

323-
if find_spec("numpy") is not None:
325+
numpy_exists = find_spec("numpy") is not None
326+
327+
# Preserve MissingModule behavior when numpy is not installed
328+
# and when their pygame module dependencies are unavailable
329+
330+
if numpy_exists and not isinstance(pixelcopy, MissingModule):
324331
surfarray = lazy_import("surfarray")
325-
sndarray = lazy_import("sndarray")
326332
else:
327-
# Preserve MissingModule behavior when numpy is not installed
328333
surfarray = MissingModule("surfarray", urgent=0)
334+
335+
if numpy_exists and not isinstance(mixer, MissingModule):
336+
sndarray = lazy_import("sndarray")
337+
else:
329338
sndarray = MissingModule("sndarray", urgent=0)
330339

331-
del LazyLoader, find_spec, lazy_import, module_from_spec
340+
del LazyLoader, find_spec, lazy_import, module_from_spec, numpy_exists
332341

333342
try:
334343
import pygame._debug

0 commit comments

Comments
 (0)