8000 don't uvloop.install on import (#2303) · IBMZ-Linux-OSS-Python/black@df1c86c · GitHub
[go: up one dir, main page]

Skip to content

Commit df1c86c

Browse files
don't uvloop.install on import (psf#2303)
1 parent f2a3fee commit df1c86c

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Correct max string length calculation when there are string operators (#2292)
88
- Fixed option usage when using the `--code` flag (#2259)
9+
- Do not call `uvloop.install()` when _Black_ is used as a library (#2303)
910

1011
## 21.5b2
1112

src/black/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from black.mode import Mode, TargetVersion
3939
from black.mode import Feature, supports_feature, VERSION_TO_FEATURES
4040
from black.cache import read_cache, write_cache, get_cache_info, filter_cached, Cache
41-
from black.concurrency import cancel, shutdown
41+
from black.concurrency import cancel, shutdown, maybe_install_uvloop
4242
from black.output import dump_to_file, diff, color_diff, out, err
4343
from black.report import Report, Changed
4444
from black.files import find_project_root, find_pyproject_toml, parse_pyproject_toml
@@ -54,14 +54,6 @@
5454

5555
from _black_version import version as __version__
5656

57-
# If our environment has uvloop installed lets use it
58-
try:
59< 8000 /code>-
import uvloop
60-
61-
uvloop.install()
62-
except ImportError:
63-
pass
64-
6557
# types
6658
FileContent = str
6759
Encoding = str
@@ -1112,6 +1104,7 @@ def patch_click() -> None:
11121104

11131105

11141106
def patched_main() -> None:
1107+
maybe_install_uvloop()
11151108
freeze_support()
11161109
patch_click()
11171110
main()

src/black/concurrency.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66
from black.output import err
77

88

9+
def maybe_install_uvloop() -> None:
10+
"""If our environment has uvloop installed we use it.
11+
12+
This is called only from command-line entry points to avoid
13+
interfering with the parent process if Black is used as a library.
14+
15+
"""
16+
try:
17+
import uvloop
18+
19+
uvloop.install()
20+
except ImportError:
21+
pass
22+
23+
924
def cancel(tasks: Iterable["asyncio.Task[Any]"]) -> None:
1025
"""asyncio signal handler that cancels all `tasks` and reports to stderr."""
1126
err("Aborted!")

src/blackd/__init__.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,9 @@
2020
sys.exit(-1)
2121

2222
import black
23+
from black.concurrency import maybe_install_uvloop
2324
import click
2425

25-
# If our environment has uvloop installed lets use it
26-
try:
27-
import uvloop
28-
29-
uvloop.install()
30-
except ImportError:
31-
pass
32-
3326
from _black_version import version as __version__
3427

3528
# This is used internally by tests to shut down the server prematurely
@@ -210,6 +203,7 @@ def parse_python_variant_header(value: str) -> Tuple[bool, Set[black.TargetVersi
210203

211204

212205
def patched_main() -> None:
206+
maybe_install_uvloop()
213207
freeze_support()
214208
black.patch_click()
215209
main()

0 commit comments

Comments
 (0)
0