MCPcopy
hub / github.com/prompt-toolkit/ptpython / eval_async

Method eval_async

src/ptpython/repl.py:334–371  ·  view source on GitHub ↗

Evaluate the line and print the result.

(self, line: str)

Source from the content-addressed store, hash-verified

332 return None
333
334 async def eval_async(self, line: str) -> object:
335 """
336 Evaluate the line and print the result.
337 """
338 # WORKAROUND: Due to a bug in Jedi, the current directory is removed
339 # from sys.path. See: https://github.com/davidhalter/jedi/issues/1148
340 if "" not in sys.path:
341 sys.path.insert(0, "")
342
343 if line.lstrip().startswith("!"):
344 # Run as shell command
345 os.system(line[1:])
346 else:
347 # Try eval first
348 try:
349 code = self._compile_with_flags(line, "eval")
350 except SyntaxError:
351 pass
352 else:
353 # No syntax errors for eval. Do eval.
354 result = eval(code, self.get_globals(), self.get_locals())
355
356 if _has_coroutine_flag(code):
357 result = await result
358
359 self._store_eval_result(result)
360 return result
361
362 # If not a valid `eval` expression, compile as `exec` expression
363 # but still run with eval to get an awaitable in case of a
364 # awaitable expression.
365 code = self._compile_with_flags(line, "exec")
366 result = eval(code, self.get_globals(), self.get_locals())
367
368 if _has_coroutine_flag(code):
369 result = await result
370
371 return None
372
373 def _store_eval_result(self, result: object) -> None:
374 locals: dict[str, Any] = self.get_locals()

Callers 1

evalMethod · 0.95

Calls 4

_compile_with_flagsMethod · 0.95
_store_eval_resultMethod · 0.95
_has_coroutine_flagFunction · 0.85
get_globalsMethod · 0.80

Tested by

no test coverage detected