Skip to content

Commit 3474352

Browse files
committed
remove to_bytes
1 parent 14ca5b6 commit 3474352

File tree

2 files changed

+7
-35
lines changed

2 files changed

+7
-35
lines changed

llvm/utils/lit/lit/TestRunner.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import lit.ShUtil as ShUtil
2222
import lit.Test as Test
2323
import lit.util
24-
from lit.util import to_bytes
2524
from lit.BooleanExpression import BooleanExpression
2625

2726

@@ -1419,19 +1418,11 @@ def parseIntegratedTestScriptCommands(source_path, keywords):
14191418
(line_number, command_type, line).
14201419
"""
14211420

1422-
# This code is carefully written to be dual compatible with Python 2.5+ and
1423-
# Python 3 without requiring input files to always have valid codings. The
1424-
# trick we use is to open the file in binary mode and use the regular
1425-
# expression library to find the commands, with it scanning strings in
1426-
# Python2 and bytes in Python3.
1427-
#
1428-
# Once we find a match, we do require each script line to be decodable to
1429-
# UTF-8, so we convert the outputs to UTF-8 before returning. This way the
1430-
# remaining code can work with "strings" agnostic of the executing Python
1431-
# version.
1421+
# We use `bytes` for scanning input files to avoid requiring them to always
1422+
# have valid codings.
14321423

14331424
keywords_re = re.compile(
1434-
to_bytes("(%s)(.*)\n" % ("|".join(re.escape(k) for k in keywords),))
1425+
b"(%s)(.*)\n" % (b"|".join(re.escape(k.encode("utf-8")) for k in keywords),)
14351426
)
14361427

14371428
f = open(source_path, "rb")
@@ -1440,8 +1431,8 @@ def parseIntegratedTestScriptCommands(source_path, keywords):
14401431
data = f.read()
14411432

14421433
# Ensure the data ends with a newline.
1443-
if not data.endswith(to_bytes("\n")):
1444-
data = data + to_bytes("\n")
1434+
if not data.endswith(b"\n"):
1435+
data = data + b"\n"
14451436

14461437
# Iterate over the matches.
14471438
line_number = 1
@@ -1451,14 +1442,12 @@ def parseIntegratedTestScriptCommands(source_path, keywords):
14511442
# newlines.
14521443
match_position = match.start()
14531444
line_number += data.count(
1454-
to_bytes("\n"), last_match_position, match_position
1445+
b"\n", last_match_position, match_position
14551446
)
14561447
last_match_position = match_position
14571448

14581449
# Convert the keyword and line to UTF-8 strings and yield the
1459-
# command. Note that we take care to return regular strings in
1460-
# Python 2, to avoid other code having to differentiate between the
1461-
# str and unicode types.
1450+
# command.
14621451
#
14631452
# Opening the file in binary mode prevented Windows \r newline
14641453
# characters from being converted to Unix \n newlines, so manually

llvm/utils/lit/lit/util.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,6 @@ def make_word_regex(word):
3333
return r"\b" + word + r"\b"
3434

3535

36-
def to_bytes(s):
37-
"""Return the parameter as type 'bytes', possibly encoding it.
38-
39-
In Python2, the 'bytes' type is the same as 'str'. In Python3, they
40-
are distinct.
41-
42-
"""
43-
if isinstance(s, bytes):
44-
# In Python2, this branch is taken for both 'str' and 'bytes'.
45-
# In Python3, this branch is taken only for 'bytes'.
46-
return s
47-
# In Python2, 's' is a 'unicode' object.
48-
# In Python3, 's' is a 'str' object.
49-
# Encode to UTF-8 to get 'bytes' data.
50-
return s.encode("utf-8")
51-
52-
5336
def usable_core_count():
5437
"""Return the number of cores the current process can use, if supported.
5538
Otherwise, return the total number of cores (like `os.cpu_count()`).

0 commit comments

Comments
 (0)