【发布时间】:2021-03-12 22:46:17
【问题描述】:
当我从 wexpect 运行 example code 时,它会冻结在 spawn 方法上。
import wexpect
prompt = '[A-Z]\:.+>'
child = wexpect.spawn('cmd.exe')
child.expect(prompt) # Wait for startup prompt
child.sendline('dir') # List the current directory
child.expect(prompt)
print(child.before) # Print the list
child.sendline('exit')
我发现 wexpect 中的 connect_to_child 方法(下面的代码从 repository 复制而来)会导致问题。它不断抛出“无管道”异常,并陷入永无止境的循环。
def connect_to_child(self):
pipe_name = 'wexpect_{}'.format(self.console_pid)
pipe_full_path = r'\\.\pipe\{}'.format(pipe_name)
logger.debug(f'Trying to connect to pipe: {pipe_full_path}')
while True:
try:
self.pipe = win32file.CreateFile(
pipe_full_path,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
logger.debug('Pipe found')
res = win32pipe.SetNamedPipeHandleState(self.pipe, win32pipe.PIPE_READMODE_MESSAGE,
None, None)
if res == 0:
logger.debug(f"SetNamedPipeHandleState return code: {res}")
return
except pywintypes.error as e:
if e.args[0] == winerror.ERROR_FILE_NOT_FOUND: # 2
logger.debug("no pipe, trying again in a bit later")
time.sleep(0.2)
else:
raise
我使用的是 Windows 10、python 3.9.1 和 wexpect 4.0.0
【问题讨论】:
-
你有没有想过这个问题?我在 win10、python 3.9.6 和 wexpect 4.0.0 上遇到了完全相同的问题。