Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions newsfragments/160.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix importing ``trio_asyncio`` on Python 3.14 and later, where asyncio renamed
``BaseDefaultEventLoopPolicy``.
17 changes: 17 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import subprocess
import pytest
import trio_asyncio
import asyncio
Expand All @@ -8,6 +9,22 @@
from exceptiongroup import ExceptionGroup, BaseExceptionGroup


def test_import_with_private_base_event_loop_policy() -> None:
script = """
import asyncio.events

base_policy = getattr(asyncio.events, "BaseDefaultEventLoopPolicy", None)
if base_policy is None:
base_policy = asyncio.events._BaseDefaultEventLoopPolicy
else:
del asyncio.events.BaseDefaultEventLoopPolicy
asyncio.events._BaseDefaultEventLoopPolicy = base_policy

import trio_asyncio
"""
subprocess.run([sys.executable, "-c", script], check=True)


class Seen:
flag = 0

Expand Down
10 changes: 9 additions & 1 deletion trio_asyncio/_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ def _in_trio_context_other_than_sync_loop():
return False


class _TrioPolicy(asyncio.events.BaseDefaultEventLoopPolicy):
# Python 3.14 renamed this private implementation class with no compatibility
# alias. Keep supporting older Python versions while accepting the new name.
try:
_BaseDefaultEventLoopPolicy = asyncio.events.BaseDefaultEventLoopPolicy
except AttributeError:
_BaseDefaultEventLoopPolicy = asyncio.events._BaseDefaultEventLoopPolicy


class _TrioPolicy(_BaseDefaultEventLoopPolicy):
@staticmethod
def _loop_factory():
raise RuntimeError("Event loop creations shouldn't get here")
Expand Down
Loading