Skip to content

Commit 401cdd3

Browse files
boegelFlamefire
authored andcommitted
let caller decide whether or not to copy whole file + determine whether file-like object is called just based on 'read' method
1 parent d32e203 commit 401cdd3

File tree

2 files changed

+1
-8
lines changed

2 files changed

+1
-8
lines changed

easybuild/tools/filetools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def write_file(path, data, append=False, forced=False, backup=False, always_over
246246
# cfr. https://docs.python.org/3/library/functions.html#open
247247
mode = 'a' if append else 'w'
248248

249-
data_is_file_obj = all(hasattr(data, x) for x in ('read', 'seek', 'tell', 'write'))
249+
data_is_file_obj = hasattr(data, 'read')
250250

251251
# special care must be taken with binary data in Python 3
252252
if sys.version_info[0] >= 3 and (isinstance(data, bytes) or data_is_file_obj):
@@ -258,7 +258,6 @@ def write_file(path, data, append=False, forced=False, backup=False, always_over
258258
with open_file(path, mode) as fh:
259259
if data_is_file_obj:
260260
# if a file-like object was provided, use copyfileobj (which reads the file in chunks)
261-
data.seek(0)
262261
shutil.copyfileobj(data, fh)
263262
else:
264263
fh.write(data)

test/framework/filetools.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,12 +716,6 @@ def test_write_file_obj(self):
716716
ft.write_file(fp_out, fh)
717717
self.assertEqual(ft.read_file(fp_out), ft.read_file(fp))
718718

719-
# works fine even if same data was already read through the provided file handle
720-
with ft.open_file(fp, 'rb') as fh:
721-
fh.read(4)
722-
ft.write_file(fp_out, fh)
723-
self.assertEqual(ft.read_file(fp_out), ft.read_file(fp))
724-
725719
# Write a binary file
726720
fp = os.path.join(self.test_prefix, 'test.bin')
727721
fp_out = os.path.join(self.test_prefix, 'test_out.bin')

0 commit comments

Comments
 (0)