Skip to content

Commit 340ccf7

Browse files
committed
add new force_string argument to write_chunk helper in order to help converge on consistent stringification in all branches and still allow proper unit testing of invalid data. In 'experimental' we currently implicitly default to such forced stringification because of the explicit force_utf8 wrapping.
git-svn-id: svn+ssh://svn.code.sf.net/p/migrid/code/trunk@6043 b75ad72c-e7d7-11dd-a971-7dbc132099af
1 parent 649ab31 commit 340ccf7

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

mig/shared/fileio.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ def _auto_adjust_mode(data, mode):
8686

8787

8888
def _write_chunk(path, chunk, offset, logger=None, mode='r+b',
89-
make_parent=True, create_file=True):
89+
make_parent=True, create_file=True, force_string=False):
9090
"""Internal helper to wrap writing of chunks with offset to path.
9191
The optional make_parent and create_file are used to decide if the parent
9292
directory and the file should be created if it doesn't already exist.
93+
The optional force_string is used to force contents to string even if it's
94+
another type.
9395
"""
9496
# logger.debug("writing chunk to %r at offset %d" % (path, offset))
9597

@@ -130,7 +132,11 @@ def _write_chunk(path, chunk, offset, logger=None, mode='r+b',
130132
filehandle.write('\0')
131133
# logger.debug("write %r chunk of size %d at position %d" %
132134
# (path, len(chunk), filehandle.tell()))
133-
filehandle.write(chunk)
135+
# NOTE: any value/type of chunk is forced to str if requested
136+
if force_string:
137+
filehandle.write(str(chunk))
138+
else:
139+
filehandle.write(chunk)
134140
# logger.debug("file %r chunk written at %d" % (path, offset))
135141
return True
136142
except Exception as err:
@@ -139,7 +145,7 @@ def _write_chunk(path, chunk, offset, logger=None, mode='r+b',
139145
return False
140146

141147

142-
def write_chunk(path, chunk, offset, logger, mode='r+b'):
148+
def write_chunk(path, chunk, offset, logger, mode='r+b', force_string=False):
143149
"""Wrapper to handle writing of chunks with offset to path.
144150
Creates file first if it doesn't already exist.
145151
"""
@@ -149,10 +155,12 @@ def write_chunk(path, chunk, offset, logger, mode='r+b'):
149155
# TODO: enable this again once throuroughly tested and assured py2+3 safe
150156
# mode = _auto_adjust_mode(chunk, mode)
151157

152-
return _write_chunk(path, chunk, offset, logger, mode)
158+
return _write_chunk(path, chunk, offset, logger, mode,
159+
force_string=force_string)
153160

154161

155-
def write_file(content, path, logger, mode='w', make_parent=True, umask=None):
162+
def write_file(content, path, logger, mode='w', make_parent=True, umask=None,
163+
force_string=False):
156164
"""Wrapper to handle writing of contents to path"""
157165
if not logger:
158166
logger = null_logger("dummy")
@@ -165,7 +173,8 @@ def write_file(content, path, logger, mode='w', make_parent=True, umask=None):
165173
#mode = _auto_adjust_mode(content, mode)
166174

167175
retval = _write_chunk(path, content, offset=0, logger=logger, mode=mode,
168-
make_parent=make_parent, create_file=False)
176+
make_parent=make_parent, create_file=False,
177+
force_string=force_string)
169178

170179
if umask is not None:
171180
os.umask(old_umask)

tests/test_mig_shared_fileio.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def setUp(self):
6666
cleanpath(os.path.dirname(DUMMY_FILE_WRITECHUNK), self)
6767

6868
def test_return_false_on_invalid_data(self):
69-
did_succeed = fileio.write_chunk(self.tmp_path, 1234, 0, self.logger)
69+
# NOTE: we make sure to disable any forced stringification here
70+
did_succeed = fileio.write_chunk(self.tmp_path, 1234, 0, self.logger,
71+
force_string=False)
7072
self.assertFalse(did_succeed)
7173

7274
def test_return_false_on_invalid_offset(self):
@@ -144,7 +146,9 @@ def setUp(self):
144146
cleanpath(os.path.dirname(DUMMY_FILE_WRITEFILE), self)
145147

146148
def test_return_false_on_invalid_data(self):
147-
did_succeed = fileio.write_file(1234, self.tmp_path, self.logger)
149+
# NOTE: we make sure to disable any forced stringification here
150+
did_succeed = fileio.write_file(1234, self.tmp_path, self.logger,
151+
force_string=False)
148152
self.assertFalse(did_succeed)
149153

150154
def test_return_false_on_invalid_dir(self):

0 commit comments

Comments
 (0)