-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-110944: Move pty helper to test.support and add basic pdb completion test #111826
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e35c29
Add readline tests for pdb completion
gaogaotiantian ae64318
Disable test with libedit
gaogaotiantian ddd67d5
Improve comments
gaogaotiantian 2bc94b7
Update Lib/test/test_pdb.py
gaogaotiantian fe6c8ab
Use commands instead of cont
gaogaotiantian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Helper to run a script in a pseudo-terminal. | ||
""" | ||
import os | ||
import selectors | ||
import subprocess | ||
import sys | ||
from contextlib import ExitStack | ||
from errno import EIO | ||
|
||
from test.support.import_helper import import_module | ||
|
||
def run_pty(script, input=b"dummy input\r", env=None): | ||
pty = import_module('pty') | ||
output = bytearray() | ||
[master, slave] = pty.openpty() | ||
args = (sys.executable, '-c', script) | ||
proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave, env=env) | ||
os.close(slave) | ||
with ExitStack() as cleanup: | ||
cleanup.enter_context(proc) | ||
def terminate(proc): | ||
try: | ||
proc.terminate() | ||
except ProcessLookupError: | ||
# Workaround for Open/Net BSD bug (Issue 16762) | ||
pass | ||
cleanup.callback(terminate, proc) | ||
cleanup.callback(os.close, master) | ||
# Avoid using DefaultSelector and PollSelector. Kqueue() does not | ||
# work with pseudo-terminals on OS X < 10.9 (Issue 20365) and Open | ||
# BSD (Issue 20667). Poll() does not work with OS X 10.6 or 10.4 | ||
# either (Issue 20472). Hopefully the file descriptor is low enough | ||
# to use with select(). | ||
sel = cleanup.enter_context(selectors.SelectSelector()) | ||
sel.register(master, selectors.EVENT_READ | selectors.EVENT_WRITE) | ||
os.set_blocking(master, False) | ||
while True: | ||
for [_, events] in sel.select(): | ||
if events & selectors.EVENT_READ: | ||
try: | ||
chunk = os.read(master, 0x10000) | ||
except OSError as err: | ||
# Linux raises EIO when slave is closed (Issue 5380) | ||
if err.errno != EIO: | ||
raise | ||
chunk = b"" | ||
if not chunk: | ||
return output | ||
output.extend(chunk) | ||
if events & selectors.EVENT_WRITE: | ||
try: | ||
input = input[os.write(master, input):] | ||
except OSError as err: | ||
# Apparently EIO means the slave was closed | ||
if err.errno != EIO: | ||
raise | ||
input = b"" # Stop writing | ||
if not input: | ||
sel.modify(master, selectors.EVENT_READ) |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,6 +15,8 @@ | |||||||||||||
from io import StringIO | ||||||||||||||
from test import support | ||||||||||||||
from test.support import os_helper | ||||||||||||||
from test.support.import_helper import import_module | ||||||||||||||
from test.support.pty_helper import run_pty | ||||||||||||||
# This little helper class is essential for testing pdb under doctest. | ||||||||||||||
from test.test_doctest import _FakeInput | ||||||||||||||
from unittest.mock import patch | ||||||||||||||
|
@@ -3235,6 +3237,32 @@ def test_checkline_is_not_executable(self): | |||||||||||||
self.assertFalse(db.checkline(os_helper.TESTFN, lineno)) | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
@support.requires_subprocess() | ||||||||||||||
class PdbTestReadline(unittest.TestCase): | ||||||||||||||
def setUpClass(): | ||||||||||||||
# Ensure that the readline module is loaded | ||||||||||||||
# If this fails, the test is skipped because SkipTest will be raised | ||||||||||||||
readline = import_module('readline') | ||||||||||||||
if readline.__doc__ and "libedit" in readline.__doc__: | ||||||||||||||
raise unittest.SkipTest("libedit readline is not supported for pdb") | ||||||||||||||
|
||||||||||||||
def test_basic_completion(self): | ||||||||||||||
script = textwrap.dedent(""" | ||||||||||||||
import pdb; pdb.Pdb().set_trace() | ||||||||||||||
print('hello') | ||||||||||||||
""") | ||||||||||||||
|
||||||||||||||
# List everything starting with 'co', there should be multiple matches | ||||||||||||||
# then add ntin and complete 'contin' to 'continue' | ||||||||||||||
input = b"co\t\tntin\t\n" | ||||||||||||||
|
||||||||||||||
output = run_pty(script, input) | ||||||||||||||
|
||||||||||||||
self.assertIn(b'cont', output) | ||||||||||||||
self.assertIn(b'condition', output) | ||||||||||||||
self.assertIn(b'continue', output) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Also, IMO it would be nice to assert that the
Suggested change
|
||||||||||||||
|
||||||||||||||
|
||||||||||||||
def load_tests(loader, tests, pattern): | ||||||||||||||
from test import test_pdb | ||||||||||||||
tests.addTest(doctest.DocTestSuite(test_pdb)) | ||||||||||||||
|
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.