Skip to content

Commit 175b4ab

Browse files
authored
Normalize paths and replace instead of rename to make atomic files work in Windows. (hedyorg#3497)
Paths are now normalized by os. Some forward/backward slashes sometimes cause errors in finding files on windows. os.rename in utils changed to os.replace to make sure it works on Windows aswell (atomic when succesfull)
1 parent ba417f5 commit 175b4ab

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

tests/Highlighter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,11 @@ def get_state_machine(self, level, lang="en"):
207207
208208
Returns a state machine.
209209
"""
210-
root_dir = os.path.dirname(__file__) +"/.."
210+
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
211+
trad_file = os.path.normpath(root_dir+'/highlighting/highlighting-trad.json')
211212

212213
# get traduction
213-
with open(f'{root_dir}/highlighting/highlighting-trad.json') as file_regex_trad:
214+
with open(trad_file, 'r', encoding='utf-8') as file_regex_trad:
214215
data_regex_trad = json.load(file_regex_trad)
215216

216217
if lang not in data_regex_trad.keys():
@@ -219,7 +220,8 @@ def get_state_machine(self, level, lang="en"):
219220
regex_trad = data_regex_trad[lang]
220221

221222
# get state_machine
222-
with open(f'{root_dir}/highlighting/highlighting.json') as file_regex:
223+
sm_file = os.path.normpath(root_dir+'/highlighting/highlighting.json')
224+
with open(sm_file, 'r', encoding='utf-8') as file_regex:
223225
data_regex = json.load(file_regex)
224226

225227
# apply translation of keywords

tests/tests_z_yamlfile.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def test_load_yaml_equivalent(self):
1212
although we're not going to fail the test on the numbers we get from that.
1313
"""
1414
n = 50
15-
15+
1616
# Pick a file with unicode in it so we're sure it gets handled properly
17-
file = YamlFile(
18-
os.path.join(os.path.dirname(__file__), '..', 'content/adventures/hu.yaml'),
19-
try_pickle=True)
17+
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
18+
yaml_file = os.path.normpath(root_dir+'/content/adventures/hu.yaml')
19+
file = YamlFile(yaml_file, try_pickle=True)
2020

2121
# Remove pickled version of this file if it exists, it may
2222
# influence the tests
@@ -37,4 +37,4 @@ def test_load_yaml_equivalent(self):
3737
cached_seconds = time.time() - start
3838

3939
self.assertEqual(original_data, cached_data)
40-
print(f'YAML loading takes {original_seconds / n} seconds, unpickling takes {cached_seconds / n} ({original_seconds / cached_seconds:.1f}x faster)')
40+
print(f'YAML loading takes {original_seconds / n} seconds, unpickling takes {cached_seconds / n} ({original_seconds / cached_seconds:.1f}x faster)')

utils.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,19 @@ def atomic_write_file(filename, mode='wb'):
227227
"""Write to a filename atomically.
228228
229229
First write to a unique tempfile, then rename the tempfile into
230-
place. Use as a context manager:
230+
place. Use replace instead of rename to make it atomic on windows as well.
231+
Use as a context manager:
231232
232233
with atomic_write_file('file.txt') as f:
233234
f.write('hello')
234-
235-
THIS WON'T WORK ON WINDOWS -- atomic file renames don't overwrite
236-
on Windows. We now just swallow the exception, someone else already wrote the file?)
237235
"""
238236

239237
tmp_file = f'{filename}.{os.getpid()}'
240238
with open(tmp_file, mode) as f:
241239
yield f
240+
241+
os.replace(tmp_file, filename)
242242

243-
try:
244-
os.rename(tmp_file, filename)
245-
except IOError:
246-
pass
247243

248244
# This function takes a date in milliseconds from the Unix epoch and transforms it into a printable date
249245
# It operates by converting the date to a string, removing its last 3 digits, converting it back to an int

0 commit comments

Comments
 (0)