Exemplo n.º 1
0
    def test_multiple_inheritance(self):
        class W_A(W_Root):
            a = 1
            b = 2

        class W_C(W_A):
            b = 3

        W_A.typedef = typedef.TypeDef(
            "A",
            a=typedef.interp_attrproperty("a", cls=W_A),
            b=typedef.interp_attrproperty("b", cls=W_A),
        )

        class W_B(W_Root):
            pass

        def standalone_method(space, w_obj):
            if isinstance(w_obj, W_A):
                return space.w_True
            else:
                return space.w_False

        W_B.typedef = typedef.TypeDef("B", c=interp2app(standalone_method))
        W_C.typedef = typedef.TypeDef("C", (
            W_A.typedef,
            W_B.typedef,
        ))

        w_o1 = self.space.wrap(W_C())
        w_o2 = self.space.wrap(W_B())
        w_c = self.space.gettypefor(W_C)
        w_b = self.space.gettypefor(W_B)
        w_a = self.space.gettypefor(W_A)
        assert w_c.mro_w == [
            w_c,
            w_a,
            w_b,
            self.space.w_object,
        ]
        for w_tp in w_c.mro_w:
            assert self.space.isinstance_w(w_o1, w_tp)

        def assert_attr(w_obj, name, value):
            assert self.space.unwrap(
                self.space.getattr(w_obj, self.space.wrap(name))) == value

        def assert_method(w_obj, name, value):
            assert self.space.unwrap(self.space.call_method(w_obj,
                                                            name)) == value

        assert_attr(w_o1, "a", 1)
        assert_attr(w_o1, "b", 3)
        assert_method(w_o1, "c", True)
        assert_method(w_o2, "c", False)
Exemplo n.º 2
0
def Space(*args, **kwds):
    space = ReflectiveObjSpace(*args, **kwds)
    patch_space_in_place(space, 'reflective', proxymaker)
    w___pypy__ = space.getbuiltinmodule("__pypy__")
    space.setattr(w___pypy__, space.wrap('set_reflectivespace'),
                  space.wrap(app_set_reflectivespace))
    W_SpaceAccess.typedef = typedef.TypeDef("ObjSpace", **W_SpaceAccess_dict)
    return space
Exemplo n.º 3
0
    def test_destructor(self):
        space = self.space

        class W_Level1(W_Root):
            def __init__(self, space1):
                assert space1 is space
                self.register_finalizer(space)

            def _finalize_(self):
                space.call_method(w_seen, 'append', space.wrap(1))

        W_Level1.typedef = typedef.TypeDef(
            'level1', __new__=typedef.generic_new_descr(W_Level1))
        #
        w_seen = space.newlist([])
        W_Level1(space)
        gc.collect()
        gc.collect()
        assert space.str_w(space.repr(w_seen)) == "[]"  # not called yet
        ec = space.getexecutioncontext()
        self.space.user_del_action.perform(ec, None)
        assert space.unwrap(w_seen) == [1]  # called by user_del_action
        #
        w_seen = space.newlist([])
        self.space.appexec([self.space.gettypeobject(W_Level1.typedef)],
                           """(level1):
            class A3(level1):
                pass
            A3()
        """)
        gc.collect()
        gc.collect()
        assert space.unwrap(w_seen) == [1]
        #
        w_seen = space.newlist([])
        self.space.appexec(
            [self.space.gettypeobject(W_Level1.typedef), w_seen],
            """(level1, seen):
            class A4(level1):
                def __del__(self):
                    seen.append(4)
            A4()
        """)
        gc.collect()
        gc.collect()
        assert space.unwrap(w_seen) == [4, 1]  # user __del__, and _finalize_
        #
        w_seen = space.newlist([])
        self.space.appexec([self.space.gettypeobject(W_Level1.typedef)],
                           """(level2):
            class A5(level2):
                pass
            A5()
        """)
        gc.collect()
        gc.collect()
        assert space.unwrap(w_seen) == [1]  # _finalize_ only
Exemplo n.º 4
0
    def test_unhashable(self):
        class W_SomeType(W_Root):
            pass

        W_SomeType.typedef = typedef.TypeDef('some_type', __hash__=None)
        w_obj = self.space.wrap(W_SomeType())
        self.space.appexec([w_obj], """(obj):
            assert type(obj).__hash__ is None
            err = raises(TypeError, hash, obj)
            assert str(err.value) == "'some_type' objects are unhashable"
            """)
