[Python-checkins] r86372 - in python/branches/py3k/Lib/test: script_helper.py test_cmd_line.py

antoine.pitrou python-checkins at python.org
Tue Nov 9 22:33:55 CET 2010


Author: antoine.pitrou
Date: Tue Nov  9 22:33:55 2010
New Revision: 86372

Log:
Use script_helper in one more test



Modified:
   python/branches/py3k/Lib/test/script_helper.py
   python/branches/py3k/Lib/test/test_cmd_line.py

Modified: python/branches/py3k/Lib/test/script_helper.py
==============================================================================
--- python/branches/py3k/Lib/test/script_helper.py	(original)
+++ python/branches/py3k/Lib/test/script_helper.py	Tue Nov  9 22:33:55 2010
@@ -15,11 +15,17 @@
 from test.support import make_legacy_pyc
 
 # Executing the interpreter in a subprocess
-def _assert_python(expected_success, *args):
-    cmd_line = [sys.executable, '-E']
+def _assert_python(expected_success, *args, **env_vars):
+    cmd_line = [sys.executable]
+    if env_vars:
+        env = env_vars
+    else:
+        env = os.environ
+        cmd_line.append('-E')
     cmd_line.extend(args)
     p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
-                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+                         env=env)
     try:
         out, err = p.communicate()
     finally:
@@ -33,11 +39,19 @@
             "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
     return rc, out, err
 
-def assert_python_ok(*args):
-    return _assert_python(True, *args)
-
-def assert_python_failure(*args):
-    return _assert_python(False, *args)
+def assert_python_ok(*args, **env_vars):
+    """
+    Assert that running the interpreter with `args` and optional environment
+    variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
+    """
+    return _assert_python(True, *args, **env_vars)
+
+def assert_python_failure(*args, **env_vars):
+    """
+    Assert that running the interpreter with `args` and optional environment
+    variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
+    """
+    return _assert_python(False, *args, **env_vars)
 
 def spawn_python(*args):
     cmd_line = [sys.executable, '-E']

Modified: python/branches/py3k/Lib/test/test_cmd_line.py
==============================================================================
--- python/branches/py3k/Lib/test/test_cmd_line.py	(original)
+++ python/branches/py3k/Lib/test/test_cmd_line.py	Tue Nov  9 22:33:55 2010
@@ -5,18 +5,8 @@
 import test.support, unittest
 import os
 import sys
-from test.script_helper import spawn_python, kill_python, assert_python_ok, assert_python_failure
-
-# spawn_python normally enforces use of -E to avoid environmental effects
-# but one test checks PYTHONPATH behaviour explicitly
-# XXX (ncoghlan): Give script_helper.spawn_python an option to switch
-# off the -E flag that is normally inserted automatically
 import subprocess
-def _spawn_python_with_env(*args):
-    cmd_line = [sys.executable]
-    cmd_line.extend(args)
-    return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
-                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+from test.script_helper import spawn_python, kill_python, assert_python_ok, assert_python_failure
 
 
 # XXX (ncoghlan): Move to script_helper and make consistent with run_python
@@ -217,23 +207,19 @@
         self.assertTrue(data.startswith(b'x'), data)
 
     def test_large_PYTHONPATH(self):
-        with test.support.EnvironmentVarGuard() as env:
-            path1 = "ABCDE" * 100
-            path2 = "FGHIJ" * 100
-            env['PYTHONPATH'] = path1 + os.pathsep + path2
-
-            code = """
-import sys
-path = ":".join(sys.path)
-path = path.encode("ascii", "backslashreplace")
-sys.stdout.buffer.write(path)"""
-            code = code.strip().splitlines()
-            code = '; '.join(code)
-            p = _spawn_python_with_env('-S', '-c', code)
-            stdout, _ = p.communicate()
-            p.stdout.close()
-            self.assertIn(path1.encode('ascii'), stdout)
-            self.assertIn(path2.encode('ascii'), stdout)
+        path1 = "ABCDE" * 100
+        path2 = "FGHIJ" * 100
+        path = path1 + os.pathsep + path2
+
+        code = """if 1:
+            import sys
+            path = ":".join(sys.path)
+            path = path.encode("ascii", "backslashreplace")
+            sys.stdout.buffer.write(path)"""
+        rc, out, err = assert_python_ok('-S', '-c', code,
+                                        PYTHONPATH=path)
+        self.assertIn(path1.encode('ascii'), out)
+        self.assertIn(path2.encode('ascii'), out)
 
 
 def test_main():


More information about the Python-checkins mailing list