[Jython-checkins] jython: Some update of regrtest, added json, fixed test_atexit.
frank.wierzbicki
jython-checkins at python.org
Wed Mar 14 04:09:28 CET 2012
http://hg.python.org/jython/rev/04ba7316b47f
changeset: 6328:04ba7316b47f
user: Frank Wierzbicki <fwierzbicki at gmail.com>
date: Mon Mar 12 15:55:20 2012 -0700
summary:
Some update of regrtest, added json, fixed test_atexit.
files:
CPythonLib.includes | 1 +
Lib/test/regrtest.py | 9 +-
Lib/test/test_atexit.py | 84 ++++++++++++++++++++++++++++
Lib/test/test_builtin.py | 6 +-
Lib/test/test_support.py | 74 +++++++++++++++---------
5 files changed, 140 insertions(+), 34 deletions(-)
diff --git a/CPythonLib.includes b/CPythonLib.includes
--- a/CPythonLib.includes
+++ b/CPythonLib.includes
@@ -11,6 +11,7 @@
test/**
unittest/**
xml/etree/**
+json/**
# Lib files, in alphabetical order:
__future__.py
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -133,6 +133,7 @@
import time
import traceback
import warnings
+import unittest
# keep a reference to the ascii module to workaround #7140 bug
# (see issue #7027)
import encodings.ascii
@@ -359,7 +360,7 @@
tests = map(removepy, tests)
stdtests = STDTESTS[:]
- nottests = NOTTESTS[:]
+ nottests = NOTTESTS.copy()
if exclude:
for arg in args:
if arg in stdtests:
@@ -508,11 +509,11 @@
'test_py3kwarn',
]
-NOTTESTS = [
+NOTTESTS = {
'test_support',
'test_future1',
'test_future2',
- ]
+}
def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
"""Return a list of all applicable test modules."""
@@ -613,7 +614,7 @@
stdout=stdout.getvalue(),
stderr=stderr.getvalue())
return -2
- except (ImportError, test_support.TestSkipped), msg:
+ except (ImportError, unittest.SkipTest), msg:
if not quiet:
print test, "skipped --", msg
sys.stdout.flush()
diff --git a/Lib/test/test_atexit.py b/Lib/test/test_atexit.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_atexit.py
@@ -0,0 +1,84 @@
+import sys
+import unittest
+import StringIO
+import atexit
+from test import test_support
+if not test_support.is_jython:
+ from imp import reload
+
+class TestCase(unittest.TestCase):
+ def setUp(self):
+ s = StringIO.StringIO()
+ self.save_stdout = sys.stdout
+ self.save_stderr = sys.stderr
+ sys.stdout = sys.stderr = self.subst_io = s
+ self.save_handlers = atexit._exithandlers
+ atexit._exithandlers = []
+
+ def tearDown(self):
+ sys.stdout = self.save_stdout
+ sys.stderr = self.save_stderr
+ atexit._exithandlers = self.save_handlers
+
+ def test_args(self):
+ atexit.register(self.h1)
+ atexit.register(self.h4)
+ atexit.register(self.h4, 4, kw="abc")
+ atexit._run_exitfuncs()
+ self.assertEqual(self.subst_io.getvalue(),
+ "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n")
+
+ def test_badargs(self):
+ atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
+ self.assertRaises(TypeError, atexit._run_exitfuncs)
+
+ def test_order(self):
+ atexit.register(self.h1)
+ atexit.register(self.h2)
+ atexit.register(self.h3)
+ atexit._run_exitfuncs()
+ self.assertEqual(self.subst_io.getvalue(), "h3\nh2\nh1\n")
+
+ if not test_support.is_jython:
+ def test_sys_override(self):
+ # be sure a preset sys.exitfunc is handled properly
+ exfunc = sys.exitfunc
+ sys.exitfunc = self.h1
+ reload(atexit)
+ try:
+ atexit.register(self.h2)
+ atexit._run_exitfuncs()
+ finally:
+ sys.exitfunc = exfunc
+ self.assertEqual(self.subst_io.getvalue(), "h2\nh1\n")
+
+ def test_raise(self):
+ atexit.register(self.raise1)
+ atexit.register(self.raise2)
+ self.assertRaises(TypeError, atexit._run_exitfuncs)
+
+ ### helpers
+ def h1(self):
+ print "h1"
+
+ def h2(self):
+ print "h2"
+
+ def h3(self):
+ print "h3"
+
+ def h4(self, *args, **kwargs):
+ print "h4", args, kwargs
+
+ def raise1(self):
+ raise TypeError
+
+ def raise2(self):
+ raise SystemError
+
+def test_main():
+ test_support.run_unittest(TestCase)
+
+
+if __name__ == "__main__":
+ test_main()
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -4,7 +4,7 @@
import unittest
import warnings
from test.test_support import (fcmp, have_unicode, TESTFN, unlink,
- run_unittest, _check_py3k_warnings, check_warnings,
+ run_unittest, check_py3k_warnings, check_warnings,
is_jython)
from operator import neg
@@ -418,7 +418,7 @@
f.write('z = z+1\n')
f.write('z = z*2\n')
f.close()
- with _check_py3k_warnings(("execfile.. not supported in 3.x",
+ with check_py3k_warnings(("execfile.. not supported in 3.x",
DeprecationWarning)):
execfile(TESTFN)
@@ -1581,7 +1581,7 @@
self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0)
def _run_unittest(*args):
- with _check_py3k_warnings(
+ with check_py3k_warnings(
(".+ not supported in 3.x", DeprecationWarning),
(".+ is renamed to imp.reload", DeprecationWarning),
("classic int division", DeprecationWarning)):
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -15,18 +15,21 @@
import importlib
import re
-__all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_module",
+__all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
"verbose", "use_resources", "max_memuse", "record_original_stdout",
"get_original_stdout", "unload", "unlink", "rmtree", "forget",
"is_resource_enabled", "requires", "find_unused_port", "bind_port",
"fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ",
- "findfile", "verify", "vereq", "sortdict", "check_syntax_error",
+ "SAVEDCWD", "temp_cwd", "findfile", "sortdict", "check_syntax_error",
"open_urlresource", "check_warnings", "check_py3k_warnings",
"CleanImport", "EnvironmentVarGuard", "captured_output",
"captured_stdout", "TransientResource", "transient_internet",
"run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
"BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
- "import_fresh_module", "threading_cleanup", "reap_children"]
+ "threading_cleanup", "reap_children", "cpython_only",
+ "check_impl_detail", "get_attribute", "py3k_bytes",
+ "import_fresh_module", "threading_cleanup", "reap_children",
+ "strip_python_stderr"]
class Error(Exception):
"""Base class for regression test exceptions."""
@@ -34,17 +37,7 @@
class TestFailed(Error):
"""Test failed."""
-class TestSkipped(Error):
- """Test skipped.
-
- This can be raised to indicate that a test was deliberatly
- skipped, but not because a feature wasn't available. For
- example, if some resource can't be used, such as the network
- appears to be unavailable, this should be raised instead of
- TestFailed.
- """
-
-class ResourceDenied(TestSkipped):
+class ResourceDenied(unittest.SkipTest):
"""Test skipped because it requested a disallowed resource.
This is raised when a test calls requires() for a resource that
@@ -68,18 +61,16 @@
def import_module(name, deprecated=False):
- """Import the module to be tested, raising TestSkipped if it is not
- available."""
- with warnings.catch_warnings():
- if deprecated:
- warnings.filterwarnings("ignore", ".+ (module|package)",
- DeprecationWarning)
+ """Import and return the module to be tested, raising SkipTest if
+ it is not available.
+
+ If deprecated is True, any module or package deprecation messages
+ will be suppressed."""
+ with _ignore_deprecated_imports(deprecated):
try:
- module = __import__(name, level=0)
- except ImportError:
- raise TestSkipped("No module named " + name)
- else:
- return module
+ return importlib.import_module(name)
+ except ImportError, msg:
+ raise unittest.SkipTest(str(msg))
def _save_and_remove_module(name, orig_modules):
@@ -95,7 +86,6 @@
orig_modules[modname] = sys.modules[modname]
del sys.modules[modname]
-
def _save_and_block_module(name, orig_modules):
"""Helper function to save and block a module in sys.modules
@@ -147,6 +137,17 @@
return fresh_module
+def get_attribute(obj, name):
+ """Get an attribute, raising SkipTest if AttributeError is raised."""
+ try:
+ attribute = getattr(obj, name)
+ except AttributeError:
+ raise unittest.SkipTest("module %s has no attribute %s" % (
+ obj.__name__, name))
+ else:
+ return attribute
+
+
verbose = 1 # Flag set to 0 by regrtest.py
use_resources = None # Flag set to [] by regrtest.py
junit_xml_dir = None # Option set by regrtest.py
@@ -438,12 +439,14 @@
unlink(TESTFN)
del fp
-def findfile(file, here=__file__):
+def findfile(file, here=__file__, subdir=None):
"""Try to find a file on sys.path and the working directory. If it is not
found the argument passed to the function is returned (this does not
necessarily signal failure; could still be the legitimate path)."""
if os.path.isabs(file):
return file
+ if subdir is not None:
+ file = os.path.join(subdir, file)
path = sys.path
path = [os.path.dirname(here)] + path
for dn in path:
@@ -1066,6 +1069,23 @@
count += 1
time.sleep(0.1)
+def reap_threads(func):
+ """Use this function when threads are being used. This will
+ ensure that the threads are cleaned up even when the test fails.
+ If threading is unavailable this function does nothing.
+ """
+ if not thread:
+ return func
+
+ @functools.wraps(func)
+ def decorator(*args):
+ key = threading_setup()
+ try:
+ return func(*args)
+ finally:
+ threading_cleanup(*key)
+ return decorator
+
def reap_children():
"""Use this function at the end of test_main() whenever sub-processes
are started. This will help ensure that no extra children (zombies)
--
Repository URL: http://hg.python.org/jython
More information about the Jython-checkins
mailing list