I'm only backporting the tests (which run fine), as well as
a shortened version of Lib/test/script_helper.py. Merged revisions 86395 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r86395 | antoine.pitrou | 2010-11-10 14:55:25 +0100 (mer., 10 nov. 2010) | 4 lines Issue #10372: Import the warnings module only after the IO library is initialized, so as to avoid bootstrap issues with the '-W' option. ........
This commit is contained in:
parent
0a69b856d5
commit
cb4f929812
|
@ -0,0 +1,123 @@
|
|||
# Common utility functions used by various script execution tests
|
||||
# e.g. test_cmd_line, test_cmd_line_script and test_runpy
|
||||
|
||||
import sys
|
||||
import os
|
||||
import os.path
|
||||
import tempfile
|
||||
import subprocess
|
||||
import py_compile
|
||||
import contextlib
|
||||
import shutil
|
||||
import zipfile
|
||||
|
||||
|
||||
# Executing the interpreter in a subprocess
|
||||
def _assert_python(expected_success, *args, **env_vars):
|
||||
cmd_line = [sys.executable]
|
||||
if not env_vars:
|
||||
cmd_line.append('-E')
|
||||
cmd_line.extend(args)
|
||||
# Need to preserve the original environment, for in-place testing of
|
||||
# shared library builds.
|
||||
env = os.environ.copy()
|
||||
env.update(env_vars)
|
||||
p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
env=env)
|
||||
try:
|
||||
out, err = p.communicate()
|
||||
finally:
|
||||
subprocess._cleanup()
|
||||
p.stdout.close()
|
||||
p.stderr.close()
|
||||
rc = p.returncode
|
||||
if (rc and expected_success) or (not rc and not expected_success):
|
||||
raise AssertionError(
|
||||
"Process return code is %d, "
|
||||
"stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
|
||||
return rc, out, err
|
||||
|
||||
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']
|
||||
cmd_line.extend(args)
|
||||
return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
|
||||
def kill_python(p):
|
||||
p.stdin.close()
|
||||
data = p.stdout.read()
|
||||
p.stdout.close()
|
||||
# try to cleanup the child so we don't appear to leak when running
|
||||
# with regrtest -R.
|
||||
p.wait()
|
||||
subprocess._cleanup()
|
||||
return data
|
||||
|
||||
# Script creation utilities
|
||||
@contextlib.contextmanager
|
||||
def temp_dir():
|
||||
dirname = tempfile.mkdtemp()
|
||||
dirname = os.path.realpath(dirname)
|
||||
try:
|
||||
yield dirname
|
||||
finally:
|
||||
shutil.rmtree(dirname)
|
||||
|
||||
def make_script(script_dir, script_basename, source):
|
||||
script_filename = script_basename+os.extsep+'py'
|
||||
script_name = os.path.join(script_dir, script_filename)
|
||||
# The script should be encoded to UTF-8, the default string encoding
|
||||
script_file = open(script_name, 'w', encoding='utf-8')
|
||||
script_file.write(source)
|
||||
script_file.close()
|
||||
return script_name
|
||||
|
||||
def make_pkg(pkg_dir, init_source=''):
|
||||
os.mkdir(pkg_dir)
|
||||
make_script(pkg_dir, '__init__', init_source)
|
||||
|
||||
def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
|
||||
source, depth=1, compiled=False):
|
||||
unlink = []
|
||||
init_name = make_script(zip_dir, '__init__', '')
|
||||
unlink.append(init_name)
|
||||
init_basename = os.path.basename(init_name)
|
||||
script_name = make_script(zip_dir, script_basename, source)
|
||||
unlink.append(script_name)
|
||||
if compiled:
|
||||
init_name = py_compile(init_name, doraise=True)
|
||||
script_name = py_compile(script_name, doraise=True)
|
||||
unlink.extend((init_name, script_name))
|
||||
pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
|
||||
script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
|
||||
zip_filename = zip_basename+os.extsep+'zip'
|
||||
zip_name = os.path.join(zip_dir, zip_filename)
|
||||
zip_file = zipfile.ZipFile(zip_name, 'w')
|
||||
for name in pkg_names:
|
||||
init_name_in_zip = os.path.join(name, init_basename)
|
||||
zip_file.write(init_name, init_name_in_zip)
|
||||
zip_file.write(script_name, script_name_in_zip)
|
||||
zip_file.close()
|
||||
for name in unlink:
|
||||
os.unlink(name)
|
||||
#if test.support.verbose:
|
||||
# zip_file = zipfile.ZipFile(zip_name, 'r')
|
||||
# print 'Contents of %r:' % zip_name
|
||||
# zip_file.printdir()
|
||||
# zip_file.close()
|
||||
return zip_name, os.path.join(zip_name, script_name_in_zip)
|
|
@ -7,6 +7,7 @@ import unittest
|
|||
import tempfile
|
||||
import subprocess
|
||||
from test import support
|
||||
from test.script_helper import assert_python_ok
|
||||
|
||||
from test import warning_tests
|
||||
|
||||
|
@ -394,6 +395,22 @@ class WCmdLineTests(unittest.TestCase):
|
|||
self.module._setoption('error::Warning::0')
|
||||
self.assertRaises(UserWarning, self.module.warn, 'convert to error')
|
||||
|
||||
def test_improper_option(self):
|
||||
# Same as above, but check that the message is printed out when
|
||||
# the interpreter is executed. This also checks that options are
|
||||
# actually parsed at all.
|
||||
rc, out, err = assert_python_ok("-Wxxx", "-c", "pass")
|
||||
self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err)
|
||||
|
||||
def test_warnings_bootstrap(self):
|
||||
# Check that the warnings module does get loaded when -W<some option>
|
||||
# is used (see issue #10372 for an example of silent bootstrap failure).
|
||||
rc, out, err = assert_python_ok("-Wi", "-c",
|
||||
"import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)")
|
||||
# '-Wi' was observed
|
||||
self.assertFalse(out.strip())
|
||||
self.assertNotIn(b'RuntimeWarning', err)
|
||||
|
||||
class CWCmdLineTests(BaseTest, WCmdLineTests):
|
||||
module = c_warnings
|
||||
|
||||
|
|
|
@ -10,6 +10,9 @@ What's New in Python 3.1.3?
|
|||
Core and Builtins
|
||||
-----------------
|
||||
|
||||
- Issue #10372: Import the warnings module only after the IO library is
|
||||
initialized, so as to avoid bootstrap issues with the '-W' option.
|
||||
|
||||
- Issue #10221: dict.pop(k) now has a key error message that includes the
|
||||
missing key (same message d[k] returns for missing keys).
|
||||
|
||||
|
|
Loading…
Reference in New Issue