TypeError: cannot use a string pattern on a bytes-like object : Pyexpect In Python3

Viewed 173

I am trying to use pexpect module (version 4.8.0) with Python 3.6.8. I get an error

TypeError: cannot use a string pattern on a bytes-like object

Here is my code:

buff = BytesIO()
child = pexpect.spawn(path, args, logfile=buff, timeout=self.PASSPHRASE_TIMEOUT, **kwargs)
while True:
     idx = child.expect([self.PASSPHRASE_RE,pexpect.EOF])
     if idx == 0:
        child.sendline(passphrase)
     elif idx == 1:
          child.wait()
          break

I am getting an error in line idx = child.expect([self.PASSPHRASE_RE,pexpect.EOF])

Note: I have already tried some solutions that are there on StackOverflow like:

  1. pass encoding('utf-8') parameter in pexpect.spawn.
  2. Replace pexpect.spawn with pexpect.spawnu

But no luck again same error.

Please please help me I have alreday wasted 3 days to resolve this errors.

1 Answers

The issue was with the PASSPHRASE_RE type which was sre.SRE_Pattern.

I just converted it into the string using PASSPHRASE_RE.pattern

Related