diff options
Diffstat (limited to 'TOOLS/file2string.py')
-rwxr-xr-x | TOOLS/file2string.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/TOOLS/file2string.py b/TOOLS/file2string.py index cb121e4884..6cdd1a72ae 100755 --- a/TOOLS/file2string.py +++ b/TOOLS/file2string.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python # Convert the contents of a file into a C string constant. # Note that the compiler will implicitly add an extra 0 byte at the end @@ -7,6 +7,10 @@ import sys +# Indexing a byte string yields int on Python 3.x, and a str on Python 2.x +def pord(c): + return ord(c) if type(c) == str else c + def main(infile): conv = ['\\' + ("%03o" % c) for c in range(256)] safe_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" \ @@ -16,7 +20,7 @@ def main(infile): for c, esc in ("\nn", "\tt", r"\\", '""'): conv[ord(c)] = '\\' + esc for line in infile: - sys.stdout.write('"' + ''.join(conv[c] for c in line) + '"\n') + sys.stdout.write('"' + ''.join(conv[pord(c)] for c in line) + '"\n') with open(sys.argv[1], 'rb') as infile: sys.stdout.write("// Generated from %s\n\n" % sys.argv[1]) |