Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
c3beb16d
Commit
c3beb16d
authored
Apr 15, 2016
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
tools/mpy-tool.py: Add support for Python 2.7.
parent
091dcaea
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tools/mpy-tool.py
+30
-13
30 additions, 13 deletions
tools/mpy-tool.py
with
30 additions
and
13 deletions
tools/mpy-tool.py
+
30
−
13
View file @
c3beb16d
...
@@ -24,6 +24,23 @@
...
@@ -24,6 +24,23 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# THE SOFTWARE.
# Python 2/3 compatibility code
from
__future__
import
print_function
import
platform
if
platform
.
python_version_tuple
()[
0
]
==
'
2
'
:
str_cons
=
lambda
val
,
enc
=
None
:
val
bytes_cons
=
lambda
val
,
enc
=
None
:
bytearray
(
val
)
is_str_type
=
lambda
o
:
type
(
o
)
is
str
is_bytes_type
=
lambda
o
:
type
(
o
)
is
bytearray
is_int_type
=
lambda
o
:
type
(
o
)
is
int
or
type
(
o
)
is
long
else
:
str_cons
=
str
bytes_cons
=
bytes
is_str_type
=
lambda
o
:
type
(
o
)
is
str
is_bytes_type
=
lambda
o
:
type
(
o
)
is
bytes
is_int_type
=
lambda
o
:
type
(
o
)
is
int
# end compatibility code
import
sys
import
sys
from
collections
import
namedtuple
from
collections
import
namedtuple
...
@@ -67,7 +84,7 @@ def make_opcode_format():
...
@@ -67,7 +84,7 @@ def make_opcode_format():
Q
=
1
Q
=
1
V
=
2
V
=
2
O
=
3
O
=
3
return
bytes
((
return
bytes
_cons
((
# this table is taken verbatim from py/bc.c
# this table is taken verbatim from py/bc.c
OC4
(
U
,
U
,
U
,
U
),
# 0x00-0x03
OC4
(
U
,
U
,
U
,
U
),
# 0x00-0x03
OC4
(
U
,
U
,
U
,
U
),
# 0x04-0x07
OC4
(
U
,
U
,
U
,
U
),
# 0x04-0x07
...
@@ -255,16 +272,16 @@ class RawCode:
...
@@ -255,16 +272,16 @@ class RawCode:
# generate constant objects
# generate constant objects
for
i
,
obj
in
enumerate
(
self
.
objs
):
for
i
,
obj
in
enumerate
(
self
.
objs
):
obj_name
=
'
const_obj_%s_%u
'
%
(
self
.
escaped_name
,
i
)
obj_name
=
'
const_obj_%s_%u
'
%
(
self
.
escaped_name
,
i
)
if
type
(
obj
)
is
str
:
if
is_str_
type
(
obj
):
obj
=
bytes
(
obj
,
'
utf8
'
)
obj
=
bytes
_cons
(
obj
,
'
utf8
'
)
print
(
'
STATIC const mp_obj_str_t %s =
'
print
(
'
STATIC const mp_obj_str_t %s =
'
'
{{&mp_type_str}, 0, %u, (const byte*)
"
%s
"
};
'
'
{{&mp_type_str}, 0, %u, (const byte*)
"
%s
"
};
'
%
(
obj_name
,
len
(
obj
),
''
.
join
((
'
\\
x%02x
'
%
b
)
for
b
in
obj
)))
%
(
obj_name
,
len
(
obj
),
''
.
join
((
'
\\
x%02x
'
%
b
)
for
b
in
obj
)))
elif
type
(
obj
)
is
bytes
:
elif
is_bytes_
type
(
obj
):
print
(
'
STATIC const mp_obj_str_t %s =
'
print
(
'
STATIC const mp_obj_str_t %s =
'
'
{{&mp_type_bytes}, 0, %u, (const byte*)
"
%s
"
};
'
'
{{&mp_type_bytes}, 0, %u, (const byte*)
"
%s
"
};
'
%
(
obj_name
,
len
(
obj
),
''
.
join
((
'
\\
x%02x
'
%
b
)
for
b
in
obj
)))
%
(
obj_name
,
len
(
obj
),
''
.
join
((
'
\\
x%02x
'
%
b
)
for
b
in
obj
)))
elif
type
(
obj
)
is
int
:
elif
is_int_
type
(
obj
):
if
config
.
MICROPY_LONGINT_IMPL
==
config
.
MICROPY_LONGINT_IMPL_NONE
:
if
config
.
MICROPY_LONGINT_IMPL
==
config
.
MICROPY_LONGINT_IMPL_NONE
:
# TODO check if we can actually fit this long-int into a small-int
# TODO check if we can actually fit this long-int into a small-int
raise
FreezeError
(
self
,
'
target does not support long int
'
)
raise
FreezeError
(
self
,
'
target does not support long int
'
)
...
@@ -327,7 +344,7 @@ class RawCode:
...
@@ -327,7 +344,7 @@ class RawCode:
def
read_uint
(
f
):
def
read_uint
(
f
):
i
=
0
i
=
0
while
True
:
while
True
:
b
=
f
.
read
(
1
)[
0
]
b
=
bytes_cons
(
f
.
read
(
1
)
)
[
0
]
i
=
(
i
<<
7
)
|
(
b
&
0x7f
)
i
=
(
i
<<
7
)
|
(
b
&
0x7f
)
if
b
&
0x80
==
0
:
if
b
&
0x80
==
0
:
break
break
...
@@ -337,7 +354,7 @@ global_qstrs = []
...
@@ -337,7 +354,7 @@ global_qstrs = []
qstr_type
=
namedtuple
(
'
qstr
'
,
(
'
str
'
,
'
qstr_esc
'
,
'
qstr_id
'
))
qstr_type
=
namedtuple
(
'
qstr
'
,
(
'
str
'
,
'
qstr_esc
'
,
'
qstr_id
'
))
def
read_qstr
(
f
):
def
read_qstr
(
f
):
ln
=
read_uint
(
f
)
ln
=
read_uint
(
f
)
data
=
str
(
f
.
read
(
ln
),
'
utf8
'
)
data
=
str
_cons
(
f
.
read
(
ln
),
'
utf8
'
)
qstr_esc
=
qstrutil
.
qstr_escape
(
data
)
qstr_esc
=
qstrutil
.
qstr_escape
(
data
)
global_qstrs
.
append
(
qstr_type
(
data
,
qstr_esc
,
'
MP_QSTR_
'
+
qstr_esc
))
global_qstrs
.
append
(
qstr_type
(
data
,
qstr_esc
,
'
MP_QSTR_
'
+
qstr_esc
))
return
len
(
global_qstrs
)
-
1
return
len
(
global_qstrs
)
-
1
...
@@ -349,15 +366,15 @@ def read_obj(f):
...
@@ -349,15 +366,15 @@ def read_obj(f):
else
:
else
:
buf
=
f
.
read
(
read_uint
(
f
))
buf
=
f
.
read
(
read_uint
(
f
))
if
obj_type
==
b
'
s
'
:
if
obj_type
==
b
'
s
'
:
return
str
(
buf
,
'
utf8
'
)
return
str
_cons
(
buf
,
'
utf8
'
)
elif
obj_type
==
b
'
b
'
:
elif
obj_type
==
b
'
b
'
:
return
buf
return
bytes_cons
(
buf
)
elif
obj_type
==
b
'
i
'
:
elif
obj_type
==
b
'
i
'
:
return
int
(
str
(
buf
,
'
ascii
'
),
10
)
return
int
(
str
_cons
(
buf
,
'
ascii
'
),
10
)
elif
obj_type
==
b
'
f
'
:
elif
obj_type
==
b
'
f
'
:
return
float
(
str
(
buf
,
'
ascii
'
))
return
float
(
str
_cons
(
buf
,
'
ascii
'
))
elif
obj_type
==
b
'
c
'
:
elif
obj_type
==
b
'
c
'
:
return
complex
(
str
(
buf
,
'
ascii
'
))
return
complex
(
str
_cons
(
buf
,
'
ascii
'
))
else
:
else
:
assert
0
assert
0
...
@@ -389,7 +406,7 @@ def read_raw_code(f):
...
@@ -389,7 +406,7 @@ def read_raw_code(f):
def
read_mpy
(
filename
):
def
read_mpy
(
filename
):
with
open
(
filename
,
'
rb
'
)
as
f
:
with
open
(
filename
,
'
rb
'
)
as
f
:
header
=
f
.
read
(
4
)
header
=
bytes_cons
(
f
.
read
(
4
)
)
if
header
[
0
]
!=
ord
(
'
M
'
):
if
header
[
0
]
!=
ord
(
'
M
'
):
raise
Exception
(
'
not a valid .mpy file
'
)
raise
Exception
(
'
not a valid .mpy file
'
)
if
header
[
1
]
!=
0
:
if
header
[
1
]
!=
0
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment