Skip to content

Commit e6d1576

Browse files
DarwinAwardWinneruntitaker
authored andcommitted
Make atomic_write work on paths with no directory separator (untitaker#20)
Fixes untitaker#19.
1 parent 25ef5ec commit e6d1576

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

atomicwrites/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def _sync_directory(directory):
4343

4444
def _replace_atomic(src, dst):
4545
os.rename(src, dst)
46-
_sync_directory(os.path.dirname(dst))
46+
_sync_directory(os.path.normpath(os.path.dirname(dst)))
4747

4848
def _move_atomic(src, dst):
4949
os.link(src, dst)
5050
os.unlink(src)
5151

52-
src_dir = os.path.dirname(src)
53-
dst_dir = os.path.dirname(dst)
52+
src_dir = os.path.normpath(os.path.dirname(src))
53+
dst_dir = os.path.normpath(os.path.dirname(dst))
5454
_sync_directory(dst_dir)
5555
if src_dir != dst_dir:
5656
_sync_directory(src_dir)
@@ -161,7 +161,7 @@ def _open(self, get_fileobject):
161161
def get_fileobject(self, dir=None, **kwargs):
162162
'''Return the temporary file to use.'''
163163
if dir is None:
164-
dir = os.path.dirname(self._path)
164+
dir = os.path.normpath(os.path.dirname(self._path))
165165
return tempfile.NamedTemporaryFile(mode=self._mode, dir=dir,
166166
delete=False, **kwargs)
167167

tests/test_atomicwrites.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import errno
2+
import os
23

34
from atomicwrites import atomic_write
45

@@ -65,3 +66,24 @@ def test_open_reraise(tmpdir):
6566
f.name = "asdf"
6667
# Now trigger our own exception.
6768
assert False, "Intentional failure for testing purposes"
69+
70+
71+
def test_atomic_write_in_pwd(tmpdir):
72+
orig_curdir = os.getcwd()
73+
try:
74+
os.chdir(str(tmpdir))
75+
fname = 'ha'
76+
for i in range(2):
77+
with atomic_write(str(fname), overwrite=True) as f:
78+
f.write('hoho')
79+
80+
with pytest.raises(OSError) as excinfo:
81+
with atomic_write(str(fname), overwrite=False) as f:
82+
f.write('haha')
83+
84+
assert excinfo.value.errno == errno.EEXIST
85+
86+
assert open(fname).read() == 'hoho'
87+
assert len(tmpdir.listdir()) == 1
88+
finally:
89+
os.chdir(orig_curdir)

0 commit comments

Comments
 (0)