11import unittest , test .support
2+ from test .script_helper import assert_python_ok , assert_python_failure
23import sys , io , os
34import struct
45import subprocess
@@ -86,74 +87,50 @@ def test_excepthook(self):
8687 # Python/pythonrun.c::PyErr_PrintEx() is tricky.
8788
8889 def test_exit (self ):
89-
90+ # call with two arguments
9091 self .assertRaises (TypeError , sys .exit , 42 , 42 )
9192
9293 # call without argument
93- try :
94- sys .exit (0 )
95- except SystemExit as exc :
96- self .assertEqual (exc .code , 0 )
97- except :
98- self .fail ("wrong exception" )
99- else :
100- self .fail ("no exception" )
94+ rc , out , err = assert_python_ok ('-c' , 'import sys; sys.exit()' )
95+ self .assertEqual (rc , 0 )
96+ self .assertEqual (out , b'' )
97+ self .assertEqual (err , b'' )
10198
102- # call with tuple argument with one entry
103- # entry will be unpacked
104- try :
99+ # call with integer argument
100+ with self .assertRaises (SystemExit ) as cm :
105101 sys .exit (42 )
106- except SystemExit as exc :
107- self .assertEqual (exc .code , 42 )
108- except :
109- self .fail ("wrong exception" )
110- else :
111- self .fail ("no exception" )
102+ self .assertEqual (cm .exception .code , 42 )
112103
113- # call with integer argument
114- try :
104+ # call with tuple argument with one entry
105+ # entry will be unpacked
106+ with self .assertRaises (SystemExit ) as cm :
115107 sys .exit ((42 ,))
116- except SystemExit as exc :
117- self .assertEqual (exc .code , 42 )
118- except :
119- self .fail ("wrong exception" )
120- else :
121- self .fail ("no exception" )
108+ self .assertEqual (cm .exception .code , 42 )
122109
123110 # call with string argument
124- try :
111+ with self . assertRaises ( SystemExit ) as cm :
125112 sys .exit ("exit" )
126- except SystemExit as exc :
127- self .assertEqual (exc .code , "exit" )
128- except :
129- self .fail ("wrong exception" )
130- else :
131- self .fail ("no exception" )
113+ self .assertEqual (cm .exception .code , "exit" )
132114
133115 # call with tuple argument with two entries
134- try :
116+ with self . assertRaises ( SystemExit ) as cm :
135117 sys .exit ((17 , 23 ))
136- except SystemExit as exc :
137- self .assertEqual (exc .code , (17 , 23 ))
138- except :
139- self .fail ("wrong exception" )
140- else :
141- self .fail ("no exception" )
118+ self .assertEqual (cm .exception .code , (17 , 23 ))
142119
143120 # test that the exit machinery handles SystemExits properly
144- rc = subprocess .call ([sys .executable , "-c" ,
145- "raise SystemExit(47)" ])
121+ rc , out , err = assert_python_failure ('-c' , 'raise SystemExit(47)' )
146122 self .assertEqual (rc , 47 )
123+ self .assertEqual (out , b'' )
124+ self .assertEqual (err , b'' )
147125
148- def check_exit_message (code , expected , env = None ):
149- process = subprocess .Popen ([sys .executable , "-c" , code ],
150- stderr = subprocess .PIPE , env = env )
151- stdout , stderr = process .communicate ()
152- self .assertEqual (process .returncode , 1 )
153- self .assertTrue (stderr .startswith (expected ),
154- "%s doesn't start with %s" % (ascii (stderr ), ascii (expected )))
126+ def check_exit_message (code , expected , ** env_vars ):
127+ rc , out , err = assert_python_failure ('-c' , code , ** env_vars )
128+ self .assertEqual (rc , 1 )
129+ self .assertEqual (out , b'' )
130+ self .assertTrue (err .startswith (expected ),
131+ "%s doesn't start with %s" % (ascii (err ), ascii (expected )))
155132
156- # test that stderr buffer if flushed before the exit message is written
133+ # test that stderr buffer is flushed before the exit message is written
157134 # into stderr
158135 check_exit_message (
159136 r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")' ,
@@ -167,11 +144,9 @@ def check_exit_message(code, expected, env=None):
167144
168145 # test that the unicode message is encoded to the stderr encoding
169146 # instead of the default encoding (utf8)
170- env = os .environ .copy ()
171- env ['PYTHONIOENCODING' ] = 'latin-1'
172147 check_exit_message (
173148 r'import sys; sys.exit("h\xe9")' ,
174- b"h\xe9 " , env = env )
149+ b"h\xe9 " , PYTHONIOENCODING = 'latin-1' )
175150
176151 def test_getdefaultencoding (self ):
177152 self .assertRaises (TypeError , sys .getdefaultencoding , 42 )
0 commit comments