◐ Shell
reader mode source ↗
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
12 changes: 3 additions & 9 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from operator import lt, le, gt, ge, eq, ne, truediv, floordiv, mod

from test import support
from test.support import is_resource_enabled, ALWAYS_EQ, LARGEST, SMALLEST

import datetime as datetime_module
from datetime import MINYEAR, MAXYEAR
Expand Down Expand Up @@ -5076,15 +5076,9 @@ def test_extra_attributes(self):
x.abc = 1

def test_check_arg_types(self):
class Number:
def __init__(self, value):
self.value = value
def __int__(self):
return self.value

for xx in [decimal.Decimal(10),
decimal.Decimal('10.9'),
Number(10)]:
with self.assertWarns(DeprecationWarning):
self.assertEqual(datetime(10, 10, 10, 10, 10, 10, 10),
datetime(xx, xx, xx, xx, xx, xx, xx))
Expand All @@ -5093,7 +5087,7 @@ def __int__(self):
r'\(got type str\)$'):
datetime(10, 10, '10')

f10 = Number(10.9)
with self.assertRaisesRegex(TypeError, '^__int__ returned non-int '
r'\(type float\)$'):
datetime(10, 10, f10)
40 changes: 28 additions & 12 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"run_with_tz", "PGO", "missing_compiler_executable", "fd_count",
"ALWAYS_EQ", "NEVER_EQ", "LARGEST", "SMALLEST",
"LOOPBACK_TIMEOUT", "INTERNET_TIMEOUT", "SHORT_TIMEOUT", "LONG_TIMEOUT",
]


Expand Down Expand Up @@ -3189,22 +3190,37 @@ def with_pymalloc():
return _testcapi.WITH_PYMALLOC


class FakePath:
"""Simple implementing of the path protocol.
"""
def __init__(self, path):
self.path = path

def __repr__(self):
return f'<FakePath {self.path!r}>'

def __fspath__(self):
if (isinstance(self.path, BaseException) or
isinstance(self.path, type) and
issubclass(self.path, BaseException)):
raise self.path
else:
return self.path


class _ALWAYS_EQ:
Expand Down
16 changes: 2 additions & 14 deletions Lib/test/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import contextlib
import unittest
from test import support
from itertools import permutations, product
from random import randrange, sample, choice
import warnings
Expand Down @@ -2510,22 +2511,9 @@ def test_memoryview_sizeof(self):
check(memoryview(a), vsize(base_struct + 3 * per_dim))

def test_memoryview_struct_module(self):

class INT(object):
def __init__(self, val):
self.val = val
def __int__(self):
return self.val

class IDX(object):
def __init__(self, val):
self.val = val
def __index__(self):
return self.val

def f(): return 7

values = [INT(9), IDX(9),
2.2+3j, Decimal("-21.1"), 12.2, Fraction(5, 2),
[1,2,3], {4,5,6}, {7:8}, (), (9,),
True, False, None, Ellipsis,
Expand Down
36 changes: 13 additions & 23 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import test.support
import test.string_tests
import test.list_tests
from test.support import bigaddrspacetest, MAX_Py_ssize_t
from test.support.script_helper import assert_python_failure


Expand All @@ -35,13 +35,6 @@ def check_bytes_warnings(func):
return func


class Indexable:
def __init__(self, value=0):
self.value = value
def __index__(self):
return self.value


class BaseBytesTest:

def test_basics(self):
Expand Down Expand Up @@ -133,11 +126,11 @@ def __index__(self):
self.assertEqual(bytes(a), b'*' * 1000) # should not crash

def test_from_index(self):
b = self.type2test([Indexable(), Indexable(1), Indexable(254),
Indexable(255)])
self.assertEqual(list(b), [0, 1, 254, 255])
self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
self.assertRaises(ValueError, self.type2test, [Indexable(256)])

def test_from_buffer(self):
a = self.type2test(array.array('B', [1, 2, 3]))
Expand Down Expand Up @@ -208,11 +201,8 @@ def test_constructor_overflow(self):
def test_constructor_exceptions(self):
# Issue #34974: bytes and bytearray constructors replace unexpected
# exceptions.
class BadInt:
def __index__(self):
1/0
self.assertRaises(ZeroDivisionError, self.type2test, BadInt())
self.assertRaises(ZeroDivisionError, self.type2test, [BadInt()])

class BadIterable:
def __iter__(self):
Expand Up @@ -1267,7 +1257,7 @@ def test_setitem(self):
self.assertEqual(b, bytearray([1, 100, 3]))
b[-1] = 200
self.assertEqual(b, bytearray([1, 100, 200]))
b[0] = Indexable(10)
self.assertEqual(b, bytearray([10, 100, 200]))
try:
b[3] = 0
Expand All @@ -1285,7 +1275,7 @@ def test_setitem(self):
except ValueError:
pass
try:
b[0] = Indexable(-1)
self.fail("Didn't raise ValueError")
except ValueError:
pass
Expand Down Expand Up @@ -1483,7 +1473,7 @@ def test_extend(self):
self.assertRaises(ValueError, a.extend, [0, 1, 2, -1])
self.assertEqual(len(a), 0)
a = bytearray(b'')
a.extend([Indexable(ord('a'))])
self.assertEqual(a, b'a')

def test_remove(self):
Expand All @@ -1500,7 +1490,7 @@ def test_remove(self):
b.remove(ord('h'))
self.assertEqual(b, b'e')
self.assertRaises(TypeError, lambda: b.remove(b'e'))
b.remove(Indexable(ord('e')))
self.assertEqual(b, b'')

# test values outside of the ascii range: (0, 127)
Expand Down Expand Up @@ -1533,7 +1523,7 @@ def test_append(self):
self.assertEqual(len(b), 1)
self.assertRaises(TypeError, lambda: b.append(b'o'))
b = bytearray()
b.append(Indexable(ord('A')))
self.assertEqual(b, b'A')

def test_insert(self):
Expand All @@ -1545,7 +1535,7 @@ def test_insert(self):
self.assertEqual(b, b'mississippi')
self.assertRaises(TypeError, lambda: b.insert(0, b'1'))
b = bytearray()
b.insert(0, Indexable(ord('A')))
self.assertEqual(b, b'A')

def test_copied(self):
Expand Down
Loading
Toggle all file notes Toggle all file annotations