Skip to content

Commit 3459fe8

Browse files
authored
[Python3] Make update-checkout work with Python 2 & 3 (#33081)
* Python3 compatibility for Benchmarks Three issues addressed here: 1. Dependency on dictionary iteration order CharacterProperties.swift.gyb iterated a dictionary to produce its output. Changed this to a list of tuples to ensure the order is predictable. 2. Python3 `map` returns an iterator, not a list Changed a bunch of `map` calls to `list(map(...))` to ensure the result is a list 3. Python3 `int()` expects a string, won't accept a list of characters Added a concatenation step that is effectively a no-op on Python2 * Make update-checkout work with Python3 Python3 doesn't like to pickle local functions, so make the multiprocessing initialization function be global. Python3 multiprocessing also seems to require conditional execution of the top-level code. TBH, I'm not entirely sure why Python 2 didn't need this. * Adjust whitespace per python-lint
1 parent 1f73684 commit 3459fe8

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

utils/update-checkout

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import sys
44

55
import update_checkout
66

7-
# The internal Windows implementation tries to import the current module using
8-
# sys.modules[__name__]. This file (called 'update-checkout') is not a valid
9-
# Python module name, as it contains a '-' and doesn't end with '.py'. This
10-
# results in errors running update-checkout on Windows:'ImportError: No module
11-
# named update-checkout'.
12-
# As such, we need to manually set sys.modules[__name__] to a valid module
13-
# identifier to work around these errors.
14-
sys.modules[__name__] = sys.modules['update_checkout']
15-
update_checkout.main()
7+
if __name__ == '__main__':
8+
# The internal Windows implementation tries to import the current module using
9+
# sys.modules[__name__]. This file (called 'update-checkout') is not a valid
10+
# Python module name, as it contains a '-' and doesn't end with '.py'. This
11+
# results in errors running update-checkout on Windows:'ImportError: No module
12+
# named update-checkout'.
13+
# As such, we need to manually set sys.modules[__name__] to a valid module
14+
# identifier to work around these errors.
15+
sys.modules[__name__] = sys.modules['update_checkout']
16+
17+
update_checkout.main()

utils/update_checkout/update_checkout/update_checkout.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
SCRIPT_DIR = os.path.dirname(SCRIPT_FILE)
3030

3131

32+
def child_init(lck):
33+
global lock
34+
lock = lck
35+
36+
3237
def run_parallel(fn, pool_args, n_processes=0):
3338
"""Function used to run a given closure in parallel.
3439
@@ -37,17 +42,13 @@ def run_parallel(fn, pool_args, n_processes=0):
3742
parallel implementation.
3843
"""
3944

40-
def init(lck):
41-
global lock
42-
lock = lck
43-
4445
if n_processes == 0:
4546
n_processes = cpu_count() * 2
4647

4748
lk = Lock()
4849
print("Running ``%s`` with up to %d processes." %
4950
(fn.__name__, n_processes))
50-
pool = Pool(processes=n_processes, initializer=init, initargs=(lk,))
51+
pool = Pool(processes=n_processes, initializer=child_init, initargs=(lk,))
5152
results = pool.map_async(func=fn, iterable=pool_args).get(999999)
5253
pool.close()
5354
pool.join()

0 commit comments

Comments
 (0)