diff options
author | Tim Peters <tim.peters@gmail.com> | 2004-07-31 21:14:28 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2004-07-31 21:14:28 (GMT) |
commit | 579f7355fe2c90b3f7001135c0ec0766dad6d244 (patch) | |
tree | ea6e02a208d1d22bf4257eef6129ecf06816b78c | |
parent | 9c8fe1a9b5b6bd1788d045e53f4a5a65eacaa0f1 (diff) | |
download | cpython-579f7355fe2c90b3f7001135c0ec0766dad6d244.zip cpython-579f7355fe2c90b3f7001135c0ec0766dad6d244.tar.gz cpython-579f7355fe2c90b3f7001135c0ec0766dad6d244.tar.bz2 |
Be more careful about reverting mutuations to system-wide (sys) variables.
This fixes 15 spurious test failures on Windows (probably all due to
the test leaving a wrong path in sys.argv[0], which then prevented
regrtest.py from finding the expected-output files for tests running
after test_optparse).
-rw-r--r-- | Lib/test/test_optparse.py | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py index b850cec..2f1f5d2 100644 --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -125,19 +125,25 @@ and kwargs %(kwargs)r def assertParseFail(self, cmdline_args, expected_output): """Assert the parser fails with the expected message.""" - sys.stderr = StringIO() - self.assertRaises(self.parser.parse_args, (cmdline_args,), None, - SystemExit, expected_output, - self.redirected_stderr) - sys.stderr = sys.__stderr__ + save_stderr = sys.stderr + try: + sys.stderr = StringIO() + self.assertRaises(self.parser.parse_args, (cmdline_args,), None, + SystemExit, expected_output, + self.redirected_stderr) + finally: + sys.stderr = save_stderr def assertStdoutEquals(self, cmdline_args, expected_output): """Assert the parser prints the expected output on stdout.""" - sys.stdout = StringIO() - self.assertRaises(self.parser.parse_args, (cmdline_args,), None, - SystemExit, expected_output, - self.redirected_stdout) - sys.stdout = sys.__stdout__ + save_stdout = sys.stdout + try: + sys.stdout = StringIO() + self.assertRaises(self.parser.parse_args, (cmdline_args,), None, + SystemExit, expected_output, + self.redirected_stdout) + finally: + sys.stdout = save_stdout def assertTypeError(self, func, expected_output, *args): """Assert a TypeError is raised when executing func.""" @@ -426,16 +432,21 @@ class TestProgName(BaseTest): def test_default_progname(self): # Make sure that program name taken from sys.argv[0] by default. - sys.argv[0] = "/foo/bar/baz.py" - parser = OptionParser("usage: %prog ...", version="%prog 1.2") - expected_usage = "usage: baz.py ...\n" - self.assertUsage(parser, expected_usage) - self.assertVersion(parser, "baz.py 1.2") - self.assertHelp(parser, - expected_usage + "\n" + - "options:\n" - " --version show program's version number and exit\n" - " -h, --help show this help message and exit\n") + save_argv = sys.argv[:] + try: + # XXX Should the path be hard-coding forward-slashes? + sys.argv[0] = "/foo/bar/baz.py" + parser = OptionParser("usage: %prog ...", version="%prog 1.2") + expected_usage = "usage: baz.py ...\n" + self.assertUsage(parser, expected_usage) + self.assertVersion(parser, "baz.py 1.2") + self.assertHelp(parser, + expected_usage + "\n" + + "options:\n" + " --version show program's version number and exit\n" + " -h, --help show this help message and exit\n") + finally: + sys.argv[:] = save_argv def test_custom_progname(self): parser = OptionParser(prog="thingy", |