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

Fix WriteFile with unicode data

Unicode is fun, indeed:

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

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 <>
Reviewed-by: default avatarMichael Hanselmann <>
parent fd09d178
......@@ -135,11 +135,15 @@ def WriteFile(file_name, fn=None, data=None,
if callable(prewrite):
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
......@@ -258,6 +258,11 @@ class TestWriteFile(unittest.TestCase):
utils.WriteFile(, data=data)
self.assertEqual(utils.ReadFile(, data)
def testWriteSimpleUnicode(self):
data = u"abc"
utils.WriteFile(, data=data)
self.assertEqual(utils.ReadFile(, data)
def testErrors(self):
self.assertRaises(errors.ProgrammerError, utils.WriteFile,, 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