Skip to content

Commit da25a66

Browse files
committed
Some update of regrtest, added json, fixed test_atexit.
1 parent d5fa0b9 commit da25a66

5 files changed

Lines changed: 140 additions & 34 deletions

File tree

‎CPythonLib.includes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ logging/*
1111
test/**
1212
unittest/**
1313
xml/etree/**
14+
json/**
1415

1516
# Lib files, in alphabetical order:
1617
__future__.py

‎Lib/test/regrtest.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
import time
134134
import traceback
135135
import warnings
136+
import unittest
136137
# keep a reference to the ascii module to workaround #7140 bug
137138
# (see issue #7027)
138139
import encodings.ascii
@@ -359,7 +360,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
359360
tests = map(removepy, tests)
360361

361362
stdtests = STDTESTS[:]
362-
nottests = NOTTESTS[:]
363+
nottests = NOTTESTS.copy()
363364
if exclude:
364365
for arg in args:
365366
if arg in stdtests:
@@ -508,11 +509,11 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
508509
'test_py3kwarn',
509510
]
510511

511-
NOTTESTS = [
512+
NOTTESTS = {
512513
'test_support',
513514
'test_future1',
514515
'test_future2',
515-
]
516+
}
516517

517518
def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
518519
"""Return a list of all applicable test modules."""
@@ -613,7 +614,7 @@ def runtest_inner(test, verbose, quiet, test_times,
613614
stdout=stdout.getvalue(),
614615
stderr=stderr.getvalue())
615616
return -2
616-
except (ImportError, test_support.TestSkipped), msg:
617+
except (ImportError, unittest.SkipTest), msg:
617618
if not quiet:
618619
print test, "skipped --", msg
619620
sys.stdout.flush()

‎Lib/test/test_atexit.py‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import sys
2+
import unittest
3+
import StringIO
4+
import atexit
5+
from test import test_support
6+
if not test_support.is_jython:
7+
from imp import reload
8+
9+
class TestCase(unittest.TestCase):
10+
def setUp(self):
11+
s = StringIO.StringIO()
12+
self.save_stdout = sys.stdout
13+
self.save_stderr = sys.stderr
14+
sys.stdout = sys.stderr = self.subst_io = s
15+
self.save_handlers = atexit._exithandlers
16+
atexit._exithandlers = []
17+
18+
def tearDown(self):
19+
sys.stdout = self.save_stdout
20+
sys.stderr = self.save_stderr
21+
atexit._exithandlers = self.save_handlers
22+
23+
def test_args(self):
24+
atexit.register(self.h1)
25+
atexit.register(self.h4)
26+
atexit.register(self.h4, 4, kw="abc")
27+
atexit._run_exitfuncs()
28+
self.assertEqual(self.subst_io.getvalue(),
29+
"h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n")
30+
31+
def test_badargs(self):
32+
atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
33+
self.assertRaises(TypeError, atexit._run_exitfuncs)
34+
35+
def test_order(self):
36+
atexit.register(self.h1)
37+
atexit.register(self.h2)
38+
atexit.register(self.h3)
39+
atexit._run_exitfuncs()
40+
self.assertEqual(self.subst_io.getvalue(), "h3\nh2\nh1\n")
41+
42+
if not test_support.is_jython:
43+
def test_sys_override(self):
44+
# be sure a preset sys.exitfunc is handled properly
45+
exfunc = sys.exitfunc
46+
sys.exitfunc = self.h1
47+
reload(atexit)
48+
try:
49+
atexit.register(self.h2)
50+
atexit._run_exitfuncs()
51+
finally:
52+
sys.exitfunc = exfunc
53+
self.assertEqual(self.subst_io.getvalue(), "h2\nh1\n")
54+
55+
def test_raise(self):
56+
atexit.register(self.raise1)
57+
atexit.register(self.raise2)
58+
self.assertRaises(TypeError, atexit._run_exitfuncs)
59+
60+
### helpers
61+
def h1(self):
62+
print "h1"
63+
64+
def h2(self):
65+
print "h2"
66+
67+
def h3(self):
68+
print "h3"
69+
70+
def h4(self, *args, **kwargs):
71+
print "h4", args, kwargs
72+
73+
def raise1(self):
74+
raise TypeError
75+
76+
def raise2(self):
77+
raise SystemError
78+
79+
def test_main():
80+
test_support.run_unittest(TestCase)
81+
82+
83+
if __name__ == "__main__":
84+
test_main()

‎Lib/test/test_builtin.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import unittest
55
import warnings
66
from test.test_support import (fcmp, have_unicode, TESTFN, unlink,
7-
run_unittest, _check_py3k_warnings, check_warnings,
7+
run_unittest, check_py3k_warnings, check_warnings,
88
is_jython)
99
from operator import neg
1010

@@ -418,7 +418,7 @@ def keys(self):
418418
f.write('z = z+1\n')
419419
f.write('z = z*2\n')
420420
f.close()
421-
with _check_py3k_warnings(("execfile.. not supported in 3.x",
421+
with check_py3k_warnings(("execfile.. not supported in 3.x",
422422
DeprecationWarning)):
423423
execfile(TESTFN)
424424

@@ -1581,7 +1581,7 @@ def test_baddecorator(self):
15811581
self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0)
15821582

15831583
def _run_unittest(*args):
1584-
with _check_py3k_warnings(
1584+
with check_py3k_warnings(
15851585
(".+ not supported in 3.x", DeprecationWarning),
15861586
(".+ is renamed to imp.reload", DeprecationWarning),
15871587
("classic int division", DeprecationWarning)):

‎Lib/test/test_support.py‎

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,29 @@
1515
import importlib
1616
import re
1717

18-
__all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_module",
18+
__all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
1919
"verbose", "use_resources", "max_memuse", "record_original_stdout",
2020
"get_original_stdout", "unload", "unlink", "rmtree", "forget",
2121
"is_resource_enabled", "requires", "find_unused_port", "bind_port",
2222
"fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ",
23-
"findfile", "verify", "vereq", "sortdict", "check_syntax_error",
23+
"SAVEDCWD", "temp_cwd", "findfile", "sortdict", "check_syntax_error",
2424
"open_urlresource", "check_warnings", "check_py3k_warnings",
2525
"CleanImport", "EnvironmentVarGuard", "captured_output",
2626
"captured_stdout", "TransientResource", "transient_internet",
2727
"run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
2828
"BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
29-
"import_fresh_module", "threading_cleanup", "reap_children"]
29+
"threading_cleanup", "reap_children", "cpython_only",
30+
"check_impl_detail", "get_attribute", "py3k_bytes",
31+
"import_fresh_module", "threading_cleanup", "reap_children",
32+
"strip_python_stderr"]
3033

3134
class Error(Exception):
3235
"""Base class for regression test exceptions."""
3336

3437
class TestFailed(Error):
3538
"""Test failed."""
3639

37-
class TestSkipped(Error):
38-
"""Test skipped.
39-
40-
This can be raised to indicate that a test was deliberatly
41-
skipped, but not because a feature wasn't available. For
42-
example, if some resource can't be used, such as the network
43-
appears to be unavailable, this should be raised instead of
44-
TestFailed.
45-
"""
46-
47-
class ResourceDenied(TestSkipped):
40+
class ResourceDenied(unittest.SkipTest):
4841
"""Test skipped because it requested a disallowed resource.
4942
5043
This is raised when a test calls requires() for a resource that
@@ -68,18 +61,16 @@ def _ignore_deprecated_imports(ignore=True):
6861

6962

7063
def import_module(name, deprecated=False):
71-
"""Import the module to be tested, raising TestSkipped if it is not
72-
available."""
73-
with warnings.catch_warnings():
74-
if deprecated:
75-
warnings.filterwarnings("ignore", ".+ (module|package)",
76-
DeprecationWarning)
64+
"""Import and return the module to be tested, raising SkipTest if
65+
it is not available.
66+
67+
If deprecated is True, any module or package deprecation messages
68+
will be suppressed."""
69+
with _ignore_deprecated_imports(deprecated):
7770
try:
78-
module = __import__(name, level=0)
79-
except ImportError:
80-
raise TestSkipped("No module named " + name)
81-
else:
82-
return module
71+
return importlib.import_module(name)
72+
except ImportError, msg:
73+
raise unittest.SkipTest(str(msg))
8374

8475

8576
def _save_and_remove_module(name, orig_modules):
@@ -95,7 +86,6 @@ def _save_and_remove_module(name, orig_modules):
9586
orig_modules[modname] = sys.modules[modname]
9687
del sys.modules[modname]
9788

98-
9989
def _save_and_block_module(name, orig_modules):
10090
"""Helper function to save and block a module in sys.modules
10191
@@ -147,6 +137,17 @@ def import_fresh_module(name, fresh=(), blocked=(), deprecated=False):
147137
return fresh_module
148138

149139

140+
def get_attribute(obj, name):
141+
"""Get an attribute, raising SkipTest if AttributeError is raised."""
142+
try:
143+
attribute = getattr(obj, name)
144+
except AttributeError:
145+
raise unittest.SkipTest("module %s has no attribute %s" % (
146+
obj.__name__, name))
147+
else:
148+
return attribute
149+
150+
150151
verbose = 1 # Flag set to 0 by regrtest.py
151152
use_resources = None # Flag set to [] by regrtest.py
152153
junit_xml_dir = None # Option set by regrtest.py
@@ -438,12 +439,14 @@ def make_jar_classloader(jar):
438439
unlink(TESTFN)
439440
del fp
440441

441-
def findfile(file, here=__file__):
442+
def findfile(file, here=__file__, subdir=None):
442443
"""Try to find a file on sys.path and the working directory. If it is not
443444
found the argument passed to the function is returned (this does not
444445
necessarily signal failure; could still be the legitimate path)."""
445446
if os.path.isabs(file):
446447
return file
448+
if subdir is not None:
449+
file = os.path.join(subdir, file)
447450
path = sys.path
448451
path = [os.path.dirname(here)] + path
449452
for dn in path:
@@ -1066,6 +1069,23 @@ def threading_cleanup(num_active, num_limbo):
10661069
count += 1
10671070
time.sleep(0.1)
10681071

1072+
def reap_threads(func):
1073+
"""Use this function when threads are being used. This will
1074+
ensure that the threads are cleaned up even when the test fails.
1075+
If threading is unavailable this function does nothing.
1076+
"""
1077+
if not thread:
1078+
return func
1079+
1080+
@functools.wraps(func)
1081+
def decorator(*args):
1082+
key = threading_setup()
1083+
try:
1084+
return func(*args)
1085+
finally:
1086+
threading_cleanup(*key)
1087+
return decorator
1088+
10691089
def reap_children():
10701090
"""Use this function at the end of test_main() whenever sub-processes
10711091
are started. This will help ensure that no extra children (zombies)

0 commit comments

Comments
 (0)