Skip to content

Commit d8e3dc1

Browse files
d-w-moorealanking
authored andcommitted
[#495] append modes seek to end on data_object.open(...)
1 parent fc5eaea commit d8e3dc1

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

irods/manager/data_object_manager.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,13 @@ def make_FileOpenRequest(**extra_opts):
450450
# Use case: auto_close has defaulted to the irods.configuration getter.
451451
# access entry in irods.configuration
452452
auto_close = auto_close()
453-
454453
if auto_close:
455-
return ManagedBufferedRandom(raw, _session = self.sess)
456-
457-
return io.BufferedRandom(raw)
454+
ret_value = ManagedBufferedRandom(raw, _session = self.sess)
455+
else:
456+
ret_value = io.BufferedRandom(raw)
457+
if 'a' in mode:
458+
ret_value.seek(0,io.SEEK_END)
459+
return ret_value
458460

459461
def trim(self, path, **options):
460462

irods/test/data_obj_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import random
1313
import re
14+
import six
1415
import socket
1516
import stat
1617
import string
@@ -2001,6 +2002,41 @@ def test_settings_load_and_save_471(self):
20012002
config.load(**load_logging_options)
20022003
self.assertTrue(config.data_objects.auto_close, RANDOM_VALUE - i - 1)
20032004

2005+
def test_append_mode_will_append_to_data_object__issue_495(self):
2006+
append_string = b'to_be_written'.lower()
2007+
reverse_bytes = lambda s: ''.join(reversed(s)) if six.PY2 else bytes(reversed(s))
2008+
session, data = (self.sess, self.sess.data_objects)
2009+
testfile = '{}/issue_495'.format(helpers.home_collection(session))
2010+
# Make sure data object doesn't exist.
2011+
self.assertFalse(data.exists(testfile))
2012+
try:
2013+
# Append to a previously nonexistent data object.
2014+
with data.open(testfile,'a') as f:
2015+
f.write(append_string.upper())
2016+
# Test that the data object, once opened, has the right offset. Then perform a second append.
2017+
with data.open(testfile,'a+') as f:
2018+
self.assertTrue(f.tell(), len(append_string))
2019+
f.write(append_string)
2020+
# Verify original content written at offset 0.
2021+
with data.open(testfile,'r') as f:
2022+
self.assertEqual(f.read(), append_string.upper()+append_string)
2023+
# Do an open for read/write (in particular, not appending); then perform a write.
2024+
# (This overwrites the original content at offset 0.)
2025+
with data.open(testfile,'r+') as f:
2026+
f.write(reverse_bytes(append_string))
2027+
# Final test of data object content:
2028+
# Test that (1) the non-appending write has modified the first half of the content, and
2029+
# (2) the second append has placed the written content in the right location.
2030+
with data.open(testfile,'r') as f:
2031+
f.seek(0,io.SEEK_END)
2032+
self.assertEqual(f.tell(), 2*len(append_string))
2033+
f.seek(0)
2034+
self.assertEqual(f.read(), reverse_bytes(append_string) + append_string)
2035+
finally:
2036+
if data.exists(testfile):
2037+
data.unlink(testfile,force=True)
2038+
2039+
20042040
if __name__ == '__main__':
20052041
# let the tests find the parent irods lib
20062042
sys.path.insert(0, os.path.abspath('../..'))

0 commit comments

Comments
 (0)