示例#1
0
    assert_raises(
        ValueError,
        lambda: d.__ior__('FooBar'),
        _msg='dictionary update sequence element #0 has length 1; 2 is required'
    )
    assert_raises(TypeError,
                  lambda: d.__ior__(42),
                  _msg='\'int\' object is not iterable')
    assert_raises(
        TypeError,
        lambda: d.__ior__([36]),
        _msg=
        'cannot convert dictionary update sequence element #0 to a sequence')
    assert_raises(
        TypeError,
        lambda: d.__ior__(set([36])),
        _msg=
        'cannot convert dictionary update sequence element #0 to a sequence')
    res = d.__ior__(['aa'])
    assert res == {1: 2, 'a': 'a'}, f"unexpected result of dict union {res=}"
    assert_raises(TypeError,
                  lambda: d.__ior__(None),
                  _msg='TypeError: \'NoneType\' object is not iterable')


skip_if_unsupported(3, 9, test_dunion_ior0)
skip_if_unsupported(3, 9, test_dunion_or0)
skip_if_unsupported(3, 9, test_dunion_or1)
skip_if_unsupported(3, 9, test_dunion_ror0)
skip_if_unsupported(3, 9, test_dunion_other_types)
示例#2
0
def test_dunion_other_types():
    def perf_test_or(other_obj):
        d = {1: 2}
        return d.__or__(other_obj) is NotImplemented

    def perf_test_ior(other_obj):
        d = {1: 2}
        return d.__ior__(other_obj) is NotImplemented

    def perf_test_ror(other_obj):
        d = {1: 2}
        return d.__ror__(other_obj) is NotImplemented

    test_fct = {
        '__or__': perf_test_or,
        '__ror__': perf_test_ror,
        '__ior__': perf_test_ior
    }
    others = ['FooBar', 42, [36], set([19]), ['aa'], None]
    for tfn, tf in test_fct.items():
        for other in others:
            assert tf(other), f"Failed: dict {tfn}, accepted {other}"


testutils.skip_if_unsupported(3, 9, test_dunion_ior0)
testutils.skip_if_unsupported(3, 9, test_dunion_or0)
testutils.skip_if_unsupported(3, 9, test_dunion_or1)
testutils.skip_if_unsupported(3, 9, test_dunion_ror0)
testutils.skip_if_unsupported(3, 9, test_dunion_other_types)
示例#3
0
import testutils


# __complex__
def test__complex__():
    z = 3 + 4j
    assert z.__complex__() == z
    assert type(z.__complex__()) == complex

    class complex_subclass(complex):
        pass

    z = complex_subclass(3 + 4j)
    assert z.__complex__() == 3 + 4j
    assert type(z.__complex__()) == complex


testutils.skip_if_unsupported(3, 11, test__complex__)
示例#4
0
    assert s_uc.removesuffix('🖖') == s_ref_uc[:-1]
    assert s_uc.removesuffix('oo🖖') == s_ref_uc[:-3]
    assert s_uc.removesuffix('foo🖖') == s_ref_uc[:-4]

    assert s_uc.removesuffix('😱') == s_ref_uc
    assert s_uc.removesuffix('foo') == s_ref_uc
    assert s_uc.removesuffix(' ') == s_ref_uc
    assert s_uc.removesuffix('🖖_') == s_ref_uc
    assert s_uc.removesuffix('🖖 ') == s_ref_uc
    assert s_uc.removesuffix('🖖-') == s_ref_uc
    assert s_uc.removesuffix('🖖#') == s_ref_uc


def test_removesuffix_types():
    s = '0123456'
    s_ref = '0123456'
    others = [0, 6, ['6']]
    found = False
    for o in others:
        try:
            s.removesuffix(o)
        except:
            found = True

        assert found, f'Removesuffix accepts other type: {type(o)}: {o=}'


skip_if_unsupported(3, 9, test_removeprefix)
skip_if_unsupported(3, 9, test_removeprefix_types)
skip_if_unsupported(3, 9, test_removesuffix)
skip_if_unsupported(3, 9, test_removesuffix_types)
示例#5
0
assert id(a) != id(a * 0)
assert id(a) == id(a * 1)  # only cases
assert id(a) == id(1 * a)  # when `id` stays the same
assert id(a) != id(a * 2)


class SubBytes(bytes):
    pass


b = SubBytes(b'0123abc*&')
assert id(b) == id(b)
assert id(b) != id(b * -1)
assert id(b) != id(b * 0)
assert id(b) != id(b * 1)
assert id(b) != id(b * 2)


class B1(bytearray):
    def __new__(cls, value):
        assert type(value) == bytes
        me = super().__new__(cls, value)
        me.foo = 'bar'
        return me


b = B1.fromhex('a0a1a2')
assert b.foo == 'bar'

skip_if_unsupported(3, 11, test__bytes__)
示例#6
0
assert int.__trunc__
assert int.__floor__
assert int.__ceil__

# assert float.__trunc__


def float_floor_exists():
    assert float.__floor__


def float_ceil_exists():
    assert float.__ceil__


skip_if_unsupported(3, 9, float_floor_exists)
skip_if_unsupported(3, 9, float_ceil_exists)

assert math.trunc(2) == 2
assert math.ceil(3) == 3
assert math.floor(4) == 4

assert math.trunc(2.2) == 2
assert math.ceil(3.3) == 4
assert math.floor(4.4) == 4

assert isinstance(math.trunc(2.2), int)
assert isinstance(math.ceil(3.3), int)
assert isinstance(math.floor(4.4), int)