diff options
author | Damien George <damien.p.george@gmail.com> | 2016-11-16 23:53:55 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-11-16 23:53:55 +1100 |
commit | e01e214259ead9a71497c55550ede5e3d1bfe197 (patch) | |
tree | 0e1e454972f7839f679147bb3303f40ef3d6de27 /stmhal/make-stmconst.py | |
parent | 87f18c08c981cf257c39475ecbaded2a4aba3c28 (diff) | |
download | micropython-e01e214259ead9a71497c55550ede5e3d1bfe197.tar.gz micropython-e01e214259ead9a71497c55550ede5e3d1bfe197.zip |
stmhal/make-stmconst.py: Restore Python 2 compatibility.
Diffstat (limited to 'stmhal/make-stmconst.py')
-rw-r--r-- | stmhal/make-stmconst.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/stmhal/make-stmconst.py b/stmhal/make-stmconst.py index 7afafe7206..9aecbb97cf 100644 --- a/stmhal/make-stmconst.py +++ b/stmhal/make-stmconst.py @@ -12,6 +12,20 @@ from __future__ import print_function import argparse import re +# Python 2/3 compatibility +import platform +if platform.python_version_tuple()[0] == '2': + def convert_bytes_to_str(b): + return b +elif platform.python_version_tuple()[0] == '3': + def convert_bytes_to_str(b): + try: + return str(b, 'utf8') + except ValueError: + # some files have invalid utf8 bytes, so filter them out + return ''.join(chr(l) for l in b if l <= 126) +# end compatibility code + # given a list of (name,regex) pairs, find the first one that matches the given line def re_match_first(regexs, line): for name, regex in regexs: @@ -48,11 +62,7 @@ class Lexer: def next_match(self, strictly_next=False): while True: line = self.file.readline() - try: - line = str(line, 'utf8') - except ValueError: - # some files have invalid utf8 bytes, so filter them out - line = ''.join(chr(l) for l in line if l <= 126) + line = convert_bytes_to_str(line) self.line_number += 1 if len(line) == 0: return ('EOF', None) |