diff --git a/Lib/test/support/script_helper.py b/Lib/test/support/script_helper.py index 27a47f2c4e..9b43c3330a 100644 --- a/Lib/test/support/script_helper.py +++ b/Lib/test/support/script_helper.py @@ -8,7 +8,7 @@ import os import os.path import subprocess import py_compile -import zipfile +# import zipfile XXX RustPython from importlib.util import source_from_cache from test.support import make_legacy_pyc, strip_python_stderr diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py new file mode 100644 index 0000000000..54834fa1a7 --- /dev/null +++ b/Lib/test/test_builtin.py @@ -0,0 +1,2044 @@ +# Python test set -- built-in functions + +import ast +import builtins +import collections +import decimal +# import fractions XXX RustPython +import io +import locale +import os +import pickle +import platform +import random +import re +import sys +import traceback +import types +import unittest +import warnings +from contextlib import ExitStack +from operator import neg +from test.support import ( + EnvironmentVarGuard, TESTFN, check_warnings, swap_attr, unlink) +from test.support.script_helper import assert_python_ok +from unittest.mock import MagicMock, patch +try: + import pty, signal +except ImportError: + pty = signal = None + + +class Squares: + + def __init__(self, max): + self.max = max + self.sofar = [] + + def __len__(self): return len(self.sofar) + + def __getitem__(self, i): + if not 0 <= i < self.max: raise IndexError + n = len(self.sofar) + while n <= i: + self.sofar.append(n*n) + n += 1 + return self.sofar[i] + +class StrSquares: + + def __init__(self, max): + self.max = max + self.sofar = [] + + def __len__(self): + return len(self.sofar) + + def __getitem__(self, i): + if not 0 <= i < self.max: + raise IndexError + n = len(self.sofar) + while n <= i: + self.sofar.append(str(n*n)) + n += 1 + return self.sofar[i] + +class BitBucket: + def write(self, line): + pass + +test_conv_no_sign = [ + ('0', 0), + ('1', 1), + ('9', 9), + ('10', 10), + ('99', 99), + ('100', 100), + ('314', 314), + (' 314', 314), + ('314 ', 314), + (' \t\t 314 \t\t ', 314), + (repr(sys.maxsize), sys.maxsize), + (' 1x', ValueError), + (' 1 ', 1), + (' 1\02 ', ValueError), + ('', ValueError), + (' ', ValueError), + (' \t\t ', ValueError), + # (str(br'\u0663\u0661\u0664 ','raw-unicode-escape'), 314), XXX RustPython + (chr(0x200), ValueError), +] + +test_conv_sign = [ + ('0', 0), + ('1', 1), + ('9', 9), + ('10', 10), + ('99', 99), + ('100', 100), + ('314', 314), + (' 314', ValueError), + ('314 ', 314), + (' \t\t 314 \t\t ', ValueError), + (repr(sys.maxsize), sys.maxsize), + (' 1x', ValueError), + (' 1 ', ValueError), + (' 1\02 ', ValueError), + ('', ValueError), + (' ', ValueError), + (' \t\t ', ValueError), + # (str(br'\u0663\u0661\u0664 ','raw-unicode-escape'), 314), XXX RustPython + (chr(0x200), ValueError), +] + +class TestFailingBool: + def __bool__(self): + raise RuntimeError + +class TestFailingIter: + def __iter__(self): + raise RuntimeError + +def filter_char(arg): + return ord(arg) > ord("d") + +def map_char(arg): + return chr(ord(arg)+1) + +class BuiltinTest(unittest.TestCase): + # Helper to check picklability + def check_iter_pickle(self, it, seq, proto): + itorg = it + d = pickle.dumps(it, proto) + it = pickle.loads(d) + self.assertEqual(type(itorg), type(it)) + self.assertEqual(list(it), seq) + + #test the iterator after dropping one from it + it = pickle.loads(d) + try: + next(it) + except StopIteration: + return + d = pickle.dumps(it, proto) + it = pickle.loads(d) + self.assertEqual(list(it), seq[1:]) + + def test_import(self): + __import__('sys') + __import__('time') + __import__('string') + __import__(name='sys') + __import__(name='time', level=0) + self.assertRaises(ImportError, __import__, 'spamspam') + self.assertRaises(TypeError, __import__, 1, 2, 3, 4) + self.assertRaises(ValueError, __import__, '') + self.assertRaises(TypeError, __import__, 'sys', name='sys') + # embedded null character + self.assertRaises(ModuleNotFoundError, __import__, 'string\x00') + + def test_abs(self): + # int + self.assertEqual(abs(0), 0) + self.assertEqual(abs(1234), 1234) + self.assertEqual(abs(-1234), 1234) + self.assertTrue(abs(-sys.maxsize-1) > 0) + # float + self.assertEqual(abs(0.0), 0.0) + self.assertEqual(abs(3.14), 3.14) + self.assertEqual(abs(-3.14), 3.14) + # str + self.assertRaises(TypeError, abs, 'a') + # bool + self.assertEqual(abs(True), 1) + self.assertEqual(abs(False), 0) + # other + self.assertRaises(TypeError, abs) + self.assertRaises(TypeError, abs, None) + class AbsClass(object): + def __abs__(self): + return -5 + self.assertEqual(abs(AbsClass()), -5) + + def test_all(self): + self.assertEqual(all([2, 4, 6]), True) + self.assertEqual(all([2, None, 6]), False) + self.assertRaises(RuntimeError, all, [2, TestFailingBool(), 6]) + self.assertRaises(RuntimeError, all, TestFailingIter()) + self.assertRaises(TypeError, all, 10) # Non-iterable + self.assertRaises(TypeError, all) # No args + self.assertRaises(TypeError, all, [2, 4, 6], []) # Too many args + self.assertEqual(all([]), True) # Empty iterator + self.assertEqual(all([0, TestFailingBool()]), False)# Short-circuit + S = [50, 60] + self.assertEqual(all(x > 42 for x in S), True) + S = [50, 40, 60] + self.assertEqual(all(x > 42 for x in S), False) + + def test_any(self): + self.assertEqual(any([None, None, None]), False) + self.assertEqual(any([None, 4, None]), True) + self.assertRaises(RuntimeError, any, [None, TestFailingBool(), 6]) + self.assertRaises(RuntimeError, any, TestFailingIter()) + self.assertRaises(TypeError, any, 10) # Non-iterable + self.assertRaises(TypeError, any) # No args + self.assertRaises(TypeError, any, [2, 4, 6], []) # Too many args + self.assertEqual(any([]), False) # Empty iterator + self.assertEqual(any([1, TestFailingBool()]), True) # Short-circuit + S = [40, 60, 30] + self.assertEqual(any(x > 42 for x in S), True) + S = [10, 20, 30] + self.assertEqual(any(x > 42 for x in S), False) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_ascii(self): + self.assertEqual(ascii(''), '\'\'') + self.assertEqual(ascii(0), '0') + self.assertEqual(ascii(()), '()') + self.assertEqual(ascii([]), '[]') + self.assertEqual(ascii({}), '{}') + a = [] + a.append(a) + self.assertEqual(ascii(a), '[[...]]') + a = {} + a[0] = a + self.assertEqual(ascii(a), '{0: {...}}') + # Advanced checks for unicode strings + def _check_uni(s): + self.assertEqual(ascii(s), repr(s)) + _check_uni("'") + _check_uni('"') + _check_uni('"\'') + _check_uni('\0') + _check_uni('\r\n\t .') + # Unprintable non-ASCII characters + _check_uni('\x85') + _check_uni('\u1fff') + _check_uni('\U00012fff') + # Lone surrogates + _check_uni('\ud800') + _check_uni('\udfff') + # Issue #9804: surrogates should be joined even for printable + # wide characters (UCS-2 builds). + self.assertEqual(ascii('\U0001d121'), "'\\U0001d121'") + # All together + s = "'\0\"\n\r\t abcd\x85é\U00012fff\uD800\U0001D121xxx." + self.assertEqual(ascii(s), + r"""'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud800\U0001d121xxx.'""") + + def test_neg(self): + x = -sys.maxsize-1 + self.assertTrue(isinstance(x, int)) + self.assertEqual(-x, sys.maxsize+1) + + def test_callable(self): + self.assertTrue(callable(len)) + self.assertFalse(callable("a")) + self.assertTrue(callable(callable)) + self.assertTrue(callable(lambda x, y: x + y)) + self.assertFalse(callable(__builtins__)) + def f(): pass + self.assertTrue(callable(f)) + + class C1: + def meth(self): pass + self.assertTrue(callable(C1)) + c = C1() + self.assertTrue(callable(c.meth)) + self.assertFalse(callable(c)) + + # __call__ is looked up on the class, not the instance + c.__call__ = None + self.assertFalse(callable(c)) + c.__call__ = lambda self: 0 + self.assertFalse(callable(c)) + del c.__call__ + self.assertFalse(callable(c)) + + class C2(object): + def __call__(self): pass + c2 = C2() + self.assertTrue(callable(c2)) + c2.__call__ = None + self.assertTrue(callable(c2)) + class C3(C2): pass + c3 = C3() + self.assertTrue(callable(c3)) + + @unittest.skip("TODO: RUSTPYTHON") + def test_chr(self): + self.assertEqual(chr(32), ' ') + self.assertEqual(chr(65), 'A') + self.assertEqual(chr(97), 'a') + self.assertEqual(chr(0xff), '\xff') + self.assertRaises(ValueError, chr, 1<<24) + self.assertEqual(chr(sys.maxunicode), + str('\\U0010ffff'.encode("ascii"), 'unicode-escape')) + self.assertRaises(TypeError, chr) + self.assertEqual(chr(0x0000FFFF), "\U0000FFFF") + self.assertEqual(chr(0x00010000), "\U00010000") + self.assertEqual(chr(0x00010001), "\U00010001") + self.assertEqual(chr(0x000FFFFE), "\U000FFFFE") + self.assertEqual(chr(0x000FFFFF), "\U000FFFFF") + self.assertEqual(chr(0x00100000), "\U00100000") + self.assertEqual(chr(0x00100001), "\U00100001") + self.assertEqual(chr(0x0010FFFE), "\U0010FFFE") + self.assertEqual(chr(0x0010FFFF), "\U0010FFFF") + self.assertRaises(ValueError, chr, -1) + self.assertRaises(ValueError, chr, 0x00110000) + self.assertRaises((OverflowError, ValueError), chr, 2**32) + + def test_cmp(self): + self.assertTrue(not hasattr(builtins, "cmp")) + + @unittest.skip("TODO: RUSTPYTHON") + def test_compile(self): + compile('print(1)\n', '', 'exec') + bom = b'\xef\xbb\xbf' + compile(bom + b'print(1)\n', '', 'exec') + compile(source='pass', filename='?', mode='exec') + compile(dont_inherit=0, filename='tmp', source='0', mode='eval') + compile('pass', '?', dont_inherit=1, mode='exec') + compile(memoryview(b"text"), "name", "exec") + self.assertRaises(TypeError, compile) + self.assertRaises(ValueError, compile, 'print(42)\n', '', 'badmode') + self.assertRaises(ValueError, compile, 'print(42)\n', '', 'single', 0xff) + self.assertRaises(ValueError, compile, chr(0), 'f', 'exec') + self.assertRaises(TypeError, compile, 'pass', '?', 'exec', + mode='eval', source='0', filename='tmp') + compile('print("\xe5")\n', '', 'exec') + self.assertRaises(ValueError, compile, chr(0), 'f', 'exec') + self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad') + + # test the optimize argument + + codestr = '''def f(): + """doc""" + debug_enabled = False + if __debug__: + debug_enabled = True + try: + assert False + except AssertionError: + return (True, f.__doc__, debug_enabled, __debug__) + else: + return (False, f.__doc__, debug_enabled, __debug__) + ''' + def f(): """doc""" + values = [(-1, __debug__, f.__doc__, __debug__, __debug__), + (0, True, 'doc', True, True), + (1, False, 'doc', False, False), + (2, False, None, False, False)] + for optval, *expected in values: + # test both direct compilation and compilation via AST + codeobjs = [] + codeobjs.append(compile(codestr, "", "exec", optimize=optval)) + tree = ast.parse(codestr) + codeobjs.append(compile(tree, "", "exec", optimize=optval)) + for code in codeobjs: + ns = {} + exec(code, ns) + rv = ns['f']() + self.assertEqual(rv, tuple(expected)) + + def test_delattr(self): + sys.spam = 1 + delattr(sys, 'spam') + self.assertRaises(TypeError, delattr) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_dir(self): + # dir(wrong number of arguments) + self.assertRaises(TypeError, dir, 42, 42) + + # dir() - local scope + local_var = 1 + self.assertIn('local_var', dir()) + + # dir(module) + self.assertIn('exit', dir(sys)) + + # dir(module_with_invalid__dict__) + class Foo(types.ModuleType): + __dict__ = 8 + f = Foo("foo") + self.assertRaises(TypeError, dir, f) + + # dir(type) + self.assertIn("strip", dir(str)) + self.assertNotIn("__mro__", dir(str)) + + # dir(obj) + class Foo(object): + def __init__(self): + self.x = 7 + self.y = 8 + self.z = 9 + f = Foo() + self.assertIn("y", dir(f)) + + # dir(obj_no__dict__) + class Foo(object): + __slots__ = [] + f = Foo() + self.assertIn("__repr__", dir(f)) + + # dir(obj_no__class__with__dict__) + # (an ugly trick to cause getattr(f, "__class__") to fail) + class Foo(object): + __slots__ = ["__class__", "__dict__"] + def __init__(self): + self.bar = "wow" + f = Foo() + self.assertNotIn("__repr__", dir(f)) + self.assertIn("bar", dir(f)) + + # dir(obj_using __dir__) + class Foo(object): + def __dir__(self): + return ["kan", "ga", "roo"] + f = Foo() + self.assertTrue(dir(f) == ["ga", "kan", "roo"]) + + # dir(obj__dir__tuple) + class Foo(object): + def __dir__(self): + return ("b", "c", "a") + res = dir(Foo()) + self.assertIsInstance(res, list) + self.assertTrue(res == ["a", "b", "c"]) + + # dir(obj__dir__not_sequence) + class Foo(object): + def __dir__(self): + return 7 + f = Foo() + self.assertRaises(TypeError, dir, f) + + # dir(traceback) + try: + raise IndexError + except: + self.assertEqual(len(dir(sys.exc_info()[2])), 4) + + # test that object has a __dir__() + self.assertEqual(sorted([].__dir__()), dir([])) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_divmod(self): + self.assertEqual(divmod(12, 7), (1, 5)) + self.assertEqual(divmod(-12, 7), (-2, 2)) + self.assertEqual(divmod(12, -7), (-2, -2)) + self.assertEqual(divmod(-12, -7), (1, -5)) + + self.assertEqual(divmod(-sys.maxsize-1, -1), (sys.maxsize+1, 0)) + + for num, denom, exp_result in [ (3.25, 1.0, (3.0, 0.25)), + (-3.25, 1.0, (-4.0, 0.75)), + (3.25, -1.0, (-4.0, -0.75)), + (-3.25, -1.0, (3.0, -0.25))]: + result = divmod(num, denom) + self.assertAlmostEqual(result[0], exp_result[0]) + self.assertAlmostEqual(result[1], exp_result[1]) + + self.assertRaises(TypeError, divmod) + + @unittest.skip("TODO: RUSTPYTHON") + def test_eval(self): + self.assertEqual(eval('1+1'), 2) + self.assertEqual(eval(' 1+1\n'), 2) + globals = {'a': 1, 'b': 2} + locals = {'b': 200, 'c': 300} + self.assertEqual(eval('a', globals) , 1) + self.assertEqual(eval('a', globals, locals), 1) + self.assertEqual(eval('b', globals, locals), 200) + self.assertEqual(eval('c', globals, locals), 300) + globals = {'a': 1, 'b': 2} + locals = {'b': 200, 'c': 300} + bom = b'\xef\xbb\xbf' + self.assertEqual(eval(bom + b'a', globals, locals), 1) + self.assertEqual(eval('"\xe5"', globals), "\xe5") + self.assertRaises(TypeError, eval) + self.assertRaises(TypeError, eval, ()) + self.assertRaises(SyntaxError, eval, bom[:2] + b'a') + + class X: + def __getitem__(self, key): + raise ValueError + self.assertRaises(ValueError, eval, "foo", {}, X()) + + @unittest.skip("TODO: RUSTPYTHON") + def test_general_eval(self): + # Tests that general mappings can be used for the locals argument + + class M: + "Test mapping interface versus possible calls from eval()." + def __getitem__(self, key): + if key == 'a': + return 12 + raise KeyError + def keys(self): + return list('xyz') + + m = M() + g = globals() + self.assertEqual(eval('a', g, m), 12) + self.assertRaises(NameError, eval, 'b', g, m) + self.assertEqual(eval('dir()', g, m), list('xyz')) + self.assertEqual(eval('globals()', g, m), g) + self.assertEqual(eval('locals()', g, m), m) + self.assertRaises(TypeError, eval, 'a', m) + class A: + "Non-mapping" + pass + m = A() + self.assertRaises(TypeError, eval, 'a', g, m) + + # Verify that dict subclasses work as well + class D(dict): + def __getitem__(self, key): + if key == 'a': + return 12 + return dict.__getitem__(self, key) + def keys(self): + return list('xyz') + + d = D() + self.assertEqual(eval('a', g, d), 12) + self.assertRaises(NameError, eval, 'b', g, d) + self.assertEqual(eval('dir()', g, d), list('xyz')) + self.assertEqual(eval('globals()', g, d), g) + self.assertEqual(eval('locals()', g, d), d) + + # Verify locals stores (used by list comps) + eval('[locals() for i in (2,3)]', g, d) + eval('[locals() for i in (2,3)]', g, collections.UserDict()) + + class SpreadSheet: + "Sample application showing nested, calculated lookups." + _cells = {} + def __setitem__(self, key, formula): + self._cells[key] = formula + def __getitem__(self, key): + return eval(self._cells[key], globals(), self) + + ss = SpreadSheet() + ss['a1'] = '5' + ss['a2'] = 'a1*6' + ss['a3'] = 'a2*7' + self.assertEqual(ss['a3'], 210) + + # Verify that dir() catches a non-list returned by eval + # SF bug #1004669 + class C: + def __getitem__(self, item): + raise KeyError(item) + def keys(self): + return 1 # used to be 'a' but that's no longer an error + self.assertRaises(TypeError, eval, 'dir()', globals(), C()) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_exec(self): + g = {} + exec('z = 1', g) + if '__builtins__' in g: + del g['__builtins__'] + self.assertEqual(g, {'z': 1}) + + exec('z = 1+1', g) + if '__builtins__' in g: + del g['__builtins__'] + self.assertEqual(g, {'z': 2}) + g = {} + l = {} + + with check_warnings(): + warnings.filterwarnings("ignore", "global statement", + module="") + exec('global a; a = 1; b = 2', g, l) + if '__builtins__' in g: + del g['__builtins__'] + if '__builtins__' in l: + del l['__builtins__'] + self.assertEqual((g, l), ({'a': 1}, {'b': 2})) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_exec_globals(self): + code = compile("print('Hello World!')", "", "exec") + # no builtin function + self.assertRaisesRegex(NameError, "name 'print' is not defined", + exec, code, {'__builtins__': {}}) + # __builtins__ must be a mapping type + self.assertRaises(TypeError, + exec, code, {'__builtins__': 123}) + + # no __build_class__ function + code = compile("class A: pass", "", "exec") + self.assertRaisesRegex(NameError, "__build_class__ not found", + exec, code, {'__builtins__': {}}) + + class frozendict_error(Exception): + pass + + class frozendict(dict): + def __setitem__(self, key, value): + raise frozendict_error("frozendict is readonly") + + # read-only builtins + if isinstance(__builtins__, types.ModuleType): + frozen_builtins = frozendict(__builtins__.__dict__) + else: + frozen_builtins = frozendict(__builtins__) + code = compile("__builtins__['superglobal']=2; print(superglobal)", "test", "exec") + self.assertRaises(frozendict_error, + exec, code, {'__builtins__': frozen_builtins}) + + # read-only globals + namespace = frozendict({}) + code = compile("x=1", "test", "exec") + self.assertRaises(frozendict_error, + exec, code, namespace) + + def test_exec_redirected(self): + savestdout = sys.stdout + sys.stdout = None # Whatever that cannot flush() + try: + # Used to raise SystemError('error return without exception set') + exec('a') + except NameError: + pass + finally: + sys.stdout = savestdout + + def test_filter(self): + self.assertEqual(list(filter(lambda c: 'a' <= c <= 'z', 'Hello World')), list('elloorld')) + self.assertEqual(list(filter(None, [1, 'hello', [], [3], '', None, 9, 0])), [1, 'hello', [3], 9]) + self.assertEqual(list(filter(lambda x: x > 0, [1, -3, 9, 0, 2])), [1, 9, 2]) + self.assertEqual(list(filter(None, Squares(10))), [1, 4, 9, 16, 25, 36, 49, 64, 81]) + self.assertEqual(list(filter(lambda x: x%2, Squares(10))), [1, 9, 25, 49, 81]) + def identity(item): + return 1 + filter(identity, Squares(5)) + self.assertRaises(TypeError, filter) + class BadSeq(object): + def __getitem__(self, index): + if index<4: + return 42 + raise ValueError + self.assertRaises(ValueError, list, filter(lambda x: x, BadSeq())) + def badfunc(): + pass + self.assertRaises(TypeError, list, filter(badfunc, range(5))) + + # test bltinmodule.c::filtertuple() + self.assertEqual(list(filter(None, (1, 2))), [1, 2]) + self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4]) + self.assertRaises(TypeError, list, filter(42, (1, 2))) + + @unittest.skip("TODO: RUSTPYTHON") + def test_filter_pickle(self): + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + f1 = filter(filter_char, "abcdeabcde") + f2 = filter(filter_char, "abcdeabcde") + self.check_iter_pickle(f1, list(f2), proto) + + def test_getattr(self): + self.assertTrue(getattr(sys, 'stdout') is sys.stdout) + self.assertRaises(TypeError, getattr, sys, 1) + self.assertRaises(TypeError, getattr, sys, 1, "foo") + self.assertRaises(TypeError, getattr) + self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode)) + # unicode surrogates are not encodable to the default encoding (utf8) + self.assertRaises(AttributeError, getattr, 1, "\uDAD1\uD51E") + + def test_hasattr(self): + self.assertTrue(hasattr(sys, 'stdout')) + self.assertRaises(TypeError, hasattr, sys, 1) + self.assertRaises(TypeError, hasattr) + self.assertEqual(False, hasattr(sys, chr(sys.maxunicode))) + + # Check that hasattr propagates all exceptions outside of + # AttributeError. + class A: + def __getattr__(self, what): + raise SystemExit + self.assertRaises(SystemExit, hasattr, A(), "b") + class B: + def __getattr__(self, what): + raise ValueError + self.assertRaises(ValueError, hasattr, B(), "b") + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_hash(self): + hash(None) + self.assertEqual(hash(1), hash(1)) + self.assertEqual(hash(1), hash(1.0)) + hash('spam') + self.assertEqual(hash('spam'), hash(b'spam')) + hash((0,1,2,3)) + def f(): pass + self.assertRaises(TypeError, hash, []) + self.assertRaises(TypeError, hash, {}) + # Bug 1536021: Allow hash to return long objects + class X: + def __hash__(self): + return 2**100 + self.assertEqual(type(hash(X())), int) + class Z(int): + def __hash__(self): + return self + self.assertEqual(hash(Z(42)), hash(42)) + + def test_hex(self): + self.assertEqual(hex(16), '0x10') + self.assertEqual(hex(-16), '-0x10') + self.assertRaises(TypeError, hex, {}) + + def test_id(self): + id(None) + id(1) + id(1.0) + id('spam') + id((0,1,2,3)) + id([0,1,2,3]) + id({'spam': 1, 'eggs': 2, 'ham': 3}) + + # Test input() later, alphabetized as if it were raw_input + + def test_iter(self): + self.assertRaises(TypeError, iter) + self.assertRaises(TypeError, iter, 42, 42) + lists = [("1", "2"), ["1", "2"], "12"] + for l in lists: + i = iter(l) + self.assertEqual(next(i), '1') + self.assertEqual(next(i), '2') + self.assertRaises(StopIteration, next, i) + + def test_isinstance(self): + class C: + pass + class D(C): + pass + class E: + pass + c = C() + d = D() + e = E() + self.assertTrue(isinstance(c, C)) + self.assertTrue(isinstance(d, C)) + self.assertTrue(not isinstance(e, C)) + self.assertTrue(not isinstance(c, D)) + self.assertTrue(not isinstance('foo', E)) + self.assertRaises(TypeError, isinstance, E, 'foo') + self.assertRaises(TypeError, isinstance) + + def test_issubclass(self): + class C: + pass + class D(C): + pass + class E: + pass + c = C() + d = D() + e = E() + self.assertTrue(issubclass(D, C)) + self.assertTrue(issubclass(C, C)) + self.assertTrue(not issubclass(C, D)) + self.assertRaises(TypeError, issubclass, 'foo', E) + self.assertRaises(TypeError, issubclass, E, 'foo') + self.assertRaises(TypeError, issubclass) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_len(self): + self.assertEqual(len('123'), 3) + self.assertEqual(len(()), 0) + self.assertEqual(len((1, 2, 3, 4)), 4) + self.assertEqual(len([1, 2, 3, 4]), 4) + self.assertEqual(len({}), 0) + self.assertEqual(len({'a':1, 'b': 2}), 2) + class BadSeq: + def __len__(self): + raise ValueError + self.assertRaises(ValueError, len, BadSeq()) + class InvalidLen: + def __len__(self): + return None + self.assertRaises(TypeError, len, InvalidLen()) + class FloatLen: + def __len__(self): + return 4.5 + self.assertRaises(TypeError, len, FloatLen()) + class NegativeLen: + def __len__(self): + return -10 + self.assertRaises(ValueError, len, NegativeLen()) + class HugeLen: + def __len__(self): + return sys.maxsize + 1 + self.assertRaises(OverflowError, len, HugeLen()) + class HugeNegativeLen: + def __len__(self): + return -sys.maxsize-10 + self.assertRaises(ValueError, len, HugeNegativeLen()) + class NoLenMethod(object): pass + self.assertRaises(TypeError, len, NoLenMethod()) + + def test_map(self): + self.assertEqual( + list(map(lambda x: x*x, range(1,4))), + [1, 4, 9] + ) + try: + from math import sqrt + except ImportError: + def sqrt(x): + return pow(x, 0.5) + self.assertEqual( + list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])), + [[4.0, 2.0], [9.0, 3.0]] + ) + self.assertEqual( + list(map(lambda x, y: x+y, [1,3,2], [9,1,4])), + [10, 4, 6] + ) + + def plus(*v): + accu = 0 + for i in v: accu = accu + i + return accu + self.assertEqual( + list(map(plus, [1, 3, 7])), + [1, 3, 7] + ) + self.assertEqual( + list(map(plus, [1, 3, 7], [4, 9, 2])), + [1+4, 3+9, 7+2] + ) + self.assertEqual( + list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])), + [1+4+1, 3+9+1, 7+2+0] + ) + self.assertEqual( + list(map(int, Squares(10))), + [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + ) + def Max(a, b): + if a is None: + return b + if b is None: + return a + return max(a, b) + self.assertEqual( + list(map(Max, Squares(3), Squares(2))), + [0, 1] + ) + self.assertRaises(TypeError, map) + self.assertRaises(TypeError, map, lambda x: x, 42) + class BadSeq: + def __iter__(self): + raise ValueError + yield None + self.assertRaises(ValueError, list, map(lambda x: x, BadSeq())) + def badfunc(x): + raise RuntimeError + self.assertRaises(RuntimeError, list, map(badfunc, range(5))) + + @unittest.skip("TODO: RUSTPYTHON") + def test_map_pickle(self): + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + m1 = map(map_char, "Is this the real life?") + m2 = map(map_char, "Is this the real life?") + self.check_iter_pickle(m1, list(m2), proto) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_max(self): + self.assertEqual(max('123123'), '3') + self.assertEqual(max(1, 2, 3), 3) + self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3) + self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3) + + self.assertEqual(max(1, 2, 3.0), 3.0) + self.assertEqual(max(1, 2.0, 3), 3) + self.assertEqual(max(1.0, 2, 3), 3) + + self.assertRaises(TypeError, max) + self.assertRaises(TypeError, max, 42) + self.assertRaises(ValueError, max, ()) + class BadSeq: + def __getitem__(self, index): + raise ValueError + self.assertRaises(ValueError, max, BadSeq()) + + for stmt in ( + "max(key=int)", # no args + "max(default=None)", + "max(1, 2, default=None)", # require container for default + "max(default=None, key=int)", + "max(1, key=int)", # single arg not iterable + "max(1, 2, keystone=int)", # wrong keyword + "max(1, 2, key=int, abc=int)", # two many keywords + "max(1, 2, key=1)", # keyfunc is not callable + ): + try: + exec(stmt, globals()) + except TypeError: + pass + else: + self.fail(stmt) + + self.assertEqual(max((1,), key=neg), 1) # one elem iterable + self.assertEqual(max((1,2), key=neg), 1) # two elem iterable + self.assertEqual(max(1, 2, key=neg), 1) # two elems + + self.assertEqual(max((), default=None), None) # zero elem iterable + self.assertEqual(max((1,), default=None), 1) # one elem iterable + self.assertEqual(max((1,2), default=None), 2) # two elem iterable + + self.assertEqual(max((), default=1, key=neg), 1) + self.assertEqual(max((1, 2), default=3, key=neg), 1) + + self.assertEqual(max((1, 2), key=None), 2) + + data = [random.randrange(200) for i in range(100)] + keys = dict((elem, random.randrange(50)) for elem in data) + f = keys.__getitem__ + self.assertEqual(max(data, key=f), + sorted(reversed(data), key=f)[-1]) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_min(self): + self.assertEqual(min('123123'), '1') + self.assertEqual(min(1, 2, 3), 1) + self.assertEqual(min((1, 2, 3, 1, 2, 3)), 1) + self.assertEqual(min([1, 2, 3, 1, 2, 3]), 1) + + self.assertEqual(min(1, 2, 3.0), 1) + self.assertEqual(min(1, 2.0, 3), 1) + self.assertEqual(min(1.0, 2, 3), 1.0) + + self.assertRaises(TypeError, min) + self.assertRaises(TypeError, min, 42) + self.assertRaises(ValueError, min, ()) + class BadSeq: + def __getitem__(self, index): + raise ValueError + self.assertRaises(ValueError, min, BadSeq()) + + for stmt in ( + "min(key=int)", # no args + "min(default=None)", + "min(1, 2, default=None)", # require container for default + "min(default=None, key=int)", + "min(1, key=int)", # single arg not iterable + "min(1, 2, keystone=int)", # wrong keyword + "min(1, 2, key=int, abc=int)", # two many keywords + "min(1, 2, key=1)", # keyfunc is not callable + ): + try: + exec(stmt, globals()) + except TypeError: + pass + else: + self.fail(stmt) + + self.assertEqual(min((1,), key=neg), 1) # one elem iterable + self.assertEqual(min((1,2), key=neg), 2) # two elem iterable + self.assertEqual(min(1, 2, key=neg), 2) # two elems + + self.assertEqual(min((), default=None), None) # zero elem iterable + self.assertEqual(min((1,), default=None), 1) # one elem iterable + self.assertEqual(min((1,2), default=None), 1) # two elem iterable + + self.assertEqual(min((), default=1, key=neg), 1) + self.assertEqual(min((1, 2), default=1, key=neg), 2) + + self.assertEqual(min((1, 2), key=None), 1) + + data = [random.randrange(200) for i in range(100)] + keys = dict((elem, random.randrange(50)) for elem in data) + f = keys.__getitem__ + self.assertEqual(min(data, key=f), + sorted(data, key=f)[0]) + + def test_next(self): + it = iter(range(2)) + self.assertEqual(next(it), 0) + self.assertEqual(next(it), 1) + self.assertRaises(StopIteration, next, it) + self.assertRaises(StopIteration, next, it) + self.assertEqual(next(it, 42), 42) + + class Iter(object): + def __iter__(self): + return self + def __next__(self): + raise StopIteration + + it = iter(Iter()) + self.assertEqual(next(it, 42), 42) + self.assertRaises(StopIteration, next, it) + + def gen(): + yield 1 + return + + it = gen() + self.assertEqual(next(it), 1) + self.assertRaises(StopIteration, next, it) + self.assertEqual(next(it, 42), 42) + + def test_oct(self): + self.assertEqual(oct(100), '0o144') + self.assertEqual(oct(-100), '-0o144') + self.assertRaises(TypeError, oct, ()) + + def write_testfile(self): + # NB the first 4 lines are also used to test input, below + fp = open(TESTFN, 'w') + self.addCleanup(unlink, TESTFN) + with fp: + fp.write('1+1\n') + fp.write('The quick brown fox jumps over the lazy dog') + fp.write('.\n') + fp.write('Dear John\n') + fp.write('XXX'*100) + fp.write('YYY'*100) + + @unittest.skip("TODO: RUSTPYTHON") + def test_open(self): + self.write_testfile() + fp = open(TESTFN, 'r') + with fp: + self.assertEqual(fp.readline(4), '1+1\n') + self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n') + self.assertEqual(fp.readline(4), 'Dear') + self.assertEqual(fp.readline(100), ' John\n') + self.assertEqual(fp.read(300), 'XXX'*100) + self.assertEqual(fp.read(1000), 'YYY'*100) + + # embedded null bytes and characters + self.assertRaises(ValueError, open, 'a\x00b') + self.assertRaises(ValueError, open, b'a\x00b') + + @unittest.skip("TODO: RUSTPYTHON") + @unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled") + def test_open_default_encoding(self): + old_environ = dict(os.environ) + try: + # try to get a user preferred encoding different than the current + # locale encoding to check that open() uses the current locale + # encoding and not the user preferred encoding + for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): + if key in os.environ: + del os.environ[key] + + self.write_testfile() + current_locale_encoding = locale.getpreferredencoding(False) + fp = open(TESTFN, 'w') + with fp: + self.assertEqual(fp.encoding, current_locale_encoding) + finally: + os.environ.clear() + os.environ.update(old_environ) + + @unittest.skip("TODO: RUSTPYTHON") + def test_open_non_inheritable(self): + fileobj = open(__file__) + with fileobj: + self.assertFalse(os.get_inheritable(fileobj.fileno())) + + def test_ord(self): + self.assertEqual(ord(' '), 32) + self.assertEqual(ord('A'), 65) + self.assertEqual(ord('a'), 97) + self.assertEqual(ord('\x80'), 128) + self.assertEqual(ord('\xff'), 255) + + self.assertEqual(ord(b' '), 32) + self.assertEqual(ord(b'A'), 65) + self.assertEqual(ord(b'a'), 97) + self.assertEqual(ord(b'\x80'), 128) + self.assertEqual(ord(b'\xff'), 255) + + self.assertEqual(ord(chr(sys.maxunicode)), sys.maxunicode) + self.assertRaises(TypeError, ord, 42) + + self.assertEqual(ord(chr(0x10FFFF)), 0x10FFFF) + self.assertEqual(ord("\U0000FFFF"), 0x0000FFFF) + self.assertEqual(ord("\U00010000"), 0x00010000) + self.assertEqual(ord("\U00010001"), 0x00010001) + self.assertEqual(ord("\U000FFFFE"), 0x000FFFFE) + self.assertEqual(ord("\U000FFFFF"), 0x000FFFFF) + self.assertEqual(ord("\U00100000"), 0x00100000) + self.assertEqual(ord("\U00100001"), 0x00100001) + self.assertEqual(ord("\U0010FFFE"), 0x0010FFFE) + self.assertEqual(ord("\U0010FFFF"), 0x0010FFFF) + + @unittest.skip("TODO: RUSTPYTHON") + def test_pow(self): + self.assertEqual(pow(0,0), 1) + self.assertEqual(pow(0,1), 0) + self.assertEqual(pow(1,0), 1) + self.assertEqual(pow(1,1), 1) + + self.assertEqual(pow(2,0), 1) + self.assertEqual(pow(2,10), 1024) + self.assertEqual(pow(2,20), 1024*1024) + self.assertEqual(pow(2,30), 1024*1024*1024) + + self.assertEqual(pow(-2,0), 1) + self.assertEqual(pow(-2,1), -2) + self.assertEqual(pow(-2,2), 4) + self.assertEqual(pow(-2,3), -8) + + self.assertAlmostEqual(pow(0.,0), 1.) + self.assertAlmostEqual(pow(0.,1), 0.) + self.assertAlmostEqual(pow(1.,0), 1.) + self.assertAlmostEqual(pow(1.,1), 1.) + + self.assertAlmostEqual(pow(2.,0), 1.) + self.assertAlmostEqual(pow(2.,10), 1024.) + self.assertAlmostEqual(pow(2.,20), 1024.*1024.) + self.assertAlmostEqual(pow(2.,30), 1024.*1024.*1024.) + + self.assertAlmostEqual(pow(-2.,0), 1.) + self.assertAlmostEqual(pow(-2.,1), -2.) + self.assertAlmostEqual(pow(-2.,2), 4.) + self.assertAlmostEqual(pow(-2.,3), -8.) + + for x in 2, 2.0: + for y in 10, 10.0: + for z in 1000, 1000.0: + if isinstance(x, float) or \ + isinstance(y, float) or \ + isinstance(z, float): + self.assertRaises(TypeError, pow, x, y, z) + else: + self.assertAlmostEqual(pow(x, y, z), 24.0) + + self.assertAlmostEqual(pow(-1, 0.5), 1j) + self.assertAlmostEqual(pow(-1, 1/3), 0.5 + 0.8660254037844386j) + + self.assertRaises(ValueError, pow, -1, -2, 3) + self.assertRaises(ValueError, pow, 1, 2, 0) + + self.assertRaises(TypeError, pow) + + @unittest.skip("TODO: RUSTPYTHON") + def test_input(self): + self.write_testfile() + fp = open(TESTFN, 'r') + savestdin = sys.stdin + savestdout = sys.stdout # Eats the echo + try: + sys.stdin = fp + sys.stdout = BitBucket() + self.assertEqual(input(), "1+1") + self.assertEqual(input(), 'The quick brown fox jumps over the lazy dog.') + self.assertEqual(input('testing\n'), 'Dear John') + + # SF 1535165: don't segfault on closed stdin + # sys.stdout must be a regular file for triggering + sys.stdout = savestdout + sys.stdin.close() + self.assertRaises(ValueError, input) + + sys.stdout = BitBucket() + sys.stdin = io.StringIO("NULL\0") + self.assertRaises(TypeError, input, 42, 42) + sys.stdin = io.StringIO(" 'whitespace'") + self.assertEqual(input(), " 'whitespace'") + sys.stdin = io.StringIO() + self.assertRaises(EOFError, input) + + del sys.stdout + self.assertRaises(RuntimeError, input, 'prompt') + del sys.stdin + self.assertRaises(RuntimeError, input, 'prompt') + finally: + sys.stdin = savestdin + sys.stdout = savestdout + fp.close() + + # test_int(): see test_int.py for tests of built-in function int(). + + def test_repr(self): + self.assertEqual(repr(''), '\'\'') + self.assertEqual(repr(0), '0') + self.assertEqual(repr(()), '()') + self.assertEqual(repr([]), '[]') + self.assertEqual(repr({}), '{}') + a = [] + a.append(a) + self.assertEqual(repr(a), '[[...]]') + a = {} + a[0] = a + self.assertEqual(repr(a), '{0: {...}}') + + @unittest.skip("TODO: RUSTPYTHON") + def test_round(self): + self.assertEqual(round(0.0), 0.0) + self.assertEqual(type(round(0.0)), int) + self.assertEqual(round(1.0), 1.0) + self.assertEqual(round(10.0), 10.0) + self.assertEqual(round(1000000000.0), 1000000000.0) + self.assertEqual(round(1e20), 1e20) + + self.assertEqual(round(-1.0), -1.0) + self.assertEqual(round(-10.0), -10.0) + self.assertEqual(round(-1000000000.0), -1000000000.0) + self.assertEqual(round(-1e20), -1e20) + + self.assertEqual(round(0.1), 0.0) + self.assertEqual(round(1.1), 1.0) + self.assertEqual(round(10.1), 10.0) + self.assertEqual(round(1000000000.1), 1000000000.0) + + self.assertEqual(round(-1.1), -1.0) + self.assertEqual(round(-10.1), -10.0) + self.assertEqual(round(-1000000000.1), -1000000000.0) + + self.assertEqual(round(0.9), 1.0) + self.assertEqual(round(9.9), 10.0) + self.assertEqual(round(999999999.9), 1000000000.0) + + self.assertEqual(round(-0.9), -1.0) + self.assertEqual(round(-9.9), -10.0) + self.assertEqual(round(-999999999.9), -1000000000.0) + + self.assertEqual(round(-8.0, -1), -10.0) + self.assertEqual(type(round(-8.0, -1)), float) + + self.assertEqual(type(round(-8.0, 0)), float) + self.assertEqual(type(round(-8.0, 1)), float) + + # Check even / odd rounding behaviour + self.assertEqual(round(5.5), 6) + self.assertEqual(round(6.5), 6) + self.assertEqual(round(-5.5), -6) + self.assertEqual(round(-6.5), -6) + + # Check behavior on ints + self.assertEqual(round(0), 0) + self.assertEqual(round(8), 8) + self.assertEqual(round(-8), -8) + self.assertEqual(type(round(0)), int) + self.assertEqual(type(round(-8, -1)), int) + self.assertEqual(type(round(-8, 0)), int) + self.assertEqual(type(round(-8, 1)), int) + + # test new kwargs + self.assertEqual(round(number=-8.0, ndigits=-1), -10.0) + + self.assertRaises(TypeError, round) + + # test generic rounding delegation for reals + class TestRound: + def __round__(self): + return 23 + + class TestNoRound: + pass + + self.assertEqual(round(TestRound()), 23) + + self.assertRaises(TypeError, round, 1, 2, 3) + self.assertRaises(TypeError, round, TestNoRound()) + + t = TestNoRound() + t.__round__ = lambda *args: args + self.assertRaises(TypeError, round, t) + self.assertRaises(TypeError, round, t, 0) + + # Some versions of glibc for alpha have a bug that affects + # float -> integer rounding (floor, ceil, rint, round) for + # values in the range [2**52, 2**53). See: + # + # http://sources.redhat.com/bugzilla/show_bug.cgi?id=5350 + # + # We skip this test on Linux/alpha if it would fail. + linux_alpha = (platform.system().startswith('Linux') and + platform.machine().startswith('alpha')) + system_round_bug = round(5e15+1) != 5e15+1 + @unittest.skipIf(linux_alpha and system_round_bug, + "test will fail; failure is probably due to a " + "buggy system round function") + def test_round_large(self): + # Issue #1869: integral floats should remain unchanged + self.assertEqual(round(5e15-1), 5e15-1) + self.assertEqual(round(5e15), 5e15) + self.assertEqual(round(5e15+1), 5e15+1) + self.assertEqual(round(5e15+2), 5e15+2) + self.assertEqual(round(5e15+3), 5e15+3) + + @unittest.skip("TODO: RUSTPYTHON") + def test_bug_27936(self): + # Verify that ndigits=None means the same as passing in no argument + for x in [1234, + 1234.56, + decimal.Decimal('1234.56'), + fractions.Fraction(123456, 100)]: + self.assertEqual(round(x, None), round(x)) + self.assertEqual(type(round(x, None)), type(round(x))) + + def test_setattr(self): + setattr(sys, 'spam', 1) + self.assertEqual(sys.spam, 1) + self.assertRaises(TypeError, setattr, sys, 1, 'spam') + self.assertRaises(TypeError, setattr) + + # test_str(): see test_unicode.py and test_bytes.py for str() tests. + + @unittest.skip("TODO: RUSTPYTHON") + def test_sum(self): + self.assertEqual(sum([]), 0) + self.assertEqual(sum(list(range(2,8))), 27) + self.assertEqual(sum(iter(list(range(2,8)))), 27) + self.assertEqual(sum(Squares(10)), 285) + self.assertEqual(sum(iter(Squares(10))), 285) + self.assertEqual(sum([[1], [2], [3]], []), [1, 2, 3]) + + self.assertEqual(sum(range(10), 1000), 1045) + self.assertEqual(sum(range(10), start=1000), 1045) + + self.assertRaises(TypeError, sum) + self.assertRaises(TypeError, sum, 42) + self.assertRaises(TypeError, sum, ['a', 'b', 'c']) + self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '') + self.assertRaises(TypeError, sum, [b'a', b'c'], b'') + values = [bytearray(b'a'), bytearray(b'b')] + self.assertRaises(TypeError, sum, values, bytearray(b'')) + self.assertRaises(TypeError, sum, [[1], [2], [3]]) + self.assertRaises(TypeError, sum, [{2:3}]) + self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3}) + + class BadSeq: + def __getitem__(self, index): + raise ValueError + self.assertRaises(ValueError, sum, BadSeq()) + + empty = [] + sum(([x] for x in range(10)), empty) + self.assertEqual(empty, []) + + def test_type(self): + self.assertEqual(type(''), type('123')) + self.assertNotEqual(type(''), type(())) + + # We don't want self in vars(), so these are static methods + + @staticmethod + def get_vars_f0(): + return vars() + + @staticmethod + def get_vars_f2(): + BuiltinTest.get_vars_f0() + a = 1 + b = 2 + return vars() + + class C_get_vars(object): + def getDict(self): + return {'a':2} + __dict__ = property(fget=getDict) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_vars(self): + self.assertEqual(set(vars()), set(dir())) + self.assertEqual(set(vars(sys)), set(dir(sys))) + self.assertEqual(self.get_vars_f0(), {}) + self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2}) + self.assertRaises(TypeError, vars, 42, 42) + self.assertRaises(TypeError, vars, 42) + self.assertEqual(vars(self.C_get_vars()), {'a':2}) + + def test_zip(self): + a = (1, 2, 3) + b = (4, 5, 6) + t = [(1, 4), (2, 5), (3, 6)] + self.assertEqual(list(zip(a, b)), t) + b = [4, 5, 6] + self.assertEqual(list(zip(a, b)), t) + b = (4, 5, 6, 7) + self.assertEqual(list(zip(a, b)), t) + class I: + def __getitem__(self, i): + if i < 0 or i > 2: raise IndexError + return i + 4 + self.assertEqual(list(zip(a, I())), t) + self.assertEqual(list(zip()), []) + self.assertEqual(list(zip(*[])), []) + self.assertRaises(TypeError, zip, None) + class G: + pass + self.assertRaises(TypeError, zip, a, G()) + self.assertRaises(RuntimeError, zip, a, TestFailingIter()) + + # Make sure zip doesn't try to allocate a billion elements for the + # result list when one of its arguments doesn't say how long it is. + # A MemoryError is the most likely failure mode. + class SequenceWithoutALength: + def __getitem__(self, i): + if i == 5: + raise IndexError + else: + return i + self.assertEqual( + list(zip(SequenceWithoutALength(), range(2**30))), + list(enumerate(range(5))) + ) + + class BadSeq: + def __getitem__(self, i): + if i == 5: + raise ValueError + else: + return i + self.assertRaises(ValueError, list, zip(BadSeq(), BadSeq())) + + @unittest.skip("TODO: RUSTPYTHON") + def test_zip_pickle(self): + a = (1, 2, 3) + b = (4, 5, 6) + t = [(1, 4), (2, 5), (3, 6)] + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + z1 = zip(a, b) + self.check_iter_pickle(z1, t, proto) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_format(self): + # Test the basic machinery of the format() builtin. Don't test + # the specifics of the various formatters + self.assertEqual(format(3, ''), '3') + + # Returns some classes to use for various tests. There's + # an old-style version, and a new-style version + def classes_new(): + class A(object): + def __init__(self, x): + self.x = x + def __format__(self, format_spec): + return str(self.x) + format_spec + class DerivedFromA(A): + pass + + class Simple(object): pass + class DerivedFromSimple(Simple): + def __init__(self, x): + self.x = x + def __format__(self, format_spec): + return str(self.x) + format_spec + class DerivedFromSimple2(DerivedFromSimple): pass + return A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2 + + def class_test(A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2): + self.assertEqual(format(A(3), 'spec'), '3spec') + self.assertEqual(format(DerivedFromA(4), 'spec'), '4spec') + self.assertEqual(format(DerivedFromSimple(5), 'abc'), '5abc') + self.assertEqual(format(DerivedFromSimple2(10), 'abcdef'), + '10abcdef') + + class_test(*classes_new()) + + def empty_format_spec(value): + # test that: + # format(x, '') == str(x) + # format(x) == str(x) + self.assertEqual(format(value, ""), str(value)) + self.assertEqual(format(value), str(value)) + + # for builtin types, format(x, "") == str(x) + empty_format_spec(17**13) + empty_format_spec(1.0) + empty_format_spec(3.1415e104) + empty_format_spec(-3.1415e104) + empty_format_spec(3.1415e-104) + empty_format_spec(-3.1415e-104) + empty_format_spec(object) + empty_format_spec(None) + + # TypeError because self.__format__ returns the wrong type + class BadFormatResult: + def __format__(self, format_spec): + return 1.0 + self.assertRaises(TypeError, format, BadFormatResult(), "") + + # TypeError because format_spec is not unicode or str + self.assertRaises(TypeError, format, object(), 4) + self.assertRaises(TypeError, format, object(), object()) + + # tests for object.__format__ really belong elsewhere, but + # there's no good place to put them + x = object().__format__('') + self.assertTrue(x.startswith(' the child exited + break + lines.append(line) + # Check the result was got and corresponds to the user's terminal input + if len(lines) != 2: + # Something went wrong, try to get at stderr + # Beware of Linux raising EIO when the slave is closed + child_output = bytearray() + while True: + try: + chunk = os.read(fd, 3000) + except OSError: # Assume EIO + break + if not chunk: + break + child_output.extend(chunk) + os.close(fd) + child_output = child_output.decode("ascii", "ignore") + self.fail("got %d lines in pipe but expected 2, child output was:\n%s" + % (len(lines), child_output)) + os.close(fd) + + # Wait until the child process completes + os.waitpid(pid, 0) + + return lines + + def check_input_tty(self, prompt, terminal_input, stdio_encoding=None): + if not sys.stdin.isatty() or not sys.stdout.isatty(): + self.skipTest("stdin and stdout must be ttys") + def child(wpipe): + # Check the error handlers are accounted for + if stdio_encoding: + sys.stdin = io.TextIOWrapper(sys.stdin.detach(), + encoding=stdio_encoding, + errors='surrogateescape') + sys.stdout = io.TextIOWrapper(sys.stdout.detach(), + encoding=stdio_encoding, + errors='replace') + print("tty =", sys.stdin.isatty() and sys.stdout.isatty(), file=wpipe) + print(ascii(input(prompt)), file=wpipe) + lines = self.run_child(child, terminal_input + b"\r\n") + # Check we did exercise the GNU readline path + self.assertIn(lines[0], {'tty = True', 'tty = False'}) + if lines[0] != 'tty = True': + self.skipTest("standard IO in should have been a tty") + input_result = eval(lines[1]) # ascii() -> eval() roundtrip + if stdio_encoding: + expected = terminal_input.decode(stdio_encoding, 'surrogateescape') + else: + expected = terminal_input.decode(sys.stdin.encoding) # what else? + self.assertEqual(input_result, expected) + + def test_input_tty(self): + # Test input() functionality when wired to a tty (the code path + # is different and invokes GNU readline if available). + self.check_input_tty("prompt", b"quux") + + def test_input_tty_non_ascii(self): + # Check stdin/stdout encoding is used when invoking GNU readline + self.check_input_tty("prompté", b"quux\xe9", "utf-8") + + def test_input_tty_non_ascii_unicode_errors(self): + # Check stdin/stdout error handler is used when invoking GNU readline + self.check_input_tty("prompté", b"quux\xe9", "ascii") + + def test_input_no_stdout_fileno(self): + # Issue #24402: If stdin is the original terminal but stdout.fileno() + # fails, do not use the original stdout file descriptor + def child(wpipe): + print("stdin.isatty():", sys.stdin.isatty(), file=wpipe) + sys.stdout = io.StringIO() # Does not support fileno() + input("prompt") + print("captured:", ascii(sys.stdout.getvalue()), file=wpipe) + lines = self.run_child(child, b"quux\r") + expected = ( + "stdin.isatty(): True", + "captured: 'prompt'", + ) + self.assertSequenceEqual(lines, expected) + +class TestSorted(unittest.TestCase): + + def test_basic(self): + data = list(range(100)) + copy = data[:] + random.shuffle(copy) + self.assertEqual(data, sorted(copy)) + self.assertNotEqual(data, copy) + + data.reverse() + random.shuffle(copy) + self.assertEqual(data, sorted(copy, key=lambda x: -x)) + self.assertNotEqual(data, copy) + random.shuffle(copy) + self.assertEqual(data, sorted(copy, reverse=1)) + self.assertNotEqual(data, copy) + + def test_bad_arguments(self): + # Issue #29327: The first argument is positional-only. + sorted([]) + with self.assertRaises(TypeError): + sorted(iterable=[]) + # Other arguments are keyword-only + sorted([], key=None) + with self.assertRaises(TypeError): + sorted([], None) + + def test_inputtypes(self): + s = 'abracadabra' + types = [list, tuple, str] + for T in types: + self.assertEqual(sorted(s), sorted(T(s))) + + s = ''.join(set(s)) # unique letters only + types = [str, set, frozenset, list, tuple, dict.fromkeys] + for T in types: + self.assertEqual(sorted(s), sorted(T(s))) + + def test_baddecorator(self): + data = 'The quick Brown fox Jumped over The lazy Dog'.split() + self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0) + + +class ShutdownTest(unittest.TestCase): + + @unittest.skip("TODO: RUSTPYTHON") + def test_cleanup(self): + # Issue #19255: builtins are still available at shutdown + code = """if 1: + import builtins + import sys + + class C: + def __del__(self): + print("before") + # Check that builtins still exist + len(()) + print("after") + + c = C() + # Make this module survive until builtins and sys are cleaned + builtins.here = sys.modules[__name__] + sys.here = sys.modules[__name__] + # Create a reference loop so that this module needs to go + # through a GC phase. + here = sys.modules[__name__] + """ + # Issue #20599: Force ASCII encoding to get a codec implemented in C, + # otherwise the codec may be unloaded before C.__del__() is called, and + # so print("before") fails because the codec cannot be used to encode + # "before" to sys.stdout.encoding. For example, on Windows, + # sys.stdout.encoding is the OEM code page and these code pages are + # implemented in Python + rc, out, err = assert_python_ok("-c", code, + PYTHONIOENCODING="ascii") + self.assertEqual(["before", "after"], out.decode().splitlines()) + + +class TestType(unittest.TestCase): + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_new_type(self): + A = type('A', (), {}) + self.assertEqual(A.__name__, 'A') + self.assertEqual(A.__qualname__, 'A') + self.assertEqual(A.__module__, __name__) + self.assertEqual(A.__bases__, (object,)) + self.assertIs(A.__base__, object) + x = A() + self.assertIs(type(x), A) + self.assertIs(x.__class__, A) + + class B: + def ham(self): + return 'ham%d' % self + C = type('C', (B, int), {'spam': lambda self: 'spam%s' % self}) + self.assertEqual(C.__name__, 'C') + self.assertEqual(C.__qualname__, 'C') + self.assertEqual(C.__module__, __name__) + self.assertEqual(C.__bases__, (B, int)) + self.assertIs(C.__base__, int) + self.assertIn('spam', C.__dict__) + self.assertNotIn('ham', C.__dict__) + x = C(42) + self.assertEqual(x, 42) + self.assertIs(type(x), C) + self.assertIs(x.__class__, C) + self.assertEqual(x.ham(), 'ham42') + self.assertEqual(x.spam(), 'spam42') + self.assertEqual(x.to_bytes(2, 'little'), b'\x2a\x00') + + def test_type_nokwargs(self): + with self.assertRaises(TypeError): + type('a', (), {}, x=5) + with self.assertRaises(TypeError): + type('a', (), dict={}) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_type_name(self): + for name in 'A', '\xc4', '\U0001f40d', 'B.A', '42', '': + with self.subTest(name=name): + A = type(name, (), {}) + self.assertEqual(A.__name__, name) + self.assertEqual(A.__qualname__, name) + self.assertEqual(A.__module__, __name__) + with self.assertRaises(ValueError): + type('A\x00B', (), {}) + with self.assertRaises(ValueError): + type('A\udcdcB', (), {}) + with self.assertRaises(TypeError): + type(b'A', (), {}) + + C = type('C', (), {}) + for name in 'A', '\xc4', '\U0001f40d', 'B.A', '42', '': + with self.subTest(name=name): + C.__name__ = name + self.assertEqual(C.__name__, name) + self.assertEqual(C.__qualname__, 'C') + self.assertEqual(C.__module__, __name__) + + A = type('C', (), {}) + with self.assertRaises(ValueError): + A.__name__ = 'A\x00B' + self.assertEqual(A.__name__, 'C') + with self.assertRaises(ValueError): + A.__name__ = 'A\udcdcB' + self.assertEqual(A.__name__, 'C') + with self.assertRaises(TypeError): + A.__name__ = b'A' + self.assertEqual(A.__name__, 'C') + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_type_qualname(self): + A = type('A', (), {'__qualname__': 'B.C'}) + self.assertEqual(A.__name__, 'A') + self.assertEqual(A.__qualname__, 'B.C') + self.assertEqual(A.__module__, __name__) + with self.assertRaises(TypeError): + type('A', (), {'__qualname__': b'B'}) + self.assertEqual(A.__qualname__, 'B.C') + + A.__qualname__ = 'D.E' + self.assertEqual(A.__name__, 'A') + self.assertEqual(A.__qualname__, 'D.E') + with self.assertRaises(TypeError): + A.__qualname__ = b'B' + self.assertEqual(A.__qualname__, 'D.E') + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_type_doc(self): + for doc in 'x', '\xc4', '\U0001f40d', 'x\x00y', b'x', 42, None: + A = type('A', (), {'__doc__': doc}) + self.assertEqual(A.__doc__, doc) + with self.assertRaises(UnicodeEncodeError): + type('A', (), {'__doc__': 'x\udcdcy'}) + + A = type('A', (), {}) + self.assertEqual(A.__doc__, None) + for doc in 'x', '\xc4', '\U0001f40d', 'x\x00y', 'x\udcdcy', b'x', 42, None: + A.__doc__ = doc + self.assertEqual(A.__doc__, doc) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_bad_args(self): + with self.assertRaises(TypeError): + type() + with self.assertRaises(TypeError): + type('A', ()) + with self.assertRaises(TypeError): + type('A', (), {}, ()) + with self.assertRaises(TypeError): + type('A', (), dict={}) + with self.assertRaises(TypeError): + type('A', [], {}) + with self.assertRaises(TypeError): + type('A', (), types.MappingProxyType({})) + with self.assertRaises(TypeError): + type('A', (None,), {}) + with self.assertRaises(TypeError): + type('A', (bool,), {}) + with self.assertRaises(TypeError): + type('A', (int, str), {}) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_bad_slots(self): + with self.assertRaises(TypeError): + type('A', (), {'__slots__': b'x'}) + with self.assertRaises(TypeError): + type('A', (int,), {'__slots__': 'x'}) + with self.assertRaises(TypeError): + type('A', (), {'__slots__': ''}) + with self.assertRaises(TypeError): + type('A', (), {'__slots__': '42'}) + with self.assertRaises(TypeError): + type('A', (), {'__slots__': 'x\x00y'}) + with self.assertRaises(ValueError): + type('A', (), {'__slots__': 'x', 'x': 0}) + with self.assertRaises(TypeError): + type('A', (), {'__slots__': ('__dict__', '__dict__')}) + with self.assertRaises(TypeError): + type('A', (), {'__slots__': ('__weakref__', '__weakref__')}) + + class B: + pass + with self.assertRaises(TypeError): + type('A', (B,), {'__slots__': '__dict__'}) + with self.assertRaises(TypeError): + type('A', (B,), {'__slots__': '__weakref__'}) + + @unittest.skip("TODO: RUSTPYTHON") + def test_namespace_order(self): + # bpo-34320: namespace should preserve order + od = collections.OrderedDict([('a', 1), ('b', 2)]) + od.move_to_end('a') + expected = list(od.items()) + + C = type('C', (), od) + self.assertEqual(list(C.__dict__.items())[:2], [('b', 2), ('a', 1)]) + + +def load_tests(loader, tests, pattern): + # XXX RustPython + # from doctest import DocTestSuite + # tests.addTest(DocTestSuite(builtins)) + return tests + +if __name__ == "__main__": + unittest.main() diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py new file mode 100644 index 0000000000..ffb48abf9c --- /dev/null +++ b/Lib/test/test_exceptions.py @@ -0,0 +1,1410 @@ +# Python test set -- part 5, built-in exceptions + +import copy +import os +import sys +import unittest +import pickle +import weakref +import errno + +from test.support import (TESTFN, captured_stderr, check_impl_detail, + check_warnings, cpython_only, gc_collect, run_unittest, + no_tracing, unlink, import_module, script_helper, + SuppressCrashReport) +class NaiveException(Exception): + def __init__(self, x): + self.x = x + +class SlottedNaiveException(Exception): + __slots__ = ('x',) + def __init__(self, x): + self.x = x + +class BrokenStrException(Exception): + def __str__(self): + raise Exception("str() is broken") + +# XXX This is not really enough, each *operation* should be tested! + +class ExceptionTests(unittest.TestCase): + + def raise_catch(self, exc, excname): + try: + raise exc("spam") + except exc as err: + buf1 = str(err) + try: + raise exc("spam") + except exc as err: + buf2 = str(err) + self.assertEqual(buf1, buf2) + self.assertEqual(exc.__name__, excname) + + @unittest.skip("TODO: RUSTPYTHON") + def testRaising(self): + self.raise_catch(AttributeError, "AttributeError") + self.assertRaises(AttributeError, getattr, sys, "undefined_attribute") + + self.raise_catch(EOFError, "EOFError") + fp = open(TESTFN, 'w') + fp.close() + fp = open(TESTFN, 'r') + savestdin = sys.stdin + try: + try: + import marshal + marshal.loads(b'') + except EOFError: + pass + finally: + sys.stdin = savestdin + fp.close() + unlink(TESTFN) + + self.raise_catch(OSError, "OSError") + self.assertRaises(OSError, open, 'this file does not exist', 'r') + + self.raise_catch(ImportError, "ImportError") + self.assertRaises(ImportError, __import__, "undefined_module") + + self.raise_catch(IndexError, "IndexError") + x = [] + self.assertRaises(IndexError, x.__getitem__, 10) + + self.raise_catch(KeyError, "KeyError") + x = {} + self.assertRaises(KeyError, x.__getitem__, 'key') + + self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt") + + self.raise_catch(MemoryError, "MemoryError") + + self.raise_catch(NameError, "NameError") + try: x = undefined_variable + except NameError: pass + + self.raise_catch(OverflowError, "OverflowError") + x = 1 + for dummy in range(128): + x += x # this simply shouldn't blow up + + self.raise_catch(RuntimeError, "RuntimeError") + self.raise_catch(RecursionError, "RecursionError") + + self.raise_catch(SyntaxError, "SyntaxError") + try: exec('/\n') + except SyntaxError: pass + + self.raise_catch(IndentationError, "IndentationError") + + self.raise_catch(TabError, "TabError") + try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", + '', 'exec') + except TabError: pass + else: self.fail("TabError not raised") + + self.raise_catch(SystemError, "SystemError") + + self.raise_catch(SystemExit, "SystemExit") + self.assertRaises(SystemExit, sys.exit, 0) + + self.raise_catch(TypeError, "TypeError") + try: [] + () + except TypeError: pass + + self.raise_catch(ValueError, "ValueError") + self.assertRaises(ValueError, chr, 17<<16) + + self.raise_catch(ZeroDivisionError, "ZeroDivisionError") + try: x = 1/0 + except ZeroDivisionError: pass + + self.raise_catch(Exception, "Exception") + try: x = 1/0 + except Exception as e: pass + + self.raise_catch(StopAsyncIteration, "StopAsyncIteration") + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def testSyntaxErrorMessage(self): + # make sure the right exception message is raised for each of + # these code fragments + + def ckmsg(src, msg): + try: + compile(src, '', 'exec') + except SyntaxError as e: + if e.msg != msg: + self.fail("expected %s, got %s" % (msg, e.msg)) + else: + self.fail("failed to get expected SyntaxError") + + s = '''if 1: + try: + continue + except: + pass''' + + ckmsg(s, "'continue' not properly in loop") + ckmsg("continue\n", "'continue' not properly in loop") + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def testSyntaxErrorMissingParens(self): + def ckmsg(src, msg, exception=SyntaxError): + try: + compile(src, '', 'exec') + except exception as e: + if e.msg != msg: + self.fail("expected %s, got %s" % (msg, e.msg)) + else: + self.fail("failed to get expected SyntaxError") + + s = '''print "old style"''' + ckmsg(s, "Missing parentheses in call to 'print'. " + "Did you mean print(\"old style\")?") + + s = '''print "old style",''' + ckmsg(s, "Missing parentheses in call to 'print'. " + "Did you mean print(\"old style\", end=\" \")?") + + s = '''exec "old style"''' + ckmsg(s, "Missing parentheses in call to 'exec'") + + # should not apply to subclasses, see issue #31161 + s = '''if True:\nprint "No indent"''' + ckmsg(s, "expected an indented block", IndentationError) + + s = '''if True:\n print()\n\texec "mixed tabs and spaces"''' + ckmsg(s, "inconsistent use of tabs and spaces in indentation", TabError) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def testSyntaxErrorOffset(self): + def check(src, lineno, offset): + with self.assertRaises(SyntaxError) as cm: + compile(src, '', 'exec') + self.assertEqual(cm.exception.lineno, lineno) + self.assertEqual(cm.exception.offset, offset) + + check('def fact(x):\n\treturn x!\n', 2, 10) + check('1 +\n', 1, 4) + check('def spam():\n print(1)\n print(2)', 3, 10) + check('Python = "Python" +', 1, 20) + check('Python = "\u1e54\xfd\u0163\u0125\xf2\xf1" +', 1, 20) + check('x = "a', 1, 7) + check('lambda x: x = 2', 1, 1) + + # Errors thrown by compile.c + check('class foo:return 1', 1, 11) + check('def f():\n continue', 2, 3) + check('def f():\n break', 2, 3) + check('try:\n pass\nexcept:\n pass\nexcept ValueError:\n pass', 2, 3) + + # Errors thrown by tokenizer.c + check('(0x+1)', 1, 3) + check('x = 0xI', 1, 6) + check('0010 + 2', 1, 4) + check('x = 32e-+4', 1, 8) + check('x = 0o9', 1, 6) + + # Errors thrown by symtable.c + check('x = [(yield i) for i in range(3)]', 1, 5) + check('def f():\n from _ import *', 1, 1) + check('def f(x, x):\n pass', 1, 1) + check('def f(x):\n nonlocal x', 2, 3) + check('def f(x):\n x = 1\n global x', 3, 3) + check('nonlocal x', 1, 1) + check('def f():\n global x\n nonlocal x', 2, 3) + + # Errors thrown by ast.c + check('for 1 in []: pass', 1, 5) + check('def f(*):\n pass', 1, 7) + check('[*x for x in xs]', 1, 2) + check('def f():\n x, y: int', 2, 3) + check('(yield i) = 2', 1, 1) + check('foo(x for x in range(10), 100)', 1, 5) + check('foo(1=2)', 1, 5) + + # Errors thrown by future.c + check('from __future__ import doesnt_exist', 1, 1) + check('from __future__ import braces', 1, 1) + check('x=1\nfrom __future__ import division', 2, 1) + + + @cpython_only + def testSettingException(self): + # test that setting an exception at the C level works even if the + # exception object can't be constructed. + + class BadException(Exception): + def __init__(self_): + raise RuntimeError("can't instantiate BadException") + + class InvalidException: + pass + + def test_capi1(): + import _testcapi + try: + _testcapi.raise_exception(BadException, 1) + except TypeError as err: + exc, err, tb = sys.exc_info() + co = tb.tb_frame.f_code + self.assertEqual(co.co_name, "test_capi1") + self.assertTrue(co.co_filename.endswith('test_exceptions.py')) + else: + self.fail("Expected exception") + + def test_capi2(): + import _testcapi + try: + _testcapi.raise_exception(BadException, 0) + except RuntimeError as err: + exc, err, tb = sys.exc_info() + co = tb.tb_frame.f_code + self.assertEqual(co.co_name, "__init__") + self.assertTrue(co.co_filename.endswith('test_exceptions.py')) + co2 = tb.tb_frame.f_back.f_code + self.assertEqual(co2.co_name, "test_capi2") + else: + self.fail("Expected exception") + + def test_capi3(): + import _testcapi + self.assertRaises(SystemError, _testcapi.raise_exception, + InvalidException, 1) + + if not sys.platform.startswith('java'): + test_capi1() + test_capi2() + test_capi3() + + def test_WindowsError(self): + try: + WindowsError + except NameError: + pass + else: + self.assertIs(WindowsError, OSError) + self.assertEqual(str(OSError(1001)), "1001") + self.assertEqual(str(OSError(1001, "message")), + "[Errno 1001] message") + # POSIX errno (9 aka EBADF) is untranslated + w = OSError(9, 'foo', 'bar') + self.assertEqual(w.errno, 9) + self.assertEqual(w.winerror, None) + self.assertEqual(str(w), "[Errno 9] foo: 'bar'") + # ERROR_PATH_NOT_FOUND (win error 3) becomes ENOENT (2) + w = OSError(0, 'foo', 'bar', 3) + self.assertEqual(w.errno, 2) + self.assertEqual(w.winerror, 3) + self.assertEqual(w.strerror, 'foo') + self.assertEqual(w.filename, 'bar') + self.assertEqual(w.filename2, None) + self.assertEqual(str(w), "[WinError 3] foo: 'bar'") + # Unknown win error becomes EINVAL (22) + w = OSError(0, 'foo', None, 1001) + self.assertEqual(w.errno, 22) + self.assertEqual(w.winerror, 1001) + self.assertEqual(w.strerror, 'foo') + self.assertEqual(w.filename, None) + self.assertEqual(w.filename2, None) + self.assertEqual(str(w), "[WinError 1001] foo") + # Non-numeric "errno" + w = OSError('bar', 'foo') + self.assertEqual(w.errno, 'bar') + self.assertEqual(w.winerror, None) + self.assertEqual(w.strerror, 'foo') + self.assertEqual(w.filename, None) + self.assertEqual(w.filename2, None) + + @unittest.skipUnless(sys.platform == 'win32', + 'test specific to Windows') + def test_windows_message(self): + """Should fill in unknown error code in Windows error message""" + ctypes = import_module('ctypes') + # this error code has no message, Python formats it as hexadecimal + code = 3765269347 + with self.assertRaisesRegex(OSError, 'Windows Error 0x%x' % code): + ctypes.pythonapi.PyErr_SetFromWindowsErr(code) + + @unittest.skip("TODO: RUSTPYTHON") + def testAttributes(self): + # test that exception attributes are happy + + exceptionList = [ + (BaseException, (), {'args' : ()}), + (BaseException, (1, ), {'args' : (1,)}), + (BaseException, ('foo',), + {'args' : ('foo',)}), + (BaseException, ('foo', 1), + {'args' : ('foo', 1)}), + (SystemExit, ('foo',), + {'args' : ('foo',), 'code' : 'foo'}), + (OSError, ('foo',), + {'args' : ('foo',), 'filename' : None, 'filename2' : None, + 'errno' : None, 'strerror' : None}), + (OSError, ('foo', 'bar'), + {'args' : ('foo', 'bar'), + 'filename' : None, 'filename2' : None, + 'errno' : 'foo', 'strerror' : 'bar'}), + (OSError, ('foo', 'bar', 'baz'), + {'args' : ('foo', 'bar'), + 'filename' : 'baz', 'filename2' : None, + 'errno' : 'foo', 'strerror' : 'bar'}), + (OSError, ('foo', 'bar', 'baz', None, 'quux'), + {'args' : ('foo', 'bar'), 'filename' : 'baz', 'filename2': 'quux'}), + (OSError, ('errnoStr', 'strErrorStr', 'filenameStr'), + {'args' : ('errnoStr', 'strErrorStr'), + 'strerror' : 'strErrorStr', 'errno' : 'errnoStr', + 'filename' : 'filenameStr'}), + (OSError, (1, 'strErrorStr', 'filenameStr'), + {'args' : (1, 'strErrorStr'), 'errno' : 1, + 'strerror' : 'strErrorStr', + 'filename' : 'filenameStr', 'filename2' : None}), + (SyntaxError, (), {'msg' : None, 'text' : None, + 'filename' : None, 'lineno' : None, 'offset' : None, + 'print_file_and_line' : None}), + (SyntaxError, ('msgStr',), + {'args' : ('msgStr',), 'text' : None, + 'print_file_and_line' : None, 'msg' : 'msgStr', + 'filename' : None, 'lineno' : None, 'offset' : None}), + (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', + 'textStr')), + {'offset' : 'offsetStr', 'text' : 'textStr', + 'args' : ('msgStr', ('filenameStr', 'linenoStr', + 'offsetStr', 'textStr')), + 'print_file_and_line' : None, 'msg' : 'msgStr', + 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}), + (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', + 'textStr', 'print_file_and_lineStr'), + {'text' : None, + 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', + 'textStr', 'print_file_and_lineStr'), + 'print_file_and_line' : None, 'msg' : 'msgStr', + 'filename' : None, 'lineno' : None, 'offset' : None}), + (UnicodeError, (), {'args' : (),}), + (UnicodeEncodeError, ('ascii', 'a', 0, 1, + 'ordinal not in range'), + {'args' : ('ascii', 'a', 0, 1, + 'ordinal not in range'), + 'encoding' : 'ascii', 'object' : 'a', + 'start' : 0, 'reason' : 'ordinal not in range'}), + (UnicodeDecodeError, ('ascii', bytearray(b'\xff'), 0, 1, + 'ordinal not in range'), + {'args' : ('ascii', bytearray(b'\xff'), 0, 1, + 'ordinal not in range'), + 'encoding' : 'ascii', 'object' : b'\xff', + 'start' : 0, 'reason' : 'ordinal not in range'}), + (UnicodeDecodeError, ('ascii', b'\xff', 0, 1, + 'ordinal not in range'), + {'args' : ('ascii', b'\xff', 0, 1, + 'ordinal not in range'), + 'encoding' : 'ascii', 'object' : b'\xff', + 'start' : 0, 'reason' : 'ordinal not in range'}), + (UnicodeTranslateError, ("\u3042", 0, 1, "ouch"), + {'args' : ('\u3042', 0, 1, 'ouch'), + 'object' : '\u3042', 'reason' : 'ouch', + 'start' : 0, 'end' : 1}), + (NaiveException, ('foo',), + {'args': ('foo',), 'x': 'foo'}), + (SlottedNaiveException, ('foo',), + {'args': ('foo',), 'x': 'foo'}), + ] + try: + # More tests are in test_WindowsError + exceptionList.append( + (WindowsError, (1, 'strErrorStr', 'filenameStr'), + {'args' : (1, 'strErrorStr'), + 'strerror' : 'strErrorStr', 'winerror' : None, + 'errno' : 1, + 'filename' : 'filenameStr', 'filename2' : None}) + ) + except NameError: + pass + + for exc, args, expected in exceptionList: + try: + e = exc(*args) + except: + print("\nexc=%r, args=%r" % (exc, args), file=sys.stderr) + raise + else: + # Verify module name + if not type(e).__name__.endswith('NaiveException'): + self.assertEqual(type(e).__module__, 'builtins') + # Verify no ref leaks in Exc_str() + s = str(e) + for checkArgName in expected: + value = getattr(e, checkArgName) + self.assertEqual(repr(value), + repr(expected[checkArgName]), + '%r.%s == %r, expected %r' % ( + e, checkArgName, + value, expected[checkArgName])) + + # test for pickling support + for p in [pickle]: + for protocol in range(p.HIGHEST_PROTOCOL + 1): + s = p.dumps(e, protocol) + new = p.loads(s) + for checkArgName in expected: + got = repr(getattr(new, checkArgName)) + want = repr(expected[checkArgName]) + self.assertEqual(got, want, + 'pickled "%r", attribute "%s' % + (e, checkArgName)) + + def testWithTraceback(self): + try: + raise IndexError(4) + except: + tb = sys.exc_info()[2] + + e = BaseException().with_traceback(tb) + self.assertIsInstance(e, BaseException) + self.assertEqual(e.__traceback__, tb) + + e = IndexError(5).with_traceback(tb) + self.assertIsInstance(e, IndexError) + self.assertEqual(e.__traceback__, tb) + + class MyException(Exception): + pass + + e = MyException().with_traceback(tb) + self.assertIsInstance(e, MyException) + self.assertEqual(e.__traceback__, tb) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def testInvalidTraceback(self): + try: + Exception().__traceback__ = 5 + except TypeError as e: + self.assertIn("__traceback__ must be a traceback", str(e)) + else: + self.fail("No exception raised") + + @unittest.skip("TODO: RUSTPYTHON") + def testInvalidAttrs(self): + self.assertRaises(TypeError, setattr, Exception(), '__cause__', 1) + self.assertRaises(TypeError, delattr, Exception(), '__cause__') + self.assertRaises(TypeError, setattr, Exception(), '__context__', 1) + self.assertRaises(TypeError, delattr, Exception(), '__context__') + + def testNoneClearsTracebackAttr(self): + try: + raise IndexError(4) + except: + tb = sys.exc_info()[2] + + e = Exception() + e.__traceback__ = tb + e.__traceback__ = None + self.assertEqual(e.__traceback__, None) + + def testChainingAttrs(self): + e = Exception() + self.assertIsNone(e.__context__) + self.assertIsNone(e.__cause__) + + e = TypeError() + self.assertIsNone(e.__context__) + self.assertIsNone(e.__cause__) + + class MyException(OSError): + pass + + e = MyException() + self.assertIsNone(e.__context__) + self.assertIsNone(e.__cause__) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def testChainingDescriptors(self): + try: + raise Exception() + except Exception as exc: + e = exc + + self.assertIsNone(e.__context__) + self.assertIsNone(e.__cause__) + self.assertFalse(e.__suppress_context__) + + e.__context__ = NameError() + e.__cause__ = None + self.assertIsInstance(e.__context__, NameError) + self.assertIsNone(e.__cause__) + self.assertTrue(e.__suppress_context__) + e.__suppress_context__ = False + self.assertFalse(e.__suppress_context__) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def testKeywordArgs(self): + # test that builtin exception don't take keyword args, + # but user-defined subclasses can if they want + self.assertRaises(TypeError, BaseException, a=1) + + class DerivedException(BaseException): + def __init__(self, fancy_arg): + BaseException.__init__(self) + self.fancy_arg = fancy_arg + + x = DerivedException(fancy_arg=42) + self.assertEqual(x.fancy_arg, 42) + + @no_tracing + def testInfiniteRecursion(self): + def f(): + return f() + self.assertRaises(RecursionError, f) + + def g(): + try: + return g() + except ValueError: + return -1 + self.assertRaises(RecursionError, g) + + def test_str(self): + # Make sure both instances and classes have a str representation. + self.assertTrue(str(Exception)) + self.assertTrue(str(Exception('a'))) + self.assertTrue(str(Exception('a', 'b'))) + + def testExceptionCleanupNames(self): + # Make sure the local variable bound to the exception instance by + # an "except" statement is only visible inside the except block. + try: + raise Exception() + except Exception as e: + self.assertTrue(e) + del e + self.assertNotIn('e', locals()) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def testExceptionCleanupState(self): + # Make sure exception state is cleaned up as soon as the except + # block is left. See #2507 + + class MyException(Exception): + def __init__(self, obj): + self.obj = obj + class MyObj: + pass + + def inner_raising_func(): + # Create some references in exception value and traceback + local_ref = obj + raise MyException(obj) + + # Qualified "except" with "as" + obj = MyObj() + wr = weakref.ref(obj) + try: + inner_raising_func() + except MyException as e: + pass + obj = None + obj = wr() + self.assertIsNone(obj) + + # Qualified "except" without "as" + obj = MyObj() + wr = weakref.ref(obj) + try: + inner_raising_func() + except MyException: + pass + obj = None + obj = wr() + self.assertIsNone(obj) + + # Bare "except" + obj = MyObj() + wr = weakref.ref(obj) + try: + inner_raising_func() + except: + pass + obj = None + obj = wr() + self.assertIsNone(obj) + + # "except" with premature block leave + obj = MyObj() + wr = weakref.ref(obj) + for i in [0]: + try: + inner_raising_func() + except: + break + obj = None + obj = wr() + self.assertIsNone(obj) + + # "except" block raising another exception + obj = MyObj() + wr = weakref.ref(obj) + try: + try: + inner_raising_func() + except: + raise KeyError + except KeyError as e: + # We want to test that the except block above got rid of + # the exception raised in inner_raising_func(), but it + # also ends up in the __context__ of the KeyError, so we + # must clear the latter manually for our test to succeed. + e.__context__ = None + obj = None + obj = wr() + # guarantee no ref cycles on CPython (don't gc_collect) + if check_impl_detail(cpython=False): + gc_collect() + self.assertIsNone(obj) + + # Some complicated construct + obj = MyObj() + wr = weakref.ref(obj) + try: + inner_raising_func() + except MyException: + try: + try: + raise + finally: + raise + except MyException: + pass + obj = None + if check_impl_detail(cpython=False): + gc_collect() + obj = wr() + self.assertIsNone(obj) + + # Inside an exception-silencing "with" block + class Context: + def __enter__(self): + return self + def __exit__ (self, exc_type, exc_value, exc_tb): + return True + obj = MyObj() + wr = weakref.ref(obj) + with Context(): + inner_raising_func() + obj = None + if check_impl_detail(cpython=False): + gc_collect() + obj = wr() + self.assertIsNone(obj) + + def test_exception_target_in_nested_scope(self): + # issue 4617: This used to raise a SyntaxError + # "can not delete variable 'e' referenced in nested scope" + def print_error(): + e + try: + something + except Exception as e: + print_error() + # implicit "del e" here + + def test_generator_leaking(self): + # Test that generator exception state doesn't leak into the calling + # frame + def yield_raise(): + try: + raise KeyError("caught") + except KeyError: + yield sys.exc_info()[0] + yield sys.exc_info()[0] + yield sys.exc_info()[0] + g = yield_raise() + self.assertEqual(next(g), KeyError) + self.assertEqual(sys.exc_info()[0], None) + self.assertEqual(next(g), KeyError) + self.assertEqual(sys.exc_info()[0], None) + self.assertEqual(next(g), None) + + # Same test, but inside an exception handler + try: + raise TypeError("foo") + except TypeError: + g = yield_raise() + self.assertEqual(next(g), KeyError) + self.assertEqual(sys.exc_info()[0], TypeError) + self.assertEqual(next(g), KeyError) + self.assertEqual(sys.exc_info()[0], TypeError) + self.assertEqual(next(g), TypeError) + del g + self.assertEqual(sys.exc_info()[0], TypeError) + + def test_generator_leaking2(self): + # See issue 12475. + def g(): + yield + try: + raise RuntimeError + except RuntimeError: + it = g() + next(it) + try: + next(it) + except StopIteration: + pass + self.assertEqual(sys.exc_info(), (None, None, None)) + + def test_generator_leaking3(self): + # See issue #23353. When gen.throw() is called, the caller's + # exception state should be save and restored. + def g(): + try: + yield + except ZeroDivisionError: + yield sys.exc_info()[1] + it = g() + next(it) + try: + 1/0 + except ZeroDivisionError as e: + self.assertIs(sys.exc_info()[1], e) + gen_exc = it.throw(e) + self.assertIs(sys.exc_info()[1], e) + self.assertIs(gen_exc, e) + self.assertEqual(sys.exc_info(), (None, None, None)) + + def test_generator_leaking4(self): + # See issue #23353. When an exception is raised by a generator, + # the caller's exception state should still be restored. + def g(): + try: + 1/0 + except ZeroDivisionError: + yield sys.exc_info()[0] + raise + it = g() + try: + raise TypeError + except TypeError: + # The caller's exception state (TypeError) is temporarily + # saved in the generator. + tp = next(it) + self.assertIs(tp, ZeroDivisionError) + try: + next(it) + # We can't check it immediately, but while next() returns + # with an exception, it shouldn't have restored the old + # exception state (TypeError). + except ZeroDivisionError as e: + self.assertIs(sys.exc_info()[1], e) + # We used to find TypeError here. + self.assertEqual(sys.exc_info(), (None, None, None)) + + def test_generator_doesnt_retain_old_exc(self): + def g(): + self.assertIsInstance(sys.exc_info()[1], RuntimeError) + yield + self.assertEqual(sys.exc_info(), (None, None, None)) + it = g() + try: + raise RuntimeError + except RuntimeError: + next(it) + self.assertRaises(StopIteration, next, it) + + def test_generator_finalizing_and_exc_info(self): + # See #7173 + def simple_gen(): + yield 1 + def run_gen(): + gen = simple_gen() + try: + raise RuntimeError + except RuntimeError: + return next(gen) + run_gen() + gc_collect() + self.assertEqual(sys.exc_info(), (None, None, None)) + + def _check_generator_cleanup_exc_state(self, testfunc): + # Issue #12791: exception state is cleaned up as soon as a generator + # is closed (reference cycles are broken). + class MyException(Exception): + def __init__(self, obj): + self.obj = obj + class MyObj: + pass + + def raising_gen(): + try: + raise MyException(obj) + except MyException: + yield + + obj = MyObj() + wr = weakref.ref(obj) + g = raising_gen() + next(g) + testfunc(g) + g = obj = None + obj = wr() + self.assertIsNone(obj) + + def test_generator_throw_cleanup_exc_state(self): + def do_throw(g): + try: + g.throw(RuntimeError()) + except RuntimeError: + pass + self._check_generator_cleanup_exc_state(do_throw) + + def test_generator_close_cleanup_exc_state(self): + def do_close(g): + g.close() + self._check_generator_cleanup_exc_state(do_close) + + def test_generator_del_cleanup_exc_state(self): + def do_del(g): + g = None + self._check_generator_cleanup_exc_state(do_del) + + def test_generator_next_cleanup_exc_state(self): + def do_next(g): + try: + next(g) + except StopIteration: + pass + else: + self.fail("should have raised StopIteration") + self._check_generator_cleanup_exc_state(do_next) + + def test_generator_send_cleanup_exc_state(self): + def do_send(g): + try: + g.send(None) + except StopIteration: + pass + else: + self.fail("should have raised StopIteration") + self._check_generator_cleanup_exc_state(do_send) + + # def test_3114(self): + # # Bug #3114: in its destructor, MyObject retrieves a pointer to + # # obsolete and/or deallocated objects. + # class MyObject: + # def __del__(self): + # nonlocal e + # e = sys.exc_info() + # e = () + # try: + # raise Exception(MyObject()) + # except: + # pass + # self.assertEqual(e, (None, None, None)) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_unicode_change_attributes(self): + # See issue 7309. This was a crasher. + + u = UnicodeEncodeError('baz', 'xxxxx', 1, 5, 'foo') + self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: foo") + u.end = 2 + self.assertEqual(str(u), "'baz' codec can't encode character '\\x78' in position 1: foo") + u.end = 5 + u.reason = 0x345345345345345345 + self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: 965230951443685724997") + u.encoding = 4000 + self.assertEqual(str(u), "'4000' codec can't encode characters in position 1-4: 965230951443685724997") + u.start = 1000 + self.assertEqual(str(u), "'4000' codec can't encode characters in position 1000-4: 965230951443685724997") + + u = UnicodeDecodeError('baz', b'xxxxx', 1, 5, 'foo') + self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: foo") + u.end = 2 + self.assertEqual(str(u), "'baz' codec can't decode byte 0x78 in position 1: foo") + u.end = 5 + u.reason = 0x345345345345345345 + self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: 965230951443685724997") + u.encoding = 4000 + self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1-4: 965230951443685724997") + u.start = 1000 + self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1000-4: 965230951443685724997") + + u = UnicodeTranslateError('xxxx', 1, 5, 'foo') + self.assertEqual(str(u), "can't translate characters in position 1-4: foo") + u.end = 2 + self.assertEqual(str(u), "can't translate character '\\x78' in position 1: foo") + u.end = 5 + u.reason = 0x345345345345345345 + self.assertEqual(str(u), "can't translate characters in position 1-4: 965230951443685724997") + u.start = 1000 + self.assertEqual(str(u), "can't translate characters in position 1000-4: 965230951443685724997") + + def test_unicode_errors_no_object(self): + # See issue #21134. + klasses = UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError + for klass in klasses: + self.assertEqual(str(klass.__new__(klass)), "") + + @no_tracing + def test_badisinstance(self): + # Bug #2542: if issubclass(e, MyException) raises an exception, + # it should be ignored + class Meta(type): + def __subclasscheck__(cls, subclass): + raise ValueError() + class MyException(Exception, metaclass=Meta): + pass + + with captured_stderr() as stderr: + try: + raise KeyError() + except MyException as e: + self.fail("exception should not be a MyException") + except KeyError: + pass + except: + self.fail("Should have raised KeyError") + else: + self.fail("Should have raised KeyError") + + def g(): + try: + return g() + except RecursionError: + return sys.exc_info() + e, v, tb = g() + self.assertIsInstance(v, RecursionError, type(v)) + self.assertIn("maximum recursion depth exceeded", str(v)) + + @cpython_only + def test_recursion_normalizing_exception(self): + # Issue #22898. + # Test that a RecursionError is raised when tstate->recursion_depth is + # equal to recursion_limit in PyErr_NormalizeException() and check + # that a ResourceWarning is printed. + # Prior to #22898, the recursivity of PyErr_NormalizeException() was + # controlled by tstate->recursion_depth and a PyExc_RecursionErrorInst + # singleton was being used in that case, that held traceback data and + # locals indefinitely and would cause a segfault in _PyExc_Fini() upon + # finalization of these locals. + code = """if 1: + import sys + from _testcapi import get_recursion_depth + + class MyException(Exception): pass + + def setrecursionlimit(depth): + while 1: + try: + sys.setrecursionlimit(depth) + return depth + except RecursionError: + # sys.setrecursionlimit() raises a RecursionError if + # the new recursion limit is too low (issue #25274). + depth += 1 + + def recurse(cnt): + cnt -= 1 + if cnt: + recurse(cnt) + else: + generator.throw(MyException) + + def gen(): + f = open(%a, mode='rb', buffering=0) + yield + + generator = gen() + next(generator) + recursionlimit = sys.getrecursionlimit() + depth = get_recursion_depth() + try: + # Upon the last recursive invocation of recurse(), + # tstate->recursion_depth is equal to (recursion_limit - 1) + # and is equal to recursion_limit when _gen_throw() calls + # PyErr_NormalizeException(). + recurse(setrecursionlimit(depth + 2) - depth - 1) + finally: + sys.setrecursionlimit(recursionlimit) + print('Done.') + """ % __file__ + rc, out, err = script_helper.assert_python_failure("-Wd", "-c", code) + # Check that the program does not fail with SIGABRT. + self.assertEqual(rc, 1) + self.assertIn(b'RecursionError', err) + self.assertIn(b'ResourceWarning', err) + self.assertIn(b'Done.', out) + + @cpython_only + def test_recursion_normalizing_infinite_exception(self): + # Issue #30697. Test that a RecursionError is raised when + # PyErr_NormalizeException() maximum recursion depth has been + # exceeded. + code = """if 1: + import _testcapi + try: + raise _testcapi.RecursingInfinitelyError + finally: + print('Done.') + """ + rc, out, err = script_helper.assert_python_failure("-c", code) + self.assertEqual(rc, 1) + self.assertIn(b'RecursionError: maximum recursion depth exceeded ' + b'while normalizing an exception', err) + self.assertIn(b'Done.', out) + + @cpython_only + def test_recursion_normalizing_with_no_memory(self): + # Issue #30697. Test that in the abort that occurs when there is no + # memory left and the size of the Python frames stack is greater than + # the size of the list of preallocated MemoryError instances, the + # Fatal Python error message mentions MemoryError. + code = """if 1: + import _testcapi + class C(): pass + def recurse(cnt): + cnt -= 1 + if cnt: + recurse(cnt) + else: + _testcapi.set_nomemory(0) + C() + recurse(16) + """ + with SuppressCrashReport(): + rc, out, err = script_helper.assert_python_failure("-c", code) + self.assertIn(b'Fatal Python error: Cannot recover from ' + b'MemoryErrors while normalizing exceptions.', err) + + @cpython_only + def test_MemoryError(self): + # PyErr_NoMemory always raises the same exception instance. + # Check that the traceback is not doubled. + import traceback + from _testcapi import raise_memoryerror + def raiseMemError(): + try: + raise_memoryerror() + except MemoryError as e: + tb = e.__traceback__ + else: + self.fail("Should have raises a MemoryError") + return traceback.format_tb(tb) + + tb1 = raiseMemError() + tb2 = raiseMemError() + self.assertEqual(tb1, tb2) + + @cpython_only + def test_exception_with_doc(self): + import _testcapi + doc2 = "This is a test docstring." + doc4 = "This is another test docstring." + + self.assertRaises(SystemError, _testcapi.make_exception_with_doc, + "error1") + + # test basic usage of PyErr_NewException + error1 = _testcapi.make_exception_with_doc("_testcapi.error1") + self.assertIs(type(error1), type) + self.assertTrue(issubclass(error1, Exception)) + self.assertIsNone(error1.__doc__) + + # test with given docstring + error2 = _testcapi.make_exception_with_doc("_testcapi.error2", doc2) + self.assertEqual(error2.__doc__, doc2) + + # test with explicit base (without docstring) + error3 = _testcapi.make_exception_with_doc("_testcapi.error3", + base=error2) + self.assertTrue(issubclass(error3, error2)) + + # test with explicit base tuple + class C(object): + pass + error4 = _testcapi.make_exception_with_doc("_testcapi.error4", doc4, + (error3, C)) + self.assertTrue(issubclass(error4, error3)) + self.assertTrue(issubclass(error4, C)) + self.assertEqual(error4.__doc__, doc4) + + # test with explicit dictionary + error5 = _testcapi.make_exception_with_doc("_testcapi.error5", "", + error4, {'a': 1}) + self.assertTrue(issubclass(error5, error4)) + self.assertEqual(error5.a, 1) + self.assertEqual(error5.__doc__, "") + + @cpython_only + def test_memory_error_cleanup(self): + # Issue #5437: preallocated MemoryError instances should not keep + # traceback objects alive. + from _testcapi import raise_memoryerror + class C: + pass + wr = None + def inner(): + nonlocal wr + c = C() + wr = weakref.ref(c) + raise_memoryerror() + # We cannot use assertRaises since it manually deletes the traceback + try: + inner() + except MemoryError as e: + self.assertNotEqual(wr(), None) + else: + self.fail("MemoryError not raised") + self.assertEqual(wr(), None) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + @no_tracing + def test_recursion_error_cleanup(self): + # Same test as above, but with "recursion exceeded" errors + class C: + pass + wr = None + def inner(): + nonlocal wr + c = C() + wr = weakref.ref(c) + inner() + # We cannot use assertRaises since it manually deletes the traceback + try: + inner() + except RecursionError as e: + self.assertNotEqual(wr(), None) + else: + self.fail("RecursionError not raised") + self.assertEqual(wr(), None) + + def test_errno_ENOTDIR(self): + # Issue #12802: "not a directory" errors are ENOTDIR even on Windows + with self.assertRaises(OSError) as cm: + os.listdir(__file__) + self.assertEqual(cm.exception.errno, errno.ENOTDIR, cm.exception) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_unraisable(self): + # Issue #22836: PyErr_WriteUnraisable() should give sensible reports + class BrokenDel: + def __del__(self): + exc = ValueError("del is broken") + # The following line is included in the traceback report: + raise exc + + class BrokenExceptionDel: + def __del__(self): + exc = BrokenStrException() + # The following line is included in the traceback report: + raise exc + + for test_class in (BrokenDel, BrokenExceptionDel): + with self.subTest(test_class): + obj = test_class() + with captured_stderr() as stderr: + del obj + report = stderr.getvalue() + self.assertIn("Exception ignored", report) + self.assertIn(test_class.__del__.__qualname__, report) + self.assertIn("test_exceptions.py", report) + self.assertIn("raise exc", report) + if test_class is BrokenExceptionDel: + self.assertIn("BrokenStrException", report) + self.assertIn("", report) + else: + self.assertIn("ValueError", report) + self.assertIn("del is broken", report) + self.assertTrue(report.endswith("\n")) + + @unittest.skip("TODO: RUSTPYTHON") + def test_unhandled(self): + # Check for sensible reporting of unhandled exceptions + for exc_type in (ValueError, BrokenStrException): + with self.subTest(exc_type): + try: + exc = exc_type("test message") + # The following line is included in the traceback report: + raise exc + except exc_type: + with captured_stderr() as stderr: + sys.__excepthook__(*sys.exc_info()) + report = stderr.getvalue() + self.assertIn("test_exceptions.py", report) + self.assertIn("raise exc", report) + self.assertIn(exc_type.__name__, report) + if exc_type is BrokenStrException: + self.assertIn("", report) + else: + self.assertIn("test message", report) + self.assertTrue(report.endswith("\n")) + + @cpython_only + def test_memory_error_in_PyErr_PrintEx(self): + code = """if 1: + import _testcapi + class C(): pass + _testcapi.set_nomemory(0, %d) + C() + """ + + # Issue #30817: Abort in PyErr_PrintEx() when no memory. + # Span a large range of tests as the CPython code always evolves with + # changes that add or remove memory allocations. + for i in range(1, 20): + rc, out, err = script_helper.assert_python_failure("-c", code % i) + self.assertIn(rc, (1, 120)) + self.assertIn(b'MemoryError', err) + + def test_yield_in_nested_try_excepts(self): + #Issue #25612 + class MainError(Exception): + pass + + class SubError(Exception): + pass + + def main(): + try: + raise MainError() + except MainError: + try: + yield + except SubError: + pass + raise + + coro = main() + coro.send(None) + with self.assertRaises(MainError): + coro.throw(SubError()) + + def test_generator_doesnt_retain_old_exc2(self): + #Issue 28884#msg282532 + def g(): + try: + raise ValueError + except ValueError: + yield 1 + self.assertEqual(sys.exc_info(), (None, None, None)) + yield 2 + + gen = g() + + try: + raise IndexError + except IndexError: + self.assertEqual(next(gen), 1) + self.assertEqual(next(gen), 2) + + def test_raise_in_generator(self): + #Issue 25612#msg304117 + def g(): + yield 1 + raise + yield 2 + + with self.assertRaises(ZeroDivisionError): + i = g() + try: + 1/0 + except: + next(i) + next(i) + + +class ImportErrorTests(unittest.TestCase): + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_attributes(self): + # Setting 'name' and 'path' should not be a problem. + exc = ImportError('test') + self.assertIsNone(exc.name) + self.assertIsNone(exc.path) + + exc = ImportError('test', name='somemodule') + self.assertEqual(exc.name, 'somemodule') + self.assertIsNone(exc.path) + + exc = ImportError('test', path='somepath') + self.assertEqual(exc.path, 'somepath') + self.assertIsNone(exc.name) + + exc = ImportError('test', path='somepath', name='somename') + self.assertEqual(exc.name, 'somename') + self.assertEqual(exc.path, 'somepath') + + msg = "'invalid' is an invalid keyword argument for ImportError" + with self.assertRaisesRegex(TypeError, msg): + ImportError('test', invalid='keyword') + + with self.assertRaisesRegex(TypeError, msg): + ImportError('test', name='name', invalid='keyword') + + with self.assertRaisesRegex(TypeError, msg): + ImportError('test', path='path', invalid='keyword') + + with self.assertRaisesRegex(TypeError, msg): + ImportError(invalid='keyword') + + with self.assertRaisesRegex(TypeError, msg): + ImportError('test', invalid='keyword', another=True) + + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_reset_attributes(self): + exc = ImportError('test', name='name', path='path') + self.assertEqual(exc.args, ('test',)) + self.assertEqual(exc.msg, 'test') + self.assertEqual(exc.name, 'name') + self.assertEqual(exc.path, 'path') + + # Reset not specified attributes + exc.__init__() + self.assertEqual(exc.args, ()) + self.assertEqual(exc.msg, None) + self.assertEqual(exc.name, None) + self.assertEqual(exc.path, None) + + def test_non_str_argument(self): + # Issue #15778 + with check_warnings(('', BytesWarning), quiet=True): + arg = b'abc' + exc = ImportError(arg) + self.assertEqual(str(arg), str(exc)) + + @unittest.skip("TODO: RUSTPYTHON") + def test_copy_pickle(self): + for kwargs in (dict(), + dict(name='somename'), + dict(path='somepath'), + dict(name='somename', path='somepath')): + orig = ImportError('test', **kwargs) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + exc = pickle.loads(pickle.dumps(orig, proto)) + self.assertEqual(exc.args, ('test',)) + self.assertEqual(exc.msg, 'test') + self.assertEqual(exc.name, orig.name) + self.assertEqual(exc.path, orig.path) + for c in copy.copy, copy.deepcopy: + exc = c(orig) + self.assertEqual(exc.args, ('test',)) + self.assertEqual(exc.msg, 'test') + self.assertEqual(exc.name, orig.name) + self.assertEqual(exc.path, orig.path) + + +if __name__ == '__main__': + unittest.main() diff --git a/vm/src/obj/objcoroinner.rs b/vm/src/obj/objcoroinner.rs index e544e96fee..dfb2ab264b 100644 --- a/vm/src/obj/objcoroinner.rs +++ b/vm/src/obj/objcoroinner.rs @@ -4,13 +4,14 @@ use crate::frame::{ExecutionResult, FrameRef}; use crate::pyobject::{PyObjectRef, PyResult}; use crate::vm::VirtualMachine; -use std::cell::Cell; +use std::cell::{Cell, RefCell}; #[derive(Debug)] pub struct Coro { frame: FrameRef, closed: Cell, running: Cell, + exceptions: RefCell>, } impl Coro { @@ -19,6 +20,7 @@ impl Coro { frame, closed: Cell::new(false), running: Cell::new(false), + exceptions: RefCell::new(vec![]), } } @@ -29,15 +31,32 @@ impl Coro { } } + fn run_with_context(&self, func: F, vm: &VirtualMachine) -> PyResult + where + F: FnOnce() -> PyResult, + { + self.running.set(true); + let curr_exception_stack_len = vm.exceptions.borrow().len(); + vm.exceptions + .borrow_mut() + .append(&mut self.exceptions.borrow_mut()); + let result = func(); + self.exceptions.replace( + vm.exceptions + .borrow_mut() + .split_off(curr_exception_stack_len), + ); + self.running.set(false); + result + } + pub fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult { if self.closed.get() { return Err(objiter::new_stop_iteration(vm)); } self.frame.push_value(value.clone()); - self.running.set(true); - let result = vm.run_frame(self.frame.clone()); - self.running.set(false); + let result = self.run_with_context(|| vm.run_frame(self.frame.clone()), vm); self.maybe_close(&result); result?.into_result(vm) } @@ -53,9 +72,8 @@ impl Coro { return Err(exceptions::normalize(exc_type, exc_val, exc_tb, vm)?); } vm.frames.borrow_mut().push(self.frame.clone()); - self.running.set(true); - let result = self.frame.gen_throw(vm, exc_type, exc_val, exc_tb); - self.running.set(false); + let result = + self.run_with_context(|| self.frame.gen_throw(vm, exc_type, exc_val, exc_tb), vm); self.maybe_close(&result); vm.frames.borrow_mut().pop(); result?.into_result(vm) @@ -66,14 +84,17 @@ impl Coro { return Ok(()); } vm.frames.borrow_mut().push(self.frame.clone()); - self.running.set(true); - let result = self.frame.gen_throw( + let result = self.run_with_context( + || { + self.frame.gen_throw( + vm, + vm.ctx.exceptions.generator_exit.clone().into_object(), + vm.get_none(), + vm.get_none(), + ) + }, vm, - vm.ctx.exceptions.generator_exit.clone().into_object(), - vm.get_none(), - vm.get_none(), ); - self.running.set(false); vm.frames.borrow_mut().pop(); self.closed.set(true); match result { diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index d81ae56c95..801b852ded 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -413,18 +413,14 @@ fn os_rmdir(path: PyStringRef, dir_fd: DirFd, vm: &VirtualMachine) -> PyResult<( } fn os_listdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { - match fs::read_dir(path.as_str()) { - Ok(iter) => { - let res: PyResult> = iter - .map(|entry| match entry { - Ok(path) => Ok(vm.ctx.new_str(path.file_name().into_string().unwrap())), - Err(s) => Err(convert_io_error(vm, s)), - }) - .collect(); - Ok(vm.ctx.new_list(res?)) - } - Err(s) => Err(vm.new_os_error(s.to_string())), - } + let res: PyResult> = fs::read_dir(path.as_str()) + .map_err(|err| convert_io_error(vm, err))? + .map(|entry| match entry { + Ok(path) => Ok(vm.ctx.new_str(path.file_name().into_string().unwrap())), + Err(s) => Err(convert_io_error(vm, s)), + }) + .collect(); + Ok(vm.ctx.new_list(res?)) } fn bytes_as_osstr<'a>(b: &'a [u8], vm: &VirtualMachine) -> PyResult<&'a ffi::OsStr> {