-
Notifications
You must be signed in to change notification settings - Fork 478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Issue #558] Coerce compiled regex patterns type according to spawn encoding #559
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
068424c
to
5887e61
Compare
Some tests from myself: Test codeimport re
from lib.pexpect.pexpect import spawn, EOF, TIMEOUT
for spawn_string, encoding in [("ã", None), ("ã", 'ascii'), ("a", 'ascii'), ("ã", 'utf-8')]:
for pattern in ['ã', b'a', r'ã']:
try:
print('Spawn string: "{}"\tSpawn encoding: "{}"\tMatch pattern: "{}"'
.format(spawn_string, encoding, pattern))
spawn_object = spawn('echo "{}"'.format(spawn_string), encoding=encoding)
except UnicodeEncodeError as erro:
print('Spawn string format and encoding mismatch')
continue
index = -1
while index < 1:
try:
index = spawn_object.expect([re.compile(pattern), EOF, TIMEOUT])
except UnicodeDecodeError as erro:
print('Invalid pattern')
break
if index == 0:
print('Match') Before fixPython 3Spawn string: "ã" Spawn encoding: "None" Match pattern: "ã"
Traceback (most recent call last):
File "<stdin>", line 12, in <module>
File "<Pexpect path>/pexpect/spawnbase.py", line 341, in expect
timeout, searchwindowsize, async_)
File "<Pexpect path>/pexpect/spawnbase.py", line 369, in expect_list
return exp.expect_loop(timeout)
File "<Pexpect path>/pexpect/expect.py", line 103, in expect_loop
idx = self.new_data(incoming)
File "<Pexpect path>/pexpect/expect.py", line 29, in new_data
index = searcher.search(window, len(data))
File "<Pexpect path>/pexpect/expect.py", line 293, in search
match = s.search(buffer, searchstart)
TypeError: cannot use a string pattern on a bytes-like object Python 2Spawn string: "ã" Spawn encoding: "None" Match pattern: "ã"
Match
Spawn string: "ã" Spawn encoding: "None" Match pattern: "a"
Spawn string: "ã" Spawn encoding: "None" Match pattern: "ã"
Match
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "ã"
Invalid pattern
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "a"
Invalid pattern
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "ã"
Invalid pattern
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "ã"
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "a"
Match
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "ã"
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "ã" # Not nice
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "a"
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "ã" # Not nice After fixPython 3Spawn string: "ã" Spawn encoding: "None" Match pattern: "ã"
Match
Spawn string: "ã" Spawn encoding: "None" Match pattern: "b'a'"
Spawn string: "ã" Spawn encoding: "None" Match pattern: "ã"
Match
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "ã"
Spawn string format and encoding mismatch
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "b'a'"
Spawn string format and encoding mismatch
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "ã"
Spawn string format and encoding mismatch
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "ã"
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "b'a'"
Match
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "ã"
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "ã"
Match
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "b'a'"
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "ã"
Match Python 2Spawn string: "ã" Spawn encoding: "None" Match pattern: "ã"
Match
Spawn string: "ã" Spawn encoding: "None" Match pattern: "a"
Spawn string: "ã" Spawn encoding: "None" Match pattern: "ã"
Match
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "ã"
Invalid pattern
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "a"
Invalid pattern
Spawn string: "ã" Spawn encoding: "ascii" Match pattern: "ã"
Invalid pattern
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "ã"
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "a"
Match
Spawn string: "a" Spawn encoding: "ascii" Match pattern: "ã"
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "ã" # Nice
Match
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "a"
Spawn string: "ã" Spawn encoding: "utf-8" Match pattern: "ã" # Nice
Match |
d0cac26
to
823f68e
Compare
0e53f6f
to
ec916dd
Compare
I messed this thread too much. Sorry for the mess. I'll start over with a fresh branch and PR. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change extends the type coercing already present in exact matches for compiled regex matches. In short: if spawn encoding is
None
, it changes the compiled regex to have a bytes type pattern. Otherwise, changes to non-bytes type pattern.