[Python-checkins] cpython (2.7): Issue #20510: Rewrote test_exit in test_sys to match existing comments
zach.ware
python-checkins at python.org
Tue Feb 18 15:42:08 CET 2014
http://hg.python.org/cpython/rev/63f0a1e95d2b
changeset: 89249:63f0a1e95d2b
branch: 2.7
parent: 89243:465d81c1b2fc
user: Zachary Ware <zachary.ware at gmail.com>
date: Tue Feb 18 08:36:14 2014 -0600
summary:
Issue #20510: Rewrote test_exit in test_sys to match existing comments
and to modernize. Initial patch by Gareth Rees.
files:
Lib/test/test_sys.py | 86 +++++++++++--------------------
Misc/NEWS | 4 +
2 files changed, 35 insertions(+), 55 deletions(-)
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1,5 +1,6 @@
# -*- coding: iso-8859-1 -*-
import unittest, test.test_support
+from test.script_helper import assert_python_ok, assert_python_failure
import sys, os, cStringIO
import struct
import operator
@@ -114,90 +115,65 @@
clear_check(exc)
def test_exit(self):
+ # call with two arguments
self.assertRaises(TypeError, sys.exit, 42, 42)
# call without argument
- try:
- sys.exit(0)
- except SystemExit, exc:
- self.assertEqual(exc.code, 0)
- except:
- self.fail("wrong exception")
- else:
- self.fail("no exception")
+ rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()')
+ self.assertEqual(rc, 0)
+ self.assertEqual(out, b'')
+ self.assertEqual(err, b'')
+
+ # call with integer argument
+ with self.assertRaises(SystemExit) as cm:
+ sys.exit(42)
+ self.assertEqual(cm.exception.code, 42)
# call with tuple argument with one entry
# entry will be unpacked
- try:
- sys.exit(42)
- except SystemExit, exc:
- self.assertEqual(exc.code, 42)
- except:
- self.fail("wrong exception")
- else:
- self.fail("no exception")
-
- # call with integer argument
- try:
+ with self.assertRaises(SystemExit) as cm:
sys.exit((42,))
- except SystemExit, exc:
- self.assertEqual(exc.code, 42)
- except:
- self.fail("wrong exception")
- else:
- self.fail("no exception")
+ self.assertEqual(cm.exception.code, 42)
# call with string argument
- try:
+ with self.assertRaises(SystemExit) as cm:
sys.exit("exit")
- except SystemExit, exc:
- self.assertEqual(exc.code, "exit")
- except:
- self.fail("wrong exception")
- else:
- self.fail("no exception")
+ self.assertEqual(cm.exception.code, "exit")
# call with tuple argument with two entries
- try:
+ with self.assertRaises(SystemExit) as cm:
sys.exit((17, 23))
- except SystemExit, exc:
- self.assertEqual(exc.code, (17, 23))
- except:
- self.fail("wrong exception")
- else:
- self.fail("no exception")
+ self.assertEqual(cm.exception.code, (17, 23))
# test that the exit machinery handles SystemExits properly
- import subprocess
# both unnormalized...
- rc = subprocess.call([sys.executable, "-c",
- "raise SystemExit, 46"])
+ rc, out, err = assert_python_failure('-c', 'raise SystemExit, 46')
self.assertEqual(rc, 46)
+ self.assertEqual(out, b'')
+ self.assertEqual(err, b'')
# ... and normalized
- rc = subprocess.call([sys.executable, "-c",
- "raise SystemExit(47)"])
+ rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)')
self.assertEqual(rc, 47)
+ self.assertEqual(out, b'')
+ self.assertEqual(err, b'')
- def check_exit_message(code, expected, env=None):
- process = subprocess.Popen([sys.executable, "-c", code],
- stderr=subprocess.PIPE, env=env)
- stdout, stderr = process.communicate()
- self.assertEqual(process.returncode, 1)
- self.assertTrue(stderr.startswith(expected),
- "%s doesn't start with %s" % (repr(stderr), repr(expected)))
+ def check_exit_message(code, expected, **env_vars):
+ rc, out, err = assert_python_failure('-c', code, **env_vars)
+ self.assertEqual(rc, 1)
+ self.assertEqual(out, b'')
+ self.assertTrue(err.startswith(expected),
+ "%s doesn't start with %s" % (repr(err), repr(expected)))
- # test that stderr buffer if flushed before the exit message is written
+ # test that stderr buffer is flushed before the exit message is written
# into stderr
check_exit_message(
r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
b"unflushed,message")
# test that the unicode message is encoded to the stderr encoding
- env = os.environ.copy()
- env['PYTHONIOENCODING'] = 'latin-1'
check_exit_message(
r'import sys; sys.exit(u"h\xe9")',
- b"h\xe9", env=env)
+ b"h\xe9", PYTHONIOENCODING='latin-1')
def test_getdefaultencoding(self):
if test.test_support.have_unicode:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -243,6 +243,10 @@
Tests
-----
+- Issue #20510: Rewrote test_exit in test_sys to match existing comments,
+ use modern unittest features, and use helpers from test.script_helper
+ instead of using subprocess directly. Initial patch by Gareth Rees.
+
- Issue #20532: Tests which use _testcapi now are marked as CPython only.
- Issue #19920: Added tests for TarFile.list(). Based on patch by Vajrasky Kok.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list