Back to index

python3.2  3.2.2
Public Member Functions
test.test_cmd_line.CmdLineTest Class Reference

List of all members.

Public Member Functions

def test_directories
def verify_valid_flag
def test_optimize
def test_q
def test_site_flag
def test_usage
def test_version
def test_verbose
def test_xoptions
def test_run_module
def test_run_module_bug1764407
def test_run_code
def test_non_ascii
def test_undecodable_code
def test_osx_utf8
def test_unbuffered_output
def test_unbuffered_input
def test_large_PYTHONPATH
def test_displayhook_unencodable
def check_input
def test_stdin_readline
def test_builtin_input
def test_unmached_quote

Detailed Description

Definition at line 19 of file test_cmd_line.py.


Member Function Documentation

def test.test_cmd_line.CmdLineTest.check_input (   self,
  code,
  expected 
)

Definition at line 243 of file test_cmd_line.py.

00243 
00244     def check_input(self, code, expected):
00245         with tempfile.NamedTemporaryFile("wb+") as stdin:
00246             sep = os.linesep.encode('ASCII')
00247             stdin.write(sep.join((b'abc', b'def')))
00248             stdin.flush()
00249             stdin.seek(0)
00250             with subprocess.Popen(
00251                 (sys.executable, "-c", code),
00252                 stdin=stdin, stdout=subprocess.PIPE) as proc:
00253                 stdout, stderr = proc.communicate()
00254         self.assertEqual(stdout.rstrip(), expected)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 262 of file test_cmd_line.py.

00262 
00263     def test_builtin_input(self):
00264         # Issue #11272: check that input() strips newlines ('\n' or '\r\n')
00265         self.check_input(
00266             "print(repr(input()))",
00267             b"'abc'")

Here is the call graph for this function:

Definition at line 20 of file test_cmd_line.py.

00020 
00021     def test_directories(self):
00022         assert_python_failure('.')
00023         assert_python_failure('< .')

Here is the call graph for this function:

Definition at line 225 of file test_cmd_line.py.

00225 
00226     def test_displayhook_unencodable(self):
00227         for encoding in ('ascii', 'latin1', 'utf8'):
00228             env = os.environ.copy()
00229             env['PYTHONIOENCODING'] = encoding
00230             p = subprocess.Popen(
00231                 [sys.executable, '-i'],
00232                 stdin=subprocess.PIPE,
00233                 stdout=subprocess.PIPE,
00234                 stderr=subprocess.STDOUT,
00235                 env=env)
00236             # non-ascii, surrogate, non-BMP printable, non-BMP unprintable
00237             text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF"
00238             p.stdin.write(ascii(text).encode('ascii') + b"\n")
00239             p.stdin.write(b'exit()\n')
00240             data = kill_python(p)
00241             escaped = repr(text).encode(encoding, 'backslashreplace')
00242             self.assertIn(escaped, data)

Here is the call graph for this function:

Definition at line 210 of file test_cmd_line.py.

00210 
00211     def test_large_PYTHONPATH(self):
00212         path1 = "ABCDE" * 100
00213         path2 = "FGHIJ" * 100
00214         path = path1 + os.pathsep + path2
00215 
00216         code = """if 1:
00217             import sys
00218             path = ":".join(sys.path)
00219             path = path.encode("ascii", "backslashreplace")
00220             sys.stdout.buffer.write(path)"""
00221         rc, out, err = assert_python_ok('-S', '-c', code,
00222                                         PYTHONPATH=path)
00223         self.assertIn(path1.encode('ascii'), out)
00224         self.assertIn(path2.encode('ascii'), out)

Here is the call graph for this function:

Definition at line 104 of file test_cmd_line.py.

00104 
00105     def test_non_ascii(self):
00106         # Test handling of non-ascii data
00107         if test.support.verbose:
00108             import locale
00109             print('locale encoding = %s, filesystem encoding = %s'
00110                   % (locale.getpreferredencoding(), sys.getfilesystemencoding()))
00111         command = "assert(ord('\xe9') == 0xe9)"
00112         assert_python_ok('-c', command)

Here is the call graph for this function:

Definition at line 30 of file test_cmd_line.py.

00030 
00031     def test_optimize(self):
00032         self.verify_valid_flag('-O')
00033         self.verify_valid_flag('-OO')

Here is the call graph for this function:

Definition at line 152 of file test_cmd_line.py.

