From ec71272f94b64bfbde73a6c8719d07e54da7daa5 Mon Sep 17 00:00:00 2001 From: Oliver Caldwell Date: Sun, 3 Dec 2023 14:11:47 +0000 Subject: [PATCH] Set __name__ to __repl__ when setting up #540 --- fnl/conjure/client/python/stdio.fnl | 31 +++++++++++++++++------------ lua/conjure/client/python/stdio.lua | 6 +++--- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/fnl/conjure/client/python/stdio.fnl b/fnl/conjure/client/python/stdio.fnl index a8511ee3..d9d99f17 100644 --- a/fnl/conjure/client/python/stdio.fnl +++ b/fnl/conjure/client/python/stdio.fnl @@ -199,18 +199,23 @@ (display-repl-status :stopped) (a.assoc (state) :repl nil)))) -; By default, there is no way for us to tell the difference between -; normal stdout log messages and the result of the expression we evaluated. -; This is because if an expression results in the literal value None, the python -; interpreter will not print out anything. -; Replacing this hook ensures that the last line in the output after -; sending a command is the result of the command. -; Relevant docs: https://docs.python.org/3/library/sys.html#sys.displayhook -(def update-python-displayhook - (str.join "\n" ["import sys" - "def format_output(val):" - " print(repr(val))" - "sys.displayhook = format_output\n"])) +(def initialise-repl-code + ;; By default, there is no way for us to tell the difference between + ;; normal stdout log messages and the result of the expression we evaluated. + ;; This is because if an expression results in the literal value None, the python + ;; interpreter will not print out anything. + ;; Replacing this hook ensures that the last line in the output after + ;; sending a command is the result of the command. + ;; Relevant docs: https://docs.python.org/3/library/sys.html#sys.displayhook + + ;; We also set the `__name__` to something else so `__main__` blocks aren't executed. + (str.join + "\n" + ["import sys" + "def conjure_format_output(val):" + " print(repr(val))" + "sys.displayhook = conjure_format_output\n" + "__name__ = '__repl__'"])) (defn start [] (if (state :repl) @@ -238,7 +243,7 @@ (with-repl-or-warn (fn [repl] (repl.send - (prep-code update-python-displayhook) + (prep-code initialise-repl-code) (fn [msgs] nil) nil))))) diff --git a/lua/conjure/client/python/stdio.lua b/lua/conjure/client/python/stdio.lua index 834af1a9..d791d7ca 100644 --- a/lua/conjure/client/python/stdio.lua +++ b/lua/conjure/client/python/stdio.lua @@ -193,8 +193,8 @@ local function stop() end end _2amodule_2a["stop"] = stop -local update_python_displayhook = str.join("\n", {"import sys", "def format_output(val):", " print(repr(val))", "sys.displayhook = format_output\n"}) -do end (_2amodule_2a)["update-python-displayhook"] = update_python_displayhook +local initialise_repl_code = str.join("\n", {"import sys", "def conjure_format_output(val):", " print(repr(val))", "sys.displayhook = conjure_format_output\n", "__name__ = '__repl__'"}) +do end (_2amodule_2a)["initialise-repl-code"] = initialise_repl_code local function start() if state("repl") then return log.append({(comment_prefix .. "Can't start, REPL is already running."), (comment_prefix .. "Stop the REPL with " .. config["get-in"]({"mapping", "prefix"}) .. cfg({"mapping", "stop"}))}, {["break?"] = true}) @@ -214,7 +214,7 @@ local function start() local function _23_(msgs) return nil end - return repl.send(prep_code(update_python_displayhook), _23_, nil) + return repl.send(prep_code(initialise_repl_code), _23_, nil) end return display_repl_status("started", with_repl_or_warn(_22_)) end