Issue #18401: Fix test_pdb on Windows

* Use "with Popen" to cleanup properly the process
* Use support.temp_cwd() to properly change the working directory
* Use environ.pop() to cleanup the code
This commit is contained in:
Victor Stinner 2016-09-09 23:22:09 -07:00
parent 54de2b1edd
commit bc6262675c
1 changed files with 7 additions and 14 deletions

View File

@ -4,7 +4,6 @@ import doctest
import os import os
import pdb import pdb
import sys import sys
import tempfile
import types import types
import unittest import unittest
import subprocess import subprocess
@ -1057,19 +1056,15 @@ class PdbTestCase(unittest.TestCase):
def test_readrc_kwarg(self): def test_readrc_kwarg(self):
save_home = os.environ.get('HOME', None)
save_dir = os.getcwd()
script = textwrap.dedent(""" script = textwrap.dedent("""
import pdb; pdb.Pdb(readrc=False).set_trace() import pdb; pdb.Pdb(readrc=False).set_trace()
print('hello') print('hello')
""") """)
try:
if save_home is not None:
del os.environ['HOME']
with tempfile.TemporaryDirectory() as dirname: save_home = os.environ.pop('HOME', None)
os.chdir(dirname) try:
with support.temp_cwd():
with open('.pdbrc', 'w') as f: with open('.pdbrc', 'w') as f:
f.write("invalid\n") f.write("invalid\n")
@ -1083,8 +1078,7 @@ class PdbTestCase(unittest.TestCase):
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
) )
self.addCleanup(proc.stdout.close) with proc:
self.addCleanup(proc.stderr.close)
stdout, stderr = proc.communicate(b'q\n') stdout, stderr = proc.communicate(b'q\n')
self.assertNotIn("NameError: name 'invalid' is not defined", self.assertNotIn("NameError: name 'invalid' is not defined",
stdout.decode()) stdout.decode())
@ -1092,7 +1086,6 @@ class PdbTestCase(unittest.TestCase):
finally: finally:
if save_home is not None: if save_home is not None:
os.environ['HOME'] = save_home os.environ['HOME'] = save_home
os.chdir(save_dir)
def tearDown(self): def tearDown(self):
support.unlink(support.TESTFN) support.unlink(support.TESTFN)