Skip to content

Commit 6fa71cf

Browse files
committed
Fixed using modules_to_patch with modules without dir()
- check for existing dir() before calling it - fixes #450
1 parent 0e43445 commit 6fa71cf

File tree

4 files changed

+10
-26
lines changed

4 files changed

+10
-26
lines changed

Diff for: CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The release versions are PyPi releases.
66
#### New Features
77

88
#### Fixes
9+
* fixed using `modules_to_patch` with modules without `dir()`
10+
([#450](../../issues/450))
911
* fixed recursion error on unpickling the fake file system
1012
([#445](../../issues/445))
1113
* allow trailing path in `add_real_directory` ([#446](../../issues/446))

Diff for: pyfakefs/fake_filesystem.py

-9
Original file line numberDiff line numberDiff line change
@@ -4479,15 +4479,6 @@ class FakeIoModule(object):
44794479
my_io_module = fake_filesystem.FakeIoModule(filesystem)
44804480
"""
44814481

4482-
@staticmethod
4483-
def dir():
4484-
"""Return the list of patched function names. Used for patching
4485-
functions imported from the module.
4486-
"""
4487-
# `open` would clash with build-in `open`, so don't patch it
4488-
# if imported like `from io import open`
4489-
return ()
4490-
44914482
def __init__(self, filesystem):
44924483
"""
44934484
Args:

Diff for: pyfakefs/fake_filesystem_unittest.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,14 @@ def __init__(self, additional_skip_names=None,
347347
modnames = (
348348
(mod_name, OS_MODULE) if mod_name == 'os' else (mod_name,)
349349
)
350-
for fct_name in fake_module.dir():
351-
self._fake_module_functions[fct_name] = (
352-
modnames,
353-
getattr(fake_module, fct_name),
354-
mod_name
355-
)
350+
if (hasattr(fake_module, 'dir') and
351+
inspect.isfunction(fake_module.dir)):
352+
for fct_name in fake_module.dir():
353+
self._fake_module_functions[fct_name] = (
354+
modnames,
355+
getattr(fake_module, fct_name),
356+
mod_name
357+
)
356358
# special handling for functions in os.path
357359
fake_module = fake_filesystem.FakePathModule
358360
for fct_name in fake_module.dir():

Diff for: pyfakefs/fake_pathlib.py

-11
Original file line numberDiff line numberDiff line change
@@ -635,13 +635,6 @@ class FakePathlibModule(object):
635635
`fake_pathlib_module = fake_filesystem.FakePathlibModule(filesystem)`
636636
"""
637637

638-
@staticmethod
639-
def dir():
640-
"""Return an empty list as `Path` methods will always be called
641-
on the instances.
642-
"""
643-
return ()
644-
645638
def __init__(self, filesystem):
646639
"""
647640
Initializes the module with the given filesystem.
@@ -685,10 +678,6 @@ class FakePathlibPathModule(object):
685678
"""Patches `pathlib.Path` by passing all calls to FakePathlibModule."""
686679
fake_pathlib = None
687680

688-
@staticmethod
689-
def dir():
690-
return ()
691-
692681
def __init__(self, filesystem):
693682
if self.fake_pathlib is None:
694683
self.__class__.fake_pathlib = FakePathlibModule(filesystem)

0 commit comments

Comments
 (0)