Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7f5475

Browse files
committed
Use test.script_helper in test_pydoc
1 parent a6e81a2 commit f7f5475

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

Lib/test/test_pydoc.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
from io import StringIO
1616
from collections import namedtuple
1717
from contextlib import contextmanager
18+
19+
from test.script_helper import assert_python_ok
1820
from test.support import (
1921
TESTFN, forget, rmtree, EnvironmentVarGuard,
2022
reap_children, reap_threads, captured_output, captured_stdout, unlink
2123
)
22-
2324
from test import pydoc_mod
2425

2526
try:
@@ -201,14 +202,14 @@ class B(builtins.object)
201202
# output pattern for module with bad imports
202203
badimport_pattern = "problem in %s - ImportError: No module named %s"
203204

204-
def run_pydoc(module_name, *args):
205+
def run_pydoc(module_name, *args, **env):
205206
"""
206207
Runs pydoc on the specified module. Returns the stripped
207208
output of pydoc.
208209
"""
209-
cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name]
210-
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
211-
return output.strip()
210+
args = args + (module_name,)
211+
rc, out, err = assert_python_ok(pydoc.__file__, *args, **env)
212+
return out.strip()
212213

213214
def get_pydoc_html(module):
214215
"Returns pydoc generated output as html"
@@ -307,19 +308,20 @@ def test_badimport(self):
307308
def newdirinpath(dir):
308309
os.mkdir(dir)
309310
sys.path.insert(0, dir)
310-
yield
311-
sys.path.pop(0)
312-
rmtree(dir)
311+
try:
312+
yield
313+
finally:
314+
sys.path.pop(0)
315+
rmtree(dir)
313316

314-
with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
315-
env['PYTHONPATH'] = TESTFN
317+
with newdirinpath(TESTFN):
316318
fullmodname = os.path.join(TESTFN, modname)
317319
sourcefn = fullmodname + os.extsep + "py"
318320
for importstring, expectedinmsg in testpairs:
319321
with open(sourcefn, 'w') as f:
320322
f.write("import {}\n".format(importstring))
321323
try:
322-
result = run_pydoc(modname).decode("ascii")
324+
result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii")
323325
finally:
324326
forget(modname)
325327
expected = badimport_pattern % (modname, expectedinmsg)

0 commit comments

Comments
 (0)