[BUG FIX] [MINOR] Fix encoding with unicode characters.

When passing a unicode string to builder.CreateString, the
default encoding assumed all characters can be encoded
using ascii. Added a fix so a user can specify the
encoding and how to handle errors when creating strings.
This commit is contained in:
Faizan Rashid
2015-12-31 09:41:00 +05:00
parent 4dcaec7938
commit af23683311
2 changed files with 22 additions and 8 deletions

View File

@@ -361,14 +361,14 @@ class Builder(object):
self.PlaceUOffsetT(vectorNumElems)
return self.Offset()
def CreateString(self, s):
def CreateString(self, s, encoding='utf-8', errors='strict'):
"""CreateString writes a null-terminated byte string as a vector."""
self.assertNotNested()
self.nested = True
if isinstance(s, compat.string_types):
x = s.encode()
x = s.encode(encoding, errors)
elif isinstance(s, compat.binary_type):
x = s
else: