MCPcopy Create free account
hub / github.com/MrNeRF/LichtFeld-Studio / start_embedded_repl

Function start_embedded_repl

src/python/runner.cpp:876–988  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

874 }
875
876 void start_embedded_repl(int read_fd, int write_fd) {
877 stop_embedded_repl();
878 ensure_initialized();
879
880 auto& pm = PackageManager::instance();
881 if (!pm.is_installed("ptpython")) {
882 LOG_INFO("Installing ptpython...");
883 const auto result = pm.install("ptpython");
884 if (!result.success) {
885 LOG_WARN("Failed to install ptpython: {} (falling back to code.interact)", result.error);
886 } else {
887 update_python_path();
888 }
889 }
890
891 g_repl_read_fd = read_fd;
892 g_repl_write_fd = write_fd;
893 g_repl_running = true;
894
895 g_repl_thread = std::thread([read_fd, write_fd]() {
896 {
897 const GilAcquire gil;
898 install_output_redirect();
899
900 SceneContextGuard ctx(get_application_scene());
901
902 const std::string setup = std::format(R"(
903import sys, os, atexit
904
905_repl_read_fd = {}
906_repl_write_fd = {}
907_repl_in = os.fdopen(os.dup(_repl_read_fd), 'r', buffering=1)
908_repl_out = os.fdopen(os.dup(_repl_write_fd), 'w', buffering=1)
909
910_saved_stdin = sys.stdin
911_saved_stdout = sys.stdout
912_saved_stderr = sys.stderr
913sys.stdin = _repl_in
914sys.stdout = _repl_out
915sys.stderr = _repl_out
916
917import lichtfeld as lf
918_repl_locals = {{"lf": lf, "__name__": "__console__", "__doc__": None}}
919
920_histfile = os.path.join(os.path.expanduser("~"), ".lichtfeld", "repl_history")
921os.makedirs(os.path.dirname(_histfile), exist_ok=True)
922
923_used_ptpython = False
924try:
925 from ptpython.repl import embed as _pt_embed
926 from prompt_toolkit.output.vt100 import Vt100_Output
927 from prompt_toolkit.data_structures import Size
928 from prompt_toolkit.application import create_app_session
929
930 _pt_output = Vt100_Output(_repl_out, get_size=lambda: Size(rows=24, columns=80), enable_cpr=False)
931 _pt_input = None
932 if not _repl_in.isatty():
933 from prompt_toolkit.input.vt100 import Vt100Input

Callers 1

DrawDockedPythonConsoleFunction · 0.85

Calls 11

stop_embedded_replFunction · 0.85
ensure_initializedFunction · 0.85
instanceFunction · 0.85
update_python_pathFunction · 0.85
install_output_redirectFunction · 0.85
get_application_sceneFunction · 0.85
formatFunction · 0.85
is_installedMethod · 0.80
c_strMethod · 0.80
close_fdFunction · 0.70
installMethod · 0.45

Tested by

no test coverage detected