Skip to content

Commit 882fb15

Browse files
committed
Avoid API breakage under Python 2
1 parent 76818b3 commit 882fb15

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ docs/_build
77
build
88
dist
99
.cache
10+
.pytest_cache

atomicwrites/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
except ImportError:
1010
fcntl = None
1111

12-
__version__ = '1.1.5'
12+
__version__ = '1.2.0'
1313

1414

1515
PY2 = sys.version_info[0] == 2
@@ -23,6 +23,9 @@ def _path_to_unicode(x):
2323
return x
2424

2525

26+
DEFAULT_MODE = "wb" if PY2 else "w"
27+
28+
2629
_proper_fsync = os.fsync
2730

2831

@@ -110,7 +113,8 @@ class AtomicWriter(object):
110113
f.write(...)
111114
112115
:param path: The destination filepath. May or may not exist.
113-
:param mode: The filemode for the temporary file.
116+
:param mode: The filemode for the temporary file. This defaults to `wb` in
117+
Python 2 and `w` in Python 3.
114118
:param overwrite: If set to false, an error is raised if ``path`` exists.
115119
Errors are only raised after the file has been written to. Either way,
116120
the operation is atomic.
@@ -119,7 +123,8 @@ class AtomicWriter(object):
119123
subclass.
120124
'''
121125

122-
def __init__(self, path, mode='w', overwrite=False, **open_kwargs):
126+
def __init__(self, path, mode=DEFAULT_MODE, overwrite=False,
127+
**open_kwargs):
123128
if 'a' in mode:
124129
raise ValueError(
125130
'Appending to an existing file is not supported, because that '

tests/test_atomicwrites.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def test_atomic_write(tmpdir):
1010
fname = tmpdir.join('ha')
1111
for i in range(2):
1212
with atomic_write(str(fname), overwrite=True) as f:
13-
f.write(u'hoho')
13+
f.write('hoho')
1414

1515
with pytest.raises(OSError) as excinfo:
1616
with atomic_write(str(fname), overwrite=False) as f:
17-
f.write(u'haha')
17+
f.write('haha')
1818

1919
assert excinfo.value.errno == errno.EEXIST
2020

@@ -34,7 +34,7 @@ def test_teardown(tmpdir):
3434
def test_replace_simultaneously_created_file(tmpdir):
3535
fname = tmpdir.join('ha')
3636
with atomic_write(str(fname), overwrite=True) as f:
37-
f.write(u'hoho')
37+
f.write('hoho')
3838
fname.write('harhar')
3939
assert fname.read() == 'harhar'
4040
assert fname.read() == 'hoho'
@@ -45,7 +45,7 @@ def test_dont_remove_simultaneously_created_file(tmpdir):
4545
fname = tmpdir.join('ha')
4646
with pytest.raises(OSError) as excinfo:
4747
with atomic_write(str(fname), overwrite=False) as f:
48-
f.write(u'hoho')
48+
f.write('hoho')
4949
fname.write('harhar')
5050
assert fname.read() == 'harhar'
5151

@@ -59,11 +59,13 @@ def test_dont_remove_simultaneously_created_file(tmpdir):
5959
def test_open_reraise(tmpdir):
6060
fname = tmpdir.join('ha')
6161
with pytest.raises(AssertionError):
62-
with atomic_write(str(fname), overwrite=False) as f:
63-
# Mess with f, so commit will trigger a ValueError. We're testing
64-
# that the initial AssertionError triggered below is propagated up
65-
# the stack, not the second exception triggered during commit.
66-
f.close()
62+
aw = atomic_write(str(fname), overwrite=False)
63+
with aw:
64+
# Mess with internals, so commit will trigger a ValueError. We're
65+
# testing that the initial AssertionError triggered below is
66+
# propagated up the stack, not the second exception triggered
67+
# during commit.
68+
aw.rollback = lambda: 1 / 0
6769
# Now trigger our own exception.
6870
assert False, "Intentional failure for testing purposes"
6971

@@ -75,11 +77,11 @@ def test_atomic_write_in_pwd(tmpdir):
7577
fname = 'ha'
7678
for i in range(2):
7779
with atomic_write(str(fname), overwrite=True) as f:
78-
f.write(u'hoho')
80+
f.write('hoho')
7981

8082
with pytest.raises(OSError) as excinfo:
8183
with atomic_write(str(fname), overwrite=False) as f:
82-
f.write(u'haha')
84+
f.write('haha')
8385

8486
assert excinfo.value.errno == errno.EEXIST
8587

0 commit comments

Comments
 (0)