Skip to content

Commit 1a1ed3f

Browse files
sqwishywillmcgugan
authored andcommitted
Use os.O_RDWR for exclusive mode in Python 2.7 (#215)
* Use os.O_RDWR for exclusive mode in Python 2.7 Also doing the exclusive thing properly in openbin. (#182) * TestCase unique filename? Write bytes with openbin * FTPFS check mode.exclusive only if file info found
1 parent cd1a973 commit 1a1ed3f

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

fs/ftpfs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,8 @@ def openbin(self, path, mode="r", buffering=-1, **options):
668668
else:
669669
if info.is_dir:
670670
raise errors.FileExpected(path)
671-
if _mode.exclusive:
672-
raise errors.FileExists(path)
671+
if _mode.exclusive:
672+
raise errors.FileExists(path)
673673
ftp_file = FTPFile(self, _path, mode)
674674
return ftp_file # type: ignore
675675

fs/osfs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ def openbin(self, path, mode="r", buffering=-1, **options):
321321
_path = self.validatepath(path)
322322
sys_path = self._to_sys_path(_path)
323323
with convert_os_errors("openbin", path):
324-
if six.PY2 and _mode.exclusive and self.exists(path):
325-
raise errors.FileExists(path)
324+
if six.PY2 and _mode.exclusive:
325+
sys_path = os.open(sys_path, os.O_RDWR | os.O_CREAT | os.O_EXCL)
326326
binary_file = io.open(
327327
sys_path, mode=_mode.to_platform_bin(), buffering=buffering, **options
328328
)
@@ -416,7 +416,7 @@ def open(
416416
sys_path = self._to_sys_path(_path)
417417
with convert_os_errors("open", path):
418418
if six.PY2 and _mode.exclusive:
419-
sys_path = os.open(sys_path, os.O_CREAT | os.O_EXCL)
419+
sys_path = os.open(sys_path, os.O_RDWR | os.O_CREAT | os.O_EXCL)
420420
_encoding = encoding or "utf-8"
421421
return io.open(
422422
sys_path,

fs/test.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,10 +712,6 @@ def test_open(self):
712712
f.write(text)
713713
self.assertTrue(f.closed)
714714

715-
with self.assertRaises(errors.FileExists):
716-
with self.fs.open("foo/hello", "xt") as f:
717-
pass
718-
719715
# Read it back
720716
with self.fs.open("foo/hello", "rt") as f:
721717
self.assertIsInstance(f, io.IOBase)
@@ -971,6 +967,20 @@ def test_openbin(self):
971967
with self.assertRaises(ValueError):
972968
self.fs.openbin("foo.bin", "h")
973969

970+
def test_open_exclusive(self):
971+
with self.fs.open("test_open_exclusive", "x") as f:
972+
f.write("bananas")
973+
974+
with self.assertRaises(errors.FileExists):
975+
self.fs.open("test_open_exclusive", "x")
976+
977+
def test_openbin_exclusive(self):
978+
with self.fs.openbin("test_openbin_exclusive", "x") as f:
979+
f.write(b"bananas")
980+
981+
with self.assertRaises(errors.FileExists):
982+
self.fs.openbin("test_openbin_exclusive", "x")
983+
974984
def test_opendir(self):
975985
# Make a simple directory structure
976986
self.fs.makedir("foo")

0 commit comments

Comments
 (0)