mirror of https://github.com/python/cpython
Issue #20510: Rewrote test_exit in test_sys to match existing comments
and to modernize. Patch by Gareth Rees.
This commit is contained in:
parent
5b8d2c3af7
commit
cefe6b34de
|
@ -1,4 +1,5 @@
|
||||||
import unittest, test.support
|
import unittest, test.support
|
||||||
|
from test.script_helper import assert_python_ok, assert_python_failure
|
||||||
import sys, io, os
|
import sys, io, os
|
||||||
import struct
|
import struct
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -86,74 +87,50 @@ class SysModuleTest(unittest.TestCase):
|
||||||
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
|
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
|
||||||
|
|
||||||
def test_exit(self):
|
def test_exit(self):
|
||||||
|
# call with two arguments
|
||||||
self.assertRaises(TypeError, sys.exit, 42, 42)
|
self.assertRaises(TypeError, sys.exit, 42, 42)
|
||||||
|
|
||||||
# call without argument
|
# call without argument
|
||||||
try:
|
rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()')
|
||||||
sys.exit(0)
|
self.assertEqual(rc, 0)
|
||||||
except SystemExit as exc:
|
self.assertEqual(out, b'')
|
||||||
self.assertEqual(exc.code, 0)
|
self.assertEqual(err, b'')
|
||||||
except:
|
|
||||||
self.fail("wrong exception")
|
# call with integer argument
|
||||||
else:
|
with self.assertRaises(SystemExit) as cm:
|
||||||
self.fail("no exception")
|
sys.exit(42)
|
||||||
|
self.assertEqual(cm.exception.code, 42)
|
||||||
|
|
||||||
# call with tuple argument with one entry
|
# call with tuple argument with one entry
|
||||||
# entry will be unpacked
|
# entry will be unpacked
|
||||||
try:
|
with self.assertRaises(SystemExit) as cm:
|
||||||
sys.exit(42)
|
|
||||||
except SystemExit as exc:
|
|
||||||
self.assertEqual(exc.code, 42)
|
|
||||||
except:
|
|
||||||
self.fail("wrong exception")
|
|
||||||
else:
|
|
||||||
self.fail("no exception")
|
|
||||||
|
|
||||||
# call with integer argument
|
|
||||||
try:
|
|
||||||
sys.exit((42,))
|
sys.exit((42,))
|
||||||
except SystemExit as exc:
|
self.assertEqual(cm.exception.code, 42)
|
||||||
self.assertEqual(exc.code, 42)
|
|
||||||
except:
|
|
||||||
self.fail("wrong exception")
|
|
||||||
else:
|
|
||||||
self.fail("no exception")
|
|
||||||
|
|
||||||
# call with string argument
|
# call with string argument
|
||||||
try:
|
with self.assertRaises(SystemExit) as cm:
|
||||||
sys.exit("exit")
|
sys.exit("exit")
|
||||||
except SystemExit as exc:
|
self.assertEqual(cm.exception.code, "exit")
|
||||||
self.assertEqual(exc.code, "exit")
|
|
||||||
except:
|
|
||||||
self.fail("wrong exception")
|
|
||||||
else:
|
|
||||||
self.fail("no exception")
|
|
||||||
|
|
||||||
# call with tuple argument with two entries
|
# call with tuple argument with two entries
|
||||||
try:
|
with self.assertRaises(SystemExit) as cm:
|
||||||
sys.exit((17, 23))
|
sys.exit((17, 23))
|
||||||
except SystemExit as exc:
|
self.assertEqual(cm.exception.code, (17, 23))
|
||||||
self.assertEqual(exc.code, (17, 23))
|
|
||||||
except:
|
|
||||||
self.fail("wrong exception")
|
|
||||||
else:
|
|
||||||
self.fail("no exception")
|
|
||||||
|
|
||||||
# test that the exit machinery handles SystemExits properly
|
# test that the exit machinery handles SystemExits properly
|
||||||
rc = subprocess.call([sys.executable, "-c",
|
rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)')
|
||||||
"raise SystemExit(47)"])
|
|
||||||
self.assertEqual(rc, 47)
|
self.assertEqual(rc, 47)
|
||||||
|
self.assertEqual(out, b'')
|
||||||
|
self.assertEqual(err, b'')
|
||||||
|
|
||||||
def check_exit_message(code, expected, env=None):
|
def check_exit_message(code, expected, **env_vars):
|
||||||
process = subprocess.Popen([sys.executable, "-c", code],
|
rc, out, err = assert_python_failure('-c', code, **env_vars)
|
||||||
stderr=subprocess.PIPE, env=env)
|
self.assertEqual(rc, 1)
|
||||||
stdout, stderr = process.communicate()
|
self.assertEqual(out, b'')
|
||||||
self.assertEqual(process.returncode, 1)
|
self.assertTrue(err.startswith(expected),
|
||||||
self.assertTrue(stderr.startswith(expected),
|
"%s doesn't start with %s" % (ascii(err), ascii(expected)))
|
||||||
"%s doesn't start with %s" % (ascii(stderr), ascii(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
|
# into stderr
|
||||||
check_exit_message(
|
check_exit_message(
|
||||||
r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
|
r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
|
||||||
|
@ -167,11 +144,9 @@ class SysModuleTest(unittest.TestCase):
|
||||||
|
|
||||||
# test that the unicode message is encoded to the stderr encoding
|
# test that the unicode message is encoded to the stderr encoding
|
||||||
# instead of the default encoding (utf8)
|
# instead of the default encoding (utf8)
|
||||||
env = os.environ.copy()
|
|
||||||
env['PYTHONIOENCODING'] = 'latin-1'
|
|
||||||
check_exit_message(
|
check_exit_message(
|
||||||
r'import sys; sys.exit("h\xe9")',
|
r'import sys; sys.exit("h\xe9")',
|
||||||
b"h\xe9", env=env)
|
b"h\xe9", PYTHONIOENCODING='latin-1')
|
||||||
|
|
||||||
def test_getdefaultencoding(self):
|
def test_getdefaultencoding(self):
|
||||||
self.assertRaises(TypeError, sys.getdefaultencoding, 42)
|
self.assertRaises(TypeError, sys.getdefaultencoding, 42)
|
||||||
|
|
|
@ -110,6 +110,10 @@ IDLE
|
||||||
Tests
|
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. Patch by Gareth Rees.
|
||||||
|
|
||||||
- Issue #20532: Tests which use _testcapi are now marked as CPython only.
|
- Issue #20532: Tests which use _testcapi are now marked as CPython only.
|
||||||
|
|
||||||
- Issue #19920: Added tests for TarFile.list(). Based on patch by Vajrasky Kok.
|
- Issue #19920: Added tests for TarFile.list(). Based on patch by Vajrasky Kok.
|
||||||
|
|
Loading…
Reference in New Issue