Skip to content

Commit c3beb16

Browse files
committed
tools/mpy-tool.py: Add support for Python 2.7.
1 parent 091dcae commit c3beb16

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

tools/mpy-tool.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
# THE SOFTWARE.
2626

27+
# Python 2/3 compatibility code
28+
from __future__ import print_function
29+
import platform
30+
if platform.python_version_tuple()[0] == '2':
31+
str_cons = lambda val, enc=None: val
32+
bytes_cons = lambda val, enc=None: bytearray(val)
33+
is_str_type = lambda o: type(o) is str
34+
is_bytes_type = lambda o: type(o) is bytearray
35+
is_int_type = lambda o: type(o) is int or type(o) is long
36+
else:
37+
str_cons = str
38+
bytes_cons = bytes
39+
is_str_type = lambda o: type(o) is str
40+
is_bytes_type = lambda o: type(o) is bytes
41+
is_int_type = lambda o: type(o) is int
42+
# end compatibility code
43+
2744
import sys
2845
from collections import namedtuple
2946

@@ -67,7 +84,7 @@ def OC4(a, b, c, d):
6784
Q = 1
6885
V = 2
6986
O = 3
70-
return bytes((
87+
return bytes_cons((
7188
# this table is taken verbatim from py/bc.c
7289
OC4(U, U, U, U), # 0x00-0x03
7390
OC4(U, U, U, U), # 0x04-0x07
@@ -255,16 +272,16 @@ def freeze(self, parent_name):
255272
# generate constant objects
256273
for i, obj in enumerate(self.objs):
257274
obj_name = 'const_obj_%s_%u' % (self.escaped_name, i)
258-
if type(obj) is str:
259-
obj = bytes(obj, 'utf8')
275+
if is_str_type(obj):
276+
obj = bytes_cons(obj, 'utf8')
260277
print('STATIC const mp_obj_str_t %s = '
261278
'{{&mp_type_str}, 0, %u, (const byte*)"%s"};'
262279
% (obj_name, len(obj), ''.join(('\\x%02x' % b) for b in obj)))
263-
elif type(obj) is bytes:
280+
elif is_bytes_type(obj):
264281
print('STATIC const mp_obj_str_t %s = '
265282
'{{&mp_type_bytes}, 0, %u, (const byte*)"%s"};'
266283
% (obj_name, len(obj), ''.join(('\\x%02x' % b) for b in obj)))
267-
elif type(obj) is int:
284+
elif is_int_type(obj):
268285
if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
269286
# TODO check if we can actually fit this long-int into a small-int
270287
raise FreezeError(self, 'target does not support long int')
@@ -327,7 +344,7 @@ def freeze(self, parent_name):
327344
def read_uint(f):
328345
i = 0
329346
while True:
330-
b = f.read(1)[0]
347+
b = bytes_cons(f.read(1))[0]
331348
i = (i << 7) | (b & 0x7f)
332349
if b & 0x80 == 0:
333350
break
@@ -337,7 +354,7 @@ def read_uint(f):
337354
qstr_type = namedtuple('qstr', ('str', 'qstr_esc', 'qstr_id'))
338355
def read_qstr(f):
339356
ln = read_uint(f)
340-
data = str(f.read(ln), 'utf8')
357+
data = str_cons(f.read(ln), 'utf8')
341358
qstr_esc = qstrutil.qstr_escape(data)
342359
global_qstrs.append(qstr_type(data, qstr_esc, 'MP_QSTR_' + qstr_esc))
343360
return len(global_qstrs) - 1
@@ -349,15 +366,15 @@ def read_obj(f):
349366
else:
350367
buf = f.read(read_uint(f))
351368
if obj_type == b's':
352-
return str(buf, 'utf8')
369+
return str_cons(buf, 'utf8')
353370
elif obj_type == b'b':
354-
return buf
371+
return bytes_cons(buf)
355372
elif obj_type == b'i':
356-
return int(str(buf, 'ascii'), 10)
373+
return int(str_cons(buf, 'ascii'), 10)
357374
elif obj_type == b'f':
358-
return float(str(buf, 'ascii'))
375+
return float(str_cons(buf, 'ascii'))
359376
elif obj_type == b'c':
360-
return complex(str(buf, 'ascii'))
377+
return complex(str_cons(buf, 'ascii'))
361378
else:
362379
assert 0
363380

@@ -389,7 +406,7 @@ def read_raw_code(f):
389406

390407
def read_mpy(filename):
391408
with open(filename, 'rb') as f:
392-
header = f.read(4)
409+
header = bytes_cons(f.read(4))
393410
if header[0] != ord('M'):
394411
raise Exception('not a valid .mpy file')
395412
if header[1] != 0:

0 commit comments

Comments
 (0)