简体   繁体   English

为什么 strip() 修复这个 pexpect 脚本?

[英]Why does strip() fix this pexpect script?

I have an object.我有一个 object。

This object has a connect() method which spawns a pexpect process.这个 object 有一个connect()方法,它产生一个 pexpect 进程。

The process that's spawned is a custom serial interface.产生的进程是一个自定义串行接口。 On launch, this tool prints a menu of serial devices to connect to, like so:启动时,此工具会打印要连接的串行设备菜单,如下所示:

 libftdi device (0): A6005jpt libftdi device (1): acFX9DQf Serial device (a): /dev/cu.Bluetooth-PDA-Sync Select a device by its letter (^D to abort): libftdi 设备 (0): A6005jpt libftdi 设备 (1): acFX9DQf 串行设备 (a): /dev/cu.Bluetooth-PDA-Sync Select 设备的字母(^D 中止):

My connect() determines which number to pass based on a given devicename (eg 'acFX9DQf'): (self.connection is the pexpect spawn)我的connect()根据给定的设备名称(例如“acFX9DQf”)确定要传递的数字:(self.connection 是 pexpect spawn)

USBSERIAL_DEVICE_NAME = "acFX9DQf"    

try:
    while self.connection.expect(['libftdi device \(([0-9])\): (.*)','Serial device']) == 0:
        if self.connection.match.group(2).strip() == USBSERIAL_DEVICE_NAME:
            # do stuff
except:
    # do stuff

Now, my problem is that I connect() / kill() the process multiple times in my main logic and sometimes, one of those times, connect() decides to throw a pexpect.TIMEOUT exception, unexpectedly.现在,我的问题是我在主逻辑中多次connect() / kill()进程,有时,其中一次, connect()决定意外抛出pexpect.TIMEOUT异常。

For example, when I add the following debug statements to my logic, like so:例如,当我将以下调试语句添加到我的逻辑时,如下所示:

USBSERIAL_DEVICE_NAME = "acFX9DQf"

try:
    while self.connection.expect(['libftdi device \(([0-9])\): (.*)','Serial device'], timeout=10) == 0:
        print "MATCHED A DEVICE LINE!"
        if self.connection.match.group(2).strip() == USBSERIAL_DEVICE_NAME:
            print "MATCHED THE DESIRED USBSERIAL..."

...I get this output for many calls of connect() : ...我得到这个 output 用于许多connect()调用:

 libftdi device (0): A6005jpt MATCHED A DEVICE LINE: libftdi device (1). libftdi 设备 (0):A6005jpt 匹配设备行:libftdi 设备 (1)。 acFX9DQf MATCHED A DEVICE LINE. acFX9DQf 匹配设备线。 MATCHED THE DESIRED USBSERIAL.:.匹配所需的 USBSERIAL。:. Serial device (a): /dev/cu.Bluetooth-PDA-Sync Select a device by its letter (^D to abort): 1串行设备 (a):/dev/cu.Bluetooth-PDA-Sync Select 设备按其字母(^D 中止):1

...then, one of my connect() calls will unexpectedly do.... ...然后,我的一个connect()调用会出乎意料地做....

 libftdi device (0): A6005jpt MATCHED A DEVICE LINE: libftdi device (1): acFX9DQf Serial device (a). libftdi 设备 (0):A6005jpt 匹配设备线:libftdi 设备 (1):acFX9DQf 串行设备 (a)。 /dev/cu:Bluetooth-PDA-Sync Select a device by its letter (^D to abort): MATCHED A DEVICE LINE! /dev/cu:Bluetooth-PDA-Sync Select 设备的字母(^D 中止):匹配设备线! <<EXCEPTION>> <<异常>>

But, if I revise my code to this:但是,如果我将代码修改为:

try:
    while self.connection.expect(['libftdi device \(([0-9])\): (.*)','Serial device'], timeout=10) == 0:
        devicename = self.connection.match.group(2).strip()
        if devicename == USBSERIAL_DEVICE_NAME:
            # do stuff

My problems go away, I can run it repeatedly and no problems will occur - no exceptions.我的问题 go 消失了,我可以反复运行它,不会出现任何问题 - 没有例外。 no nothin'.没什么。

So wot in the heck is going on here?所以这里到底发生了什么? I'm not even sure where to begin with this problem.我什至不知道从哪里开始解决这个问题。

I'm guessing you're getting a TIMEOUT exception.我猜你得到一个TIMEOUT异常。 Based on where the "MATCHED A DEVICE LINE," message is in the output, my hypothesis is that the spawn instance has multiple lines when it tests for a match.根据输出中“匹配设备行”消息的位置,我的假设是spawn实例在测试匹配时有多行。 When pexpect compiles the regexes, it sets re.DOTALL , so the .* includes the newline and the additional lines.当 pexpect 编译正则表达式时,它设置re.DOTALL ,因此.*包括换行符和附加行。 This causes the test against USBSERIAL_DEVICE_NAME to fail and the loop continues its next iteration.这会导致针对USBSERIAL_DEVICE_NAME的测试失败,并且循环继续其下一次迭代。 The next call to expect blocks because there is no additional input and you get a timeout.下一次调用expect阻塞,因为没有额外的输入并且你会超时。

To fix this, you can pass in your own compiled regexes (which will lack the re.DOTALL flag) or change the .* so it explicitly doesn't match the newline (eg \S* ).要解决此问题,您可以传入自己编译的正则表达式(将缺少re.DOTALL标志)或更改.*使其明确不匹配换行符(例如\S* )。

As for why storing the matched group in a variable seems to fix things, I can only guess this introduces a subtle timing change that prevents the call to expect from receiving multiple lines of input at once.至于为什么将匹配组存储在变量中似乎可以解决问题,我只能猜测这会引入一个微妙的时间变化,阻止调用expect一次接收多行输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM