1# Python test set -- built-in functions
2
3import ast
4import asyncio
5import builtins
6import collections
7import decimal
8import fractions
9import gc
10import io
11import locale
12import os
13import pickle
14import platform
15import random
16import re
17import sys
18import traceback
19import types
20import unittest
21import warnings
22from contextlib import ExitStack
23from functools import partial
24from inspect import CO_COROUTINE
25from itertools import product
26from textwrap import dedent
27from types import AsyncGeneratorType, FunctionType
28from operator import neg
29from test import support
30from test.support import (swap_attr, maybe_get_event_loop_policy)
31from test.support.os_helper import (EnvironmentVarGuard, TESTFN, unlink)
32from test.support.script_helper import assert_python_ok
33from test.support.warnings_helper import check_warnings
34from unittest.mock import MagicMock, patch
35try:
36    import pty, signal
37except ImportError:
38    pty = signal = None
39
40
41class Squares:
42
43    def __init__(self, max):
44        self.max = max
45        self.sofar = []
46
47    def __len__(self): return len(self.sofar)
48
49    def __getitem__(self, i):
50        if not 0 <= i < self.max: raise IndexError
51        n = len(self.sofar)
52        while n <= i:
53            self.sofar.append(n*n)
54            n += 1
55        return self.sofar[i]
56
57class StrSquares:
58
59    def __init__(self, max):
60        self.max = max
61        self.sofar = []
62
63    def __len__(self):
64        return len(self.sofar)
65
66    def __getitem__(self, i):
67        if not 0 <= i < self.max:
68            raise IndexError
69        n = len(self.sofar)
70        while n <= i:
71            self.sofar.append(str(n*n))
72            n += 1
73        return self.sofar[i]
74
75class BitBucket:
76    def write(self, line):
77        pass
78
79test_conv_no_sign = [
80        ('0', 0),
81        ('1', 1),
82        ('9', 9),
83        ('10', 10),
84        ('99', 99),
85        ('100', 100),
86        ('314', 314),
87        (' 314', 314),
88        ('314 ', 314),
89        ('  \t\t  314  \t\t  ', 314),
90        (repr(sys.maxsize), sys.maxsize),
91        ('  1x', ValueError),
92        ('  1  ', 1),
93        ('  1\02  ', ValueError),
94        ('', ValueError),
95        (' ', ValueError),
96        ('  \t\t  ', ValueError),
97        (str(br'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
98        (chr(0x200), ValueError),
99]
100
101test_conv_sign = [
102        ('0', 0),
103        ('1', 1),
104        ('9', 9),
105        ('10', 10),
106        ('99', 99),
107        ('100', 100),
108        ('314', 314),
109        (' 314', ValueError),
110        ('314 ', 314),
111        ('  \t\t  314  \t\t  ', ValueError),
112        (repr(sys.maxsize), sys.maxsize),
113        ('  1x', ValueError),
114        ('  1  ', ValueError),
115        ('  1\02  ', ValueError),
116        ('', ValueError),
117        (' ', ValueError),
118        ('  \t\t  ', ValueError),
119        (str(br'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
120        (chr(0x200), ValueError),
121]
122
123class TestFailingBool:
124    def __bool__(self):
125        raise RuntimeError
126
127class TestFailingIter:
128    def __iter__(self):
129        raise RuntimeError
130
131def filter_char(arg):
132    return ord(arg) > ord("d")
133
134def map_char(arg):
135    return chr(ord(arg)+1)
136
137class BuiltinTest(unittest.TestCase):
138    # Helper to check picklability
139    def check_iter_pickle(self, it, seq, proto):
140        itorg = it
141        d = pickle.dumps(it, proto)
142        it = pickle.loads(d)
143        self.assertEqual(type(itorg), type(it))
144        self.assertEqual(list(it), seq)
145
146        #test the iterator after dropping one from it
147        it = pickle.loads(d)
148        try:
149            next(it)
150        except StopIteration:
151            return
152        d = pickle.dumps(it, proto)
153        it = pickle.loads(d)
154        self.assertEqual(list(it), seq[1:])
155
156    def test_import(self):
157        __import__('sys')
158        __import__('time')
159        __import__('string')
160        __import__(name='sys')
161        __import__(name='time', level=0)
162        self.assertRaises(ImportError, __import__, 'spamspam')
163        self.assertRaises(TypeError, __import__, 1, 2, 3, 4)
164        self.assertRaises(ValueError, __import__, '')
165        self.assertRaises(TypeError, __import__, 'sys', name='sys')
166        # Relative import outside of a package with no __package__ or __spec__ (bpo-37409).
167        with self.assertWarns(ImportWarning):
168            self.assertRaises(ImportError, __import__, '',
169                              {'__package__': None, '__spec__': None, '__name__': '__main__'},
170                              locals={}, fromlist=('foo',), level=1)
171        # embedded null character
172        self.assertRaises(ModuleNotFoundError, __import__, 'string\x00')
173
174    def test_abs(self):
175        # int
176        self.assertEqual(abs(0), 0)
177        self.assertEqual(abs(1234), 1234)
178        self.assertEqual(abs(-1234), 1234)
179        self.assertTrue(abs(-sys.maxsize-1) > 0)
180        # float
181        self.assertEqual(abs(0.0), 0.0)
182        self.assertEqual(abs(3.14), 3.14)
183        self.assertEqual(abs(-3.14), 3.14)
184        # str
185        self.assertRaises(TypeError, abs, 'a')
186        # bool
187        self.assertEqual(abs(True), 1)
188        self.assertEqual(abs(False), 0)
189        # other
190        self.assertRaises(TypeError, abs)
191        self.assertRaises(TypeError, abs, None)
192        class AbsClass(object):
193            def __abs__(self):
194                return -5
195        self.assertEqual(abs(AbsClass()), -5)
196
197    def test_all(self):
198        self.assertEqual(all([2, 4, 6]), True)
199        self.assertEqual(all([2, None, 6]), False)
200        self.assertRaises(RuntimeError, all, [2, TestFailingBool(), 6])
201        self.assertRaises(RuntimeError, all, TestFailingIter())
202        self.assertRaises(TypeError, all, 10)               # Non-iterable
203        self.assertRaises(TypeError, all)                   # No args
204        self.assertRaises(TypeError, all, [2, 4, 6], [])    # Too many args
205        self.assertEqual(all([]), True)                     # Empty iterator
206        self.assertEqual(all([0, TestFailingBool()]), False)# Short-circuit
207        S = [50, 60]
208        self.assertEqual(all(x > 42 for x in S), True)
209        S = [50, 40, 60]
210        self.assertEqual(all(x > 42 for x in S), False)
211
212    def test_any(self):
213        self.assertEqual(any([None, None, None]), False)
214        self.assertEqual(any([None, 4, None]), True)
215        self.assertRaises(RuntimeError, any, [None, TestFailingBool(), 6])
216        self.assertRaises(RuntimeError, any, TestFailingIter())
217        self.assertRaises(TypeError, any, 10)               # Non-iterable
218        self.assertRaises(TypeError, any)                   # No args
219        self.assertRaises(TypeError, any, [2, 4, 6], [])    # Too many args
220        self.assertEqual(any([]), False)                    # Empty iterator
221        self.assertEqual(any([1, TestFailingBool()]), True) # Short-circuit
222        S = [40, 60, 30]
223        self.assertEqual(any(x > 42 for x in S), True)
224        S = [10, 20, 30]
225        self.assertEqual(any(x > 42 for x in S), False)
226
227    def test_ascii(self):
228        self.assertEqual(ascii(''), '\'\'')
229        self.assertEqual(ascii(0), '0')
230        self.assertEqual(ascii(()), '()')
231        self.assertEqual(ascii([]), '[]')
232        self.assertEqual(ascii({}), '{}')
233        a = []
234        a.append(a)
235        self.assertEqual(ascii(a), '[[...]]')
236        a = {}
237        a[0] = a
238        self.assertEqual(ascii(a), '{0: {...}}')
239        # Advanced checks for unicode strings
240        def _check_uni(s):
241            self.assertEqual(ascii(s), repr(s))
242        _check_uni("'")
243        _check_uni('"')
244        _check_uni('"\'')
245        _check_uni('\0')
246        _check_uni('\r\n\t .')
247        # Unprintable non-ASCII characters
248        _check_uni('\x85')
249        _check_uni('\u1fff')
250        _check_uni('\U00012fff')
251        # Lone surrogates
252        _check_uni('\ud800')
253        _check_uni('\udfff')
254        # Issue #9804: surrogates should be joined even for printable
255        # wide characters (UCS-2 builds).
256        self.assertEqual(ascii('\U0001d121'), "'\\U0001d121'")
257        # All together
258        s = "'\0\"\n\r\t abcd\x85é\U00012fff\uD800\U0001D121xxx."
259        self.assertEqual(ascii(s),
260            r"""'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud800\U0001d121xxx.'""")
261
262    def test_neg(self):
263        x = -sys.maxsize-1
264        self.assertTrue(isinstance(x, int))
265        self.assertEqual(-x, sys.maxsize+1)
266
267    def test_callable(self):
268        self.assertTrue(callable(len))
269        self.assertFalse(callable("a"))
270        self.assertTrue(callable(callable))
271        self.assertTrue(callable(lambda x, y: x + y))
272        self.assertFalse(callable(__builtins__))
273        def f(): pass
274        self.assertTrue(callable(f))
275
276        class C1:
277            def meth(self): pass
278        self.assertTrue(callable(C1))
279        c = C1()
280        self.assertTrue(callable(c.meth))
281        self.assertFalse(callable(c))
282
283        # __call__ is looked up on the class, not the instance
284        c.__call__ = None
285        self.assertFalse(callable(c))
286        c.__call__ = lambda self: 0
287        self.assertFalse(callable(c))
288        del c.__call__
289        self.assertFalse(callable(c))
290
291        class C2(object):
292            def __call__(self): pass
293        c2 = C2()
294        self.assertTrue(callable(c2))
295        c2.__call__ = None
296        self.assertTrue(callable(c2))
297        class C3(C2): pass
298        c3 = C3()
299        self.assertTrue(callable(c3))
300
301    def test_chr(self):
302        self.assertEqual(chr(32), ' ')
303        self.assertEqual(chr(65), 'A')
304        self.assertEqual(chr(97), 'a')
305        self.assertEqual(chr(0xff), '\xff')
306        self.assertRaises(ValueError, chr, 1<<24)
307        self.assertEqual(chr(sys.maxunicode),
308                         str('\\U0010ffff'.encode("ascii"), 'unicode-escape'))
309        self.assertRaises(TypeError, chr)
310        self.assertEqual(chr(0x0000FFFF), "\U0000FFFF")
311        self.assertEqual(chr(0x00010000), "\U00010000")
312        self.assertEqual(chr(0x00010001), "\U00010001")
313        self.assertEqual(chr(0x000FFFFE), "\U000FFFFE")
314        self.assertEqual(chr(0x000FFFFF), "\U000FFFFF")
315        self.assertEqual(chr(0x00100000), "\U00100000")
316        self.assertEqual(chr(0x00100001), "\U00100001")
317        self.assertEqual(chr(0x0010FFFE), "\U0010FFFE")
318        self.assertEqual(chr(0x0010FFFF), "\U0010FFFF")
319        self.assertRaises(ValueError, chr, -1)
320        self.assertRaises(ValueError, chr, 0x00110000)
321        self.assertRaises((OverflowError, ValueError), chr, 2**32)
322
323    def test_cmp(self):
324        self.assertTrue(not hasattr(builtins, "cmp"))
325
326    def test_compile(self):
327        compile('print(1)\n', '', 'exec')
328        bom = b'\xef\xbb\xbf'
329        compile(bom + b'print(1)\n', '', 'exec')
330        compile(source='pass', filename='?', mode='exec')
331        compile(dont_inherit=False, filename='tmp', source='0', mode='eval')
332        compile('pass', '?', dont_inherit=True, mode='exec')
333        compile(memoryview(b"text"), "name", "exec")
334        self.assertRaises(TypeError, compile)
335        self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode')
336        self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'single', 0xff)
337        self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
338        self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
339                          mode='eval', source='0', filename='tmp')
340        compile('print("\xe5")\n', '', 'exec')
341        self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
342        self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad')
343
344        # test the optimize argument
345
346        codestr = '''def f():
347        """doc"""
348        debug_enabled = False
349        if __debug__:
350            debug_enabled = True
351        try:
352            assert False
353        except AssertionError:
354            return (True, f.__doc__, debug_enabled, __debug__)
355        else:
356            return (False, f.__doc__, debug_enabled, __debug__)
357        '''
358        def f(): """doc"""
359        values = [(-1, __debug__, f.__doc__, __debug__, __debug__),
360                  (0, True, 'doc', True, True),
361                  (1, False, 'doc', False, False),
362                  (2, False, None, False, False)]
363        for optval, *expected in values:
364            # test both direct compilation and compilation via AST
365            codeobjs = []
366            codeobjs.append(compile(codestr, "<test>", "exec", optimize=optval))
367            tree = ast.parse(codestr)
368            codeobjs.append(compile(tree, "<test>", "exec", optimize=optval))
369            for code in codeobjs:
370                ns = {}
371                exec(code, ns)
372                rv = ns['f']()
373                self.assertEqual(rv, tuple(expected))
374
375    def test_compile_top_level_await_no_coro(self):
376        """Make sure top level non-await codes get the correct coroutine flags"""
377        modes = ('single', 'exec')
378        code_samples = [
379            '''def f():pass\n''',
380            '''[x for x in l]''',
381            '''{x for x in l}''',
382            '''(x for x in l)''',
383            '''{x:x for x in l}''',
384        ]
385        for mode, code_sample in product(modes, code_samples):
386            source = dedent(code_sample)
387            co = compile(source,
388                            '?',
389                            mode,
390                            flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
391
392            self.assertNotEqual(co.co_flags & CO_COROUTINE, CO_COROUTINE,
393                                msg=f"source={source} mode={mode}")
394
395
396    def test_compile_top_level_await(self):
397        """Test whether code some top level await can be compiled.
398
399        Make sure it compiles only with the PyCF_ALLOW_TOP_LEVEL_AWAIT flag
400        set, and make sure the generated code object has the CO_COROUTINE flag
401        set in order to execute it with  `await eval(.....)` instead of exec,
402        or via a FunctionType.
403        """
404
405        # helper function just to check we can run top=level async-for
406        async def arange(n):
407            for i in range(n):
408                yield i
409
410        modes = ('single', 'exec')
411        code_samples = [
412            '''a = await asyncio.sleep(0, result=1)''',
413            '''async for i in arange(1):
414                   a = 1''',
415            '''async with asyncio.Lock() as l:
416                   a = 1''',
417            '''a = [x async for x in arange(2)][1]''',
418            '''a = 1 in {x async for x in arange(2)}''',
419            '''a = {x:1 async for x in arange(1)}[0]''',
420            '''a = [x async for x in arange(2) async for x in arange(2)][1]''',
421            '''a = [x async for x in (x async for x in arange(5))][1]''',
422            '''a, = [1 for x in {x async for x in arange(1)}]''',
423            '''a = [await asyncio.sleep(0, x) async for x in arange(2)][1]'''
424        ]
425        policy = maybe_get_event_loop_policy()
426        try:
427            for mode, code_sample in product(modes, code_samples):
428                source = dedent(code_sample)
429                with self.assertRaises(
430                        SyntaxError, msg=f"source={source} mode={mode}"):
431                    compile(source, '?', mode)
432
433                co = compile(source,
434                             '?',
435                             mode,
436                             flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
437
438                self.assertEqual(co.co_flags & CO_COROUTINE, CO_COROUTINE,
439                                 msg=f"source={source} mode={mode}")
440
441                # test we can create and  advance a function type
442                globals_ = {'asyncio': asyncio, 'a': 0, 'arange': arange}
443                async_f = FunctionType(co, globals_)
444                asyncio.run(async_f())
445                self.assertEqual(globals_['a'], 1)
446
447                # test we can await-eval,
448                globals_ = {'asyncio': asyncio, 'a': 0, 'arange': arange}
449                asyncio.run(eval(co, globals_))
450                self.assertEqual(globals_['a'], 1)
451        finally:
452            asyncio.set_event_loop_policy(policy)
453
454    def test_compile_top_level_await_invalid_cases(self):
455         # helper function just to check we can run top=level async-for
456        async def arange(n):
457            for i in range(n):
458                yield i
459
460        modes = ('single', 'exec')
461        code_samples = [
462            '''def f():  await arange(10)\n''',
463            '''def f():  [x async for x in arange(10)]\n''',
464            '''def f():  [await x async for x in arange(10)]\n''',
465            '''def f():
466                   async for i in arange(1):
467                       a = 1
468            ''',
469            '''def f():
470                   async with asyncio.Lock() as l:
471                       a = 1
472            '''
473        ]
474        policy = maybe_get_event_loop_policy()
475        try:
476            for mode, code_sample in product(modes, code_samples):
477                source = dedent(code_sample)
478                with self.assertRaises(
479                        SyntaxError, msg=f"source={source} mode={mode}"):
480                    compile(source, '?', mode)
481
482                with self.assertRaises(
483                        SyntaxError, msg=f"source={source} mode={mode}"):
484                    co = compile(source,
485                             '?',
486                             mode,
487                             flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
488        finally:
489            asyncio.set_event_loop_policy(policy)
490
491
492    def test_compile_async_generator(self):
493        """
494        With the PyCF_ALLOW_TOP_LEVEL_AWAIT flag added in 3.8, we want to
495        make sure AsyncGenerators are still properly not marked with the
496        CO_COROUTINE flag.
497        """
498        code = dedent("""async def ticker():
499                for i in range(10):
500                    yield i
501                    await asyncio.sleep(0)""")
502
503        co = compile(code, '?', 'exec', flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
504        glob = {}
505        exec(co, glob)
506        self.assertEqual(type(glob['ticker']()), AsyncGeneratorType)
507
508    def test_delattr(self):
509        sys.spam = 1
510        delattr(sys, 'spam')
511        self.assertRaises(TypeError, delattr)
512
513    def test_dir(self):
514        # dir(wrong number of arguments)
515        self.assertRaises(TypeError, dir, 42, 42)
516
517        # dir() - local scope
518        local_var = 1
519        self.assertIn('local_var', dir())
520
521        # dir(module)
522        self.assertIn('exit', dir(sys))
523
524        # dir(module_with_invalid__dict__)
525        class Foo(types.ModuleType):
526            __dict__ = 8
527        f = Foo("foo")
528        self.assertRaises(TypeError, dir, f)
529
530        # dir(type)
531        self.assertIn("strip", dir(str))
532        self.assertNotIn("__mro__", dir(str))
533
534        # dir(obj)
535        class Foo(object):
536            def __init__(self):
537                self.x = 7
538                self.y = 8
539                self.z = 9
540        f = Foo()
541        self.assertIn("y", dir(f))
542
543        # dir(obj_no__dict__)
544        class Foo(object):
545            __slots__ = []
546        f = Foo()
547        self.assertIn("__repr__", dir(f))
548
549        # dir(obj_no__class__with__dict__)
550        # (an ugly trick to cause getattr(f, "__class__") to fail)
551        class Foo(object):
552            __slots__ = ["__class__", "__dict__"]
553            def __init__(self):
554                self.bar = "wow"
555        f = Foo()
556        self.assertNotIn("__repr__", dir(f))
557        self.assertIn("bar", dir(f))
558
559        # dir(obj_using __dir__)
560        class Foo(object):
561            def __dir__(self):
562                return ["kan", "ga", "roo"]
563        f = Foo()
564        self.assertTrue(dir(f) == ["ga", "kan", "roo"])
565
566        # dir(obj__dir__tuple)
567        class Foo(object):
568            def __dir__(self):
569                return ("b", "c", "a")
570        res = dir(Foo())
571        self.assertIsInstance(res, list)
572        self.assertTrue(res == ["a", "b", "c"])
573
574        # dir(obj__dir__not_sequence)
575        class Foo(object):
576            def __dir__(self):
577                return 7
578        f = Foo()
579        self.assertRaises(TypeError, dir, f)
580
581        # dir(traceback)
582        try:
583            raise IndexError
584        except:
585            self.assertEqual(len(dir(sys.exc_info()[2])), 4)
586
587        # test that object has a __dir__()
588        self.assertEqual(sorted([].__dir__()), dir([]))
589
590    def test_divmod(self):
591        self.assertEqual(divmod(12, 7), (1, 5))
592        self.assertEqual(divmod(-12, 7), (-2, 2))
593        self.assertEqual(divmod(12, -7), (-2, -2))
594        self.assertEqual(divmod(-12, -7), (1, -5))
595
596        self.assertEqual(divmod(-sys.maxsize-1, -1), (sys.maxsize+1, 0))
597
598        for num, denom, exp_result in [ (3.25, 1.0, (3.0, 0.25)),
599                                        (-3.25, 1.0, (-4.0, 0.75)),
600                                        (3.25, -1.0, (-4.0, -0.75)),
601                                        (-3.25, -1.0, (3.0, -0.25))]:
602            result = divmod(num, denom)
603            self.assertAlmostEqual(result[0], exp_result[0])
604            self.assertAlmostEqual(result[1], exp_result[1])
605
606        self.assertRaises(TypeError, divmod)
607
608    def test_eval(self):
609        self.assertEqual(eval('1+1'), 2)
610        self.assertEqual(eval(' 1+1\n'), 2)
611        globals = {'a': 1, 'b': 2}
612        locals = {'b': 200, 'c': 300}
613        self.assertEqual(eval('a', globals) , 1)
614        self.assertEqual(eval('a', globals, locals), 1)
615        self.assertEqual(eval('b', globals, locals), 200)
616        self.assertEqual(eval('c', globals, locals), 300)
617        globals = {'a': 1, 'b': 2}
618        locals = {'b': 200, 'c': 300}
619        bom = b'\xef\xbb\xbf'
620        self.assertEqual(eval(bom + b'a', globals, locals), 1)
621        self.assertEqual(eval('"\xe5"', globals), "\xe5")
622        self.assertRaises(TypeError, eval)
623        self.assertRaises(TypeError, eval, ())
624        self.assertRaises(SyntaxError, eval, bom[:2] + b'a')
625
626        class X:
627            def __getitem__(self, key):
628                raise ValueError
629        self.assertRaises(ValueError, eval, "foo", {}, X())
630
631    def test_general_eval(self):
632        # Tests that general mappings can be used for the locals argument
633
634        class M:
635            "Test mapping interface versus possible calls from eval()."
636            def __getitem__(self, key):
637                if key == 'a':
638                    return 12
639                raise KeyError
640            def keys(self):
641                return list('xyz')
642
643        m = M()
644        g = globals()
645        self.assertEqual(eval('a', g, m), 12)
646        self.assertRaises(NameError, eval, 'b', g, m)
647        self.assertEqual(eval('dir()', g, m), list('xyz'))
648        self.assertEqual(eval('globals()', g, m), g)
649        self.assertEqual(eval('locals()', g, m), m)
650        self.assertRaises(TypeError, eval, 'a', m)
651        class A:
652            "Non-mapping"
653            pass
654        m = A()
655        self.assertRaises(TypeError, eval, 'a', g, m)
656
657        # Verify that dict subclasses work as well
658        class D(dict):
659            def __getitem__(self, key):
660                if key == 'a':
661                    return 12
662                return dict.__getitem__(self, key)
663            def keys(self):
664                return list('xyz')
665
666        d = D()
667        self.assertEqual(eval('a', g, d), 12)
668        self.assertRaises(NameError, eval, 'b', g, d)
669        self.assertEqual(eval('dir()', g, d), list('xyz'))
670        self.assertEqual(eval('globals()', g, d), g)
671        self.assertEqual(eval('locals()', g, d), d)
672
673        # Verify locals stores (used by list comps)
674        eval('[locals() for i in (2,3)]', g, d)
675        eval('[locals() for i in (2,3)]', g, collections.UserDict())
676
677        class SpreadSheet:
678            "Sample application showing nested, calculated lookups."
679            _cells = {}
680            def __setitem__(self, key, formula):
681                self._cells[key] = formula
682            def __getitem__(self, key):
683                return eval(self._cells[key], globals(), self)
684
685        ss = SpreadSheet()
686        ss['a1'] = '5'
687        ss['a2'] = 'a1*6'
688        ss['a3'] = 'a2*7'
689        self.assertEqual(ss['a3'], 210)
690
691        # Verify that dir() catches a non-list returned by eval
692        # SF bug #1004669
693        class C:
694            def __getitem__(self, item):
695                raise KeyError(item)
696            def keys(self):
697                return 1 # used to be 'a' but that's no longer an error
698        self.assertRaises(TypeError, eval, 'dir()', globals(), C())
699
700    def test_exec(self):
701        g = {}
702        exec('z = 1', g)
703        if '__builtins__' in g:
704            del g['__builtins__']
705        self.assertEqual(g, {'z': 1})
706
707        exec('z = 1+1', g)
708        if '__builtins__' in g:
709            del g['__builtins__']
710        self.assertEqual(g, {'z': 2})
711        g = {}
712        l = {}
713
714        with check_warnings():
715            warnings.filterwarnings("ignore", "global statement",
716                    module="<string>")
717            exec('global a; a = 1; b = 2', g, l)
718        if '__builtins__' in g:
719            del g['__builtins__']
720        if '__builtins__' in l:
721            del l['__builtins__']
722        self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
723
724    def test_exec_globals(self):
725        code = compile("print('Hello World!')", "", "exec")
726        # no builtin function
727        self.assertRaisesRegex(NameError, "name 'print' is not defined",
728                               exec, code, {'__builtins__': {}})
729        # __builtins__ must be a mapping type
730        self.assertRaises(TypeError,
731                          exec, code, {'__builtins__': 123})
732
733        # no __build_class__ function
734        code = compile("class A: pass", "", "exec")
735        self.assertRaisesRegex(NameError, "__build_class__ not found",
736                               exec, code, {'__builtins__': {}})
737
738        class frozendict_error(Exception):
739            pass
740
741        class frozendict(dict):
742            def __setitem__(self, key, value):
743                raise frozendict_error("frozendict is readonly")
744
745        # read-only builtins
746        if isinstance(__builtins__, types.ModuleType):
747            frozen_builtins = frozendict(__builtins__.__dict__)
748        else:
749            frozen_builtins = frozendict(__builtins__)
750        code = compile("__builtins__['superglobal']=2; print(superglobal)", "test", "exec")
751        self.assertRaises(frozendict_error,
752                          exec, code, {'__builtins__': frozen_builtins})
753
754        # read-only globals
755        namespace = frozendict({})
756        code = compile("x=1", "test", "exec")
757        self.assertRaises(frozendict_error,
758                          exec, code, namespace)
759
760    def test_exec_redirected(self):
761        savestdout = sys.stdout
762        sys.stdout = None # Whatever that cannot flush()
763        try:
764            # Used to raise SystemError('error return without exception set')
765            exec('a')
766        except NameError:
767            pass
768        finally:
769            sys.stdout = savestdout
770
771    def test_filter(self):
772        self.assertEqual(list(filter(lambda c: 'a' <= c <= 'z', 'Hello World')), list('elloorld'))
773        self.assertEqual(list(filter(None, [1, 'hello', [], [3], '', None, 9, 0])), [1, 'hello', [3], 9])
774        self.assertEqual(list(filter(lambda x: x > 0, [1, -3, 9, 0, 2])), [1, 9, 2])
775        self.assertEqual(list(filter(None, Squares(10))), [1, 4, 9, 16, 25, 36, 49, 64, 81])
776        self.assertEqual(list(filter(lambda x: x%2, Squares(10))), [1, 9, 25, 49, 81])
777        def identity(item):
778            return 1
779        filter(identity, Squares(5))
780        self.assertRaises(TypeError, filter)
781        class BadSeq(object):
782            def __getitem__(self, index):
783                if index<4:
784                    return 42
785                raise ValueError
786        self.assertRaises(ValueError, list, filter(lambda x: x, BadSeq()))
787        def badfunc():
788            pass
789        self.assertRaises(TypeError, list, filter(badfunc, range(5)))
790
791        # test bltinmodule.c::filtertuple()
792        self.assertEqual(list(filter(None, (1, 2))), [1, 2])
793        self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4])
794        self.assertRaises(TypeError, list, filter(42, (1, 2)))
795
796    def test_filter_pickle(self):
797        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
798            f1 = filter(filter_char, "abcdeabcde")
799            f2 = filter(filter_char, "abcdeabcde")
800            self.check_iter_pickle(f1, list(f2), proto)
801
802    def test_getattr(self):
803        self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
804        self.assertRaises(TypeError, getattr, sys, 1)
805        self.assertRaises(TypeError, getattr, sys, 1, "foo")
806        self.assertRaises(TypeError, getattr)
807        self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode))
808        # unicode surrogates are not encodable to the default encoding (utf8)
809        self.assertRaises(AttributeError, getattr, 1, "\uDAD1\uD51E")
810
811    def test_hasattr(self):
812        self.assertTrue(hasattr(sys, 'stdout'))
813        self.assertRaises(TypeError, hasattr, sys, 1)
814        self.assertRaises(TypeError, hasattr)
815        self.assertEqual(False, hasattr(sys, chr(sys.maxunicode)))
816
817        # Check that hasattr propagates all exceptions outside of
818        # AttributeError.
819        class A:
820            def __getattr__(self, what):
821                raise SystemExit
822        self.assertRaises(SystemExit, hasattr, A(), "b")
823        class B:
824            def __getattr__(self, what):
825                raise ValueError
826        self.assertRaises(ValueError, hasattr, B(), "b")
827
828    def test_hash(self):
829        hash(None)
830        self.assertEqual(hash(1), hash(1))
831        self.assertEqual(hash(1), hash(1.0))
832        hash('spam')
833        self.assertEqual(hash('spam'), hash(b'spam'))
834        hash((0,1,2,3))
835        def f(): pass
836        hash(f)
837        self.assertRaises(TypeError, hash, [])
838        self.assertRaises(TypeError, hash, {})
839        # Bug 1536021: Allow hash to return long objects
840        class X:
841            def __hash__(self):
842                return 2**100
843        self.assertEqual(type(hash(X())), int)
844        class Z(int):
845            def __hash__(self):
846                return self
847        self.assertEqual(hash(Z(42)), hash(42))
848
849    def test_hex(self):
850        self.assertEqual(hex(16), '0x10')
851        self.assertEqual(hex(-16), '-0x10')
852        self.assertRaises(TypeError, hex, {})
853
854    def test_id(self):
855        id(None)
856        id(1)
857        id(1.0)
858        id('spam')
859        id((0,1,2,3))
860        id([0,1,2,3])
861        id({'spam': 1, 'eggs': 2, 'ham': 3})
862
863    # Test input() later, alphabetized as if it were raw_input
864
865    def test_iter(self):
866        self.assertRaises(TypeError, iter)
867        self.assertRaises(TypeError, iter, 42, 42)
868        lists = [("1", "2"), ["1", "2"], "12"]
869        for l in lists:
870            i = iter(l)
871            self.assertEqual(next(i), '1')
872            self.assertEqual(next(i), '2')
873            self.assertRaises(StopIteration, next, i)
874
875    def test_isinstance(self):
876        class C:
877            pass
878        class D(C):
879            pass
880        class E:
881            pass
882        c = C()
883        d = D()
884        e = E()
885        self.assertTrue(isinstance(c, C))
886        self.assertTrue(isinstance(d, C))
887        self.assertTrue(not isinstance(e, C))
888        self.assertTrue(not isinstance(c, D))
889        self.assertTrue(not isinstance('foo', E))
890        self.assertRaises(TypeError, isinstance, E, 'foo')
891        self.assertRaises(TypeError, isinstance)
892
893    def test_issubclass(self):
894        class C:
895            pass
896        class D(C):
897            pass
898        class E:
899            pass
900        c = C()
901        d = D()
902        e = E()
903        self.assertTrue(issubclass(D, C))
904        self.assertTrue(issubclass(C, C))
905        self.assertTrue(not issubclass(C, D))
906        self.assertRaises(TypeError, issubclass, 'foo', E)
907        self.assertRaises(TypeError, issubclass, E, 'foo')
908        self.assertRaises(TypeError, issubclass)
909
910    def test_len(self):
911        self.assertEqual(len('123'), 3)
912        self.assertEqual(len(()), 0)
913        self.assertEqual(len((1, 2, 3, 4)), 4)
914        self.assertEqual(len([1, 2, 3, 4]), 4)
915        self.assertEqual(len({}), 0)
916        self.assertEqual(len({'a':1, 'b': 2}), 2)
917        class BadSeq:
918            def __len__(self):
919                raise ValueError
920        self.assertRaises(ValueError, len, BadSeq())
921        class InvalidLen:
922            def __len__(self):
923                return None
924        self.assertRaises(TypeError, len, InvalidLen())
925        class FloatLen:
926            def __len__(self):
927                return 4.5
928        self.assertRaises(TypeError, len, FloatLen())
929        class NegativeLen:
930            def __len__(self):
931                return -10
932        self.assertRaises(ValueError, len, NegativeLen())
933        class HugeLen:
934            def __len__(self):
935                return sys.maxsize + 1
936        self.assertRaises(OverflowError, len, HugeLen())
937        class HugeNegativeLen:
938            def __len__(self):
939                return -sys.maxsize-10
940        self.assertRaises(ValueError, len, HugeNegativeLen())
941        class NoLenMethod(object): pass
942        self.assertRaises(TypeError, len, NoLenMethod())
943
944    def test_map(self):
945        self.assertEqual(
946            list(map(lambda x: x*x, range(1,4))),
947            [1, 4, 9]
948        )
949        try:
950            from math import sqrt
951        except ImportError:
952            def sqrt(x):
953                return pow(x, 0.5)
954        self.assertEqual(
955            list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])),
956            [[4.0, 2.0], [9.0, 3.0]]
957        )
958        self.assertEqual(
959            list(map(lambda x, y: x+y, [1,3,2], [9,1,4])),
960            [10, 4, 6]
961        )
962
963        def plus(*v):
964            accu = 0
965            for i in v: accu = accu + i
966            return accu
967        self.assertEqual(
968            list(map(plus, [1, 3, 7])),
969            [1, 3, 7]
970        )
971        self.assertEqual(
972            list(map(plus, [1, 3, 7], [4, 9, 2])),
973            [1+4, 3+9, 7+2]
974        )
975        self.assertEqual(
976            list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])),
977            [1+4+1, 3+9+1, 7+2+0]
978        )
979        self.assertEqual(
980            list(map(int, Squares(10))),
981            [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
982        )
983        def Max(a, b):
984            if a is None:
985                return b
986            if b is None:
987                return a
988            return max(a, b)
989        self.assertEqual(
990            list(map(Max, Squares(3), Squares(2))),
991            [0, 1]
992        )
993        self.assertRaises(TypeError, map)
994        self.assertRaises(TypeError, map, lambda x: x, 42)
995        class BadSeq:
996            def __iter__(self):
997                raise ValueError
998                yield None
999        self.assertRaises(ValueError, list, map(lambda x: x, BadSeq()))
1000        def badfunc(x):
1001            raise RuntimeError
1002        self.assertRaises(RuntimeError, list, map(badfunc, range(5)))
1003
1004    def test_map_pickle(self):
1005        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1006            m1 = map(map_char, "Is this the real life?")
1007            m2 = map(map_char, "Is this the real life?")
1008            self.check_iter_pickle(m1, list(m2), proto)
1009
1010    def test_max(self):
1011        self.assertEqual(max('123123'), '3')
1012        self.assertEqual(max(1, 2, 3), 3)
1013        self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3)
1014        self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3)
1015
1016        self.assertEqual(max(1, 2, 3.0), 3.0)
1017        self.assertEqual(max(1, 2.0, 3), 3)
1018        self.assertEqual(max(1.0, 2, 3), 3)
1019
1020        with self.assertRaisesRegex(
1021            TypeError,
1022            'max expected at least 1 argument, got 0'
1023        ):
1024            max()
1025
1026        self.assertRaises(TypeError, max, 42)
1027        self.assertRaises(ValueError, max, ())
1028        class BadSeq:
1029            def __getitem__(self, index):
1030                raise ValueError
1031        self.assertRaises(ValueError, max, BadSeq())
1032
1033        for stmt in (
1034            "max(key=int)",                 # no args
1035            "max(default=None)",
1036            "max(1, 2, default=None)",      # require container for default
1037            "max(default=None, key=int)",
1038            "max(1, key=int)",              # single arg not iterable
1039            "max(1, 2, keystone=int)",      # wrong keyword
1040            "max(1, 2, key=int, abc=int)",  # two many keywords
1041            "max(1, 2, key=1)",             # keyfunc is not callable
1042            ):
1043            try:
1044                exec(stmt, globals())
1045            except TypeError:
1046                pass
1047            else:
1048                self.fail(stmt)
1049
1050        self.assertEqual(max((1,), key=neg), 1)     # one elem iterable
1051        self.assertEqual(max((1,2), key=neg), 1)    # two elem iterable
1052        self.assertEqual(max(1, 2, key=neg), 1)     # two elems
1053
1054        self.assertEqual(max((), default=None), None)    # zero elem iterable
1055        self.assertEqual(max((1,), default=None), 1)     # one elem iterable
1056        self.assertEqual(max((1,2), default=None), 2)    # two elem iterable
1057
1058        self.assertEqual(max((), default=1, key=neg), 1)
1059        self.assertEqual(max((1, 2), default=3, key=neg), 1)
1060
1061        self.assertEqual(max((1, 2), key=None), 2)
1062
1063        data = [random.randrange(200) for i in range(100)]
1064        keys = dict((elem, random.randrange(50)) for elem in data)
1065        f = keys.__getitem__
1066        self.assertEqual(max(data, key=f),
1067                         sorted(reversed(data), key=f)[-1])
1068
1069    def test_min(self):
1070        self.assertEqual(min('123123'), '1')
1071        self.assertEqual(min(1, 2, 3), 1)
1072        self.assertEqual(min((1, 2, 3, 1, 2, 3)), 1)
1073        self.assertEqual(min([1, 2, 3, 1, 2, 3]), 1)
1074
1075        self.assertEqual(min(1, 2, 3.0), 1)
1076        self.assertEqual(min(1, 2.0, 3), 1)
1077        self.assertEqual(min(1.0, 2, 3), 1.0)
1078
1079        with self.assertRaisesRegex(
1080            TypeError,
1081            'min expected at least 1 argument, got 0'
1082        ):
1083            min()
1084
1085        self.assertRaises(TypeError, min, 42)
1086        self.assertRaises(ValueError, min, ())
1087        class BadSeq:
1088            def __getitem__(self, index):
1089                raise ValueError
1090        self.assertRaises(ValueError, min, BadSeq())
1091
1092        for stmt in (
1093            "min(key=int)",                 # no args
1094            "min(default=None)",
1095            "min(1, 2, default=None)",      # require container for default
1096            "min(default=None, key=int)",
1097            "min(1, key=int)",              # single arg not iterable
1098            "min(1, 2, keystone=int)",      # wrong keyword
1099            "min(1, 2, key=int, abc=int)",  # two many keywords
1100            "min(1, 2, key=1)",             # keyfunc is not callable
1101            ):
1102            try:
1103                exec(stmt, globals())
1104            except TypeError:
1105                pass
1106            else:
1107                self.fail(stmt)
1108
1109        self.assertEqual(min((1,), key=neg), 1)     # one elem iterable
1110        self.assertEqual(min((1,2), key=neg), 2)    # two elem iterable
1111        self.assertEqual(min(1, 2, key=neg), 2)     # two elems
1112
1113        self.assertEqual(min((), default=None), None)    # zero elem iterable
1114        self.assertEqual(min((1,), default=None), 1)     # one elem iterable
1115        self.assertEqual(min((1,2), default=None), 1)    # two elem iterable
1116
1117        self.assertEqual(min((), default=1, key=neg), 1)
1118        self.assertEqual(min((1, 2), default=1, key=neg), 2)
1119
1120        self.assertEqual(min((1, 2), key=None), 1)
1121
1122        data = [random.randrange(200) for i in range(100)]
1123        keys = dict((elem, random.randrange(50)) for elem in data)
1124        f = keys.__getitem__
1125        self.assertEqual(min(data, key=f),
1126                         sorted(data, key=f)[0])
1127
1128    def test_next(self):
1129        it = iter(range(2))
1130        self.assertEqual(next(it), 0)
1131        self.assertEqual(next(it), 1)
1132        self.assertRaises(StopIteration, next, it)
1133        self.assertRaises(StopIteration, next, it)
1134        self.assertEqual(next(it, 42), 42)
1135
1136        class Iter(object):
1137            def __iter__(self):
1138                return self
1139            def __next__(self):
1140                raise StopIteration
1141
1142        it = iter(Iter())
1143        self.assertEqual(next(it, 42), 42)
1144        self.assertRaises(StopIteration, next, it)
1145
1146        def gen():
1147            yield 1
1148            return
1149
1150        it = gen()
1151        self.assertEqual(next(it), 1)
1152        self.assertRaises(StopIteration, next, it)
1153        self.assertEqual(next(it, 42), 42)
1154
1155    def test_oct(self):
1156        self.assertEqual(oct(100), '0o144')
1157        self.assertEqual(oct(-100), '-0o144')
1158        self.assertRaises(TypeError, oct, ())
1159
1160    def write_testfile(self):
1161        # NB the first 4 lines are also used to test input, below
1162        fp = open(TESTFN, 'w', encoding="utf-8")
1163        self.addCleanup(unlink, TESTFN)
1164        with fp:
1165            fp.write('1+1\n')
1166            fp.write('The quick brown fox jumps over the lazy dog')
1167            fp.write('.\n')
1168            fp.write('Dear John\n')
1169            fp.write('XXX'*100)
1170            fp.write('YYY'*100)
1171
1172    def test_open(self):
1173        self.write_testfile()
1174        fp = open(TESTFN, encoding="utf-8")
1175        with fp:
1176            self.assertEqual(fp.readline(4), '1+1\n')
1177            self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n')
1178            self.assertEqual(fp.readline(4), 'Dear')
1179            self.assertEqual(fp.readline(100), ' John\n')
1180            self.assertEqual(fp.read(300), 'XXX'*100)
1181            self.assertEqual(fp.read(1000), 'YYY'*100)
1182
1183        # embedded null bytes and characters
1184        self.assertRaises(ValueError, open, 'a\x00b')
1185        self.assertRaises(ValueError, open, b'a\x00b')
1186
1187    @unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled")
1188    def test_open_default_encoding(self):
1189        old_environ = dict(os.environ)
1190        try:
1191            # try to get a user preferred encoding different than the current
1192            # locale encoding to check that open() uses the current locale
1193            # encoding and not the user preferred encoding
1194            for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
1195                if key in os.environ:
1196                    del os.environ[key]
1197
1198            self.write_testfile()
1199            current_locale_encoding = locale.getpreferredencoding(False)
1200            with warnings.catch_warnings():
1201                warnings.simplefilter("ignore", EncodingWarning)
1202                fp = open(TESTFN, 'w')
1203            with fp:
1204                self.assertEqual(fp.encoding, current_locale_encoding)
1205        finally:
1206            os.environ.clear()
1207            os.environ.update(old_environ)
1208
1209    def test_open_non_inheritable(self):
1210        fileobj = open(__file__, encoding="utf-8")
1211        with fileobj:
1212            self.assertFalse(os.get_inheritable(fileobj.fileno()))
1213
1214    def test_ord(self):
1215        self.assertEqual(ord(' '), 32)
1216        self.assertEqual(ord('A'), 65)
1217        self.assertEqual(ord('a'), 97)
1218        self.assertEqual(ord('\x80'), 128)
1219        self.assertEqual(ord('\xff'), 255)
1220
1221        self.assertEqual(ord(b' '), 32)
1222        self.assertEqual(ord(b'A'), 65)
1223        self.assertEqual(ord(b'a'), 97)
1224        self.assertEqual(ord(b'\x80'), 128)
1225        self.assertEqual(ord(b'\xff'), 255)
1226
1227        self.assertEqual(ord(chr(sys.maxunicode)), sys.maxunicode)
1228        self.assertRaises(TypeError, ord, 42)
1229
1230        self.assertEqual(ord(chr(0x10FFFF)), 0x10FFFF)
1231        self.assertEqual(ord("\U0000FFFF"), 0x0000FFFF)
1232        self.assertEqual(ord("\U00010000"), 0x00010000)
1233        self.assertEqual(ord("\U00010001"), 0x00010001)
1234        self.assertEqual(ord("\U000FFFFE"), 0x000FFFFE)
1235        self.assertEqual(ord("\U000FFFFF"), 0x000FFFFF)
1236        self.assertEqual(ord("\U00100000"), 0x00100000)
1237        self.assertEqual(ord("\U00100001"), 0x00100001)
1238        self.assertEqual(ord("\U0010FFFE"), 0x0010FFFE)
1239        self.assertEqual(ord("\U0010FFFF"), 0x0010FFFF)
1240
1241    def test_pow(self):
1242        self.assertEqual(pow(0,0), 1)
1243        self.assertEqual(pow(0,1), 0)
1244        self.assertEqual(pow(1,0), 1)
1245        self.assertEqual(pow(1,1), 1)
1246
1247        self.assertEqual(pow(2,0), 1)
1248        self.assertEqual(pow(2,10), 1024)
1249        self.assertEqual(pow(2,20), 1024*1024)
1250        self.assertEqual(pow(2,30), 1024*1024*1024)
1251
1252        self.assertEqual(pow(-2,0), 1)
1253        self.assertEqual(pow(-2,1), -2)
1254        self.assertEqual(pow(-2,2), 4)
1255        self.assertEqual(pow(-2,3), -8)
1256
1257        self.assertAlmostEqual(pow(0.,0), 1.)
1258        self.assertAlmostEqual(pow(0.,1), 0.)
1259        self.assertAlmostEqual(pow(1.,0), 1.)
1260        self.assertAlmostEqual(pow(1.,1), 1.)
1261
1262        self.assertAlmostEqual(pow(2.,0), 1.)
1263        self.assertAlmostEqual(pow(2.,10), 1024.)
1264        self.assertAlmostEqual(pow(2.,20), 1024.*1024.)
1265        self.assertAlmostEqual(pow(2.,30), 1024.*1024.*1024.)
1266
1267        self.assertAlmostEqual(pow(-2.,0), 1.)
1268        self.assertAlmostEqual(pow(-2.,1), -2.)
1269        self.assertAlmostEqual(pow(-2.,2), 4.)
1270        self.assertAlmostEqual(pow(-2.,3), -8.)
1271
1272        for x in 2, 2.0:
1273            for y in 10, 10.0:
1274                for z in 1000, 1000.0:
1275                    if isinstance(x, float) or \
1276                       isinstance(y, float) or \
1277                       isinstance(z, float):
1278                        self.assertRaises(TypeError, pow, x, y, z)
1279                    else:
1280                        self.assertAlmostEqual(pow(x, y, z), 24.0)
1281
1282        self.assertAlmostEqual(pow(-1, 0.5), 1j)
1283        self.assertAlmostEqual(pow(-1, 1/3), 0.5 + 0.8660254037844386j)
1284
1285        # See test_pow for additional tests for three-argument pow.
1286        self.assertEqual(pow(-1, -2, 3), 1)
1287        self.assertRaises(ValueError, pow, 1, 2, 0)
1288
1289        self.assertRaises(TypeError, pow)
1290
1291        # Test passing in arguments as keywords.
1292        self.assertEqual(pow(0, exp=0), 1)
1293        self.assertEqual(pow(base=2, exp=4), 16)
1294        self.assertEqual(pow(base=5, exp=2, mod=14), 11)
1295        twopow = partial(pow, base=2)
1296        self.assertEqual(twopow(exp=5), 32)
1297        fifth_power = partial(pow, exp=5)
1298        self.assertEqual(fifth_power(2), 32)
1299        mod10 = partial(pow, mod=10)
1300        self.assertEqual(mod10(2, 6), 4)
1301        self.assertEqual(mod10(exp=6, base=2), 4)
1302
1303    def test_input(self):
1304        self.write_testfile()
1305        fp = open(TESTFN, encoding="utf-8")
1306        savestdin = sys.stdin
1307        savestdout = sys.stdout # Eats the echo
1308        try:
1309            sys.stdin = fp
1310            sys.stdout = BitBucket()
1311            self.assertEqual(input(), "1+1")
1312            self.assertEqual(input(), 'The quick brown fox jumps over the lazy dog.')
1313            self.assertEqual(input('testing\n'), 'Dear John')
1314
1315            # SF 1535165: don't segfault on closed stdin
1316            # sys.stdout must be a regular file for triggering
1317            sys.stdout = savestdout
1318            sys.stdin.close()
1319            self.assertRaises(ValueError, input)
1320
1321            sys.stdout = BitBucket()
1322            sys.stdin = io.StringIO("NULL\0")
1323            self.assertRaises(TypeError, input, 42, 42)
1324            sys.stdin = io.StringIO("    'whitespace'")
1325            self.assertEqual(input(), "    'whitespace'")
1326            sys.stdin = io.StringIO()
1327            self.assertRaises(EOFError, input)
1328
1329            del sys.stdout
1330            self.assertRaises(RuntimeError, input, 'prompt')
1331            del sys.stdin
1332            self.assertRaises(RuntimeError, input, 'prompt')
1333        finally:
1334            sys.stdin = savestdin
1335            sys.stdout = savestdout
1336            fp.close()
1337
1338    # test_int(): see test_int.py for tests of built-in function int().
1339
1340    def test_repr(self):
1341        self.assertEqual(repr(''), '\'\'')
1342        self.assertEqual(repr(0), '0')
1343        self.assertEqual(repr(()), '()')
1344        self.assertEqual(repr([]), '[]')
1345        self.assertEqual(repr({}), '{}')
1346        a = []
1347        a.append(a)
1348        self.assertEqual(repr(a), '[[...]]')
1349        a = {}
1350        a[0] = a
1351        self.assertEqual(repr(a), '{0: {...}}')
1352
1353    def test_round(self):
1354        self.assertEqual(round(0.0), 0.0)
1355        self.assertEqual(type(round(0.0)), int)
1356        self.assertEqual(round(1.0), 1.0)
1357        self.assertEqual(round(10.0), 10.0)
1358        self.assertEqual(round(1000000000.0), 1000000000.0)
1359        self.assertEqual(round(1e20), 1e20)
1360
1361        self.assertEqual(round(-1.0), -1.0)
1362        self.assertEqual(round(-10.0), -10.0)
1363        self.assertEqual(round(-1000000000.0), -1000000000.0)
1364        self.assertEqual(round(-1e20), -1e20)
1365
1366        self.assertEqual(round(0.1), 0.0)
1367        self.assertEqual(round(1.1), 1.0)
1368        self.assertEqual(round(10.1), 10.0)
1369        self.assertEqual(round(1000000000.1), 1000000000.0)
1370
1371        self.assertEqual(round(-1.1), -1.0)
1372        self.assertEqual(round(-10.1), -10.0)
1373        self.assertEqual(round(-1000000000.1), -1000000000.0)
1374
1375        self.assertEqual(round(0.9), 1.0)
1376        self.assertEqual(round(9.9), 10.0)
1377        self.assertEqual(round(999999999.9), 1000000000.0)
1378
1379        self.assertEqual(round(-0.9), -1.0)
1380        self.assertEqual(round(-9.9), -10.0)
1381        self.assertEqual(round(-999999999.9), -1000000000.0)
1382
1383        self.assertEqual(round(-8.0, -1), -10.0)
1384        self.assertEqual(type(round(-8.0, -1)), float)
1385
1386        self.assertEqual(type(round(-8.0, 0)), float)
1387        self.assertEqual(type(round(-8.0, 1)), float)
1388
1389        # Check even / odd rounding behaviour
1390        self.assertEqual(round(5.5), 6)
1391        self.assertEqual(round(6.5), 6)
1392        self.assertEqual(round(-5.5), -6)
1393        self.assertEqual(round(-6.5), -6)
1394
1395        # Check behavior on ints
1396        self.assertEqual(round(0), 0)
1397        self.assertEqual(round(8), 8)
1398        self.assertEqual(round(-8), -8)
1399        self.assertEqual(type(round(0)), int)
1400        self.assertEqual(type(round(-8, -1)), int)
1401        self.assertEqual(type(round(-8, 0)), int)
1402        self.assertEqual(type(round(-8, 1)), int)
1403
1404        # test new kwargs
1405        self.assertEqual(round(number=-8.0, ndigits=-1), -10.0)
1406
1407        self.assertRaises(TypeError, round)
1408
1409        # test generic rounding delegation for reals
1410        class TestRound:
1411            def __round__(self):
1412                return 23
1413
1414        class TestNoRound:
1415            pass
1416
1417        self.assertEqual(round(TestRound()), 23)
1418
1419        self.assertRaises(TypeError, round, 1, 2, 3)
1420        self.assertRaises(TypeError, round, TestNoRound())
1421
1422        t = TestNoRound()
1423        t.__round__ = lambda *args: args
1424        self.assertRaises(TypeError, round, t)
1425        self.assertRaises(TypeError, round, t, 0)
1426
1427    # Some versions of glibc for alpha have a bug that affects
1428    # float -> integer rounding (floor, ceil, rint, round) for
1429    # values in the range [2**52, 2**53).  See:
1430    #
1431    #   http://sources.redhat.com/bugzilla/show_bug.cgi?id=5350
1432    #
1433    # We skip this test on Linux/alpha if it would fail.
1434    linux_alpha = (platform.system().startswith('Linux') and
1435                   platform.machine().startswith('alpha'))
1436    system_round_bug = round(5e15+1) != 5e15+1
1437    @unittest.skipIf(linux_alpha and system_round_bug,
1438                     "test will fail;  failure is probably due to a "
1439                     "buggy system round function")
1440    def test_round_large(self):
1441        # Issue #1869: integral floats should remain unchanged
1442        self.assertEqual(round(5e15-1), 5e15-1)
1443        self.assertEqual(round(5e15), 5e15)
1444        self.assertEqual(round(5e15+1), 5e15+1)
1445        self.assertEqual(round(5e15+2), 5e15+2)
1446        self.assertEqual(round(5e15+3), 5e15+3)
1447
1448    def test_bug_27936(self):
1449        # Verify that ndigits=None means the same as passing in no argument
1450        for x in [1234,
1451                  1234.56,
1452                  decimal.Decimal('1234.56'),
1453                  fractions.Fraction(123456, 100)]:
1454            self.assertEqual(round(x, None), round(x))
1455            self.assertEqual(type(round(x, None)), type(round(x)))
1456
1457    def test_setattr(self):
1458        setattr(sys, 'spam', 1)
1459        self.assertEqual(sys.spam, 1)
1460        self.assertRaises(TypeError, setattr, sys, 1, 'spam')
1461        self.assertRaises(TypeError, setattr)
1462
1463    # test_str(): see test_unicode.py and test_bytes.py for str() tests.
1464
1465    def test_sum(self):
1466        self.assertEqual(sum([]), 0)
1467        self.assertEqual(sum(list(range(2,8))), 27)
1468        self.assertEqual(sum(iter(list(range(2,8)))), 27)
1469        self.assertEqual(sum(Squares(10)), 285)
1470        self.assertEqual(sum(iter(Squares(10))), 285)
1471        self.assertEqual(sum([[1], [2], [3]], []), [1, 2, 3])
1472
1473        self.assertEqual(sum(range(10), 1000), 1045)
1474        self.assertEqual(sum(range(10), start=1000), 1045)
1475        self.assertEqual(sum(range(10), 2**31-5), 2**31+40)
1476        self.assertEqual(sum(range(10), 2**63-5), 2**63+40)
1477
1478        self.assertEqual(sum(i % 2 != 0 for i in range(10)), 5)
1479        self.assertEqual(sum((i % 2 != 0 for i in range(10)), 2**31-3),
1480                         2**31+2)
1481        self.assertEqual(sum((i % 2 != 0 for i in range(10)), 2**63-3),
1482                         2**63+2)
1483        self.assertIs(sum([], False), False)
1484
1485        self.assertEqual(sum(i / 2 for i in range(10)), 22.5)
1486        self.assertEqual(sum((i / 2 for i in range(10)), 1000), 1022.5)
1487        self.assertEqual(sum((i / 2 for i in range(10)), 1000.25), 1022.75)
1488        self.assertEqual(sum([0.5, 1]), 1.5)
1489        self.assertEqual(sum([1, 0.5]), 1.5)
1490        self.assertEqual(repr(sum([-0.0])), '0.0')
1491        self.assertEqual(repr(sum([-0.0], -0.0)), '-0.0')
1492        self.assertEqual(repr(sum([], -0.0)), '-0.0')
1493
1494        self.assertRaises(TypeError, sum)
1495        self.assertRaises(TypeError, sum, 42)
1496        self.assertRaises(TypeError, sum, ['a', 'b', 'c'])
1497        self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '')
1498        self.assertRaises(TypeError, sum, [b'a', b'c'], b'')
1499        values = [bytearray(b'a'), bytearray(b'b')]
1500        self.assertRaises(TypeError, sum, values, bytearray(b''))
1501        self.assertRaises(TypeError, sum, [[1], [2], [3]])
1502        self.assertRaises(TypeError, sum, [{2:3}])
1503        self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3})
1504        self.assertRaises(TypeError, sum, [], '')
1505        self.assertRaises(TypeError, sum, [], b'')
1506        self.assertRaises(TypeError, sum, [], bytearray())
1507
1508        class BadSeq:
1509            def __getitem__(self, index):
1510                raise ValueError
1511        self.assertRaises(ValueError, sum, BadSeq())
1512
1513        empty = []
1514        sum(([x] for x in range(10)), empty)
1515        self.assertEqual(empty, [])
1516
1517    def test_type(self):
1518        self.assertEqual(type(''),  type('123'))
1519        self.assertNotEqual(type(''), type(()))
1520
1521    # We don't want self in vars(), so these are static methods
1522
1523    @staticmethod
1524    def get_vars_f0():
1525        return vars()
1526
1527    @staticmethod
1528    def get_vars_f2():
1529        BuiltinTest.get_vars_f0()
1530        a = 1
1531        b = 2
1532        return vars()
1533
1534    class C_get_vars(object):
1535        def getDict(self):
1536            return {'a':2}
1537        __dict__ = property(fget=getDict)
1538
1539    def test_vars(self):
1540        self.assertEqual(set(vars()), set(dir()))
1541        self.assertEqual(set(vars(sys)), set(dir(sys)))
1542        self.assertEqual(self.get_vars_f0(), {})
1543        self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2})
1544        self.assertRaises(TypeError, vars, 42, 42)
1545        self.assertRaises(TypeError, vars, 42)
1546        self.assertEqual(vars(self.C_get_vars()), {'a':2})
1547
1548    def iter_error(self, iterable, error):
1549        """Collect `iterable` into a list, catching an expected `error`."""
1550        items = []
1551        with self.assertRaises(error):
1552            for item in iterable:
1553                items.append(item)
1554        return items
1555
1556    def test_zip(self):
1557        a = (1, 2, 3)
1558        b = (4, 5, 6)
1559        t = [(1, 4), (2, 5), (3, 6)]
1560        self.assertEqual(list(zip(a, b)), t)
1561        b = [4, 5, 6]
1562        self.assertEqual(list(zip(a, b)), t)
1563        b = (4, 5, 6, 7)
1564        self.assertEqual(list(zip(a, b)), t)
1565        class I:
1566            def __getitem__(self, i):
1567                if i < 0 or i > 2: raise IndexError
1568                return i + 4
1569        self.assertEqual(list(zip(a, I())), t)
1570        self.assertEqual(list(zip()), [])
1571        self.assertEqual(list(zip(*[])), [])
1572        self.assertRaises(TypeError, zip, None)
1573        class G:
1574            pass
1575        self.assertRaises(TypeError, zip, a, G())
1576        self.assertRaises(RuntimeError, zip, a, TestFailingIter())
1577
1578        # Make sure zip doesn't try to allocate a billion elements for the
1579        # result list when one of its arguments doesn't say how long it is.
1580        # A MemoryError is the most likely failure mode.
1581        class SequenceWithoutALength:
1582            def __getitem__(self, i):
1583                if i == 5:
1584                    raise IndexError
1585                else:
1586                    return i
1587        self.assertEqual(
1588            list(zip(SequenceWithoutALength(), range(2**30))),
1589            list(enumerate(range(5)))
1590        )
1591
1592        class BadSeq:
1593            def __getitem__(self, i):
1594                if i == 5:
1595                    raise ValueError
1596                else:
1597                    return i
1598        self.assertRaises(ValueError, list, zip(BadSeq(), BadSeq()))
1599
1600    def test_zip_pickle(self):
1601        a = (1, 2, 3)
1602        b = (4, 5, 6)
1603        t = [(1, 4), (2, 5), (3, 6)]
1604        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1605            z1 = zip(a, b)
1606            self.check_iter_pickle(z1, t, proto)
1607
1608    def test_zip_pickle_strict(self):
1609        a = (1, 2, 3)
1610        b = (4, 5, 6)
1611        t = [(1, 4), (2, 5), (3, 6)]
1612        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1613            z1 = zip(a, b, strict=True)
1614            self.check_iter_pickle(z1, t, proto)
1615
1616    def test_zip_pickle_strict_fail(self):
1617        a = (1, 2, 3)
1618        b = (4, 5, 6, 7)
1619        t = [(1, 4), (2, 5), (3, 6)]
1620        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1621            z1 = zip(a, b, strict=True)
1622            z2 = pickle.loads(pickle.dumps(z1, proto))
1623            self.assertEqual(self.iter_error(z1, ValueError), t)
1624            self.assertEqual(self.iter_error(z2, ValueError), t)
1625
1626    def test_zip_bad_iterable(self):
1627        exception = TypeError()
1628
1629        class BadIterable:
1630            def __iter__(self):
1631                raise exception
1632
1633        with self.assertRaises(TypeError) as cm:
1634            zip(BadIterable())
1635
1636        self.assertIs(cm.exception, exception)
1637
1638    def test_zip_strict(self):
1639        self.assertEqual(tuple(zip((1, 2, 3), 'abc', strict=True)),
1640                         ((1, 'a'), (2, 'b'), (3, 'c')))
1641        self.assertRaises(ValueError, tuple,
1642                          zip((1, 2, 3, 4), 'abc', strict=True))
1643        self.assertRaises(ValueError, tuple,
1644                          zip((1, 2), 'abc', strict=True))
1645        self.assertRaises(ValueError, tuple,
1646                          zip((1, 2), (1, 2), 'abc', strict=True))
1647
1648    def test_zip_strict_iterators(self):
1649        x = iter(range(5))
1650        y = [0]
1651        z = iter(range(5))
1652        self.assertRaises(ValueError, list,
1653                          (zip(x, y, z, strict=True)))
1654        self.assertEqual(next(x), 2)
1655        self.assertEqual(next(z), 1)
1656
1657    def test_zip_strict_error_handling(self):
1658
1659        class Error(Exception):
1660            pass
1661
1662        class Iter:
1663            def __init__(self, size):
1664                self.size = size
1665            def __iter__(self):
1666                return self
1667            def __next__(self):
1668                self.size -= 1
1669                if self.size < 0:
1670                    raise Error
1671                return self.size
1672
1673        l1 = self.iter_error(zip("AB", Iter(1), strict=True), Error)
1674        self.assertEqual(l1, [("A", 0)])
1675        l2 = self.iter_error(zip("AB", Iter(2), "A", strict=True), ValueError)
1676        self.assertEqual(l2, [("A", 1, "A")])
1677        l3 = self.iter_error(zip("AB", Iter(2), "ABC", strict=True), Error)
1678        self.assertEqual(l3, [("A", 1, "A"), ("B", 0, "B")])
1679        l4 = self.iter_error(zip("AB", Iter(3), strict=True), ValueError)
1680        self.assertEqual(l4, [("A", 2), ("B", 1)])
1681        l5 = self.iter_error(zip(Iter(1), "AB", strict=True), Error)
1682        self.assertEqual(l5, [(0, "A")])
1683        l6 = self.iter_error(zip(Iter(2), "A", strict=True), ValueError)
1684        self.assertEqual(l6, [(1, "A")])
1685        l7 = self.iter_error(zip(Iter(2), "ABC", strict=True), Error)
1686        self.assertEqual(l7, [(1, "A"), (0, "B")])
1687        l8 = self.iter_error(zip(Iter(3), "AB", strict=True), ValueError)
1688        self.assertEqual(l8, [(2, "A"), (1, "B")])
1689
1690    def test_zip_strict_error_handling_stopiteration(self):
1691
1692        class Iter:
1693            def __init__(self, size):
1694                self.size = size
1695            def __iter__(self):
1696                return self
1697            def __next__(self):
1698                self.size -= 1
1699                if self.size < 0:
1700                    raise StopIteration
1701                return self.size
1702
1703        l1 = self.iter_error(zip("AB", Iter(1), strict=True), ValueError)
1704        self.assertEqual(l1, [("A", 0)])
1705        l2 = self.iter_error(zip("AB", Iter(2), "A", strict=True), ValueError)
1706        self.assertEqual(l2, [("A", 1, "A")])
1707        l3 = self.iter_error(zip("AB", Iter(2), "ABC", strict=True), ValueError)
1708        self.assertEqual(l3, [("A", 1, "A"), ("B", 0, "B")])
1709        l4 = self.iter_error(zip("AB", Iter(3), strict=True), ValueError)
1710        self.assertEqual(l4, [("A", 2), ("B", 1)])
1711        l5 = self.iter_error(zip(Iter(1), "AB", strict=True), ValueError)
1712        self.assertEqual(l5, [(0, "A")])
1713        l6 = self.iter_error(zip(Iter(2), "A", strict=True), ValueError)
1714        self.assertEqual(l6, [(1, "A")])
1715        l7 = self.iter_error(zip(Iter(2), "ABC", strict=True), ValueError)
1716        self.assertEqual(l7, [(1, "A"), (0, "B")])
1717        l8 = self.iter_error(zip(Iter(3), "AB", strict=True), ValueError)
1718        self.assertEqual(l8, [(2, "A"), (1, "B")])
1719
1720    @support.cpython_only
1721    def test_zip_result_gc(self):
1722        # bpo-42536: zip's tuple-reuse speed trick breaks the GC's assumptions
1723        # about what can be untracked. Make sure we re-track result tuples
1724        # whenever we reuse them.
1725        it = zip([[]])
1726        gc.collect()
1727        # That GC collection probably untracked the recycled internal result
1728        # tuple, which is initialized to (None,). Make sure it's re-tracked when
1729        # it's mutated and returned from __next__:
1730        self.assertTrue(gc.is_tracked(next(it)))
1731
1732    def test_format(self):
1733        # Test the basic machinery of the format() builtin.  Don't test
1734        #  the specifics of the various formatters
1735        self.assertEqual(format(3, ''), '3')
1736
1737        # Returns some classes to use for various tests.  There's
1738        #  an old-style version, and a new-style version
1739        def classes_new():
1740            class A(object):
1741                def __init__(self, x):
1742                    self.x = x
1743                def __format__(self, format_spec):
1744                    return str(self.x) + format_spec
1745            class DerivedFromA(A):
1746                pass
1747
1748            class Simple(object): pass
1749            class DerivedFromSimple(Simple):
1750                def __init__(self, x):
1751                    self.x = x
1752                def __format__(self, format_spec):
1753                    return str(self.x) + format_spec
1754            class DerivedFromSimple2(DerivedFromSimple): pass
1755            return A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2
1756
1757        def class_test(A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2):
1758            self.assertEqual(format(A(3), 'spec'), '3spec')
1759            self.assertEqual(format(DerivedFromA(4), 'spec'), '4spec')
1760            self.assertEqual(format(DerivedFromSimple(5), 'abc'), '5abc')
1761            self.assertEqual(format(DerivedFromSimple2(10), 'abcdef'),
1762                             '10abcdef')
1763
1764        class_test(*classes_new())
1765
1766        def empty_format_spec(value):
1767            # test that:
1768            #  format(x, '') == str(x)
1769            #  format(x) == str(x)
1770            self.assertEqual(format(value, ""), str(value))
1771            self.assertEqual(format(value), str(value))
1772
1773        # for builtin types, format(x, "") == str(x)
1774        empty_format_spec(17**13)
1775        empty_format_spec(1.0)
1776        empty_format_spec(3.1415e104)
1777        empty_format_spec(-3.1415e104)
1778        empty_format_spec(3.1415e-104)
1779        empty_format_spec(-3.1415e-104)
1780        empty_format_spec(object)
1781        empty_format_spec(None)
1782
1783        # TypeError because self.__format__ returns the wrong type
1784        class BadFormatResult:
1785            def __format__(self, format_spec):
1786                return 1.0
1787        self.assertRaises(TypeError, format, BadFormatResult(), "")
1788
1789        # TypeError because format_spec is not unicode or str
1790        self.assertRaises(TypeError, format, object(), 4)
1791        self.assertRaises(TypeError, format, object(), object())
1792
1793        # tests for object.__format__ really belong elsewhere, but
1794        #  there's no good place to put them
1795        x = object().__format__('')
1796        self.assertTrue(x.startswith('<object object at'))
1797
1798        # first argument to object.__format__ must be string
1799        self.assertRaises(TypeError, object().__format__, 3)
1800        self.assertRaises(TypeError, object().__format__, object())
1801        self.assertRaises(TypeError, object().__format__, None)
1802
1803        # --------------------------------------------------------------------
1804        # Issue #7994: object.__format__ with a non-empty format string is
1805        # disallowed
1806        class A:
1807            def __format__(self, fmt_str):
1808                return format('', fmt_str)
1809
1810        self.assertEqual(format(A()), '')
1811        self.assertEqual(format(A(), ''), '')
1812        self.assertEqual(format(A(), 's'), '')
1813
1814        class B:
1815            pass
1816
1817        class C(object):
1818            pass
1819
1820        for cls in [object, B, C]:
1821            obj = cls()
1822            self.assertEqual(format(obj), str(obj))
1823            self.assertEqual(format(obj, ''), str(obj))
1824            with self.assertRaisesRegex(TypeError,
1825                                        r'\b%s\b' % re.escape(cls.__name__)):
1826                format(obj, 's')
1827        # --------------------------------------------------------------------
1828
1829        # make sure we can take a subclass of str as a format spec
1830        class DerivedFromStr(str): pass
1831        self.assertEqual(format(0, DerivedFromStr('10')), '         0')
1832
1833    def test_bin(self):
1834        self.assertEqual(bin(0), '0b0')
1835        self.assertEqual(bin(1), '0b1')
1836        self.assertEqual(bin(-1), '-0b1')
1837        self.assertEqual(bin(2**65), '0b1' + '0' * 65)
1838        self.assertEqual(bin(2**65-1), '0b' + '1' * 65)
1839        self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
1840        self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
1841
1842    def test_bytearray_translate(self):
1843        x = bytearray(b"abc")
1844        self.assertRaises(ValueError, x.translate, b"1", 1)
1845        self.assertRaises(TypeError, x.translate, b"1"*256, 1)
1846
1847    def test_bytearray_extend_error(self):
1848        array = bytearray()
1849        bad_iter = map(int, "X")
1850        self.assertRaises(ValueError, array.extend, bad_iter)
1851
1852    def test_construct_singletons(self):
1853        for const in None, Ellipsis, NotImplemented:
1854            tp = type(const)
1855            self.assertIs(tp(), const)
1856            self.assertRaises(TypeError, tp, 1, 2)
1857            self.assertRaises(TypeError, tp, a=1, b=2)
1858
1859    def test_warning_notimplemented(self):
1860        # Issue #35712: NotImplemented is a sentinel value that should never
1861        # be evaluated in a boolean context (virtually all such use cases
1862        # are a result of accidental misuse implementing rich comparison
1863        # operations in terms of one another).
1864        # For the time being, it will continue to evaluate as a true value, but
1865        # issue a deprecation warning (with the eventual intent to make it
1866        # a TypeError).
1867        self.assertWarns(DeprecationWarning, bool, NotImplemented)
1868        with self.assertWarns(DeprecationWarning):
1869            self.assertTrue(NotImplemented)
1870        with self.assertWarns(DeprecationWarning):
1871            self.assertFalse(not NotImplemented)
1872
1873
1874class TestBreakpoint(unittest.TestCase):
1875    def setUp(self):
1876        # These tests require a clean slate environment.  For example, if the
1877        # test suite is run with $PYTHONBREAKPOINT set to something else, it
1878        # will mess up these tests.  Similarly for sys.breakpointhook.
1879        # Cleaning the slate here means you can't use breakpoint() to debug
1880        # these tests, but I think that's okay.  Just use pdb.set_trace() if
1881        # you must.
1882        self.resources = ExitStack()
1883        self.addCleanup(self.resources.close)
1884        self.env = self.resources.enter_context(EnvironmentVarGuard())
1885        del self.env['PYTHONBREAKPOINT']
1886        self.resources.enter_context(
1887            swap_attr(sys, 'breakpointhook', sys.__breakpointhook__))
1888
1889    def test_breakpoint(self):
1890        with patch('pdb.set_trace') as mock:
1891            breakpoint()
1892        mock.assert_called_once()
1893
1894    def test_breakpoint_with_breakpointhook_set(self):
1895        my_breakpointhook = MagicMock()
1896        sys.breakpointhook = my_breakpointhook
1897        breakpoint()
1898        my_breakpointhook.assert_called_once_with()
1899
1900    def test_breakpoint_with_breakpointhook_reset(self):
1901        my_breakpointhook = MagicMock()
1902        sys.breakpointhook = my_breakpointhook
1903        breakpoint()
1904        my_breakpointhook.assert_called_once_with()
1905        # Reset the hook and it will not be called again.
1906        sys.breakpointhook = sys.__breakpointhook__
1907        with patch('pdb.set_trace') as mock:
1908            breakpoint()
1909            mock.assert_called_once_with()
1910        my_breakpointhook.assert_called_once_with()
1911
1912    def test_breakpoint_with_args_and_keywords(self):
1913        my_breakpointhook = MagicMock()
1914        sys.breakpointhook = my_breakpointhook
1915        breakpoint(1, 2, 3, four=4, five=5)
1916        my_breakpointhook.assert_called_once_with(1, 2, 3, four=4, five=5)
1917
1918    def test_breakpoint_with_passthru_error(self):
1919        def my_breakpointhook():
1920            pass
1921        sys.breakpointhook = my_breakpointhook
1922        self.assertRaises(TypeError, breakpoint, 1, 2, 3, four=4, five=5)
1923
1924    @unittest.skipIf(sys.flags.ignore_environment, '-E was given')
1925    def test_envar_good_path_builtin(self):
1926        self.env['PYTHONBREAKPOINT'] = 'int'
1927        with patch('builtins.int') as mock:
1928            breakpoint('7')
1929            mock.assert_called_once_with('7')
1930
1931    @unittest.skipIf(sys.flags.ignore_environment, '-E was given')
1932    def test_envar_good_path_other(self):
1933        self.env['PYTHONBREAKPOINT'] = 'sys.exit'
1934        with patch('sys.exit') as mock:
1935            breakpoint()
1936            mock.assert_called_once_with()
1937
1938    @unittest.skipIf(sys.flags.ignore_environment, '-E was given')
1939    def test_envar_good_path_noop_0(self):
1940        self.env['PYTHONBREAKPOINT'] = '0'
1941        with patch('pdb.set_trace') as mock:
1942            breakpoint()
1943            mock.assert_not_called()
1944
1945    def test_envar_good_path_empty_string(self):
1946        # PYTHONBREAKPOINT='' is the same as it not being set.
1947        self.env['PYTHONBREAKPOINT'] = ''
1948        with patch('pdb.set_trace') as mock:
1949            breakpoint()
1950            mock.assert_called_once_with()
1951
1952    @unittest.skipIf(sys.flags.ignore_environment, '-E was given')
1953    def test_envar_unimportable(self):
1954        for envar in (
1955                '.', '..', '.foo', 'foo.', '.int', 'int.',
1956                '.foo.bar', '..foo.bar', '/./',
1957                'nosuchbuiltin',
1958                'nosuchmodule.nosuchcallable',
1959                ):
1960            with self.subTest(envar=envar):
1961                self.env['PYTHONBREAKPOINT'] = envar
1962                mock = self.resources.enter_context(patch('pdb.set_trace'))
1963                w = self.resources.enter_context(check_warnings(quiet=True))
1964                breakpoint()
1965                self.assertEqual(
1966                    str(w.message),
1967                    f'Ignoring unimportable $PYTHONBREAKPOINT: "{envar}"')
1968                self.assertEqual(w.category, RuntimeWarning)
1969                mock.assert_not_called()
1970
1971    def test_envar_ignored_when_hook_is_set(self):
1972        self.env['PYTHONBREAKPOINT'] = 'sys.exit'
1973        with patch('sys.exit') as mock:
1974            sys.breakpointhook = int
1975            breakpoint()
1976            mock.assert_not_called()
1977
1978
1979@unittest.skipUnless(pty, "the pty and signal modules must be available")
1980class PtyTests(unittest.TestCase):
1981    """Tests that use a pseudo terminal to guarantee stdin and stdout are
1982    terminals in the test environment"""
1983
1984    @staticmethod
1985    def handle_sighup(signum, frame):
1986        # bpo-40140: if the process is the session leader, os.close(fd)
1987        # of "pid, fd = pty.fork()" can raise SIGHUP signal:
1988        # just ignore the signal.
1989        pass
1990
1991    def run_child(self, child, terminal_input):
1992        old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
1993        try:
1994            return self._run_child(child, terminal_input)
1995        finally:
1996            signal.signal(signal.SIGHUP, old_sighup)
1997
1998    def _run_child(self, child, terminal_input):
1999        r, w = os.pipe()  # Pipe test results from child back to parent
2000        try:
2001            pid, fd = pty.fork()
2002        except (OSError, AttributeError) as e:
2003            os.close(r)
2004            os.close(w)
2005            self.skipTest("pty.fork() raised {}".format(e))
2006            raise
2007
2008        if pid == 0:
2009            # Child
2010            try:
2011                # Make sure we don't get stuck if there's a problem
2012                signal.alarm(2)
2013                os.close(r)
2014                with open(w, "w") as wpipe:
2015                    child(wpipe)
2016            except:
2017                traceback.print_exc()
2018            finally:
2019                # We don't want to return to unittest...
2020                os._exit(0)
2021
2022        # Parent
2023        os.close(w)
2024        os.write(fd, terminal_input)
2025
2026        # Get results from the pipe
2027        with open(r, encoding="utf-8") as rpipe:
2028            lines = []
2029            while True:
2030                line = rpipe.readline().strip()
2031                if line == "":
2032                    # The other end was closed => the child exited
2033                    break
2034                lines.append(line)
2035
2036        # Check the result was got and corresponds to the user's terminal input
2037        if len(lines) != 2:
2038            # Something went wrong, try to get at stderr
2039            # Beware of Linux raising EIO when the slave is closed
2040            child_output = bytearray()
2041            while True:
2042                try:
2043                    chunk = os.read(fd, 3000)
2044                except OSError:  # Assume EIO
2045                    break
2046                if not chunk:
2047                    break
2048                child_output.extend(chunk)
2049            os.close(fd)
2050            child_output = child_output.decode("ascii", "ignore")
2051            self.fail("got %d lines in pipe but expected 2, child output was:\n%s"
2052                      % (len(lines), child_output))
2053
2054        # bpo-40155: Close the PTY before waiting for the child process
2055        # completion, otherwise the child process hangs on AIX.
2056        os.close(fd)
2057
2058        support.wait_process(pid, exitcode=0)
2059
2060        return lines
2061
2062    def check_input_tty(self, prompt, terminal_input, stdio_encoding=None):
2063        if not sys.stdin.isatty() or not sys.stdout.isatty():
2064            self.skipTest("stdin and stdout must be ttys")
2065        def child(wpipe):
2066            # Check the error handlers are accounted for
2067            if stdio_encoding:
2068                sys.stdin = io.TextIOWrapper(sys.stdin.detach(),
2069                                             encoding=stdio_encoding,
2070                                             errors='surrogateescape')
2071                sys.stdout = io.TextIOWrapper(sys.stdout.detach(),
2072                                              encoding=stdio_encoding,
2073                                              errors='replace')
2074            print("tty =", sys.stdin.isatty() and sys.stdout.isatty(), file=wpipe)
2075            print(ascii(input(prompt)), file=wpipe)
2076        lines = self.run_child(child, terminal_input + b"\r\n")
2077        # Check we did exercise the GNU readline path
2078        self.assertIn(lines[0], {'tty = True', 'tty = False'})
2079        if lines[0] != 'tty = True':
2080            self.skipTest("standard IO in should have been a tty")
2081        input_result = eval(lines[1])   # ascii() -> eval() roundtrip
2082        if stdio_encoding:
2083            expected = terminal_input.decode(stdio_encoding, 'surrogateescape')
2084        else:
2085            expected = terminal_input.decode(sys.stdin.encoding)  # what else?
2086        self.assertEqual(input_result, expected)
2087
2088    def test_input_tty(self):
2089        # Test input() functionality when wired to a tty (the code path
2090        # is different and invokes GNU readline if available).
2091        self.check_input_tty("prompt", b"quux")
2092
2093    def test_input_tty_non_ascii(self):
2094        # Check stdin/stdout encoding is used when invoking GNU readline
2095        self.check_input_tty("prompté", b"quux\xe9", "utf-8")
2096
2097    def test_input_tty_non_ascii_unicode_errors(self):
2098        # Check stdin/stdout error handler is used when invoking GNU readline
2099        self.check_input_tty("prompté", b"quux\xe9", "ascii")
2100
2101    def test_input_no_stdout_fileno(self):
2102        # Issue #24402: If stdin is the original terminal but stdout.fileno()
2103        # fails, do not use the original stdout file descriptor
2104        def child(wpipe):
2105            print("stdin.isatty():", sys.stdin.isatty(), file=wpipe)
2106            sys.stdout = io.StringIO()  # Does not support fileno()
2107            input("prompt")
2108            print("captured:", ascii(sys.stdout.getvalue()), file=wpipe)
2109        lines = self.run_child(child, b"quux\r")
2110        expected = (
2111            "stdin.isatty(): True",
2112            "captured: 'prompt'",
2113        )
2114        self.assertSequenceEqual(lines, expected)
2115
2116class TestSorted(unittest.TestCase):
2117
2118    def test_basic(self):
2119        data = list(range(100))
2120        copy = data[:]
2121        random.shuffle(copy)
2122        self.assertEqual(data, sorted(copy))
2123        self.assertNotEqual(data, copy)
2124
2125        data.reverse()
2126        random.shuffle(copy)
2127        self.assertEqual(data, sorted(copy, key=lambda x: -x))
2128        self.assertNotEqual(data, copy)
2129        random.shuffle(copy)
2130        self.assertEqual(data, sorted(copy, reverse=True))
2131        self.assertNotEqual(data, copy)
2132
2133    def test_bad_arguments(self):
2134        # Issue #29327: The first argument is positional-only.
2135        sorted([])
2136        with self.assertRaises(TypeError):
2137            sorted(iterable=[])
2138        # Other arguments are keyword-only
2139        sorted([], key=None)
2140        with self.assertRaises(TypeError):
2141            sorted([], None)
2142
2143    def test_inputtypes(self):
2144        s = 'abracadabra'
2145        types = [list, tuple, str]
2146        for T in types:
2147            self.assertEqual(sorted(s), sorted(T(s)))
2148
2149        s = ''.join(set(s))  # unique letters only
2150        types = [str, set, frozenset, list, tuple, dict.fromkeys]
2151        for T in types:
2152            self.assertEqual(sorted(s), sorted(T(s)))
2153
2154    def test_baddecorator(self):
2155        data = 'The quick Brown fox Jumped over The lazy Dog'.split()
2156        self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0)
2157
2158
2159class ShutdownTest(unittest.TestCase):
2160
2161    def test_cleanup(self):
2162        # Issue #19255: builtins are still available at shutdown
2163        code = """if 1:
2164            import builtins
2165            import sys
2166
2167            class C:
2168                def __del__(self):
2169                    print("before")
2170                    # Check that builtins still exist
2171                    len(())
2172                    print("after")
2173
2174            c = C()
2175            # Make this module survive until builtins and sys are cleaned
2176            builtins.here = sys.modules[__name__]
2177            sys.here = sys.modules[__name__]
2178            # Create a reference loop so that this module needs to go
2179            # through a GC phase.
2180            here = sys.modules[__name__]
2181            """
2182        # Issue #20599: Force ASCII encoding to get a codec implemented in C,
2183        # otherwise the codec may be unloaded before C.__del__() is called, and
2184        # so print("before") fails because the codec cannot be used to encode
2185        # "before" to sys.stdout.encoding. For example, on Windows,
2186        # sys.stdout.encoding is the OEM code page and these code pages are
2187        # implemented in Python
2188        rc, out, err = assert_python_ok("-c", code,
2189                                        PYTHONIOENCODING="ascii")
2190        self.assertEqual(["before", "after"], out.decode().splitlines())
2191
2192
2193class TestType(unittest.TestCase):
2194    def test_new_type(self):
2195        A = type('A', (), {})
2196        self.assertEqual(A.__name__, 'A')
2197        self.assertEqual(A.__qualname__, 'A')
2198        self.assertEqual(A.__module__, __name__)
2199        self.assertEqual(A.__bases__, (object,))
2200        self.assertIs(A.__base__, object)
2201        x = A()
2202        self.assertIs(type(x), A)
2203        self.assertIs(x.__class__, A)
2204
2205        class B:
2206            def ham(self):
2207                return 'ham%d' % self
2208        C = type('C', (B, int), {'spam': lambda self: 'spam%s' % self})
2209        self.assertEqual(C.__name__, 'C')
2210        self.assertEqual(C.__qualname__, 'C')
2211        self.assertEqual(C.__module__, __name__)
2212        self.assertEqual(C.__bases__, (B, int))
2213        self.assertIs(C.__base__, int)
2214        self.assertIn('spam', C.__dict__)
2215        self.assertNotIn('ham', C.__dict__)
2216        x = C(42)
2217        self.assertEqual(x, 42)
2218        self.assertIs(type(x), C)
2219        self.assertIs(x.__class__, C)
2220        self.assertEqual(x.ham(), 'ham42')
2221        self.assertEqual(x.spam(), 'spam42')
2222        self.assertEqual(x.to_bytes(2, 'little'), b'\x2a\x00')
2223
2224    def test_type_nokwargs(self):
2225        with self.assertRaises(TypeError):
2226            type('a', (), {}, x=5)
2227        with self.assertRaises(TypeError):
2228            type('a', (), dict={})
2229
2230    def test_type_name(self):
2231        for name in 'A', '\xc4', '\U0001f40d', 'B.A', '42', '':
2232            with self.subTest(name=name):
2233                A = type(name, (), {})
2234                self.assertEqual(A.__name__, name)
2235                self.assertEqual(A.__qualname__, name)
2236                self.assertEqual(A.__module__, __name__)
2237        with self.assertRaises(ValueError):
2238            type('A\x00B', (), {})
2239        with self.assertRaises(ValueError):
2240            type('A\udcdcB', (), {})
2241        with self.assertRaises(TypeError):
2242            type(b'A', (), {})
2243
2244        C = type('C', (), {})
2245        for name in 'A', '\xc4', '\U0001f40d', 'B.A', '42', '':
2246            with self.subTest(name=name):
2247                C.__name__ = name
2248                self.assertEqual(C.__name__, name)
2249                self.assertEqual(C.__qualname__, 'C')
2250                self.assertEqual(C.__module__, __name__)
2251
2252        A = type('C', (), {})
2253        with self.assertRaises(ValueError):
2254            A.__name__ = 'A\x00B'
2255        self.assertEqual(A.__name__, 'C')
2256        with self.assertRaises(ValueError):
2257            A.__name__ = 'A\udcdcB'
2258        self.assertEqual(A.__name__, 'C')
2259        with self.assertRaises(TypeError):
2260            A.__name__ = b'A'
2261        self.assertEqual(A.__name__, 'C')
2262
2263    def test_type_qualname(self):
2264        A = type('A', (), {'__qualname__': 'B.C'})
2265        self.assertEqual(A.__name__, 'A')
2266        self.assertEqual(A.__qualname__, 'B.C')
2267        self.assertEqual(A.__module__, __name__)
2268        with self.assertRaises(TypeError):
2269            type('A', (), {'__qualname__': b'B'})
2270        self.assertEqual(A.__qualname__, 'B.C')
2271
2272        A.__qualname__ = 'D.E'
2273        self.assertEqual(A.__name__, 'A')
2274        self.assertEqual(A.__qualname__, 'D.E')
2275        with self.assertRaises(TypeError):
2276            A.__qualname__ = b'B'
2277        self.assertEqual(A.__qualname__, 'D.E')
2278
2279    def test_type_doc(self):
2280        for doc in 'x', '\xc4', '\U0001f40d', 'x\x00y', b'x', 42, None:
2281            A = type('A', (), {'__doc__': doc})
2282            self.assertEqual(A.__doc__, doc)
2283        with self.assertRaises(UnicodeEncodeError):
2284            type('A', (), {'__doc__': 'x\udcdcy'})
2285
2286        A = type('A', (), {})
2287        self.assertEqual(A.__doc__, None)
2288        for doc in 'x', '\xc4', '\U0001f40d', 'x\x00y', 'x\udcdcy', b'x', 42, None:
2289            A.__doc__ = doc
2290            self.assertEqual(A.__doc__, doc)
2291
2292    def test_bad_args(self):
2293        with self.assertRaises(TypeError):
2294            type()
2295        with self.assertRaises(TypeError):
2296            type('A', ())
2297        with self.assertRaises(TypeError):
2298            type('A', (), {}, ())
2299        with self.assertRaises(TypeError):
2300            type('A', (), dict={})
2301        with self.assertRaises(TypeError):
2302            type('A', [], {})
2303        with self.assertRaises(TypeError):
2304            type('A', (), types.MappingProxyType({}))
2305        with self.assertRaises(TypeError):
2306            type('A', (None,), {})
2307        with self.assertRaises(TypeError):
2308            type('A', (bool,), {})
2309        with self.assertRaises(TypeError):
2310            type('A', (int, str), {})
2311
2312    def test_bad_slots(self):
2313        with self.assertRaises(TypeError):
2314            type('A', (), {'__slots__': b'x'})
2315        with self.assertRaises(TypeError):
2316            type('A', (int,), {'__slots__': 'x'})
2317        with self.assertRaises(TypeError):
2318            type('A', (), {'__slots__': ''})
2319        with self.assertRaises(TypeError):
2320            type('A', (), {'__slots__': '42'})
2321        with self.assertRaises(TypeError):
2322            type('A', (), {'__slots__': 'x\x00y'})
2323        with self.assertRaises(ValueError):
2324            type('A', (), {'__slots__': 'x', 'x': 0})
2325        with self.assertRaises(TypeError):
2326            type('A', (), {'__slots__': ('__dict__', '__dict__')})
2327        with self.assertRaises(TypeError):
2328            type('A', (), {'__slots__': ('__weakref__', '__weakref__')})
2329
2330        class B:
2331            pass
2332        with self.assertRaises(TypeError):
2333            type('A', (B,), {'__slots__': '__dict__'})
2334        with self.assertRaises(TypeError):
2335            type('A', (B,), {'__slots__': '__weakref__'})
2336
2337    def test_namespace_order(self):
2338        # bpo-34320: namespace should preserve order
2339        od = collections.OrderedDict([('a', 1), ('b', 2)])
2340        od.move_to_end('a')
2341        expected = list(od.items())
2342
2343        C = type('C', (), od)
2344        self.assertEqual(list(C.__dict__.items())[:2], [('b', 2), ('a', 1)])
2345
2346
2347def load_tests(loader, tests, pattern):
2348    from doctest import DocTestSuite
2349    tests.addTest(DocTestSuite(builtins))
2350    return tests
2351
2352if __name__ == "__main__":
2353    unittest.main()
2354