@@ -312,23 +312,63 @@ class AsyncLoopFatalError(Exception):
312312 '''Raised to ask ``run_asyncio_loop`` to cancel remaining tasks and stop.'''
313313 pass
314314
315- def get_running_event_loop () -> asyncio .AbstractEventLoop :
316- # asyncio.get_running_loop is 3.7+.
317- if sys .version_info >= (3 , 7 ):
315+ # The helpers below depend on the Python version, which cannot change at run
316+ # time, so each is declared once per supported API and selected at import.
317+
318+ if sys .version_info >= (3 , 7 ):
319+ def get_running_event_loop () -> asyncio .AbstractEventLoop :
320+ '''Return the running event loop (asyncio.get_running_loop is 3.7+).'''
318321 return asyncio .get_running_loop ()
319- return asyncio .get_event_loop ()
320-
321- def _active_asyncio_tasks (
322- loop : asyncio .AbstractEventLoop ) -> Set [asyncio .Task ]:
323- # In Python 3.7 asyncio.Task.all_tasks was deprecated in favor of
324- # asyncio.all_tasks. This was later removed in 3.9.
325- #
326- # To provide compatibility for both check to see if our python version is
327- # greater than or equal to 3.7, and conditionally enable the new API,
328- # otherwise fallback to the original API.
329- if sys .version_info >= (3 , 7 ):
322+ else :
323+ def get_running_event_loop () -> asyncio .AbstractEventLoop :
324+ '''Return the running event loop (asyncio.get_running_loop is 3.7+).'''
325+ return asyncio .get_event_loop ()
326+
327+ if sys .version_info >= (3 , 9 ):
328+ def is_path_relative_to (path : Path , other : Path ) -> bool :
329+ '''Return whether path is equal to or located under other.'''
330+ return path .is_relative_to (other )
331+ else :
332+ def is_path_relative_to (path : Path , other : Path ) -> bool :
333+ '''Return whether path is equal to or located under other.
334+
335+ Path.is_relative_to is only available in Python 3.9+.
336+ '''
337+ try :
338+ result = Path (os .path .commonpath ([str (path ), str (other )])) == other
339+ except ValueError :
340+ # Mixing absolute and relative paths (or differing drives).
341+ result = False
342+ return result
343+
344+ if sys .version_info >= (3 , 9 ):
345+ async def async_to_thread (func : Callable [..., Any ],
346+ * args : Any ,
347+ ** kwargs : Any ) -> Any :
348+ '''Run func(*args, **kwargs) in a worker thread and await its result.'''
349+ return await asyncio .to_thread (func , * args , ** kwargs )
350+ else :
351+ async def async_to_thread (func : Callable [..., Any ],
352+ * args : Any ,
353+ ** kwargs : Any ) -> Any :
354+ '''Run func(*args, **kwargs) in a worker thread and await its result.
355+
356+ asyncio.to_thread is only available in Python 3.9+.
357+ '''
358+ return await get_running_event_loop ().run_in_executor (
359+ None , functools .partial (func , * args , ** kwargs )
360+ )
361+
362+ if sys .version_info >= (3 , 7 ):
363+ def _active_asyncio_tasks (
364+ loop : asyncio .AbstractEventLoop ) -> Set [asyncio .Task ]:
365+ # asyncio.Task.all_tasks was deprecated in 3.7 in favor of
366+ # asyncio.all_tasks, and removed in 3.9.
330367 return asyncio .all_tasks (loop )
331- return asyncio .Task .all_tasks (loop ) # type: ignore[attr-defined]
368+ else :
369+ def _active_asyncio_tasks (
370+ loop : asyncio .AbstractEventLoop ) -> Set [asyncio .Task ]:
371+ return asyncio .Task .all_tasks (loop ) # type: ignore[attr-defined]
332372
333373def _stop_asyncio_loop (loop : asyncio .AbstractEventLoop ) -> None :
334374 for task in _active_asyncio_tasks (loop ):
@@ -671,29 +711,14 @@ async def __aenter__(self) -> Optional[BinaryIO]:
671711 self ._out_handle = open (self ._temp_dir / 'diff' , 'w+b' )
672712
673713 if _DIFF_CMD is None :
674- # asyncio.to_thread is 3.9+.
675- if sys .version_info >= (3 , 9 ):
676- has_diff = await asyncio .to_thread (
677- _write_unified_diff_with_difflib ,
678- self ._from_file ,
679- self ._to_file ,
680- self ._out_handle ,
681- self ._from_label ,
682- self ._to_label
683- )
684- else :
685- loop = get_running_event_loop ()
686- has_diff = await loop .run_in_executor (
687- None ,
688- functools .partial (
689- _write_unified_diff_with_difflib ,
690- self ._from_file ,
691- self ._to_file ,
692- self ._out_handle ,
693- self ._from_label ,
694- self ._to_label
695- )
696- )
714+ has_diff = await async_to_thread (
715+ _write_unified_diff_with_difflib ,
716+ self ._from_file ,
717+ self ._to_file ,
718+ self ._out_handle ,
719+ self ._from_label ,
720+ self ._to_label
721+ )
697722 else :
698723 has_diff = await _write_unified_diff_with_diff_cmd (
699724 self ._from_file ,
0 commit comments