Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(82790)

Side by Side Diff: Lib/test/test_cmd_line.py

Issue 23670: Modifications to support iOS as a development platform
Patch Set: Created 3 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_capi.py ('k') | Lib/test/test_doctest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Tests invocation of the interpreter with various command line arguments 1 # Tests invocation of the interpreter with various command line arguments
2 # Most tests are executed with environment variables ignored 2 # Most tests are executed with environment variables ignored
3 # See test_cmd_line_script.py for testing of script execution 3 # See test_cmd_line_script.py for testing of script execution
4 4
5 import test.support, unittest 5 import test.support, unittest
6 import os 6 import os
7 import shutil 7 import shutil
8 import sys 8 import sys
9 import subprocess 9 import subprocess
10 import tempfile 10 import tempfile
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 50
51 def test_verbose(self): 51 def test_verbose(self):
52 # -v causes imports to write to stderr. If the write to 52 # -v causes imports to write to stderr. If the write to
53 # stderr itself causes an import to happen (for the output 53 # stderr itself causes an import to happen (for the output
54 # codec), a recursion loop can occur. 54 # codec), a recursion loop can occur.
55 rc, out, err = assert_python_ok('-v') 55 rc, out, err = assert_python_ok('-v')
56 self.assertNotIn(b'stack overflow', err) 56 self.assertNotIn(b'stack overflow', err)
57 rc, out, err = assert_python_ok('-vv') 57 rc, out, err = assert_python_ok('-vv')
58 self.assertNotIn(b'stack overflow', err) 58 self.assertNotIn(b'stack overflow', err)
59 59
60 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess .Popen()")
60 def test_xoptions(self): 61 def test_xoptions(self):
61 def get_xoptions(*args): 62 def get_xoptions(*args):
62 # use subprocess module directly because test.support.script_helper adds 63 # use subprocess module directly because test.support.script_helper adds
63 # "-X faulthandler" to the command line 64 # "-X faulthandler" to the command line
64 args = (sys.executable, '-E') + args 65 args = (sys.executable, '-E') + args
65 args += ('-c', 'import sys; print(sys._xoptions)') 66 args += ('-c', 'import sys; print(sys._xoptions)')
66 out = subprocess.check_output(args) 67 out = subprocess.check_output(args)
67 opts = eval(out.splitlines()[0]) 68 opts = eval(out.splitlines()[0])
68 return opts 69 return opts
69 70
70 opts = get_xoptions() 71 opts = get_xoptions()
71 self.assertEqual(opts, {}) 72 self.assertEqual(opts, {})
72 73
73 opts = get_xoptions('-Xa', '-Xb=c,d=e') 74 opts = get_xoptions('-Xa', '-Xb=c,d=e')
74 self.assertEqual(opts, {'a': True, 'b': 'c,d=e'}) 75 self.assertEqual(opts, {'a': True, 'b': 'c,d=e'})
75 76
77 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess .Popen()")
76 def test_showrefcount(self): 78 def test_showrefcount(self):
77 def run_python(*args): 79 def run_python(*args):
78 # this is similar to assert_python_ok but doesn't strip 80 # this is similar to assert_python_ok but doesn't strip
79 # the refcount from stderr. It can be replaced once 81 # the refcount from stderr. It can be replaced once
80 # assert_python_ok stops doing that. 82 # assert_python_ok stops doing that.
81 cmd = [sys.executable] 83 cmd = [sys.executable]
82 cmd.extend(args) 84 cmd.extend(args)
83 PIPE = subprocess.PIPE 85 PIPE = subprocess.PIPE
84 p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE) 86 p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE)
85 out, err = p.communicate() 87 out, err = p.communicate()
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 command = ("assert(ord(%r) == %s)" 141 command = ("assert(ord(%r) == %s)"
140 % (test.support.FS_NONASCII, ord(test.support.FS_NONASCII))) 142 % (test.support.FS_NONASCII, ord(test.support.FS_NONASCII)))
141 assert_python_ok('-c', command) 143 assert_python_ok('-c', command)
142 144
143 # On Windows, pass bytes to subprocess doesn't test how Python decodes the 145 # On Windows, pass bytes to subprocess doesn't test how Python decodes the
144 # command line, but how subprocess does decode bytes to unicode. Python 146 # command line, but how subprocess does decode bytes to unicode. Python
145 # doesn't decode the command line because Windows provides directly the 147 # doesn't decode the command line because Windows provides directly the
146 # arguments as unicode (using wmain() instead of main()). 148 # arguments as unicode (using wmain() instead of main()).
147 @unittest.skipIf(sys.platform == 'win32', 149 @unittest.skipIf(sys.platform == 'win32',
148 'Windows has a native unicode API') 150 'Windows has a native unicode API')
151 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess .Popen()")
149 def test_undecodable_code(self): 152 def test_undecodable_code(self):
150 undecodable = b"\xff" 153 undecodable = b"\xff"
151 env = os.environ.copy() 154 env = os.environ.copy()
152 # Use C locale to get ascii for the locale encoding 155 # Use C locale to get ascii for the locale encoding
153 env['LC_ALL'] = 'C' 156 env['LC_ALL'] = 'C'
154 code = ( 157 code = (
155 b'import locale; ' 158 b'import locale; '
156 b'print(ascii("' + undecodable + b'"), ' 159 b'print(ascii("' + undecodable + b'"), '
157 b'locale.getpreferredencoding())') 160 b'locale.getpreferredencoding())')
158 p = subprocess.Popen( 161 p = subprocess.Popen(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 206
204 # test invalid utf-8 207 # test invalid utf-8
205 text = ( 208 text = (
206 b'\xff' # invalid byte 209 b'\xff' # invalid byte
207 b'\xc3\xa9' # valid utf-8 character 210 b'\xc3\xa9' # valid utf-8 character
208 b'\xc3\xff' # invalid byte sequence 211 b'\xc3\xff' # invalid byte sequence
209 b'\xed\xa0\x80' # lone surrogate character (invalid) 212 b'\xed\xa0\x80' # lone surrogate character (invalid)
210 ) 213 )
211 check_output(text) 214 check_output(text)
212 215
216 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess .Popen()")
213 def test_unbuffered_output(self): 217 def test_unbuffered_output(self):
214 # Test expected operation of the '-u' switch 218 # Test expected operation of the '-u' switch
215 for stream in ('stdout', 'stderr'): 219 for stream in ('stdout', 'stderr'):
216 # Binary is unbuffered 220 # Binary is unbuffered
217 code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)" 221 code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)"
218 % stream) 222 % stream)
219 rc, out, err = assert_python_ok('-u', '-c', code) 223 rc, out, err = assert_python_ok('-u', '-c', code)
220 data = err if stream == 'stderr' else out 224 data = err if stream == 'stderr' else out
221 self.assertEqual(data, b'x', "binary %s not unbuffered" % stream) 225 self.assertEqual(data, b'x', "binary %s not unbuffered" % stream)
222 # Text is line-buffered 226 # Text is line-buffered
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 import sys 266 import sys
263 path = ":".join(sys.path) 267 path = ":".join(sys.path)
264 path = path.encode("ascii", "backslashreplace") 268 path = path.encode("ascii", "backslashreplace")
265 sys.stdout.buffer.write(path)""" 269 sys.stdout.buffer.write(path)"""
266 rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="") 270 rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="")
267 rc2, out2, err2 = assert_python_ok('-c', code, __isolated=False) 271 rc2, out2, err2 = assert_python_ok('-c', code, __isolated=False)
268 # regarding to Posix specification, outputs should be equal 272 # regarding to Posix specification, outputs should be equal
269 # for empty and unset PYTHONPATH 273 # for empty and unset PYTHONPATH
270 self.assertEqual(out1, out2) 274 self.assertEqual(out1, out2)
271 275
276 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess .Popen()")
272 def test_displayhook_unencodable(self): 277 def test_displayhook_unencodable(self):
273 for encoding in ('ascii', 'latin-1', 'utf-8'): 278 for encoding in ('ascii', 'latin-1', 'utf-8'):
274 # We are testing a PYTHON environment variable here, so we can't 279 # We are testing a PYTHON environment variable here, so we can't
275 # use -E, -I, or script_helper (which uses them). So instead we do 280 # use -E, -I, or script_helper (which uses them). So instead we do
276 # poor-man's isolation by deleting the PYTHON vars from env. 281 # poor-man's isolation by deleting the PYTHON vars from env.
277 env = {key:value for (key,value) in os.environ.copy().items() 282 env = {key:value for (key,value) in os.environ.copy().items()
278 if not key.startswith('PYTHON')} 283 if not key.startswith('PYTHON')}
279 env['PYTHONIOENCODING'] = encoding 284 env['PYTHONIOENCODING'] = encoding
280 p = subprocess.Popen( 285 p = subprocess.Popen(
281 [sys.executable, '-i'], 286 [sys.executable, '-i'],
282 stdin=subprocess.PIPE, 287 stdin=subprocess.PIPE,
283 stdout=subprocess.PIPE, 288 stdout=subprocess.PIPE,
284 stderr=subprocess.STDOUT, 289 stderr=subprocess.STDOUT,
285 env=env) 290 env=env)
286 # non-ascii, surrogate, non-BMP printable, non-BMP unprintable 291 # non-ascii, surrogate, non-BMP printable, non-BMP unprintable
287 text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF" 292 text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF"
288 p.stdin.write(ascii(text).encode('ascii') + b"\n") 293 p.stdin.write(ascii(text).encode('ascii') + b"\n")
289 p.stdin.write(b'exit()\n') 294 p.stdin.write(b'exit()\n')
290 data = kill_python(p) 295 data = kill_python(p)
291 escaped = repr(text).encode(encoding, 'backslashreplace') 296 escaped = repr(text).encode(encoding, 'backslashreplace')
292 self.assertIn(escaped, data) 297 self.assertIn(escaped, data)
293 298
299 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess .Popen()")
294 def check_input(self, code, expected): 300 def check_input(self, code, expected):
295 with tempfile.NamedTemporaryFile("wb+") as stdin: 301 with tempfile.NamedTemporaryFile("wb+") as stdin:
296 sep = os.linesep.encode('ASCII') 302 sep = os.linesep.encode('ASCII')
297 stdin.write(sep.join((b'abc', b'def'))) 303 stdin.write(sep.join((b'abc', b'def')))
298 stdin.flush() 304 stdin.flush()
299 stdin.seek(0) 305 stdin.seek(0)
300 with subprocess.Popen( 306 with subprocess.Popen(
301 (sys.executable, "-c", code), 307 (sys.executable, "-c", code),
302 stdin=stdin, stdout=subprocess.PIPE) as proc: 308 stdin=stdin, stdout=subprocess.PIPE) as proc:
303 stdout, stderr = proc.communicate() 309 stdout, stderr = proc.communicate()
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 def test_closed_stdout(self): 363 def test_closed_stdout(self):
358 # Issue #13444: if stdout has been explicitly closed, we should 364 # Issue #13444: if stdout has been explicitly closed, we should
359 # not attempt to flush it at shutdown. 365 # not attempt to flush it at shutdown.
360 code = "import sys; sys.stdout.close()" 366 code = "import sys; sys.stdout.close()"
361 rc, out, err = assert_python_ok('-c', code) 367 rc, out, err = assert_python_ok('-c', code)
362 self.assertEqual(b'', err) 368 self.assertEqual(b'', err)
363 369
364 # Issue #7111: Python should work without standard streams 370 # Issue #7111: Python should work without standard streams
365 371
366 @unittest.skipIf(os.name != 'posix', "test needs POSIX semantics") 372 @unittest.skipIf(os.name != 'posix', "test needs POSIX semantics")
373 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess .Popen()")
367 def _test_no_stdio(self, streams): 374 def _test_no_stdio(self, streams):
368 code = """if 1: 375 code = """if 1:
369 import os, sys 376 import os, sys
370 for i, s in enumerate({streams}): 377 for i, s in enumerate({streams}):
371 if getattr(sys, s) is not None: 378 if getattr(sys, s) is not None:
372 os._exit(i + 1) 379 os._exit(i + 1)
373 os._exit(42)""".format(streams=streams) 380 os._exit(42)""".format(streams=streams)
374 def preexec(): 381 def preexec():
375 if 'stdin' in streams: 382 if 'stdin' in streams:
376 os.close(0) 383 os.close(0)
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 out = subprocess.check_output([sys.executable, "-I", main], 491 out = subprocess.check_output([sys.executable, "-I", main],
485 cwd=tmpdir) 492 cwd=tmpdir)
486 self.assertEqual(out.strip(), b"ok") 493 self.assertEqual(out.strip(), b"ok")
487 494
488 def test_main(): 495 def test_main():
489 test.support.run_unittest(CmdLineTest) 496 test.support.run_unittest(CmdLineTest)
490 test.support.reap_children() 497 test.support.reap_children()
491 498
492 if __name__ == "__main__": 499 if __name__ == "__main__":
493 test_main() 500 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_capi.py ('k') | Lib/test/test_doctest.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+