Skip to content

Commit 651c62b

Browse files
committed
More robust exception handling for exceptions in inspect functions
- several inspect functions can cause side-effects that lead to exceptions - we want to ignore them all
1 parent 7ffd478 commit 651c62b

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

pyfakefs/fake_filesystem_unittest.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,10 @@ def _is_fs_module(self, mod, name, module_names):
474474
mod.__name__ in module_names
475475
or inspect.isclass(mod) and
476476
mod.__module__ in self._class_modules.get(name, []))
477-
except AttributeError:
477+
except Exception:
478478
# handle cases where the module has no __name__ or __module__
479-
# attribute - see #460
479+
# attribute - see #460, and any other exception triggered
480+
# by inspect functions
480481
return False
481482

482483
def _is_fs_function(self, fct):
@@ -486,23 +487,24 @@ def _is_fs_function(self, fct):
486487
fct.__name__ in self._fake_module_functions and
487488
fct.__module__ in self._fake_module_functions[
488489
fct.__name__])
489-
except AttributeError:
490+
except Exception:
490491
# handle cases where the function has no __name__ or __module__
491-
# attribute
492+
# attribute, or any other exception in inspect functions
492493
return False
493494

494495
def _def_values(self, item):
495496
"""Find default arguments that are file-system functions to be
496497
patched in top-level functions and members of top-level classes."""
497498
# check for module-level functions
498-
if inspect.isfunction(item):
499-
if item.__defaults__:
500-
for i, d in enumerate(item.__defaults__):
501-
if self._is_fs_function(d):
502-
yield item, i, d
503-
elif inspect.isclass(item):
504-
# check for methods in class (nested classes are ignored for now)
505-
try:
499+
try:
500+
if inspect.isfunction(item):
501+
if item.__defaults__:
502+
for i, d in enumerate(item.__defaults__):
503+
if self._is_fs_function(d):
504+
yield item, i, d
505+
elif inspect.isclass(item):
506+
# check for methods in class
507+
# (nested classes are ignored for now)
506508
with warnings.catch_warnings():
507509
# ignore deprecation warnings, see #542
508510
warnings.filterwarnings(
@@ -516,11 +518,11 @@ def _def_values(self, item):
516518
for i, d in enumerate(m.__defaults__):
517519
if self._is_fs_function(d):
518520
yield m, i, d
519-
except Exception:
520-
# Ignore any exception, examples:
521-
# ImportError: No module named '_gdbm'
522-
# _DontDoThat() (see #523)
523-
pass
521+
except Exception:
522+
# Ignore any exception, examples:
523+
# ImportError: No module named '_gdbm'
524+
# _DontDoThat() (see #523)
525+
pass
524526

525527
def _find_modules(self):
526528
"""Find and cache all modules that import file system modules.
@@ -535,10 +537,11 @@ def _find_modules(self):
535537
not inspect.ismodule(module) or
536538
module.__name__.split('.')[0] in self._skipNames):
537539
continue
538-
except AttributeError:
540+
except Exception:
539541
# workaround for some py (part of pytest) versions
540542
# where py.error has no __name__ attribute
541543
# see https://github.com/pytest-dev/py/issues/73
544+
# and any other exception triggered by inspect.ismodule
542545
continue
543546
module_items = module.__dict__.copy().items()
544547

0 commit comments

Comments
 (0)