diff options
Diffstat (limited to 'Lib/test')
| -rw-r--r-- | Lib/test/data/README | 2 | ||||
| -rw-r--r-- | Lib/test/test_exceptions.py | 27 | ||||
| -rw-r--r-- | Lib/test/test_robotparser.py | 19 | ||||
| -rw-r--r-- | Lib/test/test_runpy.py | 10 | ||||
| -rw-r--r-- | Lib/test/test_signal.py | 105 | ||||
| -rw-r--r-- | Lib/test/test_ssl.py | 2 | ||||
| -rw-r--r-- | Lib/test/test_subprocess.py | 18 | ||||
| -rw-r--r-- | Lib/test/test_tk.py | 12 | ||||
| -rw-r--r-- | Lib/test/test_ttk_guionly.py | 4 | ||||
| -rw-r--r-- | Lib/test/test_unicode.py | 1 |
10 files changed, 151 insertions, 49 deletions
diff --git a/Lib/test/data/README b/Lib/test/data/README index 8bf8c9b..bd05984 100644 --- a/Lib/test/data/README +++ b/Lib/test/data/README @@ -1,2 +1,2 @@ This empty directory serves as destination for temporary files -created by some tests. +created by some tests, in particular, the test_codecmaps_* tests. diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 76f4249..46ddc82 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -566,6 +566,33 @@ class ExceptionTests(unittest.TestCase): del g self.assertEqual(sys.exc_info()[0], TypeError) + def test_generator_leaking2(self): + # See issue 12475. + def g(): + yield + try: + raise RuntimeError + except RuntimeError: + it = g() + next(it) + try: + next(it) + except StopIteration: + pass + self.assertEqual(sys.exc_info(), (None, None, None)) + + def test_generator_doesnt_retain_old_exc(self): + def g(): + self.assertIsInstance(sys.exc_info()[1], RuntimeError) + yield + self.assertEqual(sys.exc_info(), (None, None, None)) + it = g() + try: + raise RuntimeError + except RuntimeError: + next(it) + self.assertRaises(StopIteration, next, it) + def test_generator_finalizing_and_exc_info(self): # See #7173 def simple_gen(): diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py index 2a6d047..178761d 100644 --- a/Lib/test/test_robotparser.py +++ b/Lib/test/test_robotparser.py @@ -1,7 +1,8 @@ import io import unittest import urllib.robotparser -from urllib.error import URLError +from urllib.error import URLError, HTTPError +from urllib.request import urlopen from test import support class RobotTestCase(unittest.TestCase): @@ -237,13 +238,27 @@ class NetworkTestCase(unittest.TestCase): support.requires('network') with support.transient_internet('mueblesmoraleda.com'): url = 'http://mueblesmoraleda.com' + robots_url = url + "/robots.txt" + # First check the URL is usable for our purposes, since the + # test site is a bit flaky. + try: + urlopen(robots_url) + except HTTPError as e: + if e.code not in {401, 403}: + self.skipTest( + "%r should return a 401 or 403 HTTP error, not %r" + % (robots_url, e.code)) + else: + self.skipTest( + "%r should return a 401 or 403 HTTP error, not succeed" + % (robots_url)) parser = urllib.robotparser.RobotFileParser() parser.set_url(url) try: parser.read() except URLError: self.skipTest('%s is unavailable' % url) - self.assertEqual(parser.can_fetch("*", url+"/robots.txt"), False) + self.assertEqual(parser.can_fetch("*", robots_url), False) def testPythonOrg(self): support.requires('network') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index ad3ab39..7ffb6af 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -405,6 +405,16 @@ argv0 = sys.argv[0] msg = "recursion depth exceeded" self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name) + def test_encoding(self): + with temp_dir() as script_dir: + filename = os.path.join(script_dir, 'script.py') + with open(filename, 'w', encoding='latin1') as f: + f.write(""" +#coding:latin1 +"non-ASCII: h\xe9" +""") + result = run_path(filename) + self.assertEqual(result['__doc__'], "non-ASCII: h\xe9") def test_main(): diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index ede6545..8df1bf0 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -11,7 +11,7 @@ import traceback import unittest from test import support from contextlib import closing -from test.script_helper import spawn_python +from test.script_helper import assert_python_ok, spawn_python if sys.platform in ('os2', 'riscos'): raise unittest.SkipTest("Can't test signal on %s" % sys.platform) @@ -233,49 +233,80 @@ class WindowsSignalTests(unittest.TestCase): @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") class WakeupSignalTests(unittest.TestCase): - TIMEOUT_FULL = 10 - TIMEOUT_HALF = 5 + def check_wakeup(self, test_body): + # use a subprocess to have only one thread and to not change signal + # handling of the parent process + code = """if 1: + import fcntl + import os + import signal + + def handler(signum, frame): + pass + + {} + + signal.signal(signal.SIGALRM, handler) + read, write = os.pipe() + flags = fcntl.fcntl(write, fcntl.F_GETFL, 0) + flags = flags | os.O_NONBLOCK + fcntl.fcntl(write, fcntl.F_SETFL, flags) + signal.set_wakeup_fd(write) + + test() + + os.close(read) + os.close(write) + """.format(test_body) + + assert_python_ok('-c', code) 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.assertTrue(mid_time - before_time < self.TIMEOUT_HALF) - select.select([self.read], [], [], self.TIMEOUT_FULL) - after_time = time.time() - self.assertTrue(after_time - mid_time < self.TIMEOUT_HALF) + self.check_wakeup("""def test(): + import select + import time - def test_wakeup_fd_during(self): - import select + TIMEOUT_FULL = 10 + TIMEOUT_HALF = 5 - 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.assertTrue(after_time - before_time < self.TIMEOUT_HALF) + signal.alarm(1) + before_time = time.time() + # We attempt to get a signal during the sleep, + # before select is called + time.sleep(TIMEOUT_FULL) + mid_time = time.time() + dt = mid_time - before_time + if dt >= TIMEOUT_HALF: + raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) + select.select([read], [], [], TIMEOUT_FULL) + after_time = time.time() + dt = after_time - mid_time + if dt >= TIMEOUT_HALF: + raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) + """) - def setUp(self): - import fcntl + def test_wakeup_fd_during(self): + self.check_wakeup("""def test(): + import select + import time - 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) + TIMEOUT_FULL = 10 + TIMEOUT_HALF = 5 - def tearDown(self): - signal.set_wakeup_fd(self.old_wakeup) - os.close(self.read) - os.close(self.write) - signal.signal(signal.SIGALRM, self.alrm) + signal.alarm(1) + before_time = time.time() + # We attempt to get a signal during the select call + try: + select.select([read], [], [], TIMEOUT_FULL) + except select.error: + pass + else: + raise Exception("select.error not raised") + after_time = time.time() + dt = after_time - before_time + if dt >= TIMEOUT_HALF: + raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) + """) @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") class SiginterruptTest(unittest.TestCase): diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 7edf5b2..869381a 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -60,7 +60,7 @@ def handle_error(prefix): def can_clear_options(): # 0.9.8m or higher - return ssl.OPENSSL_VERSION_INFO >= (0, 9, 8, 13, 15) + return ssl._OPENSSL_API_VERSION >= (0, 9, 8, 13, 15) def no_sslv2_implies_sslv3_hello(): # 0.9.7h or higher diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 4a563b9..08f0ecf 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -675,6 +675,24 @@ class ProcessTestCase(BaseTestCase): time.sleep(2) p.communicate(b"x" * 2**20) + @unittest.skipUnless(hasattr(signal, 'SIGALRM'), + "Requires signal.SIGALRM") + def test_communicate_eintr(self): + # Issue #12493: communicate() should handle EINTR + def handler(signum, frame): + pass + old_handler = signal.signal(signal.SIGALRM, handler) + self.addCleanup(signal.signal, signal.SIGALRM, old_handler) + + # the process is running for 2 seconds + args = [sys.executable, "-c", 'import time; time.sleep(2)'] + for stream in ('stdout', 'stderr'): + kw = {stream: subprocess.PIPE} + with subprocess.Popen(args, **kw) as process: + signal.alarm(1) + # communicate() will be interrupted by SIGALRM + process.communicate() + # context manager class _SuppressCoreFiles(object): diff --git a/Lib/test/test_tk.py b/Lib/test/test_tk.py index ab979fa..f993c53 100644 --- a/Lib/test/test_tk.py +++ b/Lib/test/test_tk.py @@ -2,15 +2,11 @@ from test import support # Skip test if _tkinter wasn't built. support.import_module('_tkinter') -import tkinter -from tkinter.test import runtktests -import unittest +# Skip test if tk cannot be initialized. +from tkinter.test.support import check_tk_availability +check_tk_availability() -try: - tkinter.Button() -except tkinter.TclError as msg: - # assuming tk is not available - raise unittest.SkipTest("tk not available: %s" % msg) +from tkinter.test import runtktests def test_main(enable_gui=False): if enable_gui: diff --git a/Lib/test/test_ttk_guionly.py b/Lib/test/test_ttk_guionly.py index bff4fc1..b8c1a4c 100644 --- a/Lib/test/test_ttk_guionly.py +++ b/Lib/test/test_ttk_guionly.py @@ -5,6 +5,10 @@ from test import support # Skip this test if _tkinter wasn't built. support.import_module('_tkinter') +# Skip test if tk cannot be initialized. +from tkinter.test.support import check_tk_availability +check_tk_availability() + from _tkinter import TclError from tkinter import ttk from tkinter.test import runtktests diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 97be587..885e740 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -788,6 +788,7 @@ class UnicodeTest(string_tests.CommonTest, self.assertEqual('%c' % '\U00021483', '\U00021483') self.assertRaises(TypeError, "%c".__mod__, "aa") self.assertRaises(ValueError, "%.1\u1032f".__mod__, (1.0/3)) + self.assertRaises(TypeError, "%i".__mod__, "aa") # formatting jobs delegated from the string implementation: self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