Exemplo n.º 5
0
    def test_getsetproperty(self):
        class W_SomeType(W_Root):
            pass

        def fget(self, space, w_self):
            assert self is prop

        prop = typedef.GetSetProperty(fget, use_closure=True)
        W_SomeType.typedef = typedef.TypeDef('some_type', x=prop)
        w_obj = self.space.wrap(W_SomeType())
        assert self.space.getattr(w_obj,
                                  self.space.wrap('x')) is self.space.w_None
Exemplo n.º 6
0
 def test_getsetproperty(self):
     class W_SomeType(W_Root):
         pass
     def fget(self, space, w_self):
         assert self is prop
     # NB. this GetSetProperty is not copied when creating the
     # W_TypeObject because of 'cls'.  Without it, a duplicate of the
     # GetSetProperty is taken and it is given the w_objclass that is
     # the W_TypeObject
     prop = typedef.GetSetProperty(fget, use_closure=True, cls=W_SomeType)
     W_SomeType.typedef = typedef.TypeDef(
         'some_type',
         x=prop)
     w_obj = self.space.wrap(W_SomeType())
     assert self.space.getattr(w_obj, self.space.wrap('x')) is self.space.w_None
Exemplo n.º 7
0
 def test_getsetproperty_arguments(self):
     class W_SomeType(W_Root):
         def fget1(space, w_self):
             assert isinstance(space, ObjSpace)
             assert isinstance(w_self, W_SomeType)
         def fget2(self, space):
             assert isinstance(space, ObjSpace)
             assert isinstance(self, W_SomeType)
     W_SomeType.typedef = typedef.TypeDef(
         'some_type',
         x1=typedef.GetSetProperty(W_SomeType.fget1),
         x2=typedef.GetSetProperty(W_SomeType.fget2),
         )
     space = self.space
     w_obj = space.wrap(W_SomeType())
     assert space.getattr(w_obj, space.wrap('x1')) == space.w_None
     assert space.getattr(w_obj, space.wrap('x2')) == space.w_None
Exemplo n.º 8
0
    def test_class_attr(self):
        class W_SomeType(W_Root):
            pass

        seen = []

        def make_me(space):
            seen.append(1)
            return space.wrap("foobar")

        W_SomeType.typedef = typedef.TypeDef('some_type',
                                             abc=typedef.ClassAttr(make_me))
        assert seen == []
        self.space.appexec([W_SomeType()], """(x):
            assert type(x).abc == "foobar"
            assert x.abc == "foobar"
            assert type(x).abc == "foobar"
        """)
        assert seen == [1]
Exemplo n.º 9
0
    def test_total_ordering(self):
        class W_SomeType(W_Root):
            def __init__(self, space, x):
                self.space = space
                self.x = x

            def descr__lt(self, w_other):
                assert isinstance(w_other, W_SomeType)
                return self.space.wrap(self.x < w_other.x)

            def descr__eq(self, w_other):
                assert isinstance(w_other, W_SomeType)
                return self.space.wrap(self.x == w_other.x)

        W_SomeType.typedef = typedef.TypeDef(
            'some_type',
            __total_ordering__ = 'auto',
            __lt__ = interp2app(W_SomeType.descr__lt),
            __eq__ = interp2app(W_SomeType.descr__eq),
            )
        space = self.space
        w_b = space.wrap(W_SomeType(space, 2))
        w_c = space.wrap(W_SomeType(space, 2))
        w_a = space.wrap(W_SomeType(space, 1))
        # explicitly defined
        assert space.is_true(space.lt(w_a, w_b))
        assert not space.is_true(space.eq(w_a, w_b))
        assert space.is_true(space.eq(w_b, w_c))
        # automatically defined
        assert space.is_true(space.le(w_a, w_b))
        assert space.is_true(space.le(w_b, w_c))
        assert space.is_true(space.gt(w_b, w_a))
        assert space.is_true(space.ge(w_b, w_a))
        assert space.is_true(space.ge(w_b, w_c))
        assert space.is_true(space.ne(w_a, w_b))
        assert not space.is_true(space.ne(w_b, w_c))
Exemplo n.º 10
0
            self.report_error("no exception")
        if not space.exception_match(w_exc_type, self.w_ExpectedException):
            self.report_error(space.text_w(space.repr(w_exc_type)))
        self.w_value = w_exc_value   # for the 'value' app-level attribute
        return space.w_True     # suppress the exception

    def report_error(self, got):
        space = self.space
        raise oefmt(space.w_AssertionError,
                    "raises() expected %s, but got %s",
                    space.text_w(space.repr(self.w_ExpectedException)),
                    got)

W_RaisesContextManager.typedef = typedef.TypeDef("RaisesContextManager",
    __enter__ = gateway.interp2app_temp(W_RaisesContextManager.enter),
    __exit__ = gateway.interp2app_temp(W_RaisesContextManager.exit),
    value = typedef.interp_attrproperty_w('w_value', cls=W_RaisesContextManager)
    )

