由于某些原因,使用FIFO功能无法正常工作,因此我实施了这种奇怪的方法。 它使用文件在控制器实例(仅从终端运行)和在从任何地方调用的不同代码之间进行通信。 还使用猴子补丁将我们的新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,因为在同一个cmd中再次运行会非常有bug。也许有更多批处理知识的人能够修复它。
主批处理脚本:
@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 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
)是您的系统上的实际路径。