Skip to content

Commit d6a7482

Browse files
authored
Add python 3.12 support (#367)
1 parent 8284fc3 commit d6a7482

File tree

4 files changed

+71
-42
lines changed

4 files changed

+71
-42
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ jobs:
2828
fail-fast: false
2929
matrix:
3030
os: [ubuntu-latest, windows-latest, macos-latest]
31-
python-version: ["3.8", "3.11"]
31+
python-version: ["3.8", "3.12"]
3232
include:
3333
- os: windows-latest
3434
python-version: "3.9"
3535
- os: ubuntu-latest
36-
python-version: "pypy-3.8"
36+
python-version: "3.11"
37+
- os: ubuntu-latest
38+
python-version: "pypy-3.9"
3739
- os: macos-latest
3840
python-version: "3.10"
3941
steps:

jupyter_core/utils/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,17 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
160160
pass
161161

162162
# Run the loop for this thread.
163-
try:
164-
loop = asyncio.get_event_loop()
165-
except RuntimeError:
166-
loop = asyncio.new_event_loop()
167-
asyncio.set_event_loop(loop)
168-
return loop.run_until_complete(inner)
163+
# In Python 3.12, a deprecation warning is raised, which
164+
# may later turn into a RuntimeError. We handle both
165+
# cases.
166+
with warnings.catch_warnings():
167+
warnings.simplefilter("ignore", DeprecationWarning)
168+
try:
169+
loop = asyncio.get_event_loop()
170+
except RuntimeError:
171+
loop = asyncio.new_event_loop()
172+
asyncio.set_event_loop(loop)
173+
return loop.run_until_complete(inner)
169174

170175
wrapped.__doc__ = coro.__doc__
171176
return wrapped

tests/test_async.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/test_utils.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Tests for utils"""
2+
3+
# Copyright (c) Jupyter Development Team.
4+
# Distributed under the terms of the Modified BSD License.
5+
6+
import asyncio
7+
import os
8+
import tempfile
9+
10+
import pytest
11+
12+
from jupyter_core.utils import deprecation, ensure_async, ensure_dir_exists, run_sync
13+
14+
15+
def test_ensure_dir_exists():
16+
with tempfile.TemporaryDirectory() as td:
17+
ensure_dir_exists(td)
18+
ensure_dir_exists(os.path.join(str(td), "foo"), 0o777)
19+
20+
21+
def test_deprecation():
22+
with pytest.deprecated_call():
23+
deprecation("foo")
24+
25+
26+
async def afunc():
27+
return "afunc"
28+
29+
30+
def func():
31+
return "func"
32+
33+
34+
sync_afunc = run_sync(afunc)
35+
36+
37+
def test_run_sync():
38+
async def foo():
39+
return 1
40+
41+
foo_sync = run_sync(foo)
42+
assert foo_sync() == 1
43+
assert foo_sync() == 1
44+
45+
asyncio.set_event_loop(None)
46+
assert foo_sync() == 1
47+
48+
asyncio.run(foo())
49+
50+
51+
def test_ensure_async():
52+
async def main():
53+
assert await ensure_async(afunc()) == "afunc"
54+
assert await ensure_async(func()) == "func"
55+
56+
asyncio.run(main())

0 commit comments

Comments
 (0)