Skip to content

Commit 9aa33f4

Browse files
cooperleesCooper Ry Leesgraingert
authored
Move to explicitly creating a new loop (#3164)
* Move to explicitly creating a new loop - >= 3.10 add a warning that `get_event_loop` will not automatically create a loop - Move to explicit API Test: - `python3.11 -m venv --upgrade-deps /tmp/tb` - `/tmp/tb/bin/pip install -e .` - Install deps and no blackd as aiohttp + yarl can't build still with 3.11 - aio-libs/aiohttp#6600 - `export PYTHONWARNINGS=error` ``` cooper@l33t:~/repos/black$ /tmp/tb/bin/black . All done! ✨ 🍰 ✨ 44 files left unchanged. ``` Fixes #3110 * Add to CHANGES.md * Fix a cooper typo yet again * Set default asyncio loop to our explicitly created one + unset on exit * Update CHANGES.md Fix my silly typo. Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: Cooper Ry Lees <[email protected]> Co-authored-by: Thomas Grainger <[email protected]>
1 parent 8900e3a commit 9aa33f4

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545

4646
<!-- Changes to Black's terminal output and error messages -->
4747

48+
- Change from deprecated `asyncio.get_event_loop()` to create our event loop which
49+
removes DeprecationWarning (#3164)
50+
4851
### Packaging
4952

5053
<!-- Changes to how Black is packaged, such as dependency requirements -->

src/black/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,6 @@ def reformat_many(
773773
from concurrent.futures import Executor, ThreadPoolExecutor, ProcessPoolExecutor
774774

775775
executor: Executor
776-
loop = asyncio.get_event_loop()
777776
worker_count = workers if workers is not None else DEFAULT_WORKERS
778777
if sys.platform == "win32":
779778
# Work around https://bugs.python.org/issue26903
@@ -788,6 +787,8 @@ def reformat_many(
788787
# any good due to the Global Interpreter Lock)
789788
executor = ThreadPoolExecutor(max_workers=1)
790789

790+
loop = asyncio.new_event_loop()
791+
asyncio.set_event_loop(loop)
791792
try:
792793
loop.run_until_complete(
793794
schedule_formatting(
@@ -801,7 +802,10 @@ def reformat_many(
801802
)
802803
)
803804
finally:
804-
shutdown(loop)
805+
try:
806+
shutdown(loop)
807+
finally:
808+
asyncio.set_event_loop(None)
805809
if executor is not None:
806810
executor.shutdown()
807811

0 commit comments

Comments
 (0)