@@ -474,9 +474,10 @@ def _is_fs_module(self, mod, name, module_names):
474
474
mod .__name__ in module_names
475
475
or inspect .isclass (mod ) and
476
476
mod .__module__ in self ._class_modules .get (name , []))
477
- except AttributeError :
477
+ except Exception :
478
478
# 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
480
481
return False
481
482
482
483
def _is_fs_function (self , fct ):
@@ -486,23 +487,24 @@ def _is_fs_function(self, fct):
486
487
fct .__name__ in self ._fake_module_functions and
487
488
fct .__module__ in self ._fake_module_functions [
488
489
fct .__name__ ])
489
- except AttributeError :
490
+ except Exception :
490
491
# handle cases where the function has no __name__ or __module__
491
- # attribute
492
+ # attribute, or any other exception in inspect functions
492
493
return False
493
494
494
495
def _def_values (self , item ):
495
496
"""Find default arguments that are file-system functions to be
496
497
patched in top-level functions and members of top-level classes."""
497
498
# 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)
506
508
with warnings .catch_warnings ():
507
509
# ignore deprecation warnings, see #542
508
510
warnings .filterwarnings (
@@ -516,11 +518,11 @@ def _def_values(self, item):
516
518
for i , d in enumerate (m .__defaults__ ):
517
519
if self ._is_fs_function (d ):
518
520
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
524
526
525
527
def _find_modules (self ):
526
528
"""Find and cache all modules that import file system modules.
@@ -535,10 +537,11 @@ def _find_modules(self):
535
537
not inspect .ismodule (module ) or
536
538
module .__name__ .split ('.' )[0 ] in self ._skipNames ):
537
539
continue
538
- except AttributeError :
540
+ except Exception :
539
541
# workaround for some py (part of pytest) versions
540
542
# where py.error has no __name__ attribute
541
543
# see https://github.com/pytest-dev/py/issues/73
544
+ # and any other exception triggered by inspect.ismodule
542
545
continue
543
546
module_items = module .__dict__ .copy ().items ()
544
547
0 commit comments