00152 
00153     def test_osx_utf8(self):
00154         def check_output(text):
00155             decoded = text.decode('utf8', 'surrogateescape')
00156             expected = ascii(decoded).encode('ascii') + b'\n'
00157 
00158             env = os.environ.copy()
00159             # C locale gives ASCII locale encoding, but Python uses UTF-8
00160             # to parse the command line arguments on Mac OS X
00161             env['LC_ALL'] = 'C'
00162 
00163             p = subprocess.Popen(
00164                 (sys.executable, "-c", "import sys; print(ascii(sys.argv[1]))", text),
00165                 stdout=subprocess.PIPE,
00166                 env=env)
00167             stdout, stderr = p.communicate()
00168             self.assertEqual(stdout, expected)
00169             self.assertEqual(p.returncode, 0)
00170 
00171         # test valid utf-8
00172         text = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8')
00173         check_output(text)
00174 
00175         # test invalid utf-8
00176         text = (
00177             b'\xff'         # invalid byte
00178             b'\xc3\xa9'     # valid utf-8 character
00179             b'\xc3\xff'     # invalid byte sequence
00180             b'\xed\xa0\x80' # lone surrogate character (invalid)
00181         )
00182         check_output(text)

Here is the call graph for this function:

Definition at line 34 of file test_cmd_line.py.

00034 
00035     def test_q(self):
00036         self.verify_valid_flag('-Qold')
00037         self.verify_valid_flag('-Qnew')
00038         self.verify_valid_flag('-Qwarn')
00039         self.verify_valid_flag('-Qwarnall')

Here is the call graph for this function:

Definition at line 93 of file test_cmd_line.py.

00093 
00094     def test_run_code(self):
00095         # Test expected operation of the '-c' switch
00096         # Switch needs an argument
00097         assert_python_failure('-c')
00098         # Check we get an error for an uncaught exception
00099         assert_python_failure('-c', 'raise Exception')
00100         # All good if execution is successful
00101         assert_python_ok('-c', 'pass')

Here is the call graph for this function:

Definition at line 70 of file test_cmd_line.py.

00070 
00071     def test_run_module(self):
00072         # Test expected operation of the '-m' switch
00073         # Switch needs an argument
00074         assert_python_failure('-m')
00075         # Check we get an error for a nonexistent module
00076         assert_python_failure('-m', 'fnord43520xyz')
00077         # Check the runpy module also gives an error for
00078         # a nonexistent module
00079         assert_python_failure('-m', 'runpy', 'fnord43520xyz'),
00080         # All good if module is located and run successfully
00081         assert_python_ok('-m', 'timeit', '-n', '1'),

Here is the call graph for this function:

Definition at line 82 of file test_cmd_line.py.

00082 
00083     def test_run_module_bug1764407(self):
00084         # -m and -i need to play well together
00085         # Runs the timeit module and checks the __main__
00086         # namespace has been populated appropriately
00087         p = spawn_python('-i', '-m', 'timeit', '-n', '1')
00088         p.stdin.write(b'Timer\n')
00089         p.stdin.write(b'exit()\n')
00090         data = kill_python(p)
00091         self.assertTrue(data.find(b'1 loop') != -1)
00092         self.assertTrue(data.find(b'__main__.Timer') != -1)

Here is the call graph for this function:

Definition at line 40 of file test_cmd_line.py.

00040 
00041     def test_site_flag(self):
00042         self.verify_valid_flag('-S')

Here is the call graph for this function:

Definition at line 255 of file test_cmd_line.py.

00255 
00256     def test_stdin_readline(self):
00257         # Issue #11272: check that sys.stdin.readline() replaces '\r\n' by '\n'
00258         # on Windows (sys.stdin is opened in binary mode)
00259         self.check_input(
00260             "import sys; print(repr(sys.stdin.readline()))",
00261             b"'abc\\n'")

Here is the call graph for this function:

Definition at line 200 of file test_cmd_line.py.

00200 
00201     def test_unbuffered_input(self):
00202         # sys.stdin still works with '-u'
00203         code = ("import sys; sys.stdout.write(sys.stdin.read(1))")
00204         p = spawn_python('-u', '-c', code)
00205         p.stdin.write(b'x')
00206         p.stdin.flush()
00207         data, rc = _kill_python_and_exit_code(p)
00208         self.assertEqual(rc, 0)
00209         self.assertTrue(data.startswith(b'x'), data)

Here is the call graph for this function:

Definition at line 183 of file test_cmd_line.py.

