|
15 | 15 | from io import StringIO |
16 | 16 | from collections import namedtuple |
17 | 17 | from contextlib import contextmanager |
| 18 | + |
| 19 | +from test.script_helper import assert_python_ok |
18 | 20 | from test.support import ( |
19 | 21 | TESTFN, forget, rmtree, EnvironmentVarGuard, |
20 | 22 | reap_children, reap_threads, captured_output, captured_stdout, unlink |
21 | 23 | ) |
22 | | - |
23 | 24 | from test import pydoc_mod |
24 | 25 |
|
25 | 26 | try: |
@@ -201,14 +202,14 @@ class B(builtins.object) |
201 | 202 | # output pattern for module with bad imports |
202 | 203 | badimport_pattern = "problem in %s - ImportError: No module named %s" |
203 | 204 |
|
204 | | -def run_pydoc(module_name, *args): |
| 205 | +def run_pydoc(module_name, *args, **env): |
205 | 206 | """ |
206 | 207 | Runs pydoc on the specified module. Returns the stripped |
207 | 208 | output of pydoc. |
208 | 209 | """ |
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() |
212 | 213 |
|
213 | 214 | def get_pydoc_html(module): |
214 | 215 | "Returns pydoc generated output as html" |
@@ -307,19 +308,20 @@ def test_badimport(self): |
307 | 308 | def newdirinpath(dir): |
308 | 309 | os.mkdir(dir) |
309 | 310 | 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) |
313 | 316 |
|
314 | | - with newdirinpath(TESTFN), EnvironmentVarGuard() as env: |
315 | | - env['PYTHONPATH'] = TESTFN |
| 317 | + with newdirinpath(TESTFN): |
316 | 318 | fullmodname = os.path.join(TESTFN, modname) |
317 | 319 | sourcefn = fullmodname + os.extsep + "py" |
318 | 320 | for importstring, expectedinmsg in testpairs: |
319 | 321 | with open(sourcefn, 'w') as f: |
320 | 322 | f.write("import {}\n".format(importstring)) |
321 | 323 | try: |
322 | | - result = run_pydoc(modname).decode("ascii") |
| 324 | + result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii") |
323 | 325 | finally: |
324 | 326 | forget(modname) |
325 | 327 | expected = badimport_pattern % (modname, expectedinmsg) |
|
0 commit comments