diff --git a/pynvim/api/nvim.py b/pynvim/api/nvim.py index b1b75eb8..b5ea84b7 100644 --- a/pynvim/api/nvim.py +++ b/pynvim/api/nvim.py @@ -1,4 +1,6 @@ """Main Nvim interface.""" + +import asyncio import os import sys import threading @@ -140,7 +142,16 @@ def __init__( self._err_cb: Callable[[str], Any] = lambda _: None else: self._err_cb = err_cb - self.loop = self._session.loop._loop + + @property + def loop(self) -> asyncio.AbstractEventLoop: + """Get the event loop (exposed to rplugins).""" # noqa + + # see #294: for python 3.4+, the only available and guaranteed + # implementation of msgpack_rpc BaseEventLoop is the AsyncioEventLoop. + # The underlying asyncio event loop is exposed to rplugins. + # pylint: disable=protected-access + return self._session.loop._loop # type: ignore def _from_nvim(self, obj: Any, decode: Optional[TDecodeMode] = None) -> Any: if decode is None: diff --git a/pynvim/msgpack_rpc/session.py b/pynvim/msgpack_rpc/session.py index 453f218b..4e8f6259 100644 --- a/pynvim/msgpack_rpc/session.py +++ b/pynvim/msgpack_rpc/session.py @@ -11,6 +11,7 @@ from pynvim.compat import check_async from pynvim.msgpack_rpc.async_session import AsyncSession +from pynvim.msgpack_rpc.event_loop.base import BaseEventLoop if sys.version_info < (3, 8): from typing_extensions import Literal @@ -42,7 +43,7 @@ class Notification(NamedTuple): Message = Union[Request, Notification] -class Session(object): +class Session: """Msgpack-rpc session layer that uses coroutines for a synchronous API. @@ -59,11 +60,15 @@ def __init__(self, async_session: AsyncSession): self._pending_messages: Deque[Message] = deque() self._is_running = False self._setup_exception: Optional[Exception] = None - self.loop = async_session.loop self._loop_thread: Optional[threading.Thread] = None self.error_wrapper: Callable[[Tuple[int, str]], Exception] = \ lambda e: Exception(e[1]) + @property + def loop(self) -> BaseEventLoop: + """Get the underlying msgpack EventLoop.""" + return self._async_session.loop + def threadsafe_call( self, fn: Callable[..., Any], *args: Any, **kwargs: Any ) -> None: