Skip to content

Commit 421ebd2

Browse files
authored
[Python3] Fix "undefined symbol 'unicode'" from python_lint (#33146)
* [Python3] Fix "undefined symbol 'unicode'" from python_lint This is a little tricky. Python 2 "unicode" was renamed to "str" in Python 3. For Python 2 compatibility, we need to use "unicode" in a couple of places, but that's not defined on Python 3, which causes python_lint errors (even if the reference is never actually executed). To workaround this, when running in Python 3, define "unicode" as a synonym for "str". This defines the symbol (avoiding the "undefined symbol" error from python lint) while preserving the correct functionality on both Python 2 and Python 3. When we drop Python 2 support (which we should do as soon as possible), we can drop this workaround and globally replace "unicode" with "str" to get the right Python 3-only functionality.
1 parent d3b5996 commit 421ebd2

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

test/Incremental/Verifier/gen-output-file-map.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
import sys
1010

1111

12+
# Python 2 `unicode` was renamed `str` in Python 3. To consistently support
13+
# both, define `unicode` to be `str` when using Python 3. Once we can drop
14+
# Python 2 support, delete this and change all uses of `unicode` to `str`.
15+
if sys.version_info[0] >= 3:
16+
unicode = str
17+
18+
1219
def fatal(msg):
1320
print(msg, file=sys.stderr)
1421
sys.exit(1)
@@ -62,21 +69,14 @@ def main(arguments):
6269
'swift-dependencies': './main-buildrecord.swiftdeps'
6370
}
6471

65-
# TODO: this is for python 2 and python 3 compatibility. Once Python 2 is
66-
# removed, this function can be removed.
67-
def to_unicode(r):
68-
if sys.version_info[0] < 3:
69-
return unicode(r)
70-
return str(r)
71-
7272
with io.open(output_path, 'w', encoding='utf-8', newline='\n') as f:
73-
f.write(to_unicode(json.dumps(all_records, ensure_ascii=False)))
73+
f.write(unicode(json.dumps(all_records, ensure_ascii=False)))
7474

7575
if args.response_output_file is not None:
7676
with io.open(args.response_output_file, 'w',
7777
encoding='utf-8', newline='\n') as f:
7878
for line in response_file_contents:
79-
f.write(to_unicode(line + " "))
79+
f.write(unicode(line + " "))
8080

8181

8282
if __name__ == '__main__':

utils/round-trip-syntax-test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ from functools import reduce
1414
logging.basicConfig(format='%(message)s', level=logging.INFO)
1515

1616

17+
# Python 2 `unicode` was renamed `str` in Python 3. To consistently support
18+
# both, define `unicode` to be `str` when using Python 3. Once we can drop
19+
# Python 2 support, delete this and change all uses of `unicode` to `str`.
20+
# (Currently, this is only here to avoid a python_lint failure from unicode
21+
# references below.)
22+
if sys.version_info[0] >= 3:
23+
unicode = str
24+
25+
1726
class RoundTripTask(object):
1827
def __init__(self, input_filename, action, swift_syntax_test,
1928
skip_bad_syntax):

0 commit comments

Comments
 (0)