Skip to content

Commit be5810b

Browse files
committed
refactor: expose event loop as a property rather than a field
event loops for Session and Nvim are final and can't be changed.
1 parent a1347ee commit be5810b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

pynvim/api/nvim.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Main Nvim interface."""
2+
3+
import asyncio
24
import os
35
import sys
46
import threading
@@ -140,7 +142,16 @@ def __init__(
140142
self._err_cb: Callable[[str], Any] = lambda _: None
141143
else:
142144
self._err_cb = err_cb
143-
self.loop = self._session.loop._loop
145+
146+
@property
147+
def loop(self) -> asyncio.AbstractEventLoop:
148+
"""Get the event loop (exposed to rplugins).""" # noqa
149+
150+
# see #294: for python 3.4+, the only available and guaranteed
151+
# implementation of msgpack_rpc BaseEventLoop is the AsyncioEventLoop.
152+
# The underlying asyncio event loop is exposed to rplugins.
153+
# pylint: disable=protected-access
154+
return self._session.loop._loop # type: ignore
144155

145156
def _from_nvim(self, obj: Any, decode: Optional[TDecodeMode] = None) -> Any:
146157
if decode is None:

pynvim/msgpack_rpc/session.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from pynvim.compat import check_async
1313
from pynvim.msgpack_rpc.async_session import AsyncSession
14+
from pynvim.msgpack_rpc.event_loop.base import BaseEventLoop
1415

1516
if sys.version_info < (3, 8):
1617
from typing_extensions import Literal
@@ -42,7 +43,7 @@ class Notification(NamedTuple):
4243
Message = Union[Request, Notification]
4344

4445

45-
class Session(object):
46+
class Session:
4647

4748
"""Msgpack-rpc session layer that uses coroutines for a synchronous API.
4849
@@ -59,11 +60,15 @@ def __init__(self, async_session: AsyncSession):
5960
self._pending_messages: Deque[Message] = deque()
6061
self._is_running = False
6162
self._setup_exception: Optional[Exception] = None
62-
self.loop = async_session.loop
6363
self._loop_thread: Optional[threading.Thread] = None
6464
self.error_wrapper: Callable[[Tuple[int, str]], Exception] = \
6565
lambda e: Exception(e[1])
6666

67+
@property
68+
def loop(self) -> BaseEventLoop:
69+
"""Get the underlying msgpack EventLoop."""
70+
return self._async_session.loop
71+
6772
def threadsafe_call(
6873
self, fn: Callable[..., Any], *args: Any, **kwargs: Any
6974
) -> None:

0 commit comments

Comments
 (0)