def pypyraises(space, w_ExpectedException, w_expr=None, __args__=None):
    """A built-in function providing the equivalent of py.test.raises()."""
    if w_expr is None:
        return W_RaisesContextManager(space, w_ExpectedException)
    args_w, kwds_w = __args__.unpack()
    if space.isinstance_w(w_expr, space.w_text):
        if args_w:
            raise oefmt(space.w_TypeError,
                        "raises() takes no argument after a string expression")
        expr = space.unwrap(w_expr)
        source = py.code.Source(expr)
        frame = space.getexecutioncontext().gettopframe()
Exemplo n.º 11
0
    def test_destructor(self):
        space = self.space

        class W_Level1(W_Root):
            def __init__(self, space1):
                assert space1 is space

            def __del__(self):
                space.call_method(w_seen, 'append', space.wrap(1))

        class W_Level2(W_Root):
            def __init__(self, space1):
                assert space1 is space

            def __del__(self):
                self.enqueue_for_destruction(space, W_Level2.destructormeth,
                                             'FOO ')

            def destructormeth(self):
                space.call_method(w_seen, 'append', space.wrap(2))

        W_Level1.typedef = typedef.TypeDef(
            'level1', __new__=typedef.generic_new_descr(W_Level1))
        W_Level2.typedef = typedef.TypeDef(
            'level2', __new__=typedef.generic_new_descr(W_Level2))
        #
        w_seen = space.newlist([])
        W_Level1(space)
        gc.collect()
        gc.collect()
        assert space.unwrap(w_seen) == [1]
        #
        w_seen = space.newlist([])
        W_Level2(space)
        gc.collect()
        gc.collect()
        assert space.str_w(space.repr(w_seen)) == "[]"  # not called yet
        ec = space.getexecutioncontext()
        self.space.user_del_action.perform(ec, None)
        assert space.unwrap(w_seen) == [2]
        #
        w_seen = space.newlist([])
        self.space.appexec([self.space.gettypeobject(W_Level1.typedef)],
                           """(level1):
            class A3(level1):
                pass
            A3()
        """)
        gc.collect()
        gc.collect()
        assert space.unwrap(w_seen) == [1]
        #
        w_seen = space.newlist([])
        self.space.appexec(
            [self.space.gettypeobject(W_Level1.typedef), w_seen],
            """(level1, seen):
            class A4(level1):
                def __del__(self):
                    seen.append(4)
            A4()
        """)
        gc.collect()
        gc.collect()
        assert space.unwrap(w_seen) == [4, 1]
        #
        w_seen = space.newlist([])
        self.space.appexec([self.space.gettypeobject(W_Level2.typedef)],
                           """(level2):
            class A5(level2):
                pass
            A5()
        """)
        gc.collect()
        gc.collect()
        assert space.unwrap(w_seen) == [2]
        #
        w_seen = space.newlist([])
        self.space.appexec(
            [self.space.gettypeobject(W_Level2.typedef), w_seen],
            """(level2, seen):
            class A6(level2):
                def __del__(self):
                    seen.append(6)
            A6()
        """)
        gc.collect()
        gc.collect()
        assert space.unwrap(w_seen) == [6, 2]
Exemplo n.º 12
0
        assert not dbstate.standard_code
        space = self.space
        if space.isinstance_w(w_buffer, space.w_unicode):
            w_buffer = space.call_method(w_buffer, 'encode',
                                         space.newtext('utf-8'))   # safe?
        revdb.send_output(space.bytes_w(w_buffer))

def descr_get_softspace(space, revdb):
    return space.newint(revdb.softspace)
def descr_set_softspace(space, revdb, w_newvalue):
    revdb.softspace = space.int_w(w_newvalue)

W_RevDBOutput.typedef = typedef.TypeDef(
    "revdb_output",
    write = gateway.interp2app(W_RevDBOutput.descr_write),
    # XXX is 'softspace' still necessary in Python 3?
    softspace = typedef.GetSetProperty(descr_get_softspace,
                                       descr_set_softspace,
                                       cls=W_RevDBOutput),
    )

def revdb_displayhook(space, w_obj):
    """Modified sys.displayhook() that also outputs '$NUM = ',
    for non-prebuilt objects.  Such objects are then recorded in
    'printed_objects'.
    """
    assert not dbstate.standard_code
    if space.is_w(w_obj, space.w_None):
        return
    uid = revdb.get_unique_id(w_obj)
    if uid > 0:
        dbstate.printed_objects[uid] = w_obj