/Lib/test/test_cmd_line.py

http://unladen-swallow.googlecode.com/ · Python · 147 lines · 107 code · 20 blank · 20 comment · 6 complexity · cf0ee90c92784f245156e8fd4175018f MD5 · raw file

  1. # Tests invocation of the interpreter with various command line arguments
  2. # All tests are executed with environment variables ignored
  3. # See test_cmd_line_script.py for testing of script execution
  4. import test.test_support, unittest
  5. import sys
  6. import subprocess
  7. try:
  8. import _llvm
  9. except ImportError:
  10. WITH_LLVM = False
  11. else:
  12. WITH_LLVM = True
  13. del _llvm
  14. def _spawn_python(*args):
  15. cmd_line = [sys.executable, '-E']
  16. cmd_line.extend(args)
  17. return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
  18. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  19. def _exhaust_python(p):
  20. p.stdin.close()
  21. data = p.stdout.read()
  22. p.stdout.close()
  23. p.wait()
  24. # try to cleanup the child so we don't appear to leak when running
  25. # with regrtest -R. This should be a no-op on Windows.
  26. subprocess._cleanup()
  27. return data, p.returncode
  28. class CmdLineTest(unittest.TestCase):
  29. def start_python(self, *args):
  30. p = _spawn_python(*args)
  31. return _exhaust_python(p)
  32. def exit_code(self, *args):
  33. cmd_line = [sys.executable, '-E']
  34. cmd_line.extend(args)
  35. return subprocess.call(cmd_line, stdout=subprocess.PIPE,
  36. stderr=subprocess.PIPE)
  37. def test_directories(self):
  38. self.assertNotEqual(self.exit_code('.'), 0)
  39. self.assertNotEqual(self.exit_code('< .'), 0)
  40. def verify_valid_flag(self, *flags):
  41. cmd_line = flags + ('-c', 'import sys; sys.exit()')
  42. data, returncode = self.start_python(*cmd_line)
  43. self.assertTrue(data == '' or data.endswith('\n'))
  44. self.assertTrue('Traceback' not in data)
  45. self.assertEqual(returncode, 0)
  46. def verify_invalid_flag(self, *flags):
  47. cmd_line = flags + ('-c', 'import sys; sys.exit()')
  48. data, returncode = self.start_python(*cmd_line)
  49. self.assertTrue('usage:' in data)
  50. self.assertEqual(returncode, 2)
  51. def test_optimize(self):
  52. # Ordered by how much optimization results: O0, O, O1, OO, O2.
  53. # -OO is supported for backwards compatibility.
  54. self.verify_valid_flag('-O0') # Oh zero.
  55. self.verify_valid_flag('-O') # Same as -O1
  56. self.verify_valid_flag('-O1')
  57. self.verify_valid_flag('-OO') # Oh oh. Same as -O2
  58. self.verify_valid_flag('-O2')
  59. self.verify_invalid_flag('-O3') # Used to be valid, no more.
  60. self.verify_invalid_flag('-O128')
  61. def test_q(self):
  62. self.verify_valid_flag('-Qold')
  63. self.verify_valid_flag('-Qnew')
  64. self.verify_valid_flag('-Qwarn')
  65. self.verify_valid_flag('-Qwarnall')
  66. if WITH_LLVM:
  67. def test_jit_flag(self):
  68. self.verify_valid_flag('-Xjit=never')
  69. self.verify_valid_flag('-Xjit=whenhot')
  70. self.verify_valid_flag('-Xjit=always')
  71. self.verify_invalid_flag('-Xjit', 'always')
  72. def test_site_flag(self):
  73. self.verify_valid_flag('-S')
  74. def test_usage(self):
  75. data, returncode = self.start_python('-h')
  76. self.assertTrue('usage' in data)
  77. self.assertEqual(returncode, 0)
  78. def test_version(self):
  79. version = 'Python %d.%d' % sys.version_info[:2]
  80. data, returncode = self.start_python('-V')
  81. self.assertTrue(data.startswith(version), data)
  82. self.assertEqual(returncode, 0)
  83. def test_run_module(self):
  84. # Test expected operation of the '-m' switch
  85. # Switch needs an argument
  86. self.assertNotEqual(self.exit_code('-m'), 0)
  87. # Check we get an error for a nonexistent module
  88. self.assertNotEqual(
  89. self.exit_code('-m', 'fnord43520xyz'),
  90. 0)
  91. # Check the runpy module also gives an error for
  92. # a nonexistent module
  93. self.assertNotEqual(
  94. self.exit_code('-m', 'runpy', 'fnord43520xyz'),
  95. 0)
  96. # All good if module is located and run successfully
  97. self.assertEqual(
  98. self.exit_code('-m', 'timeit', '-n', '1'),
  99. 0)
  100. def test_run_module_bug1764407(self):
  101. # -m and -i need to play well together
  102. # Runs the timeit module and checks the __main__
  103. # namespace has been populated appropriately
  104. p = _spawn_python('-i', '-m', 'timeit', '-n', '1')
  105. p.stdin.write('Timer\n')
  106. p.stdin.write('exit()\n')
  107. data, returncode = _exhaust_python(p)
  108. self.assertTrue(data.startswith('1 loop'))
  109. self.assertTrue('__main__.Timer' in data)
  110. self.assertEqual(returncode, 0)
  111. def test_run_code(self):
  112. # Test expected operation of the '-c' switch
  113. # Switch needs an argument
  114. self.assertNotEqual(self.exit_code('-c'), 0)
  115. # Check we get an error for an uncaught exception
  116. self.assertNotEqual(
  117. self.exit_code('-c', 'raise Exception'),
  118. 0)
  119. # All good if execution is successful
  120. self.assertEqual(
  121. self.exit_code('-c', 'pass'),
  122. 0)
  123. def test_main():
  124. test.test_support.run_unittest(CmdLineTest)
  125. test.test_support.reap_children()
  126. if __name__ == "__main__":
  127. test_main()