/opt/imh-python/lib/python3.9/site-packages/IPython/core
NameSizeModeActions
magics/-0755rm
profile/-0755rm
tests/-0755rm
__pycache__/-0755rm
alias.py100340644editdlrm
application.py189370644editdlrm
async_helpers.py42970644editdlrm
autocall.py19910644editdlrm
builtin_trap.py30090644editdlrm
compilerop.py77300644editdlrm
completer.py1178500644editdlrm
completerlib.py123650644editdlrm
crashhandler.py85080644editdlrm
debugger.py388720644editdlrm
display.py425030644editdlrm
displayhook.py129620644editdlrm
displaypub.py49440644editdlrm
display_functions.py129180644editdlrm
display_trap.py20980644editdlrm
error.py17340644editdlrm
events.py52510644editdlrm
excolors.py49280644editdlrm
extensions.py57800644editdlrm
formatters.py349810644editdlrm
getipython.py9120644editdlrm
guarded_eval.py250370644editdlrm
history.py344750644editdlrm
historyapp.py59090644editdlrm
hooks.py56630644editdlrm
inputsplitter.py290410644editdlrm
inputtransformer.py181840644editdlrm
inputtransformer2.py293930644editdlrm
interactiveshell.py1537610644editdlrm
latex_symbols.py312880644editdlrm
logger.py84410644editdlrm
macro.py17340644editdlrm
magic.py288990644editdlrm
magic_arguments.py97340644editdlrm
oinspect.py398980644editdlrm
page.py117530644editdlrm
payload.py17580644editdlrm
payloadpage.py14310644editdlrm
prefilter.py255880644editdlrm
profileapp.py106310644editdlrm
profiledir.py80290644editdlrm
prompts.py6070644editdlrm
pylabtools.py140180644editdlrm
release.py21780644editdlrm
shellapp.py178450644editdlrm
splitinput.py48360644editdlrm
ultratb.py558030644editdlrm
usage.py135420644editdlrm
__init__.py00644editdlrm
Edit: /opt/imh-python/lib/python3.9/site-packages/IPython/core/async_helpers.py (4297B)
""" Async helper function that are invalid syntax on Python 3.5 and below. This code is best effort, and may have edge cases not behaving as expected. In particular it contain a number of heuristics to detect whether code is effectively async and need to run in an event loop or not. Some constructs (like top-level `return`, or `yield`) are taken care of explicitly to actually raise a SyntaxError and stay as close as possible to Python semantics. """ import ast import asyncio import inspect from functools import wraps _asyncio_event_loop = None def get_asyncio_loop(): """asyncio has deprecated get_event_loop Replicate it here, with our desired semantics: - always returns a valid, not-closed loop - not thread-local like asyncio's, because we only want one loop for IPython - if called from inside a coroutine (e.g. in ipykernel), return the running loop .. versionadded:: 8.0 """ try: return asyncio.get_running_loop() except RuntimeError: # not inside a coroutine, # track our own global pass # not thread-local like asyncio's, # because we only track one event loop to run for IPython itself, # always in the main thread. global _asyncio_event_loop if _asyncio_event_loop is None or _asyncio_event_loop.is_closed(): _asyncio_event_loop = asyncio.new_event_loop() return _asyncio_event_loop class _AsyncIORunner: def __call__(self, coro): """ Handler for asyncio autoawait """ return get_asyncio_loop().run_until_complete(coro) def __str__(self): return "asyncio" _asyncio_runner = _AsyncIORunner() class _AsyncIOProxy: """Proxy-object for an asyncio Any coroutine methods will be wrapped in event_loop.run_ """ def __init__(self, obj, event_loop): self._obj = obj self._event_loop = event_loop def __repr__(self): return f"<_AsyncIOProxy({self._obj!r})>" def __getattr__(self, key): attr = getattr(self._obj, key) if inspect.iscoroutinefunction(attr): # if it's a coroutine method, # return a threadsafe wrapper onto the _current_ asyncio loop @wraps(attr) def _wrapped(*args, **kwargs): concurrent_future = asyncio.run_coroutine_threadsafe( attr(*args, **kwargs), self._event_loop ) return asyncio.wrap_future(concurrent_future) return _wrapped else: return attr def __dir__(self): return dir(self._obj) def _curio_runner(coroutine): """ handler for curio autoawait """ import curio return curio.run(coroutine) def _trio_runner(async_fn): import trio async def loc(coro): """ We need the dummy no-op async def to protect from trio's internal. See https://github.com/python-trio/trio/issues/89 """ return await coro return trio.run(loc, async_fn) def _pseudo_sync_runner(coro): """ A runner that does not really allow async execution, and just advance the coroutine. See discussion in https://github.com/python-trio/trio/issues/608, Credit to Nathaniel Smith """ try: coro.send(None) except StopIteration as exc: return exc.value else: # TODO: do not raise but return an execution result with the right info. raise RuntimeError( "{coro_name!r} needs a real async loop".format(coro_name=coro.__name__) ) def _should_be_async(cell: str) -> bool: """Detect if a block of code need to be wrapped in an `async def` Attempt to parse the block of code, it it compile we're fine. Otherwise we wrap if and try to compile. If it works, assume it should be async. Otherwise Return False. Not handled yet: If the block of code has a return statement as the top level, it will be seen as async. This is a know limitation. """ try: code = compile( cell, "<>", "exec", flags=getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0) ) return inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE except (SyntaxError, MemoryError): return False