00183 
00184     def test_unbuffered_output(self):
00185         # Test expected operation of the '-u' switch
00186         for stream in ('stdout', 'stderr'):
00187             # Binary is unbuffered
00188             code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)"
00189                 % stream)
00190             rc, out, err = assert_python_ok('-u', '-c', code)
00191             data = err if stream == 'stderr' else out
00192             self.assertEqual(data, b'x', "binary %s not unbuffered" % stream)
00193             # Text is line-buffered
00194             code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)"
00195                 % stream)
00196             rc, out, err = assert_python_ok('-u', '-c', code)
00197             data = err if stream == 'stderr' else out
00198             self.assertEqual(data.strip(), b'x',
00199                 "text %s not line-buffered" % stream)

Here is the call graph for this function:

Definition at line 119 of file test_cmd_line.py.

00119 
00120     def test_undecodable_code(self):
00121         undecodable = b"\xff"
00122         env = os.environ.copy()
00123         # Use C locale to get ascii for the locale encoding
00124         env['LC_ALL'] = 'C'
00125         code = (
00126             b'import locale; '
00127             b'print(ascii("' + undecodable + b'"), '
00128                 b'locale.getpreferredencoding())')
00129         p = subprocess.Popen(
00130             [sys.executable, "-c", code],
00131             stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
00132             env=env)
00133         stdout, stderr = p.communicate()
00134         if p.returncode == 1:
00135             # _Py_char2wchar() decoded b'\xff' as '\udcff' (b'\xff' is not
00136             # decodable from ASCII) and run_command() failed on
00137             # PyUnicode_AsUTF8String(). This is the expected behaviour on
00138             # Linux.
00139             pattern = b"Unable to decode the command from the command line:"
00140         elif p.returncode == 0:
00141             # _Py_char2wchar() decoded b'\xff' as '\xff' even if the locale is
00142             # C and the locale encoding is ASCII. It occurs on FreeBSD, Solaris
00143             # and Mac OS X.
00144             pattern = b"'\\xff' "
00145             # The output is followed by the encoding name, an alias to ASCII.
00146             # Examples: "US-ASCII" or "646" (ISO 646, on Solaris).
00147         else:
00148             raise AssertionError("Unknown exit code: %s, output=%a" % (p.returncode, stdout))
00149         if not stdout.startswith(pattern):
00150             raise AssertionError("%a doesn't start with %a" % (stdout, pattern))

Definition at line 268 of file test_cmd_line.py.

00268 
00269     def test_unmached_quote(self):
00270         # Issue #10206: python program starting with unmatched quote
00271         # spewed spaces to stdout
00272         rc, out, err = assert_python_failure('-c', "'")
00273         self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError')
00274         self.assertEqual(b'', out)
00275 

Here is the call graph for this function:

Definition at line 43 of file test_cmd_line.py.

00043 
00044     def test_usage(self):
00045         rc, out, err = assert_python_ok('-h')
00046         self.assertIn(b'usage', out)

Here is the call graph for this function:

Definition at line 52 of file test_cmd_line.py.

00052 
00053     def test_verbose(self):
00054         # -v causes imports to write to stderr.  If the write to
00055         # stderr itself causes an import to happen (for the output
00056         # codec), a recursion loop can occur.
00057         rc, out, err = assert_python_ok('-v')
00058         self.assertNotIn(b'stack overflow', err)
00059         rc, out, err = assert_python_ok('-vv')
00060         self.assertNotIn(b'stack overflow', err)

Here is the call graph for this function:

Definition at line 47 of file test_cmd_line.py.

00047 
00048     def test_version(self):
00049         version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
00050         rc, out, err = assert_python_ok('-V')
00051         self.assertTrue(err.startswith(version))

Here is the call graph for this function:

Definition at line 61 of file test_cmd_line.py.

00061 
00062     def test_xoptions(self):
00063         rc, out, err = assert_python_ok('-c', 'import sys; print(sys._xoptions)')
00064         opts = eval(out.splitlines()[0])
00065         self.assertEqual(opts, {})
00066         rc, out, err = assert_python_ok(
00067             '-Xa', '-Xb=c,d=e', '-c', 'import sys; print(sys._xoptions)')
00068         opts = eval(out.splitlines()[0])
00069         self.assertEqual(opts, {'a': True, 'b': 'c,d=e'})

Here is the call graph for this function:

def test.test_cmd_line.CmdLineTest.verify_valid_flag (   self,
  cmd_line 
)

Definition at line 24 of file test_cmd_line.py.

00024 
00025     def verify_valid_flag(self, cmd_line):
00026         rc, out, err = assert_python_ok(*cmd_line)
00027         self.assertTrue(out == b'' or out.endswith(b'\n'))
00028         self.assertNotIn(b'Traceback', out)
00029         self.assertNotIn(b'Traceback', err)

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following file: