Skip to content

Commit

Permalink
Coerce compiled regex patterns to bytes type in bytes mode
Browse files Browse the repository at this point in the history
  • Loading branch information
emerson.prado committed Mar 4, 2019
1 parent 03168dd commit 5887e61
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pexpect/spawnbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ def _coerce_expect_string(self, s):
return s.encode('ascii')
return s

# In bytes mode, regex patterns should also be of bytes type
def _coerce_expect_re(self, r):
p = r.pattern
if self.encoding is None and not isinstance(p, bytes):
return re.compile(p.encode('utf-8'))
return r

def _coerce_send_string(self, s):
if self.encoding is None and not isinstance(s, bytes):
return s.encode('utf-8')
Expand Down Expand Up @@ -232,6 +239,7 @@ def compile_pattern_list(self, patterns):
elif p is TIMEOUT:
compiled_pattern_list.append(TIMEOUT)
elif isinstance(p, type(re.compile(''))):
p = self._coerce_expect_re(p)
compiled_pattern_list.append(p)
else:
self._pattern_type_err(p)
Expand Down

0 comments on commit 5887e61

Please sign in to comment.