Skip to content

[2.7] bpo-30366: Backport tests for test.support. #1582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 86 additions & 35 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import time
import struct
import sysconfig
import types
try:
import thread
except ImportError:
Expand Down Expand Up @@ -155,8 +156,17 @@ def get_attribute(obj, name):
try:
attribute = getattr(obj, name)
except AttributeError:
raise unittest.SkipTest("module %s has no attribute %s" % (
obj.__name__, name))
if isinstance(obj, types.ModuleType):
msg = "module %r has no attribute %r" % (obj.__name__, name)
elif isinstance(obj, types.ClassType):
msg = "class %s has no attribute %r" % (obj.__name__, name)
elif isinstance(obj, types.InstanceType):
msg = "%s instance has no attribute %r" % (obj.__class__.__name__, name)
elif isinstance(obj, type):
msg = "type object %r has no attribute %r" % (obj.__name__, name)
else:
msg = "%r object has no attribute %r" % (type(obj).__name__, name)
raise unittest.SkipTest(msg)
else:
return attribute

Expand Down Expand Up @@ -705,6 +715,49 @@ def u(s):
# Save the initial cwd
SAVEDCWD = os.getcwd()

@contextlib.contextmanager
def temp_dir(path=None, quiet=False):
"""Return a context manager that creates a temporary directory.

Arguments:

path: the directory to create temporarily. If omitted or None,
defaults to creating a temporary directory using tempfile.mkdtemp.

quiet: if False (the default), the context manager raises an exception
on error. Otherwise, if the path is specified and cannot be
created, only a warning is issued.

"""
dir_created = False
if path is None:
import tempfile
path = tempfile.mkdtemp()
dir_created = True
path = os.path.realpath(path)
else:
if (have_unicode and isinstance(path, unicode) and
not os.path.supports_unicode_filenames):
try:
path = path.encode(sys.getfilesystemencoding() or 'ascii')
except UnicodeEncodeError:
if not quiet:
raise unittest.SkipTest('unable to encode the cwd name with '
'the filesystem encoding.')
try:
os.mkdir(path)
dir_created = True
except OSError:
if not quiet:
raise
warnings.warn('tests may fail, unable to create temp dir: ' + path,
RuntimeWarning, stacklevel=3)
try:
yield path
finally:
if dir_created:
rmtree(path)

@contextlib.contextmanager
def change_cwd(path, quiet=False):
"""Return a context manager that changes the current working directory.
Expand Down Expand Up @@ -735,38 +788,21 @@ def change_cwd(path, quiet=False):
@contextlib.contextmanager
def temp_cwd(name='tempcwd', quiet=False):
"""
Context manager that creates a temporary directory and set it as CWD.
Context manager that temporarily creates and changes the CWD.

The function temporarily changes the current working directory
after creating a temporary directory in the current directory with
name *name*. If *name* is None, the temporary directory is
created using tempfile.mkdtemp.

If *quiet* is False (default) and it is not possible to
create or change the CWD, an error is raised. If *quiet* is True,
only a warning is raised and the original CWD is used.

The new CWD is created in the current directory and it's named *name*.
If *quiet* is False (default) and it's not possible to create or change
the CWD, an error is raised. If it's True, only a warning is raised
and the original CWD is used.
"""
if (have_unicode and isinstance(name, unicode) and
not os.path.supports_unicode_filenames):
try:
name = name.encode(sys.getfilesystemencoding() or 'ascii')
except UnicodeEncodeError:
if not quiet:
raise unittest.SkipTest('unable to encode the cwd name with '
'the filesystem encoding.')
saved_dir = os.getcwd()
is_temporary = False
try:
os.mkdir(name)
os.chdir(name)
is_temporary = True
except OSError:
if not quiet:
raise
warnings.warn('tests may fail, unable to change the CWD to ' + name,
RuntimeWarning, stacklevel=3)
try:
yield os.getcwd()
finally:
os.chdir(saved_dir)
if is_temporary:
rmtree(name)
with temp_dir(path=name, quiet=quiet) as temp_path:
with change_cwd(temp_path, quiet=quiet) as cwd_dir:
yield cwd_dir

# TEST_HOME_DIR refers to the top level directory of the "test" package
# that contains Python's regression test suite
Expand Down Expand Up @@ -810,9 +846,14 @@ def make_bad_fd():
file.close()
unlink(TESTFN)

def check_syntax_error(testcase, statement):
testcase.assertRaises(SyntaxError, compile, statement,
'<test string>', 'exec')
def check_syntax_error(testcase, statement, lineno=None, offset=None):
with testcase.assertRaises(SyntaxError) as cm:
compile(statement, '<test string>', 'exec')
err = cm.exception
if lineno is not None:
testcase.assertEqual(err.lineno, lineno)
if offset is not None:
testcase.assertEqual(err.offset, offset)

def open_urlresource(url, check=None):
import urlparse, urllib2
Expand Down Expand Up @@ -1785,3 +1826,13 @@ def disable_gc():
finally:
if have_gc:
gc.enable()


def python_is_optimized():
"""Find if Python was built with optimizations."""
cflags = sysconfig.get_config_var('PY_CFLAGS') or ''
final_opt = ""
for opt in cflags.split():
if opt.startswith('-O'):
final_opt = opt
return final_opt not in ('', '-O0', '-Og')
Loading