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
5985e41a
Commit
5985e41a
authored
9 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
tools/make-frozen.py: Properly escape hex chars when making C strings.
parent
1e2f8292
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tools/make-frozen.py
+27
-7
27 additions, 7 deletions
tools/make-frozen.py
with
27 additions
and
7 deletions
tools/make-frozen.py
+
27
−
7
View file @
5985e41a
...
@@ -53,11 +53,31 @@ print("};")
...
@@ -53,11 +53,31 @@ print("};")
print
(
"
const char mp_frozen_str_content[] = {
"
)
print
(
"
const char mp_frozen_str_content[] = {
"
)
for
f
,
st
in
modules
:
for
f
,
st
in
modules
:
data
=
open
(
sys
.
argv
[
1
]
+
"
/
"
+
f
,
"
rb
"
).
read
()
data
=
open
(
sys
.
argv
[
1
]
+
"
/
"
+
f
,
"
rb
"
).
read
()
# Python2 vs Python3 tricks
data
=
repr
(
data
)
# We need to properly escape the script data to create a C string.
if
data
[
0
]
==
"
b
"
:
# When C parses hex characters of the form \x00 it keeps parsing the hex
data
=
data
[
1
:]
# data until it encounters a non-hex character. Thus one must create
data
=
data
[
1
:
-
1
]
# strings of the form "data\x01" "abc" to properly encode this kind of
data
=
data
.
replace
(
'"'
,
'
\\
"'
)
# data. We could just encode all characters as hex digits but it's nice
print
(
'"
%s
\\
0
"'
%
data
)
# to be able to read the resulting C code as ASCII when possible.
data
=
bytearray
(
data
)
# so Python2 extracts each byte as an integer
esc_dict
=
{
ord
(
'
\n
'
):
'
\\
n
'
,
ord
(
'
\r
'
):
'
\\
r
'
,
ord
(
'"'
):
'
\\
"'
,
ord
(
'
\\
'
):
'
\\\\
'
}
chrs
=
[
'"'
]
break_str
=
False
for
c
in
data
:
try
:
chrs
.
append
(
esc_dict
[
c
])
except
KeyError
:
if
32
<=
c
<=
126
:
if
break_str
:
chrs
.
append
(
'"
"'
)
break_str
=
False
chrs
.
append
(
chr
(
c
))
else
:
chrs
.
append
(
'
\\
x%02x
'
%
c
)
break_str
=
True
chrs
.
append
(
'
\\
0
"'
)
print
(
''
.
join
(
chrs
))
print
(
"
};
"
)
print
(
"
};
"
)
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