Commit 1d39e245 authored by Iustin Pop's avatar Iustin Pop
Browse files

Fix WriteFile with unicode data



Unicode is fun, indeed:

>>> len(buffer("abc"))
3
>>> len(buffer(u"abc"))
12

So we can't pass unicode data to buffer(), as the result will be to
write the in-memory (usually UTF-32) representation to disk.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent fd09d178
......@@ -135,11 +135,15 @@ def WriteFile(file_name, fn=None, data=None,
if callable(prewrite):
prewrite(fd)
if data is not None:
if isinstance(data, unicode):
data = data.encode()
assert isinstance(data, str)
to_write = len(data)
offset = 0
while offset < to_write:
written = os.write(fd, buffer(data, offset))
assert written >= 0
assert written <= to_write - offset
offset += written
assert offset == to_write
else:
......
......@@ -258,6 +258,11 @@ class TestWriteFile(unittest.TestCase):
utils.WriteFile(self.tfile.name, data=data)
self.assertEqual(utils.ReadFile(self.tfile.name), data)
def testWriteSimpleUnicode(self):
data = u"abc"
utils.WriteFile(self.tfile.name, data=data)
self.assertEqual(utils.ReadFile(self.tfile.name), data)
def testErrors(self):
self.assertRaises(errors.ProgrammerError, utils.WriteFile,
self.tfile.name, data="test", fn=lambda fd: None)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment