diff --git a/fs/ftpfs.py b/fs/ftpfs.py index 4d81f3ea..d1c2410f 100644 --- a/fs/ftpfs.py +++ b/fs/ftpfs.py @@ -668,8 +668,8 @@ def openbin(self, path, mode="r", buffering=-1, **options): else: if info.is_dir: raise errors.FileExpected(path) - if _mode.exclusive: - raise errors.FileExists(path) + if _mode.exclusive: + raise errors.FileExists(path) ftp_file = FTPFile(self, _path, mode) return ftp_file # type: ignore diff --git a/fs/osfs.py b/fs/osfs.py index 961d8bfc..e9133abc 100644 --- a/fs/osfs.py +++ b/fs/osfs.py @@ -321,8 +321,8 @@ def openbin(self, path, mode="r", buffering=-1, **options): _path = self.validatepath(path) sys_path = self._to_sys_path(_path) with convert_os_errors("openbin", path): - if six.PY2 and _mode.exclusive and self.exists(path): - raise errors.FileExists(path) + if six.PY2 and _mode.exclusive: + sys_path = os.open(sys_path, os.O_RDWR | os.O_CREAT | os.O_EXCL) binary_file = io.open( sys_path, mode=_mode.to_platform_bin(), buffering=buffering, **options ) @@ -416,7 +416,7 @@ def open( sys_path = self._to_sys_path(_path) with convert_os_errors("open", path): if six.PY2 and _mode.exclusive: - sys_path = os.open(sys_path, os.O_CREAT | os.O_EXCL) + sys_path = os.open(sys_path, os.O_RDWR | os.O_CREAT | os.O_EXCL) _encoding = encoding or "utf-8" return io.open( sys_path, diff --git a/fs/test.py b/fs/test.py index 8688b40f..a65188a4 100644 --- a/fs/test.py +++ b/fs/test.py @@ -712,10 +712,6 @@ def test_open(self): f.write(text) self.assertTrue(f.closed) - with self.assertRaises(errors.FileExists): - with self.fs.open("foo/hello", "xt") as f: - pass - # Read it back with self.fs.open("foo/hello", "rt") as f: self.assertIsInstance(f, io.IOBase) @@ -971,6 +967,20 @@ def test_openbin(self): with self.assertRaises(ValueError): self.fs.openbin("foo.bin", "h") + def test_open_exclusive(self): + with self.fs.open("test_open_exclusive", "x") as f: + f.write("bananas") + + with self.assertRaises(errors.FileExists): + self.fs.open("test_open_exclusive", "x") + + def test_openbin_exclusive(self): + with self.fs.openbin("test_openbin_exclusive", "x") as f: + f.write(b"bananas") + + with self.assertRaises(errors.FileExists): + self.fs.openbin("test_openbin_exclusive", "x") + def test_opendir(self): # Make a simple directory structure self.fs.makedir("foo")