diff options
author | Xavier de Gaye <xdegaye@users.sourceforge.net> | 2016-12-12 09:56:55 +0100 |
---|---|---|
committer | Xavier de Gaye <xdegaye@users.sourceforge.net> | 2016-12-12 09:56:55 +0100 |
commit | 8f2e46153a6958be99b898b7d8b2d247de02b02a (patch) | |
tree | e5665abf45613c703d0cbbe4b94df3f8536c6cc1 /Lib/test | |
parent | 7aa9589ed672656185eca3f8406438d6a2b7efb1 (diff) | |
parent | 6ef0f459de84252d23526cad3a4be733943d585e (diff) | |
download | cpython-8f2e46153a6958be99b898b7d8b2d247de02b02a.tar.gz |
Issue #28764: Merge 3.6.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/libregrtest/main.py | 14 | ||||
-rw-r--r-- | Lib/test/libregrtest/runtest_mp.py | 2 | ||||
-rwxr-xr-x | Lib/test/pystone.py | 277 | ||||
-rw-r--r-- | Lib/test/support/__init__.py | 9 | ||||
-rw-r--r-- | Lib/test/test_calendar.py | 4 | ||||
-rw-r--r-- | Lib/test/test_dbm_dumb.py | 14 | ||||
-rw-r--r-- | Lib/test/test_epoll.py | 2 | ||||
-rw-r--r-- | Lib/test/test_exceptions.py | 14 | ||||
-rw-r--r-- | Lib/test/test_faulthandler.py | 2 | ||||
-rw-r--r-- | Lib/test/test_httpservers.py | 10 | ||||
-rw-r--r-- | Lib/test/test_io.py | 16 | ||||
-rw-r--r-- | Lib/test/test_macurl2path.py | 31 | ||||
-rw-r--r-- | Lib/test/test_memoryio.py | 6 | ||||
-rw-r--r-- | Lib/test/test_random.py | 6 | ||||
-rw-r--r-- | Lib/test/test_range.py | 37 | ||||
-rw-r--r-- | Lib/test/test_re.py | 4 | ||||
-rw-r--r-- | Lib/test/test_socket.py | 26 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 13 | ||||
-rw-r--r-- | Lib/test/test_syntax.py | 130 | ||||
-rw-r--r-- | Lib/test/test_sys.py | 7 | ||||
-rw-r--r-- | Lib/test/test_sysconfig.py | 10 | ||||
-rw-r--r-- | Lib/test/test_timeit.py | 114 | ||||
-rw-r--r-- | Lib/test/test_zipfile.py | 58 |
23 files changed, 272 insertions, 534 deletions
diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index f0effc973c..e113ed63dd 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -179,19 +179,17 @@ class Regrtest: self.tests = [] # regex to match 'test_builtin' in line: # '0:00:00 [ 4/400] test_builtin -- test_dict took 1 sec' - regex = (r'^(?:[0-9]+:[0-9]+:[0-9]+ *)?' + regex = (r'(?:[0-9]+:[0-9]+:[0-9]+ *)?' r'(?:\[[0-9/ ]+\] *)?' - r'(test_[a-zA-Z0-9_]+)') + r'(test_[a-zA-Z0-9_]+)\b(?:\.py)?') regex = re.compile(regex) with open(os.path.join(support.SAVEDCWD, self.ns.fromfile)) as fp: for line in fp: + line = line.split('#', 1)[0] line = line.strip() - if line.startswith('#'): - continue - match = regex.match(line) - if match is None: - continue - self.tests.append(match.group(1)) + match = regex.search(line) + if match is not None: + self.tests.append(match.group(1)) removepy(self.tests) diff --git a/Lib/test/libregrtest/runtest_mp.py b/Lib/test/libregrtest/runtest_mp.py index 9604c16600..74ac4fa895 100644 --- a/Lib/test/libregrtest/runtest_mp.py +++ b/Lib/test/libregrtest/runtest_mp.py @@ -41,7 +41,7 @@ def run_test_in_subprocess(testname, ns): slaveargs = json.dumps(slaveargs) cmd = [sys.executable, *support.args_from_interpreter_flags(), - '-X', 'faulthandler', + '-u', # Unbuffered stdout and stderr '-m', 'test.regrtest', '--slaveargs', slaveargs] if ns.pgo: diff --git a/Lib/test/pystone.py b/Lib/test/pystone.py deleted file mode 100755 index cf1692ec2d..0000000000 --- a/Lib/test/pystone.py +++ /dev/null @@ -1,277 +0,0 @@ -#! /usr/bin/env python3 - -""" -"PYSTONE" Benchmark Program - -Version: Python/1.2 (corresponds to C/1.1 plus 3 Pystone fixes) - -Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013. - - Translated from ADA to C by Rick Richardson. - Every method to preserve ADA-likeness has been used, - at the expense of C-ness. - - Translated from C to Python by Guido van Rossum. - -Version History: - - Version 1.1 corrects two bugs in version 1.0: - - First, it leaked memory: in Proc1(), NextRecord ends - up having a pointer to itself. I have corrected this - by zapping NextRecord.PtrComp at the end of Proc1(). - - Second, Proc3() used the operator != to compare a - record to None. This is rather inefficient and not - true to the intention of the original benchmark (where - a pointer comparison to None is intended; the != - operator attempts to find a method __cmp__ to do value - comparison of the record). Version 1.1 runs 5-10 - percent faster than version 1.0, so benchmark figures - of different versions can't be compared directly. - - Version 1.2 changes the division to floor division. - - Under Python 3 version 1.1 would use the normal division - operator, resulting in some of the operations mistakenly - yielding floats. Version 1.2 instead uses floor division - making the benchmark an integer benchmark again. - -""" - -LOOPS = 50000 - -from time import time - -__version__ = "1.2" - -[Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6) - -class Record: - - def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0, - IntComp = 0, StringComp = 0): - self.PtrComp = PtrComp - self.Discr = Discr - self.EnumComp = EnumComp - self.IntComp = IntComp - self.StringComp = StringComp - - def copy(self): - return Record(self.PtrComp, self.Discr, self.EnumComp, - self.IntComp, self.StringComp) - -TRUE = 1 -FALSE = 0 - -def main(loops=LOOPS): - benchtime, stones = pystones(loops) - print("Pystone(%s) time for %d passes = %g" % \ - (__version__, loops, benchtime)) - print("This machine benchmarks at %g pystones/second" % stones) - - -def pystones(loops=LOOPS): - return Proc0(loops) - -IntGlob = 0 -BoolGlob = FALSE -Char1Glob = '\0' -Char2Glob = '\0' -Array1Glob = [0]*51 -Array2Glob = [x[:] for x in [Array1Glob]*51] -PtrGlb = None -PtrGlbNext = None - -def Proc0(loops=LOOPS): - global IntGlob - global BoolGlob - global Char1Glob - global Char2Glob - global Array1Glob - global Array2Glob - global PtrGlb - global PtrGlbNext - - starttime = time() - for i in range(loops): - pass - nulltime = time() - starttime - - PtrGlbNext = Record() - PtrGlb = Record() - PtrGlb.PtrComp = PtrGlbNext - PtrGlb.Discr = Ident1 - PtrGlb.EnumComp = Ident3 - PtrGlb.IntComp = 40 - PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING" - String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING" - Array2Glob[8][7] = 10 - - starttime = time() - - for i in range(loops): - Proc5() - Proc4() - IntLoc1 = 2 - IntLoc2 = 3 - String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING" - EnumLoc = Ident2 - BoolGlob = not Func2(String1Loc, String2Loc) - while IntLoc1 < IntLoc2: - IntLoc3 = 5 * IntLoc1 - IntLoc2 - IntLoc3 = Proc7(IntLoc1, IntLoc2) - IntLoc1 = IntLoc1 + 1 - Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3) - PtrGlb = Proc1(PtrGlb) - CharIndex = 'A' - while CharIndex <= Char2Glob: - if EnumLoc == Func1(CharIndex, 'C'): - EnumLoc = Proc6(Ident1) - CharIndex = chr(ord(CharIndex)+1) - IntLoc3 = IntLoc2 * IntLoc1 - IntLoc2 = IntLoc3 // IntLoc1 - IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1 - IntLoc1 = Proc2(IntLoc1) - - benchtime = time() - starttime - nulltime - if benchtime == 0.0: - loopsPerBenchtime = 0.0 - else: - loopsPerBenchtime = (loops / benchtime) - return benchtime, loopsPerBenchtime - -def Proc1(PtrParIn): - PtrParIn.PtrComp = NextRecord = PtrGlb.copy() - PtrParIn.IntComp = 5 - NextRecord.IntComp = PtrParIn.IntComp - NextRecord.PtrComp = PtrParIn.PtrComp - NextRecord.PtrComp = Proc3(NextRecord.PtrComp) - if NextRecord.Discr == Ident1: - NextRecord.IntComp = 6 - NextRecord.EnumComp = Proc6(PtrParIn.EnumComp) - NextRecord.PtrComp = PtrGlb.PtrComp - NextRecord.IntComp = Proc7(NextRecord.IntComp, 10) - else: - PtrParIn = NextRecord.copy() - NextRecord.PtrComp = None - return PtrParIn - -def Proc2(IntParIO): - IntLoc = IntParIO + 10 - while 1: - if Char1Glob == 'A': - IntLoc = IntLoc - 1 - IntParIO = IntLoc - IntGlob - EnumLoc = Ident1 - if EnumLoc == Ident1: - break - return IntParIO - -def Proc3(PtrParOut): - global IntGlob - - if PtrGlb is not None: - PtrParOut = PtrGlb.PtrComp - else: - IntGlob = 100 - PtrGlb.IntComp = Proc7(10, IntGlob) - return PtrParOut - -def Proc4(): - global Char2Glob - - BoolLoc = Char1Glob == 'A' - BoolLoc = BoolLoc or BoolGlob - Char2Glob = 'B' - -def Proc5(): - global Char1Glob - global BoolGlob - - Char1Glob = 'A' - BoolGlob = FALSE - -def Proc6(EnumParIn): - EnumParOut = EnumParIn - if not Func3(EnumParIn): - EnumParOut = Ident4 - if EnumParIn == Ident1: - EnumParOut = Ident1 - elif EnumParIn == Ident2: - if IntGlob > 100: - EnumParOut = Ident1 - else: - EnumParOut = Ident4 - elif EnumParIn == Ident3: - EnumParOut = Ident2 - elif EnumParIn == Ident4: - pass - elif EnumParIn == Ident5: - EnumParOut = Ident3 - return EnumParOut - -def Proc7(IntParI1, IntParI2): - IntLoc = IntParI1 + 2 - IntParOut = IntParI2 + IntLoc - return IntParOut - -def Proc8(Array1Par, Array2Par, IntParI1, IntParI2): - global IntGlob - - IntLoc = IntParI1 + 5 - Array1Par[IntLoc] = IntParI2 - Array1Par[IntLoc+1] = Array1Par[IntLoc] - Array1Par[IntLoc+30] = IntLoc - for IntIndex in range(IntLoc, IntLoc+2): - Array2Par[IntLoc][IntIndex] = IntLoc - Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1 - Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc] - IntGlob = 5 - -def Func1(CharPar1, CharPar2): - CharLoc1 = CharPar1 - CharLoc2 = CharLoc1 - if CharLoc2 != CharPar2: - return Ident1 - else: - return Ident2 - -def Func2(StrParI1, StrParI2): - IntLoc = 1 - while IntLoc <= 1: - if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1: - CharLoc = 'A' - IntLoc = IntLoc + 1 - if CharLoc >= 'W' and CharLoc <= 'Z': - IntLoc = 7 - if CharLoc == 'X': - return TRUE - else: - if StrParI1 > StrParI2: - IntLoc = IntLoc + 7 - return TRUE - else: - return FALSE - -def Func3(EnumParIn): - EnumLoc = EnumParIn - if EnumLoc == Ident3: return TRUE - return FALSE - -if __name__ == '__main__': - import sys - def error(msg): - print(msg, end=' ', file=sys.stderr) - print("usage: %s [number_of_loops]" % sys.argv[0], file=sys.stderr) - sys.exit(100) - nargs = len(sys.argv) - 1 - if nargs > 1: - error("%d arguments are too many;" % nargs) - elif nargs == 1: - try: loops = int(sys.argv[1]) - except ValueError: - error("Invalid argument %r;" % sys.argv[1]) - else: - loops = LOOPS - main(loops) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 1a38c4ab32..6df48c0fed 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -767,8 +767,13 @@ requires_lzma = unittest.skipUnless(lzma, 'requires lzma') is_jython = sys.platform.startswith('java') -_ANDROID_API_LEVEL = sysconfig.get_config_var('ANDROID_API_LEVEL') -is_android = (_ANDROID_API_LEVEL is not None and _ANDROID_API_LEVEL > 0) +try: + # constant used by requires_android_level() + _ANDROID_API_LEVEL = sys.getandroidapilevel() + is_android = True +except AttributeError: + # sys.getandroidapilevel() is only available on Android + is_android = False if sys.platform != 'win32': unix_shell = '/system/bin/sh' if is_android else '/bin/sh' diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index 2bc4feebbd..bd57653ffa 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -404,7 +404,7 @@ class OutputTestCase(unittest.TestCase): with support.captured_stdout() as out: week = [(1,0), (2,1), (3,2), (4,3), (5,4), (6,5), (7,6)] calendar.TextCalendar().prweek(week, 1) - self.assertEqual(out.getvalue().strip(), "1 2 3 4 5 6 7") + self.assertEqual(out.getvalue(), " 1 2 3 4 5 6 7") def test_prmonth(self): with support.captured_stdout() as out: @@ -414,7 +414,7 @@ class OutputTestCase(unittest.TestCase): def test_pryear(self): with support.captured_stdout() as out: calendar.TextCalendar().pryear(2004) - self.assertEqual(out.getvalue().strip(), result_2004_text.strip()) + self.assertEqual(out.getvalue(), result_2004_text) def test_format(self): with support.captured_stdout() as out: diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py index df531d64e4..c2703d7e0b 100644 --- a/Lib/test/test_dbm_dumb.py +++ b/Lib/test/test_dbm_dumb.py @@ -252,6 +252,20 @@ class DumbDBMTestCase(unittest.TestCase): f = dumbdbm.open(_fname, value) f.close() + def test_missing_index(self): + with dumbdbm.open(_fname, 'n') as f: + pass + os.unlink(_fname + '.dir') + for value in ('r', 'w'): + with self.assertWarnsRegex(DeprecationWarning, + "The index file is missing, the " + "semantics of the 'c' flag will " + "be used."): + f = dumbdbm.open(_fname, value) + f.close() + self.assertEqual(os.path.exists(_fname + '.dir'), value == 'w') + self.assertFalse(os.path.exists(_fname + '.bak')) + def test_invalid_flag(self): for flag in ('x', 'rf', None): with self.assertWarnsRegex(DeprecationWarning, diff --git a/Lib/test/test_epoll.py b/Lib/test/test_epoll.py index a7aff8a59e..549e0f7121 100644 --- a/Lib/test/test_epoll.py +++ b/Lib/test/test_epoll.py @@ -76,6 +76,8 @@ class TestEPoll(unittest.TestCase): self.assertRaises(ValueError, ep.fileno) if hasattr(select, "EPOLL_CLOEXEC"): select.epoll(select.EPOLL_CLOEXEC).close() + select.epoll(flags=select.EPOLL_CLOEXEC).close() + select.epoll(flags=0).close() self.assertRaises(OSError, select.epoll, flags=12356) def test_badcreate(self): diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 48379222c3..34265a517f 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -1112,6 +1112,20 @@ class ImportErrorTests(unittest.TestCase): with self.assertRaisesRegex(TypeError, msg): ImportError('test', invalid='keyword', another=True) + def test_reset_attributes(self): + exc = ImportError('test', name='name', path='path') + self.assertEqual(exc.args, ('test',)) + self.assertEqual(exc.msg, 'test') + self.assertEqual(exc.name, 'name') + self.assertEqual(exc.path, 'path') + + # Reset not specified attributes + exc.__init__() + self.assertEqual(exc.args, ()) + self.assertEqual(exc.msg, None) + self.assertEqual(exc.name, None) + self.assertEqual(exc.path, None) + def test_non_str_argument(self): # Issue #15778 with check_warnings(('', BytesWarning), quiet=True): diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index bdd8d1a2a6..67219c16ea 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -44,7 +44,7 @@ def temporary_filename(): def requires_raise(test): return (test if not is_android else - requires_android_level(24, 'raise() is buggy')(test)) + requires_android_level(24, 'raise() is buggy')(test)) class FaultHandlerTests(unittest.TestCase): def get_output(self, code, filename=None, fd=None): diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index 4e931446b9..5049538e66 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -822,6 +822,16 @@ class BaseHTTPRequestHandlerTestCase(unittest.TestCase): self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n') self.verify_get_called() + def test_extra_space(self): + result = self.send_typical_request( + b'GET /spaced out HTTP/1.1\r\n' + b'Host: dummy\r\n' + b'\r\n' + ) + self.assertTrue(result[0].startswith(b'HTTP/1.1 400 ')) + self.verify_expected_headers(result[1:result.index(b'\r\n')]) + self.assertFalse(self.handler.get_called) + def test_with_continue_1_0(self): result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') self.verify_http_server_response(result[0]) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index aaa64eadff..fc68b09d8f 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1149,6 +1149,7 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests): self.assertEqual(b"a", bufio.read(1)) self.assertEqual(b"b", bufio.read1(1)) self.assertEqual(rawio._reads, 1) + self.assertEqual(b"", bufio.read1(0)) self.assertEqual(b"c", bufio.read1(100)) self.assertEqual(rawio._reads, 1) self.assertEqual(b"d", bufio.read1(100)) @@ -1157,8 +1158,17 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests): self.assertEqual(rawio._reads, 3) self.assertEqual(b"", bufio.read1(100)) self.assertEqual(rawio._reads, 4) - # Invalid args - self.assertRaises(ValueError, bufio.read1, -1) + + def test_read1_arbitrary(self): + rawio = self.MockRawIO((b"abc", b"d", b"efg")) + bufio = self.tp(rawio) + self.assertEqual(b"a", bufio.read(1)) + self.assertEqual(b"bc", bufio.read1()) + self.assertEqual(b"d", bufio.read1()) + self.assertEqual(b"efg", bufio.read1(-1)) + self.assertEqual(rawio._reads, 3) + self.assertEqual(b"", bufio.read1()) + self.assertEqual(rawio._reads, 4) def test_readinto(self): rawio = self.MockRawIO((b"abc", b"d", b"efg")) @@ -1809,6 +1819,7 @@ class BufferedRWPairTest(unittest.TestCase): pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO()) self.assertEqual(pair.read1(3), b"abc") + self.assertEqual(pair.read1(), b"def") def test_readinto(self): for method in ("readinto", "readinto1"): @@ -3470,6 +3481,7 @@ class MiscIOTest(unittest.TestCase): self.assertRaises(ValueError, f.read) if hasattr(f, "read1"): self.assertRaises(ValueError, f.read1, 1024) + self.assertRaises(ValueError, f.read1) if hasattr(f, "readall"): self.assertRaises(ValueError, f.readall) if hasattr(f, "readinto"): diff --git a/Lib/test/test_macurl2path.py b/Lib/test/test_macurl2path.py deleted file mode 100644 index 3490d6daf1..0000000000 --- a/Lib/test/test_macurl2path.py +++ /dev/null @@ -1,31 +0,0 @@ -import macurl2path -import unittest - -class MacUrl2PathTestCase(unittest.TestCase): - def test_url2pathname(self): - self.assertEqual(":index.html", macurl2path.url2pathname("index.html")) - self.assertEqual(":bar:index.html", macurl2path.url2pathname("bar/index.html")) - self.assertEqual("foo:bar:index.html", macurl2path.url2pathname("/foo/bar/index.html")) - self.assertEqual("foo:bar", macurl2path.url2pathname("/foo/bar/")) - self.assertEqual("", macurl2path.url2pathname("/")) - self.assertRaises(RuntimeError, macurl2path.url2pathname, "http://foo.com") - self.assertEqual("index.html", macurl2path.url2pathname("///index.html")) - self.assertRaises(RuntimeError, macurl2path.url2pathname, "//index.html") - self.assertEqual(":index.html", macurl2path.url2pathname("./index.html")) - self.assertEqual(":index.html", macurl2path.url2pathname("foo/../index.html")) - self.assertEqual("::index.html", macurl2path.url2pathname("../index.html")) - - def test_pathname2url(self): - self.assertEqual("drive", macurl2path.pathname2url("drive:")) - self.assertEqual("drive/dir", macurl2path.pathname2url("drive:dir:")) - self.assertEqual("drive/dir/file", macurl2path.pathname2url("drive:dir:file")) - self.assertEqual("drive/file", macurl2path.pathname2url("drive:file")) - self.assertEqual("file", macurl2path.pathname2url("file")) - self.assertEqual("file", macurl2path.pathname2url(":file")) - self.assertEqual("dir", macurl2path.pathname2url(":dir:")) - self.assertEqual("dir/file", macurl2path.pathname2url(":dir:file")) - self.assertRaises(RuntimeError, macurl2path.pathname2url, "/") - self.assertEqual("dir/../file", macurl2path.pathname2url("dir::file")) - -if __name__ == "__main__": - unittest.main() diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py index 55b693e564..80055ce1e7 100644 --- a/Lib/test/test_memoryio.py +++ b/Lib/test/test_memoryio.py @@ -437,10 +437,8 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase): def test_read1(self): buf = self.buftype("1234567890") - memio = self.ioclass(buf) - - self.assertRaises(TypeError, memio.read1) - self.assertEqual(memio.read(), buf) + self.assertEqual(self.ioclass(buf).read1(), buf) + self.assertEqual(self.ioclass(buf).read1(-1), buf) def test_readinto(self): buf = self.buftype("1234567890") diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 5b6a4f06ba..78909dd96e 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -5,7 +5,7 @@ import time import pickle import warnings from functools import partial -from math import log, exp, pi, fsum, sin +from math import log, exp, pi, fsum, sin, factorial from test import support from fractions import Fraction @@ -118,10 +118,6 @@ class TestBasicOps: n = 5 pop = range(n) trials = 10000 # large num prevents false negatives without slowing normal case - def factorial(n): - if n == 0: - return 1 - return n * factorial(n - 1) for k in range(n): expected = factorial(n) // factorial(n-k) perms = {} diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py index 9e11e518f6..3675f250af 100644 --- a/Lib/test/test_range.py +++ b/Lib/test/test_range.py @@ -4,7 +4,6 @@ import unittest import sys import pickle import itertools -import test.support # pure Python implementations (3 args only), for comparison def pyrange(start, stop, step): @@ -494,37 +493,13 @@ class RangeTest(unittest.TestCase): test_id = "reversed(range({}, {}, {}))".format(start, end, step) self.assert_iterators_equal(iter1, iter2, test_id, limit=100) - @test.support.cpython_only - def test_range_iterator_invocation(self): - import _testcapi + def test_range_iterators_invocation(self): + # verify range iterators instances cannot be created by + # calling their type rangeiter_type = type(iter(range(0))) - - self.assertWarns(DeprecationWarning, rangeiter_type, 1, 3, 1) - - with test.support.check_warnings(('', DeprecationWarning)): - # rangeiter_new doesn't take keyword arguments - with self.assertRaises(TypeError): - rangeiter_type(a=1) - - # rangeiter_new takes exactly 3 arguments - self.assertRaises(TypeError, rangeiter_type) - self.assertRaises(TypeError, rangeiter_type, 1) - self.assertRaises(TypeError, rangeiter_type, 1, 1) - self.assertRaises(TypeError, rangeiter_type, 1, 1, 1, 1) - - # start, stop and stop must fit in C long - for good_val in [_testcapi.LONG_MAX, _testcapi.LONG_MIN]: - rangeiter_type(good_val, good_val, good_val) - for bad_val in [_testcapi.LONG_MAX + 1, _testcapi.LONG_MIN - 1]: - self.assertRaises(OverflowError, - rangeiter_type, bad_val, 1, 1) - self.assertRaises(OverflowError, - rangeiter_type, 1, bad_val, 1) - self.assertRaises(OverflowError, - rangeiter_type, 1, 1, bad_val) - - # step mustn't be zero - self.assertRaises(ValueError, rangeiter_type, 1, 1, 0) + self.assertRaises(TypeError, rangeiter_type, 1, 3, 1) + long_rangeiter_type = type(iter(range(1 << 1000))) + self.assertRaises(TypeError, long_rangeiter_type, 1, 3, 1) def test_slice(self): def check(start, stop, step=None): diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 4bdaa4b6c6..6896b4dcbe 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -126,7 +126,7 @@ class ReTests(unittest.TestCase): (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)+chr(8))) for c in 'cdehijklmopqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ': with self.subTest(c): - with self.assertWarns(DeprecationWarning): + with self.assertRaises(re.error): self.assertEqual(re.sub('a', '\\' + c, 'a'), '\\' + c) self.assertEqual(re.sub(r'^\s*', 'X', 'test'), 'Xtest') @@ -1506,7 +1506,7 @@ class ReTests(unittest.TestCase): long_overflow = 2**128 self.assertRaises(TypeError, re.finditer, "a", {}) with self.assertRaises(OverflowError): - _sre.compile("abc", 0, [long_overflow], 0, [], []) + _sre.compile("abc", 0, [long_overflow], 0, {}, ()) with self.assertRaises(TypeError): _sre.compile({}, 0, [], 0, [], []) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 59564c9063..a5657c70c6 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -888,18 +888,28 @@ class GeneralModuleTests(unittest.TestCase): self.assertRaises(OverflowError, func, 1<<34) def testNtoHErrors(self): - good_values = [ 1, 2, 3, 1, 2, 3 ] - bad_values = [ -1, -2, -3, -1, -2, -3 ] - for k in good_values: - socket.ntohl(k) + import _testcapi + s_good_values = [0, 1, 2, 0xffff] + l_good_values = s_good_values + [0xffffffff] + l_bad_values = [-1, -2, 1<<32, 1<<1000] + s_bad_values = l_bad_values + [_testcapi.INT_MIN - 1, + _testcapi.INT_MAX + 1] + s_deprecated_values = [1<<16, _testcapi.INT_MAX] + for k in s_good_values: socket.ntohs(k) - socket.htonl(k) socket.htons(k) - for k in bad_values: - self.assertRaises(OverflowError, socket.ntohl, k) + for k in l_good_values: + socket.ntohl(k) + socket.htonl(k) + for k in s_bad_values: self.assertRaises(OverflowError, socket.ntohs, k) - self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k) + for k in l_bad_values: + self.assertRaises(OverflowError, socket.ntohl, k) + self.assertRaises(OverflowError, socket.htonl, k) + for k in s_deprecated_values: + self.assertWarns(DeprecationWarning, socket.ntohs, k) + self.assertWarns(DeprecationWarning, socket.htons, k) def testGetServBy(self): eq = self.assertEqual diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 89de6d1b1a..4cfb29db73 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1015,19 +1015,6 @@ class ProcessTestCase(BaseTestCase): # time to start. self.assertEqual(p.wait(timeout=3), 0) - def test_wait_endtime(self): - """Confirm that the deprecated endtime parameter warns.""" - p = subprocess.Popen([sys.executable, "-c", "pass"]) - try: - with self.assertWarns(DeprecationWarning) as warn_cm: - p.wait(endtime=time.time()+0.01) - except subprocess.TimeoutExpired: - pass # We're not testing endtime timeout behavior. - finally: - p.kill() - self.assertIn('test_subprocess.py', warn_cm.filename) - self.assertIn('endtime', str(warn_cm.warning)) - def test_invalid_bufsize(self): # an invalid type of the bufsize argument should raise # TypeError. diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 7f7e6dafcf..dd740b4984 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -139,69 +139,73 @@ SyntaxError: Generator expression must be parenthesized if not sole argument >>> f((x for x in L), 1) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ->>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, -... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, -... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, -... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, -... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, -... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, -... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, -... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, -... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, -... i100, i101, i102, i103, i104, i105, i106, i107, i108, -... i109, i110, i111, i112, i113, i114, i115, i116, i117, -... i118, i119, i120, i121, i122, i123, i124, i125, i126, -... i127, i128, i129, i130, i131, i132, i133, i134, i135, -... i136, i137, i138, i139, i140, i141, i142, i143, i144, -... i145, i146, i147, i148, i149, i150, i151, i152, i153, -... i154, i155, i156, i157, i158, i159, i160, i161, i162, -... i163, i164, i165, i166, i167, i168, i169, i170, i171, -... i172, i173, i174, i175, i176, i177, i178, i179, i180, -... i181, i182, i183, i184, i185, i186, i187, i188, i189, -... i190, i191, i192, i193, i194, i195, i196, i197, i198, -... i199, i200, i201, i202, i203, i204, i205, i206, i207, -... i208, i209, i210, i211, i212, i213, i214, i215, i216, -... i217, i218, i219, i220, i221, i222, i223, i224, i225, -... i226, i227, i228, i229, i230, i231, i232, i233, i234, -... i235, i236, i237, i238, i239, i240, i241, i242, i243, -... i244, i245, i246, i247, i248, i249, i250, i251, i252, -... i253, i254, i255) -Traceback (most recent call last): -SyntaxError: more than 255 arguments - -The actual error cases counts positional arguments, keyword arguments, -and generator expression arguments separately. This test combines the -three. - ->>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, -... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, -... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, -... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, -... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, -... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, -... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, -... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, -... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, -... i100, i101, i102, i103, i104, i105, i106, i107, i108, -... i109, i110, i111, i112, i113, i114, i115, i116, i117, -... i118, i119, i120, i121, i122, i123, i124, i125, i126, -... i127, i128, i129, i130, i131, i132, i133, i134, i135, -... i136, i137, i138, i139, i140, i141, i142, i143, i144, -... i145, i146, i147, i148, i149, i150, i151, i152, i153, -... i154, i155, i156, i157, i158, i159, i160, i161, i162, -... i163, i164, i165, i166, i167, i168, i169, i170, i171, -... i172, i173, i174, i175, i176, i177, i178, i179, i180, -... i181, i182, i183, i184, i185, i186, i187, i188, i189, -... i190, i191, i192, i193, i194, i195, i196, i197, i198, -... i199, i200, i201, i202, i203, i204, i205, i206, i207, -... i208, i209, i210, i211, i212, i213, i214, i215, i216, -... i217, i218, i219, i220, i221, i222, i223, i224, i225, -... i226, i227, i228, i229, i230, i231, i232, i233, i234, -... i235, i236, i237, i238, i239, i240, i241, i242, i243, -... (x for x in i244), i245, i246, i247, i248, i249, i250, i251, -... i252=1, i253=1, i254=1, i255=1) -Traceback (most recent call last): -SyntaxError: more than 255 arguments +>>> def g(*args, **kwargs): +... print(args, sorted(kwargs.items())) +>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, +... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, +... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, +... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, +... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, +... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, +... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, +... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, +... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, +... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, +... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, +... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, +... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, +... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, +... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, +... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, +... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, +... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, +... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, +... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS +(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) [] + +>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8, +... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16, +... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24, +... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32, +... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40, +... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48, +... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56, +... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64, +... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72, +... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80, +... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88, +... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96, +... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103, +... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110, +... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117, +... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124, +... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131, +... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138, +... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145, +... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152, +... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159, +... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166, +... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173, +... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180, +... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187, +... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194, +... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201, +... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208, +... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215, +... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222, +... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229, +... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236, +... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243, +... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250, +... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257, +... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264, +... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271, +... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278, +... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285, +... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292, +... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299) +... # doctest: +ELLIPSIS +() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)] >>> f(lambda x: x[0] = 3) Traceback (most recent call last): diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index df9ebd4085..828421c23b 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -826,6 +826,13 @@ class SysModuleTest(unittest.TestCase): rc, stdout, stderr = assert_python_ok('-c', code) self.assertEqual(stdout.rstrip(), b'True') + @unittest.skipUnless(hasattr(sys, 'getandroidapilevel'), + 'need sys.getandroidapilevel()') + def test_getandroidapilevel(self): + level = sys.getandroidapilevel() + self.assertIsInstance(level, int) + self.assertGreater(level, 0) + @test.support.cpython_only class SizeofTest(unittest.TestCase): diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index 747b2e5815..a3baea86fe 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -5,7 +5,7 @@ import subprocess import shutil from copy import copy -from test.support import (run_unittest, TESTFN, unlink, check_warnings, +from test.support import (import_module, TESTFN, unlink, check_warnings, captured_stdout, skip_unless_symlink, change_cwd) import sysconfig @@ -388,7 +388,8 @@ class TestSysConfig(unittest.TestCase): @unittest.skipUnless(hasattr(sys.implementation, '_multiarch'), 'multiarch-specific test') def test_triplet_in_ext_suffix(self): - import ctypes, platform, re + ctypes = import_module('ctypes') + import platform, re machine = platform.machine() suffix = sysconfig.get_config_var('EXT_SUFFIX') if re.match('(aarch64|arm|mips|ppc|powerpc|s390|sparc)', machine): @@ -435,8 +436,5 @@ class MakefileTests(unittest.TestCase): }) -def test_main(): - run_unittest(TestSysConfig, MakefileTests) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index 1a95e2979c..8d4746a36e 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -12,7 +12,7 @@ from test.support import captured_stderr DEFAULT_NUMBER = 1000000 # timeit's default number of repetitions. -DEFAULT_REPEAT = 3 +DEFAULT_REPEAT = 5 # XXX: some tests are commented out that would improve the coverage but take a # long time to run because they test the default number of loops, which is @@ -226,7 +226,7 @@ class TestTimeit(unittest.TestCase): t.print_exc(s) self.assert_exc_string(s.getvalue(), 'ZeroDivisionError') - MAIN_DEFAULT_OUTPUT = "10 loops, best of 3: 1 sec per loop\n" + MAIN_DEFAULT_OUTPUT = "1 loop, best of 5: 1 sec per loop\n" def run_main(self, seconds_per_increment=1.0, switches=None, timer=None): if timer is None: @@ -252,39 +252,39 @@ class TestTimeit(unittest.TestCase): def test_main_seconds(self): s = self.run_main(seconds_per_increment=5.5) - self.assertEqual(s, "10 loops, best of 3: 5.5 sec per loop\n") + self.assertEqual(s, "1 loop, best of 5: 5.5 sec per loop\n") def test_main_milliseconds(self): s = self.run_main(seconds_per_increment=0.0055) - self.assertEqual(s, "100 loops, best of 3: 5.5 msec per loop\n") + self.assertEqual(s, "50 loops, best of 5: 5.5 msec per loop\n") def test_main_microseconds(self): s = self.run_main(seconds_per_increment=0.0000025, switches=['-n100']) - self.assertEqual(s, "100 loops, best of 3: 2.5 usec per loop\n") + self.assertEqual(s, "100 loops, best of 5: 2.5 usec per loop\n") def test_main_fixed_iters(self): s = self.run_main(seconds_per_increment=2.0, switches=['-n35']) - self.assertEqual(s, "35 loops, best of 3: 2 sec per loop\n") + self.assertEqual(s, "35 loops, best of 5: 2 sec per loop\n") def test_main_setup(self): s = self.run_main(seconds_per_increment=2.0, switches=['-n35', '-s', 'print("CustomSetup")']) - self.assertEqual(s, "CustomSetup\n" * 3 + - "35 loops, best of 3: 2 sec per loop\n") + self.assertEqual(s, "CustomSetup\n" * DEFAULT_REPEAT + + "35 loops, best of 5: 2 sec per loop\n") def test_main_multiple_setups(self): s = self.run_main(seconds_per_increment=2.0, switches=['-n35', '-s', 'a = "CustomSetup"', '-s', 'print(a)']) - self.assertEqual(s, "CustomSetup\n" * 3 + - "35 loops, best of 3: 2 sec per loop\n") + self.assertEqual(s, "CustomSetup\n" * DEFAULT_REPEAT + + "35 loops, best of 5: 2 sec per loop\n") def test_main_fixed_reps(self): s = self.run_main(seconds_per_increment=60.0, switches=['-r9']) - self.assertEqual(s, "10 loops, best of 9: 60 sec per loop\n") + self.assertEqual(s, "1 loop, best of 9: 60 sec per loop\n") def test_main_negative_reps(self): s = self.run_main(seconds_per_increment=60.0, switches=['-r-5']) - self.assertEqual(s, "10 loops, best of 1: 60 sec per loop\n") + self.assertEqual(s, "1 loop, best of 1: 60 sec per loop\n") @unittest.skipIf(sys.flags.optimize >= 2, "need __doc__") def test_main_help(self): @@ -293,56 +293,57 @@ class TestTimeit(unittest.TestCase): # the help text, but since it's there, check for it. self.assertEqual(s, timeit.__doc__ + ' ') - def test_main_using_time(self): - fake_timer = FakeTimer() - s = self.run_main(switches=['-t'], timer=fake_timer) - self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT) - self.assertIs(fake_timer.saved_timer, time.time) - - def test_main_using_clock(self): - fake_timer = FakeTimer() - s = self.run_main(switches=['-c'], timer=fake_timer) - self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT) - self.assertIs(fake_timer.saved_timer, time.clock) - def test_main_verbose(self): s = self.run_main(switches=['-v']) self.assertEqual(s, dedent("""\ - 10 loops -> 10 secs - raw times: 10 10 10 - 10 loops, best of 3: 1 sec per loop + 1 loop -> 1 secs + + raw times: 1 sec, 1 sec, 1 sec, 1 sec, 1 sec + + 1 loop, best of 5: 1 sec per loop """)) def test_main_very_verbose(self): - s = self.run_main(seconds_per_increment=0.000050, switches=['-vv']) + s = self.run_main(seconds_per_increment=0.000_030, switches=['-vv']) self.assertEqual(s, dedent("""\ - 10 loops -> 0.0005 secs - 100 loops -> 0.005 secs - 1000 loops -> 0.05 secs - 10000 loops -> 0.5 secs - raw times: 0.5 0.5 0.5 - 10000 loops, best of 3: 50 usec per loop + 1 loop -> 3e-05 secs + 2 loops -> 6e-05 secs + 5 loops -> 0.00015 secs + 10 loops -> 0.0003 secs + 20 loops -> 0.0006 secs + 50 loops -> 0.0015 secs + 100 loops -> 0.003 secs + 200 loops -> 0.006 secs + 500 loops -> 0.015 secs + 1000 loops -> 0.03 secs + 2000 loops -> 0.06 secs + 5000 loops -> 0.15 secs + 10000 loops -> 0.3 secs + + raw times: 300 msec, 300 msec, 300 msec, 300 msec, 300 msec + + 10000 loops, best of 5: 30 usec per loop """)) def test_main_with_time_unit(self): - unit_sec = self.run_main(seconds_per_increment=0.002, + unit_sec = self.run_main(seconds_per_increment=0.003, switches=['-u', 'sec']) self.assertEqual(unit_sec, - "1000 loops, best of 3: 0.002 sec per loop\n") - unit_msec = self.run_main(seconds_per_increment=0.002, + "100 loops, best of 5: 0.003 sec per loop\n") + unit_msec = self.run_main(seconds_per_increment=0.003, switches=['-u', 'msec']) self.assertEqual(unit_msec, - "1000 loops, best of 3: 2 msec per loop\n") - unit_usec = self.run_main(seconds_per_increment=0.002, + "100 loops, best of 5: 3 msec per loop\n") + unit_usec = self.run_main(seconds_per_increment=0.003, switches=['-u', 'usec']) self.assertEqual(unit_usec, - "1000 loops, best of 3: 2e+03 usec per loop\n") + "100 loops, best of 5: 3e+03 usec per loop\n") # Test invalid unit input with captured_stderr() as error_stringio: - invalid = self.run_main(seconds_per_increment=0.002, + invalid = self.run_main(seconds_per_increment=0.003, switches=['-u', 'parsec']) self.assertEqual(error_stringio.getvalue(), - "Unrecognized unit. Please select usec, msec, or sec.\n") + "Unrecognized unit. Please select nsec, usec, msec, or sec.\n") def test_main_exception(self): with captured_stderr() as error_stringio: @@ -354,26 +355,37 @@ class TestTimeit(unittest.TestCase): s = self.run_main(switches=['-n1', '1/0']) self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError') - def autorange(self, callback=None): - timer = FakeTimer(seconds_per_increment=0.001) + def autorange(self, seconds_per_increment=1/1024, callback=None): + timer = FakeTimer(seconds_per_increment=seconds_per_increment) t = timeit.Timer(stmt=self.fake_stmt, setup=self.fake_setup, timer=timer) return t.autorange(callback) def test_autorange(self): num_loops, time_taken = self.autorange() - self.assertEqual(num_loops, 1000) + self.assertEqual(num_loops, 500) + self.assertEqual(time_taken, 500/1024) + + def test_autorange_second(self): + num_loops, time_taken = self.autorange(seconds_per_increment=1.0) + self.assertEqual(num_loops, 1) self.assertEqual(time_taken, 1.0) def test_autorange_with_callback(self): def callback(a, b): print("{} {:.3f}".format(a, b)) with captured_stdout() as s: - num_loops, time_taken = self.autorange(callback) - self.assertEqual(num_loops, 1000) - self.assertEqual(time_taken, 1.0) - expected = ('10 0.010\n' - '100 0.100\n' - '1000 1.000\n') + num_loops, time_taken = self.autorange(callback=callback) + self.assertEqual(num_loops, 500) + self.assertEqual(time_taken, 500/1024) + expected = ('1 0.001\n' + '2 0.002\n' + '5 0.005\n' + '10 0.010\n' + '20 0.020\n' + '50 0.049\n' + '100 0.098\n' + '200 0.195\n' + '500 0.488\n') self.assertEqual(s.getvalue(), expected) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 0a43b20e2b..0a19d76f42 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -2055,8 +2055,9 @@ class CommandLineTest(unittest.TestCase): def test_test_command(self): zip_name = findfile('zipdir.zip') - out = self.zipfilecmd('-t', zip_name) - self.assertEqual(out.rstrip(), b'Done testing') + for opt in '-t', '--test': + out = self.zipfilecmd(opt, zip_name) + self.assertEqual(out.rstrip(), b'Done testing') zip_name = findfile('testtar.tar') rc, out, err = self.zipfilecmd_failure('-t', zip_name) self.assertEqual(out, b'') @@ -2067,9 +2068,10 @@ class CommandLineTest(unittest.TestCase): with zipfile.ZipFile(zip_name, 'r') as tf: tf.printdir(t) expected = t.getvalue().encode('ascii', 'backslashreplace') - out = self.zipfilecmd('-l', zip_name, - PYTHONIOENCODING='ascii:backslashreplace') - self.assertEqual(out, expected) + for opt in '-l', '--list': + out = self.zipfilecmd(opt, zip_name, + PYTHONIOENCODING='ascii:backslashreplace') + self.assertEqual(out, expected) @requires_zlib def test_create_command(self): @@ -2082,31 +2084,33 @@ class CommandLineTest(unittest.TestCase): f.write('test 2') files = [TESTFN, TESTFNDIR] namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt'] - try: - out = self.zipfilecmd('-c', TESTFN2, *files) - self.assertEqual(out, b'') - with zipfile.ZipFile(TESTFN2) as zf: - self.assertEqual(zf.namelist(), namelist) - self.assertEqual(zf.read(namelist[0]), b'test 1') - self.assertEqual(zf.read(namelist[2]), b'test 2') - finally: - unlink(TESTFN2) + for opt in '-c', '--create': + try: + out = self.zipfilecmd(opt, TESTFN2, *files) + self.assertEqual(out, b'') + with zipfile.ZipFile(TESTFN2) as zf: + self.assertEqual(zf.namelist(), namelist) + self.assertEqual(zf.read(namelist[0]), b'test 1') + self.assertEqual(zf.read(namelist[2]), b'test 2') + finally: + unlink(TESTFN2) def test_extract_command(self): zip_name = findfile('zipdir.zip') - with temp_dir() as extdir: - out = self.zipfilecmd('-e', zip_name, extdir) - self.assertEqual(out, b'') - with zipfile.ZipFile(zip_name) as zf: - for zi in zf.infolist(): - path = os.path.join(extdir, - zi.filename.replace('/', os.sep)) - if zi.is_dir(): - self.assertTrue(os.path.isdir(path)) - else: - self.assertTrue(os.path.isfile(path)) - with open(path, 'rb') as f: - self.assertEqual(f.read(), zf.read(zi)) + for opt in '-e', '--extract': + with temp_dir() as extdir: + out = self.zipfilecmd(opt, zip_name, extdir) + self.assertEqual(out, b'') + with zipfile.ZipFile(zip_name) as zf: + for zi in zf.infolist(): + path = os.path.join(extdir, + zi.filename.replace('/', os.sep)) + if zi.is_dir(): + self.assertTrue(os.path.isdir(path)) + else: + self.assertTrue(os.path.isfile(path)) + with open(path, 'rb') as f: + self.assertEqual(f.read(), zf.read(zi)) if __name__ == "__main__": unittest.main() |