@@ -322,6 +322,20 @@ def new_highlight_source(self):
322322 """Return new src_id for use with Buffer.add_highlight."""
323323 return self .current .buffer .add_highlight ("" , 0 , src_id = 0 )
324324
325+ def _error_wrapper (self , fn , call_point , * args , ** kwargs ):
326+ if fn is None :
327+ return None
328+ def handler ():
329+ try :
330+ fn (* args , ** kwargs )
331+ except Exception as err :
332+ msg = ("error caught while executing async callback:\n "
333+ "{0!r}\n {1}\n \n the call was requested at\n {2}"
334+ .format (err , format_exc_skip (1 , 5 ), call_point ))
335+ self ._err_cb (msg )
336+ raise
337+ return handler
338+
325339 def async_call (self , fn , * args , ** kwargs ):
326340 """Schedule `fn` to be called by the event loop soon.
327341
@@ -333,18 +347,33 @@ def async_call(self, fn, *args, **kwargs):
333347 that shouldn't block neovim.
334348 """
335349 call_point = '' .join (format_stack (None , 5 )[:- 1 ])
350+ handler = self ._error_wrapper (fn , call_point , * args , ** kwargs )
336351
337- def handler ():
338- try :
339- fn (* args , ** kwargs )
340- except Exception as err :
341- msg = ("error caught while executing async callback:\n "
342- "{0!r}\n {1}\n \n the call was requested at\n {2}"
343- .format (err , format_exc_skip (1 , 5 ), call_point ))
344- self ._err_cb (msg )
345- raise
346352 self ._session .threadsafe_call (handler )
347353
354+ def poll_fd (self , fd , on_readable = None , on_writable = None , greenlet = True ):
355+ """
356+ Invoke callbacks when the fd is ready for reading and/or writing. if
357+ `on_readable` is not None, it should be callback, which will be invoked
358+ (with no arguments) when the fd is ready for writing. Similarily if
359+ `on_writable` is not None it will be invoked when the fd is ready for
360+ writing.
361+
362+ Only one callback (of each kind) can be registered on the same fd at a
363+ time. If both readability and writability should be monitored, both
364+ callbacks must be registered by the same `poll_fd` call.
365+
366+ By default, the function is invoked in a greenlet, just like a callback
367+ scheduled by async_call.
368+
369+ Returns a function that deactivates the callback(s).
370+ """
371+ call_point = '' .join (format_stack (None , 5 )[:- 1 ])
372+ on_readable = self ._error_wrapper (on_readable , call_point )
373+ on_writable = self ._error_wrapper (on_writable , call_point )
374+ return self ._session .poll_fd (fd , on_readable , on_writable , greenlet )
375+
376+
348377
349378class Buffers (object ):
350379
0 commit comments