从嵌入式 python 安装打开 REPL
Open a REPL from an embedded python installation
我正在使用一款带有嵌入式(我希望这是正确的词)版本的 IronPython 的软件。 IP 允许用户编写与程序交互的 .py 脚本 API.
程序文件安装目录中没有可用的 ipy.exe 或 python.exe 文件。唯一的东西就是各种 IronPython DLL 文件。标准库完全可用。
没有可用的交互式命令行。我真的需要用比输出到纯文本弹出窗口更强大的东西来调试我的代码,但是当我尝试使用调试器时程序挂起:
import pdb;pdb.set_trace()
如果有一段代码我可以放入其中一个 .py 模块中,它将为当前 python 会话启动命令行 REPL,那就太好了。我已经使用 code
模块尝试了 SO 的解决方案,但它不起作用。
可以做到吗?
出于某种原因,使用 FIFO 无法正常工作,所以我改为实施这种奇怪的做法。它使用文件在控制器实例(只需从终端 运行 连接它)和从任何地方调用的不同代码中的 pdb
之间进行通信。还使用猴子补丁将我们的新 set_trace
变成 pdb
.
import time
import pdb
IN_FILE = "in.pdb"
OUT_FILE = "out.pdb"
class _Receiver(object):
def __init__(self, in_file):
self.in_file = in_file
with open(in_file, "w") as file:
file.write("")
def readline(self):
x = ""
while x == "":
time.sleep(0.1)
with open(self.in_file, "r") as file:
x = file.readline()
with open(self.in_file, "w") as file:
file.write("")
return x
class _Sender(object):
def __init__(self, out_file):
self.out_file = out_file
with open(out_file, "w") as file:
file.write("")
def write(self, x):
with open(self.out_file, "a") as file:
file.write(x)
def flush(self):
pass
mypdb = pdb.Pdb(stdin=_Receiver(in_file=IN_FILE),
stdout=_Sender(out_file=OUT_FILE))
pdb.set_trace = mypdb.set_trace
要在其他代码中使用断点:
from youcannameit import pdb
pdb.set_trace()
连接到 pdb 运行 cmd 中的主批处理脚本。它工作得很好。不如 bash 版本好,所以我会把它留在那里,以防您选择使用它。您不能改用 CRT-C 类型 q。一旦你退出,我建议打开一个新的 cmd,因为 运行 在同一个命令中再次 ning 是非常糟糕的。也许具有更多批处理知识的人可以修复它。
主批处理脚本:
@echo off
set IN_FILE="in.pdb"
set OUT_FILE="out.pdb"
set LOCK="lock.lock"
echo running > %LOCK%
start /b read.cmd
timeout 1 /NOBREAK >NUL
echo PDB:
goto :write_pdb
:print_file
type %OUT_FILE%
timeout 1 /NOBREAK >NUL
NUL > %OUT_FILE% 2>NUL
goto :read_pdb
:read_pdb
for /f %%i in (%OUT_FILE%) do set size=%%~zi
if %size% gtr 0 goto :print_file
timeout 1 /NOBREAK >NUL
goto :read_pdb
:write_pdb
set /P INPUT=%=%
echo %INPUT% > %IN_FILE%
If /I "%Input%"=="q" goto :end
If /I "%Input%"=="quit" goto :end
If /I "%Input%"=="exit" goto :end
goto :write_pdb
:end
del %LOCK%
echo Exiting..
EXIT /B 0
二次批处理脚本,以便我们可以同时打印和输入(命名为read.cmd并与主脚本放在同一文件夹中):
@echo off
set IN_FILE="in.pdb"
set OUT_FILE="out.pdb"
set LOCK="lock.lock"
goto :read_pdb
:print_file
type %OUT_FILE%
timeout 1 /NOBREAK >NUL
NUL > %OUT_FILE% 2>NUL
goto :read_pdb
:read_pdb
if not exist %LOCK% goto :end
for /f %%i in (%OUT_FILE%) do set size=%%~zi
if %size% gtr 0 goto :print_file
timeout 1 /NOBREAK >NUL
goto :read_pdb
:end
如果您坚持使用 bash 版本::
#!/bin/bash
IN_FILE="in.pdb"
OUT_FILE="out.pdb"
echo PDB:
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
kill $READER >/dev/null 2>&1
exit 1
}
read_pdb(){
while true
do
if [ -s $OUT_FILE ]
then
cat $OUT_FILE
sleep 0.05
> $OUT_FILE
fi
done
}
write_pdb(){
while true
do
sleep 0.1
read user_inp
echo $user_inp > $IN_FILE
if [ "$user_inp" == "q" ];
then
kill $READER >/dev/null 2>&1
exit 1
fi
done
}
read_pdb &
READER=$!
write_pdb
要连接到终端中的断点,只需执行 bash 脚本。您必须确保路径(IN_FILE
、OUT_FILE
、LOCK
)是您系统的实际路径。
试试这个,如果有效请报告:
import code
code.InteractiveConsole(locals=globals()).interact()
它运行一个普通的 Python 控制台:
$ python3 ./temp.py
Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
我正在使用一款带有嵌入式(我希望这是正确的词)版本的 IronPython 的软件。 IP 允许用户编写与程序交互的 .py 脚本 API.
程序文件安装目录中没有可用的 ipy.exe 或 python.exe 文件。唯一的东西就是各种 IronPython DLL 文件。标准库完全可用。
没有可用的交互式命令行。我真的需要用比输出到纯文本弹出窗口更强大的东西来调试我的代码,但是当我尝试使用调试器时程序挂起:
import pdb;pdb.set_trace()
如果有一段代码我可以放入其中一个 .py 模块中,它将为当前 python 会话启动命令行 REPL,那就太好了。我已经使用 code
模块尝试了 SO 的解决方案,但它不起作用。
可以做到吗?
出于某种原因,使用 FIFO 无法正常工作,所以我改为实施这种奇怪的做法。它使用文件在控制器实例(只需从终端 运行 连接它)和从任何地方调用的不同代码中的 pdb
之间进行通信。还使用猴子补丁将我们的新 set_trace
变成 pdb
.
import time
import pdb
IN_FILE = "in.pdb"
OUT_FILE = "out.pdb"
class _Receiver(object):
def __init__(self, in_file):
self.in_file = in_file
with open(in_file, "w") as file:
file.write("")
def readline(self):
x = ""
while x == "":
time.sleep(0.1)
with open(self.in_file, "r") as file:
x = file.readline()
with open(self.in_file, "w") as file:
file.write("")
return x
class _Sender(object):
def __init__(self, out_file):
self.out_file = out_file
with open(out_file, "w") as file:
file.write("")
def write(self, x):
with open(self.out_file, "a") as file:
file.write(x)
def flush(self):
pass
mypdb = pdb.Pdb(stdin=_Receiver(in_file=IN_FILE),
stdout=_Sender(out_file=OUT_FILE))
pdb.set_trace = mypdb.set_trace
要在其他代码中使用断点:
from youcannameit import pdb
pdb.set_trace()
连接到 pdb 运行 cmd 中的主批处理脚本。它工作得很好。不如 bash 版本好,所以我会把它留在那里,以防您选择使用它。您不能改用 CRT-C 类型 q。一旦你退出,我建议打开一个新的 cmd,因为 运行 在同一个命令中再次 ning 是非常糟糕的。也许具有更多批处理知识的人可以修复它。
主批处理脚本:
@echo off
set IN_FILE="in.pdb"
set OUT_FILE="out.pdb"
set LOCK="lock.lock"
echo running > %LOCK%
start /b read.cmd
timeout 1 /NOBREAK >NUL
echo PDB:
goto :write_pdb
:print_file
type %OUT_FILE%
timeout 1 /NOBREAK >NUL
NUL > %OUT_FILE% 2>NUL
goto :read_pdb
:read_pdb
for /f %%i in (%OUT_FILE%) do set size=%%~zi
if %size% gtr 0 goto :print_file
timeout 1 /NOBREAK >NUL
goto :read_pdb
:write_pdb
set /P INPUT=%=%
echo %INPUT% > %IN_FILE%
If /I "%Input%"=="q" goto :end
If /I "%Input%"=="quit" goto :end
If /I "%Input%"=="exit" goto :end
goto :write_pdb
:end
del %LOCK%
echo Exiting..
EXIT /B 0
二次批处理脚本,以便我们可以同时打印和输入(命名为read.cmd并与主脚本放在同一文件夹中):
@echo off
set IN_FILE="in.pdb"
set OUT_FILE="out.pdb"
set LOCK="lock.lock"
goto :read_pdb
:print_file
type %OUT_FILE%
timeout 1 /NOBREAK >NUL
NUL > %OUT_FILE% 2>NUL
goto :read_pdb
:read_pdb
if not exist %LOCK% goto :end
for /f %%i in (%OUT_FILE%) do set size=%%~zi
if %size% gtr 0 goto :print_file
timeout 1 /NOBREAK >NUL
goto :read_pdb
:end
如果您坚持使用 bash 版本::
#!/bin/bash
IN_FILE="in.pdb"
OUT_FILE="out.pdb"
echo PDB:
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
kill $READER >/dev/null 2>&1
exit 1
}
read_pdb(){
while true
do
if [ -s $OUT_FILE ]
then
cat $OUT_FILE
sleep 0.05
> $OUT_FILE
fi
done
}
write_pdb(){
while true
do
sleep 0.1
read user_inp
echo $user_inp > $IN_FILE
if [ "$user_inp" == "q" ];
then
kill $READER >/dev/null 2>&1
exit 1
fi
done
}
read_pdb &
READER=$!
write_pdb
要连接到终端中的断点,只需执行 bash 脚本。您必须确保路径(IN_FILE
、OUT_FILE
、LOCK
)是您系统的实际路径。
试试这个,如果有效请报告:
import code
code.InteractiveConsole(locals=globals()).interact()
它运行一个普通的 Python 控制台:
$ python3 ./temp.py
Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>