diff options
author | Christian Heimes <christian@cheimes.de> | 2007-12-24 08:52:31 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2007-12-24 08:52:31 (GMT) |
commit | 5fb7c2ad0b59b670898c8b1622ddc37331b5cb9e (patch) | |
tree | ea76cdd3ad8729a67920f7bdb0feee56a1600007 /Lib/test | |
parent | 81f1130908d0bccdaa701887676688e1ca3bc723 (diff) | |
download | cpython-5fb7c2ad0b59b670898c8b1622ddc37331b5cb9e.zip cpython-5fb7c2ad0b59b670898c8b1622ddc37331b5cb9e.tar.gz cpython-5fb7c2ad0b59b670898c8b1622ddc37331b5cb9e.tar.bz2 |
Merged revisions 59565-59594 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59568 | facundo.batista | 2007-12-19 13:53:01 +0100 (Wed, 19 Dec 2007) | 3 lines
Some minor cleanups. Thanks Mark Dickinson.
........
r59573 | raymond.hettinger | 2007-12-19 19:13:31 +0100 (Wed, 19 Dec 2007) | 1 line
Fix issue 1661: Flags argument silently ignored in re functions with compiled regexes.
........
r59574 | guido.van.rossum | 2007-12-19 20:41:06 +0100 (Wed, 19 Dec 2007) | 7 lines
Patch #1583 by Adam Olsen.
This adds signal.set_wakeup_fd(fd) which sets a file descriptor to
which a zero byte will be written whenever a C exception handler runs.
I added a simple C API as well, PySignal_SetWakeupFd(fd).
........
r59575 | raymond.hettinger | 2007-12-19 23:14:34 +0100 (Wed, 19 Dec 2007) | 1 line
Bigger range for non-extended opargs.
........
r59576 | guido.van.rossum | 2007-12-19 23:51:13 +0100 (Wed, 19 Dec 2007) | 5 lines
Patch #1549 by Thomas Herve.
This changes the rules for when __hash__ is inherited slightly,
by allowing it to be inherited when one or more of __lt__, __le__,
__gt__, __ge__ are overridden, as long as __eq__ and __ne__ aren't.
........
r59577 | raymond.hettinger | 2007-12-20 02:25:05 +0100 (Thu, 20 Dec 2007) | 1 line
Add comments
........
r59578 | brett.cannon | 2007-12-20 11:09:52 +0100 (Thu, 20 Dec 2007) | 3 lines
Add tests for the warnings module; specifically formatwarning and showwarning.
Still need tests for warn_explicit and simplefilter.
........
r59582 | guido.van.rossum | 2007-12-20 18:28:10 +0100 (Thu, 20 Dec 2007) | 2 lines
Patch #1672 by Joseph Armbruster. Use tempdir() to get a temporary directory.
........
r59584 | georg.brandl | 2007-12-20 22:03:02 +0100 (Thu, 20 Dec 2007) | 2 lines
Fix refleak introduced in r59576.
........
r59586 | guido.van.rossum | 2007-12-21 00:48:28 +0100 (Fri, 21 Dec 2007) | 4 lines
Improve performance of built-in any()/all() by avoiding PyIter_Next() --
using a trick found in ifilter().
Feel free to backport to 2.5.
........
r59591 | andrew.kuchling | 2007-12-22 18:27:02 +0100 (Sat, 22 Dec 2007) | 1 line
Add item
........
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_re.py | 8 | ||||
-rw-r--r-- | Lib/test/test_richcmp.py | 62 | ||||
-rw-r--r-- | Lib/test/test_signal.py | 49 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 2 | ||||
-rw-r--r-- | Lib/test/test_warnings.py | 33 |
5 files changed, 150 insertions, 4 deletions
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index f935e1d..88fecbf 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -111,6 +111,14 @@ class ReTests(unittest.TestCase): # self.assertEqual(z, y) # self.assertEqual(type(z), type(y)) + def test_bug_1661(self): + # Verify that flags do not get silently ignored with compiled patterns + pattern = re.compile('.') + self.assertRaises(ValueError, re.match, pattern, 'A', re.I) + self.assertRaises(ValueError, re.search, pattern, 'A', re.I) + self.assertRaises(ValueError, re.findall, pattern, 'A', re.I) + self.assertRaises(ValueError, re.compile, pattern, re.I) + def test_sub_template_numeric_escape(self): # bug 776311 and friends self.assertEqual(re.sub('x', r'\0', 'x'), '\0') diff --git a/Lib/test/test_richcmp.py b/Lib/test/test_richcmp.py index efe6923..3f97ece 100644 --- a/Lib/test/test_richcmp.py +++ b/Lib/test/test_richcmp.py @@ -85,6 +85,35 @@ class Vector: raise ValueError("Cannot compare vectors of different length") return other + +class SimpleOrder(object): + """ + A simple class that defines order but not full comparison. + """ + + def __init__(self, value): + self.value = value + + def __lt__(self, other): + if not isinstance(other, SimpleOrder): + return True + return self.value < other.value + + def __gt__(self, other): + if not isinstance(other, SimpleOrder): + return False + return self.value > other.value + + +class DumbEqualityWithoutHash(object): + """ + A class that define __eq__, but no __hash__: it shouldn't be hashable. + """ + + def __eq__(self, other): + return False + + opmap = { "lt": (lambda a,b: a< b, operator.lt, operator.__lt__), "le": (lambda a,b: a<=b, operator.le, operator.__le__), @@ -330,8 +359,39 @@ class ListTest(unittest.TestCase): for op in opmap["lt"]: self.assertIs(op(x, y), True) + +class HashableTest(unittest.TestCase): + """ + Test hashability of classes with rich operators defined. + """ + + def test_simpleOrderHashable(self): + """ + A class that only defines __gt__ and/or __lt__ should be hashable. + """ + a = SimpleOrder(1) + b = SimpleOrder(2) + self.assert_(a < b) + self.assert_(b > a) + self.assert_(a.__hash__ is not None) + + def test_notHashableException(self): + """ + If a class is not hashable, it should raise a TypeError with an + understandable message. + """ + a = DumbEqualityWithoutHash() + try: + hash(a) + except TypeError as e: + self.assertEquals(str(e), + "unhashable type: 'DumbEqualityWithoutHash'") + else: + raise test_support.TestFailed("Should not be here") + + def test_main(): - test_support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest) + test_support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest, HashableTest) if __name__ == "__main__": test_main() diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index c1df51e..80a0bb2 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -166,12 +166,59 @@ class BasicSignalTests(unittest.TestCase): self.assertRaises(TypeError, signal.signal, signal.SIGUSR1, None) +class WakeupSignalTests(unittest.TestCase): + TIMEOUT_FULL = 10 + TIMEOUT_HALF = 5 + + def test_wakeup_fd_early(self): + import select + + signal.alarm(1) + before_time = time.time() + # We attempt to get a signal during the sleep, + # before select is called + time.sleep(self.TIMEOUT_FULL) + mid_time = time.time() + self.assert_(mid_time - before_time < self.TIMEOUT_HALF) + select.select([self.read], [], [], self.TIMEOUT_FULL) + after_time = time.time() + self.assert_(after_time - mid_time < self.TIMEOUT_HALF) + + def test_wakeup_fd_during(self): + import select + + signal.alarm(1) + before_time = time.time() + # We attempt to get a signal during the select call + self.assertRaises(select.error, select.select, + [self.read], [], [], self.TIMEOUT_FULL) + after_time = time.time() + self.assert_(after_time - before_time < self.TIMEOUT_HALF) + + def setUp(self): + import fcntl + + self.alrm = signal.signal(signal.SIGALRM, lambda x,y:None) + self.read, self.write = os.pipe() + flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0) + flags = flags | os.O_NONBLOCK + fcntl.fcntl(self.write, fcntl.F_SETFL, flags) + self.old_wakeup = signal.set_wakeup_fd(self.write) + + def tearDown(self): + signal.set_wakeup_fd(self.old_wakeup) + os.close(self.read) + os.close(self.write) + signal.signal(signal.SIGALRM, self.alrm) + + def test_main(): if sys.platform[:3] in ('win', 'os2'): raise test_support.TestSkipped("Can't test signal on %s" % \ sys.platform) - test_support.run_unittest(BasicSignalTests, InterProcessSignalTests) + test_support.run_unittest(BasicSignalTests, InterProcessSignalTests, + WakeupSignalTests) if __name__ == "__main__": diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 806791b..a46bc38 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -242,7 +242,7 @@ class ProcessTestCase(unittest.TestCase): self.assertEquals(rc, 2) def test_cwd(self): - tmpdir = os.getenv("TEMP", "/tmp") + tmpdir = tempfile.gettempdir() # We cannot use os.path.realpath to canonicalize the path, # since it doesn't expand Tru64 {memb} strings. See bug 1063571. cwd = os.getcwd() diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index 6889e3f..8206202 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -1,5 +1,7 @@ import warnings +import linecache import os +from io import StringIO import sys import unittest from test import test_support @@ -36,6 +38,8 @@ class TestModule(unittest.TestCase): self.assert_(w.category is category) def test_filtering(self): + # Test filterwarnings(). + # Implicitly also tests resetwarnings(). with test_support.catch_warning() as w: warnings.filterwarnings("error", "", Warning, "", 0) self.assertRaises(UserWarning, warnings.warn, 'convert to error') @@ -97,6 +101,33 @@ class TestModule(unittest.TestCase): self.assertEqual(os.path.basename(w.filename), "sys") +class WarningsDisplayTests(unittest.TestCase): + + def test_formatwarning(self): + message = "msg" + category = Warning + file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' + line_num = 3 + file_line = linecache.getline(file_name, line_num).strip() + expect = "%s:%s: %s: %s\n %s\n" % (file_name, line_num, category.__name__, + message, file_line) + self.failUnlessEqual(warnings.formatwarning(message, category, + file_name, line_num), + expect) + + def test_showwarning(self): + file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' + line_num = 3 + expected_file_line = linecache.getline(file_name, line_num).strip() + message = 'msg' + category = Warning + file_object = StringIO() + expect = warnings.formatwarning(message, category, file_name, line_num) + warnings.showwarning(message, category, file_name, line_num, + file_object) + self.failUnlessEqual(file_object.getvalue(), expect) + + def test_main(verbose=None): # Obscure hack so that this test passes after reloads or repeated calls # to test_main (regrtest -R). @@ -106,7 +137,7 @@ def test_main(verbose=None): del warning_tests.__warningregistry__ if hasattr(sys, '__warningregistry__'): del sys.__warningregistry__ - test_support.run_unittest(TestModule) + test_support.run_unittest(TestModule, WarningsDisplayTests) if __name__ == "__main__": test_main(verbose=True) |