Skip to content

Commit fade927

Browse files
committed
fix incompatible len() on str(a.k.a. unicode) in python3 and str (a.k.a. bytes) in python2
1 parent c4e2c03 commit fade927

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

msgfmt.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,20 @@ def generate ():
6868
# the keys are sorted in the .mo file
6969
keys.sort()
7070
offsets = []
71-
ids = strs = ''
71+
ids = strs = b''
72+
7273
for _id in keys:
74+
_msg = MESSAGES[_id]
75+
7376
# For each string, we need size and file offset. Each string is NUL
7477
# terminated; the NUL does not count into the size.
75-
offsets.append((len(ids), len(_id), len(strs), len(MESSAGES[_id])))
76-
ids += _id + '\0'
77-
strs += MESSAGES[_id] + '\0'
78-
if sys.version_info.major >= 3:
79-
ids = ids.encode(encoding='UTF-8')
80-
strs = strs.encode(encoding='UTF-8')
78+
if sys.version_info.major >= 3:
79+
_id = _id.encode('UTF-8')
80+
_msg = _msg.encode('UTF-8')
81+
offsets.append((len(ids), len(_id), len(strs), len(_msg)))
82+
ids += _id + b'\0'
83+
strs += _msg + b'\0'
84+
8185
# The header is 7 32-bit unsigned integers. We don't use hash tables, so
8286
# the keys start right after the index tables.
8387
# translated string.
@@ -99,7 +103,7 @@ def generate ():
99103
7*4, # start of key index
100104
7*4+len(keys)*8, # start of value index
101105
0, 0) # size and offset of hash table
102-
output += array.array("i", offsets).tostring()
106+
output += array.array("i", offsets).tobytes() if sys.version_info.major >= 3 else array.array("i", offsets).tostring()
103107
output += ids
104108
output += strs
105109
return output

0 commit comments

Comments
 (0)