Skip to content

Commit f14b9f9

Browse files
committed
Correctly propagate "permission denied" error on creating directory
- fixes #507
1 parent d03db1f commit f14b9f9

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ the proposed changes so you can be ready.
1616
#### New Features
1717
* Added support for handling keyword-only arguments in some `os` functions
1818

19+
#### Fixes
20+
* Fixed behavior of `os.makedirs` in write-protected directory
21+
(see [#507](../../issues/507))
22+
1923
## [Version 3.7](https://pypi.python.org/pypi/pyfakefs/3.7)
2024

2125
This version adds support for Python 3.8.

pyfakefs/fake_filesystem.py

+3
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,9 @@ def makedirs(self, dir_name, mode=PERM_DEF, exist_ok=False):
28542854
try:
28552855
self.create_dir(dir_name, mode & ~self.umask)
28562856
except (IOError, OSError) as e:
2857+
if e.errno == errno.EACCES:
2858+
# permission denied - propagate exception
2859+
raise
28572860
if (not exist_ok or
28582861
not isinstance(self.resolve(dir_name), FakeDirectory)):
28592862
if self.is_windows_fs and e.errno == errno.ENOTDIR:

pyfakefs/tests/fake_os_test.py

+14
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,20 @@ def test_makedirs_exist_ok(self):
16941694
self.os.makedirs(directory, exist_ok=True)
16951695
self.assertTrue(self.os.path.exists(directory))
16961696

1697+
def test_makedirs_in_write_protected_dir(self):
1698+
self.check_posix_only()
1699+
directory = self.make_path('foo')
1700+
self.os.mkdir(directory, mode=0o555)
1701+
subdir = self.os.path.join(directory, 'bar')
1702+
if not is_root():
1703+
self.assert_raises_os_error(errno.EACCES, self.os.makedirs,
1704+
subdir, exist_ok=True)
1705+
self.assert_raises_os_error(errno.EACCES, self.os.makedirs,
1706+
subdir, exist_ok=False)
1707+
else:
1708+
self.os.makedirs(subdir)
1709+
self.assertTrue(self.os.path.exists(subdir))
1710+
16971711
# test fsync and fdatasync
16981712
def test_fsync_raises_on_non_int(self):
16991713
self.assertRaises(TypeError, self.os.fsync, "zero")

0 commit comments

Comments
 (0)