Beispiel #1
0
    def test_interp2app_unwrap_spec_typechecks(self):
        space = self.space
        w = space.wrap
        def g3_id(space, x):
            return space.wrap(x)
        app_g3_i = gateway.interp2app_temp(g3_id,
                                         unwrap_spec=[gateway.ObjSpace,
                                                      int])
        w_app_g3_i = space.wrap(app_g3_i)
        assert space.eq_w(space.call_function(w_app_g3_i,w(1)),w(1))
        assert space.eq_w(space.call_function(w_app_g3_i,w(1L)),w(1))        
        raises(gateway.OperationError,space.call_function,w_app_g3_i,w(sys.maxint*2))
        raises(gateway.OperationError,space.call_function,w_app_g3_i,w(None))
        raises(gateway.OperationError,space.call_function,w_app_g3_i,w("foo"))
        raises(gateway.OperationError,space.call_function,w_app_g3_i,w(1.0))

        app_g3_s = gateway.interp2app_temp(g3_id,
                                         unwrap_spec=[gateway.ObjSpace,
                                                      str])
        w_app_g3_s = space.wrap(app_g3_s)
        assert space.eq_w(space.call_function(w_app_g3_s,w("foo")),w("foo"))
        raises(gateway.OperationError,space.call_function,w_app_g3_s,w(None))
        raises(gateway.OperationError,space.call_function,w_app_g3_s,w(1))
        raises(gateway.OperationError,space.call_function,w_app_g3_s,w(1.0))
        
        app_g3_f = gateway.interp2app_temp(g3_id,
                                         unwrap_spec=[gateway.ObjSpace,
                                                      float])
        w_app_g3_f = space.wrap(app_g3_f)
        assert space.eq_w(space.call_function(w_app_g3_f,w(1.0)),w(1.0))
        assert space.eq_w(space.call_function(w_app_g3_f,w(1)),w(1.0))
        assert space.eq_w(space.call_function(w_app_g3_f,w(1L)),w(1.0))        
        raises(gateway.OperationError,space.call_function,w_app_g3_f,w(None))
        raises(gateway.OperationError,space.call_function,w_app_g3_f,w("foo"))
Beispiel #2
0
 def test_interp2app_doc(self):
     space = self.space
     def f(space, w_x):
         """foo"""
     w_f = space.wrap(gateway.interp2app_temp(f))
     assert space.unwrap(space.getattr(w_f, space.wrap('__doc__'))) == 'foo'
     #
     def g(space, w_x):
         never_called
     w_g = space.wrap(gateway.interp2app_temp(g, doc='bar'))
     assert space.unwrap(space.getattr(w_g, space.wrap('__doc__'))) == 'bar'
Beispiel #3
0
    def test_interp2app_unwrap_spec_typechecks(self):
        from rpython.rlib.rarithmetic import r_longlong

        space = self.space
        w = space.wrap

        def g3_id(space, x):
            return space.wrap(x)

        app_g3_i = gateway.interp2app_temp(g3_id,
                                           unwrap_spec=[gateway.ObjSpace, int])
        w_app_g3_i = space.wrap(app_g3_i)
        assert space.eq_w(space.call_function(w_app_g3_i, w(1)), w(1))
        assert space.eq_w(space.call_function(w_app_g3_i, w(1L)), w(1))
        space.raises_w(space.w_OverflowError, space.call_function, w_app_g3_i,
                       w(sys.maxint * 2))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_i,
                       w(None))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_i,
                       w("foo"))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_i,
                       w(1.0))

        app_g3_s = gateway.interp2app_temp(
            g3_id, unwrap_spec=[gateway.ObjSpace, 'text'])
        w_app_g3_s = space.wrap(app_g3_s)
        assert space.eq_w(space.call_function(w_app_g3_s, w("foo")), w("foo"))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_s,
                       w(None))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_s,
                       w(1))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_s,
                       w(1.0))

        app_g3_f = gateway.interp2app_temp(
            g3_id, unwrap_spec=[gateway.ObjSpace, float])
        w_app_g3_f = space.wrap(app_g3_f)
        assert space.eq_w(space.call_function(w_app_g3_f, w(1.0)), w(1.0))
        assert space.eq_w(space.call_function(w_app_g3_f, w(1)), w(1.0))
        assert space.eq_w(space.call_function(w_app_g3_f, w(1L)), w(1.0))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_f,
                       w(None))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_f,
                       w("foo"))

        app_g3_r = gateway.interp2app_temp(
            g3_id, unwrap_spec=[gateway.ObjSpace, r_longlong])
        w_app_g3_r = space.wrap(app_g3_r)
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_r,
                       w(1.0))
Beispiel #4
0
 def test_unwrap_spec_default_applevel_bug2(self):
     space = self.space
     def g(space, w_x, w_y=None, __args__=None):
         return w_x
     w_g = space.wrap(gateway.interp2app_temp(g))
     w_42 = space.call_function(w_g, space.wrap(42))
     assert space.int_w(w_42) == 42
     py.test.raises(gateway.OperationError, space.call_function, w_g)
     #
     def g(space, w_x, w_y=None, args_w=None):
         return w_x
     w_g = space.wrap(gateway.interp2app_temp(g))
     w_42 = space.call_function(w_g, space.wrap(42))
     assert space.int_w(w_42) == 42
     py.test.raises(gateway.OperationError, space.call_function, w_g)
Beispiel #5
0
    def test_interp2app_fastcall_method_with_space(self):
        class W_X(W_Root):
            def descr_f(self, space, w_x):
                return w_x

        app_f = gateway.interp2app_temp(
            W_X.descr_f,
            unwrap_spec=['self', gateway.ObjSpace, gateway.W_Root])

        w_app_f = self.space.wrap(app_f)

        assert isinstance(w_app_f.code, gateway.BuiltinCode2)

        called = []
        fastcall_2 = w_app_f.code.fastcall_2

        def witness_fastcall_2(space, w_func, w_a, w_b):
            called.append(w_func)
            return fastcall_2(space, w_func, w_a, w_b)

        w_app_f.code.fastcall_2 = witness_fastcall_2
        space = self.space

        w_res = space.call_function(w_app_f, W_X(), space.wrap(3))

        assert space.is_true(w_res)
        assert called == [w_app_f]
Beispiel #6
0
    def test_interp2app_fastcall_method(self):
        space = self.space
        w = space.wrap
        w_3 = w(3)

        def f(space, w_self, w_x):
            return w_x

        app_f = gateway.interp2app_temp(
            f, unwrap_spec=[gateway.ObjSpace, gateway.W_Root, gateway.W_Root])
        w_app_f = w(app_f)

        # sanity
        assert isinstance(w_app_f.code, gateway.BuiltinCode2)

        called = []
        fastcall_2 = w_app_f.code.fastcall_2

        def witness_fastcall_2(space, w_func, w_a, w_b):
            called.append(w_func)
            return fastcall_2(space, w_func, w_a, w_b)

        w_app_f.code.fastcall_2 = witness_fastcall_2

        w_res = space.appexec([w_app_f, w_3], """(f, x):
        class A(object):
           m = f # not a builtin function, so works as method
        y = A().m(x)
        b = A().m
        z = b(x)
        return y is x and z is x
        """)

        assert space.is_true(w_res)
        assert called == [w_app_f, w_app_f]
Beispiel #7
0
    def test_plain(self):
        space = self.space

        def g(space, w_a, w_x):
            return space.newtuple([space.wrap('g'), w_a, w_x])

        w_g = space.wrap(
            gateway.interp2app_temp(
                g,
                unwrap_spec=[gateway.ObjSpace, gateway.W_Root,
                             gateway.W_Root]))

        args = argument.Arguments(space, [space.wrap(-1), space.wrap(0)])

        w_res = space.call_args(w_g, args)
        assert space.is_true(space.eq(w_res, space.wrap(('g', -1, 0))))

        w_self = space.wrap('self')

        args0 = argument.Arguments(space, [space.wrap(0)])
        args = args0.prepend(w_self)

        w_res = space.call_args(w_g, args)
        assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 0))))

        args3 = argument.Arguments(space, [space.wrap(3)])
        w_res = space.call_obj_args(w_g, w_self, args3)
        assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 3))))
Beispiel #8
0
    def test_interp2app_fastcall_method(self):
        space = self.space
        w = space.wrap
        w_3 = w(3)

        def f(space, w_self, w_x):
            return w_x
        app_f = gateway.interp2app_temp(f, unwrap_spec=[gateway.ObjSpace,
                                                        gateway.W_Root,
                                                        gateway.W_Root])
        w_app_f = w(app_f)

        # sanity
        assert isinstance(w_app_f.code, gateway.BuiltinCode2)

        called = []
        fastcall_2 = w_app_f.code.fastcall_2
        def witness_fastcall_2(space, w_func, w_a, w_b):
            called.append(w_func)
            return fastcall_2(space, w_func, w_a, w_b)

        w_app_f.code.fastcall_2 = witness_fastcall_2    
    
        w_res = space.appexec([w_app_f, w_3], """(f, x):
        class A(object):
           m = f # not a builtin function, so works as method
        y = A().m(x)
        b = A().m
        z = b(x)
        return y is x and z is x
        """)

        assert space.is_true(w_res)
        assert called == [w_app_f, w_app_f]       
Beispiel #9
0
    def test_interp2app_fastcall_method_with_space(self):
        class W_X(W_Root):
            def descr_f(self, space, w_x):
                return w_x

        app_f = gateway.interp2app_temp(W_X.descr_f, unwrap_spec=['self',
                                        gateway.ObjSpace, gateway.W_Root])

        w_app_f = self.space.wrap(app_f)

        assert isinstance(w_app_f.code, gateway.BuiltinCode2)

        called = []
        fastcall_2 = w_app_f.code.fastcall_2
        def witness_fastcall_2(space, w_func, w_a, w_b):
            called.append(w_func)
            return fastcall_2(space, w_func, w_a, w_b)

        w_app_f.code.fastcall_2 = witness_fastcall_2
        space = self.space

        w_res = space.call_function(w_app_f, W_X(), space.wrap(3))

        assert space.is_true(w_res)
        assert called == [w_app_f]
Beispiel #10
0
    def test_interp2app_fastcall(self):
        space = self.space
        w = space.wrap
        w_3 = w(3)

        def f(space):
            return w_3
        app_f = gateway.interp2app_temp(f, unwrap_spec=[gateway.ObjSpace])
        w_app_f = w(app_f)

        # sanity
        assert isinstance(w_app_f.code, gateway.BuiltinCode0)

        called = []
        fastcall_0 = w_app_f.code.fastcall_0
        def witness_fastcall_0(space, w_func):
            called.append(w_func)
            return fastcall_0(space, w_func)

        w_app_f.code.fastcall_0 = witness_fastcall_0

        w_3 = space.newint(3)
        w_res = space.call_function(w_app_f)

        assert w_res is w_3
        assert called == [w_app_f]

        called = []

        w_res = space.appexec([w_app_f], """(f):
        return f()
        """)

        assert w_res is w_3
        assert called == [w_app_f]
Beispiel #11
0
    def setup_class(cls):
        space = gettestobjspace(usemodules=("thread", "time"))
        cls.space = space

        if option.runappdirect:

            def plain_waitfor(condition, delay=1):
                adaptivedelay = 0.04
                limit = time.time() + NORMAL_TIMEOUT * delay
                while time.time() <= limit:
                    time.sleep(adaptivedelay)
                    gc.collect()
                    if condition():
                        return
                    adaptivedelay *= 1.05
                print "*** timed out ***"

            cls.w_waitfor = plain_waitfor
        else:
            cls.w_waitfor = space.wrap(interp2app_temp(waitfor))
        cls.w_busywait = space.appexec(
            [],
            """():
            import time
            return time.sleep
        """,
        )
Beispiel #12
0
    def test_interp2app_unwrap_spec_utf8(self):
        space = self.space
        w = space.wrap

        def g3_u(space, utf8):
            return space.wrap(utf8)

        app_g3_u = gateway.interp2app_temp(
            g3_u, unwrap_spec=[gateway.ObjSpace, 'utf8'])
        w_app_g3_u = space.wrap(app_g3_u)
        encoded = u"gęść".encode('utf8')
        assert self.space.eq_w(space.call_function(w_app_g3_u, w(u"gęść")),
                               w(encoded))
        assert self.space.eq_w(space.call_function(w_app_g3_u, w("foo")),
                               w("foo"))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_u,
                       w(None))
        space.raises_w(space.w_TypeError, space.call_function, w_app_g3_u,
                       w(42))
        w_ascii = space.appexec([], """():
            import sys
            return sys.getdefaultencoding() == 'ascii'""")
        if space.is_true(w_ascii):
            raises(gateway.OperationError, space.call_function, w_app_g3_u,
                   w("\x80"))
Beispiel #13
0
    def test_interp2app_fastcall(self):
        space = self.space
        w = space.wrap
        w_3 = w(3)

        def f(space):
            return w_3
        app_f = gateway.interp2app_temp(f, unwrap_spec=[gateway.ObjSpace])
        w_app_f = w(app_f)

        # sanity
        assert isinstance(w_app_f.code, gateway.BuiltinCode0)

        called = []
        fastcall_0 = w_app_f.code.fastcall_0
        def witness_fastcall_0(space, w_func):
            called.append(w_func)
            return fastcall_0(space, w_func)

        w_app_f.code.fastcall_0 = witness_fastcall_0

        w_3 = space.newint(3)
        w_res = space.call_function(w_app_f)

        assert w_res is w_3
        assert called == [w_app_f]

        called = []

        w_res = space.appexec([w_app_f], """(f):
        return f()
        """)

        assert w_res is w_3
        assert called == [w_app_f]
Beispiel #14
0
    def test_plain(self):
        space = self.space

        def g(space, w_a, w_x):
            return space.newtuple([space.wrap('g'), w_a, w_x])

        w_g = space.wrap(gateway.interp2app_temp(g,
                         unwrap_spec=[gateway.ObjSpace,
                                      gateway.W_Root,
                                      gateway.W_Root]))

        args = argument.Arguments(space, [space.wrap(-1), space.wrap(0)])

        w_res = space.call_args(w_g, args)
        assert space.is_true(space.eq(w_res, space.wrap(('g', -1, 0))))
        
        w_self = space.wrap('self')

        args0 = argument.Arguments(space, [space.wrap(0)])
        args = args0.prepend(w_self)

        w_res = space.call_args(w_g, args)
        assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 0))))

        args3 = argument.Arguments(space, [space.wrap(3)])
        w_res = space.call_obj_args(w_g, w_self, args3)
        assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 3))))
Beispiel #15
0
    def test_unwrap_spec_default_applevel_bogus(self):
        space = self.space

        @gateway.unwrap_spec(w_x=WrappedDefault(42), y=int)
        def g(space, w_x, y):
            never_called

        py.test.raises(AssertionError, space.wrap, gateway.interp2app_temp(g))
Beispiel #16
0
    def test_unwrap_spec_default_applevel_bogus(self):
        space = self.space

        @gateway.unwrap_spec(w_x=WrappedDefault(42), y=int)
        def g(space, w_x, y):
            never_called

        py.test.raises(AssertionError, space.wrap, gateway.interp2app_temp(g))
Beispiel #17
0
 def test_func_defaults(self):
     from pypy.interpreter import gateway
     def g(w_a=None):
         pass
     app_g = gateway.interp2app_temp(g)
     space = self.space
     w_g = space.wrap(app_g)
     w_defs = space.getattr(w_g, space.wrap("func_defaults"))
     assert space.is_w(w_defs, space.w_None)
Beispiel #18
0
 def test_unwrap_spec_decorator(self):
     space = self.space
     @gateway.unwrap_spec(gateway.ObjSpace, gateway.W_Root, int)
     def g(space, w_thing, i):
         return space.newtuple([w_thing, space.wrap(i)])
     w_g = space.wrap(gateway.interp2app_temp(g))
     args = argument.Arguments(space, [space.wrap(-1), space.wrap(0)])
     w_res = space.call_args(w_g, args)
     assert space.eq_w(w_res, space.wrap((-1, 0)))
Beispiel #19
0
    def test_base_regular_descr_mismatch(self):
        space = self.space

        def f():
            raise gateway.DescrMismatch

        w_f = space.wrap(gateway.interp2app_temp(f, unwrap_spec=[]))
        args = argument.Arguments(space, [])
        space.raises_w(space.w_SystemError, space.call_args, w_f, args)
Beispiel #20
0
    def test_unwrap_spec_kwonly_default_2(self):
        space = self.space

        @gateway.unwrap_spec(w_x2=WrappedDefault(50))
        def g(space, w_x2=None):
            return w_x2

        w_g = space.wrap(gateway.interp2app_temp(g))
        w_res = space.call_function(w_g)
        assert space.eq_w(w_res, space.wrap(50))
Beispiel #21
0
    def test_interp2app_unwrap_spec_fsencode(self):
        space = self.space
        w = space.wrap

        def f(filename):
            return space.wrapbytes(filename)

        app_f = gateway.interp2app_temp(f, unwrap_spec=["fsencode"])
        w_app_f = space.wrap(app_f)
        assert space.eq_w(space.call_function(w_app_f, w(u"\udc80")), space.wrapbytes("\x80"))
Beispiel #22
0
def test_path_or_fd_nullable(space):
    @unwrap_spec(p=path_or_fd(allow_fd=False, nullable=True))
    def f(space, p=None):
        return p.w_path

    w_f = space.wrap(interp2app_temp(f))
    res = space.call_function(w_f, space.w_None)
    assert res is space.w_None
    res = space.call_function(w_f)
    assert res is space.w_None

    @unwrap_spec(p=path_or_fd(allow_fd=False))
    def g(space, p):
        return p

    w_g = space.wrap(interp2app_temp(g))
    with pytest.raises(OperationError) as exc:
        res = space.call_function(w_g, space.w_None)
    assert exc.value.match(space, space.w_TypeError)
Beispiel #23
0
    def test_interp2app_unwrap_spec_fsencode(self):
        space = self.space
        w = space.wrap

        def f(filename):
            return space.wrapbytes(filename)

        app_f = gateway.interp2app_temp(f, unwrap_spec=['fsencode'])
        w_app_f = space.wrap(app_f)
        assert space.eq_w(space.call_function(w_app_f, w(u'\udc80')),
                          space.wrapbytes('\x80'))
Beispiel #24
0
    def test_unwrap_spec_default_applevel_bytes(self):
        space = self.space

        @gateway.unwrap_spec(w_x=WrappedDefault("foo"))
        def g(space, w_x):
            return w_x

        w_g = space.wrap(gateway.interp2app_temp(g))
        args = argument.Arguments(space, [])
        w_res = space.call_args(w_g, args)
        assert space.eq_w(w_res, space.wrapbytes("foo"))
Beispiel #25
0
    def test_unwrap_spec_default_applevel_bytes(self):
        space = self.space

        @gateway.unwrap_spec(w_x=WrappedDefault('foo'))
        def g(space, w_x):
            return w_x

        w_g = space.wrap(gateway.interp2app_temp(g))
        args = argument.Arguments(space, [])
        w_res = space.call_args(w_g, args)
        assert space.eq_w(w_res, space.newbytes('foo'))
Beispiel #26
0
    def test_interp2app_unwrap_spec(self):
        space = self.space
        w = space.wrap

        def g3(space, w_a, w_b):
            return space.add(w_a, w_b)

        app_g3 = gateway.interp2app_temp(g3, unwrap_spec=[gateway.ObjSpace, gateway.W_Root, gateway.W_Root])
        w_app_g3 = space.wrap(app_g3)
        assert self.space.eq_w(space.call(w_app_g3, space.newtuple([w("foo"), w("bar")]), space.newdict()), w("foobar"))
        assert self.space.eq_w(space.call_function(w_app_g3, w("foo"), w("bar")), w("foobar"))
Beispiel #27
0
    def test_unwrap_spec_default_bytes(self):
        space = self.space

        @gateway.unwrap_spec(s='bufferstr')
        def g(space, s=''):
            return space.wrap(type(s) is str)

        w_g = space.wrap(gateway.interp2app_temp(g))
        args = argument.Arguments(space, [])
        w_res = space.call_args(w_g, args)
        assert space.eq_w(w_res, space.w_True)
Beispiel #28
0
    def test_interp2app_unwrap_spec_int_float(self):
        space = self.space
        w = space.wrap

        def g3_if(space, i0, f1):
            return space.wrap(i0 + f1)

        app_g3_if = gateway.interp2app_temp(g3_if, unwrap_spec=[gateway.ObjSpace, int, float])
        w_app_g3_if = space.wrap(app_g3_if)
        assert self.space.eq_w(space.call(w_app_g3_if, space.newtuple([w(1), w(1.0)]), space.newdict()), w(2.0))
        assert self.space.eq_w(space.call_function(w_app_g3_if, w(1), w(1.0)), w(2.0))
Beispiel #29
0
    def test_unwrap_spec_default_bytes(self):
        space = self.space

        @gateway.unwrap_spec(s="bufferstr")
        def g(space, s=""):
            return space.wrap(type(s) is str)

        w_g = space.wrap(gateway.interp2app_temp(g))
        args = argument.Arguments(space, [])
        w_res = space.call_args(w_g, args)
        assert space.eq_w(w_res, space.w_True)
Beispiel #30
0
    def test_posonly_args(self):
        space = self.space

        @gateway.unwrap_spec(w_x2=WrappedDefault(50))
        def g(space, w_t, w_x2, __posonly__):
            assert space.eq_w(w_t, space.newint(1))
            return w_x2

        w_g = space.wrap(gateway.interp2app_temp(g))
        w_res = space.call_function(w_g, space.wrap(1))
        assert space.eq_w(w_res, space.wrap(50))
Beispiel #31
0
    def test_pass_trough_arguments0_descr_mismatch(self):
        space = self.space

        def f(space, __args__):
            raise gateway.DescrMismatch

        w_f = space.wrap(
            gateway.interp2app_temp(
                f, unwrap_spec=[gateway.ObjSpace, gateway.Arguments]))
        args = argument.Arguments(space, [])
        space.raises_w(space.w_SystemError, space.call_args, w_f, args)
Beispiel #32
0
    def test_unwrap_spec_kwonly_default(self):
        space = self.space

        @gateway.unwrap_spec(w_x2=WrappedDefault(50), y2=int)
        def g(space, w_x1, w_x2, __kwonly__, w_y1, y2=200):
            return space.sub(space.sub(w_x1, w_x2), space.sub(w_y1, w(y2)))

        w_g = space.wrap(gateway.interp2app_temp(g))
        w = space.wrap
        w1 = w(1)

        for i in range(6):
            py.test.raises(gateway.OperationError, space.call_function, w_g,
                           *(i * (w1, )))

        def expected(x1, x2=50, y1="missing", y2=200):
            return (x1 - x2) - (y1 - y2)

        def check(*args, **kwds):
            a = argument.Arguments(space, [],
                                   w_stararg=w(args),
                                   w_starstararg=w(kwds))
            w_res = space.call_args(w_g, a)
            assert space.eq_w(w_res, w(expected(*args, **kwds)))

            del kwds['y1']
            a = argument.Arguments(space, [],
                                   w_stararg=w(args),
                                   w_starstararg=w(kwds))
            py.test.raises(gateway.OperationError, space.call_args, w_g, a)

            args += (1234, )
            a = argument.Arguments(space, [],
                                   w_stararg=w(args),
                                   w_starstararg=w(kwds))
            py.test.raises(gateway.OperationError, space.call_args, w_g, a)

        check(5, y1=1234)
        check(5, 1, y1=1234)
        check(5, x2=1, y1=1234)
        check(5, y1=1234, y2=343)
        check(5, 1, y1=1234, y2=343)
        check(5, x2=1, y1=1234, y2=343)
        check(
            x1=5,
            y1=1234,
        )
        check(
            x1=5,
            x2=1,
            y1=1234,
        )
        check(x1=5, y1=1234, y2=343)
        check(x1=5, x2=1, y1=1234, y2=343)
Beispiel #33
0
    def test_func_defaults(self):
        from pypy.interpreter import gateway

        def g(w_a=gateway.NoneNotWrapped):
            pass

        app_g = gateway.interp2app_temp(g)
        space = self.space
        w_g = space.wrap(app_g)
        w_defs = space.getattr(w_g, space.wrap("func_defaults"))
        assert space.is_w(w_defs, space.w_None)
Beispiel #34
0
    def test_interp2app_classmethod(self):
        space = self.space
        w = space.wrap

        def g_run(space, w_type):
            assert space.is_w(w_type, space.w_str)
            return w(42)

        app_g_run = gateway.interp2app_temp(g_run, unwrap_spec=[gateway.ObjSpace, gateway.W_Root], as_classmethod=True)
        w_app_g_run = space.wrap(app_g_run)
        w_bound = space.get(w_app_g_run, w("hello"), space.w_str)
        assert space.eq_w(space.call_function(w_bound), w(42))
Beispiel #35
0
    def test_interp2app_unwrap_spec_r_uint(self):
        space = self.space
        w = space.wrap

        def g3_ll(space, n):
            return space.wrap(n * 3)

        app_g3_ll = gateway.interp2app_temp(g3_ll, unwrap_spec=[gateway.ObjSpace, gateway.r_uint])
        w_app_g3_ll = space.wrap(app_g3_ll)
        w_big = w(gateway.r_uint(sys.maxint + 100))
        assert space.eq_w(space.call_function(w_app_g3_ll, w_big), w(gateway.r_uint((sys.maxint + 100) * 3)))
        space.raises_w(space.w_OverflowError, space.call_function, w_app_g3_ll, w(10L ** 100))
        space.raises_w(space.w_ValueError, space.call_function, w_app_g3_ll, w(-1))
Beispiel #36
0
    def test_interp2app_unwrap_spec_unicode(self):
        space = self.space
        w = space.wrap

        def g3_u(space, uni):
            return space.wrap(len(uni))

        app_g3_u = gateway.interp2app_temp(g3_u, unwrap_spec=[gateway.ObjSpace, unicode])
        w_app_g3_u = space.wrap(app_g3_u)
        assert self.space.eq_w(space.call_function(w_app_g3_u, w(u"foo")), w(3))
        assert self.space.eq_w(space.call_function(w_app_g3_u, w("baz")), w(3))
        raises(gateway.OperationError, space.call_function, w_app_g3_u, w(None))
        raises(gateway.OperationError, space.call_function, w_app_g3_u, w(42))
Beispiel #37
0
    def test_interp2app_unwrap_spec_args_w(self):
        space = self.space
        w = space.wrap

        def g3_args_w(space, args_w):
            return space.add(args_w[0], args_w[1])

        app_g3_args_w = gateway.interp2app_temp(g3_args_w, unwrap_spec=[gateway.ObjSpace, "args_w"])
        w_app_g3_args_w = space.wrap(app_g3_args_w)
        assert self.space.eq_w(
            space.call(w_app_g3_args_w, space.newtuple([w("foo"), w("bar")]), space.newdict()), w("foobar")
        )
        assert self.space.eq_w(space.call_function(w_app_g3_args_w, w("foo"), w("bar")), w("foobar"))
Beispiel #38
0
    def test_interp2app_unwrap_spec_typechecks(self):
        space = self.space
        w = space.wrap

        def g3_id(space, x):
            return space.wrap(x)

        app_g3_i = gateway.interp2app_temp(g3_id,
                                           unwrap_spec=[gateway.ObjSpace, int])
        w_app_g3_i = space.wrap(app_g3_i)
        assert space.eq_w(space.call_function(w_app_g3_i, w(1)), w(1))
        assert space.eq_w(space.call_function(w_app_g3_i, w(1L)), w(1))
        raises(gateway.OperationError, space.call_function, w_app_g3_i,
               w(sys.maxint * 2))
        raises(gateway.OperationError, space.call_function, w_app_g3_i,
               w(None))
        raises(gateway.OperationError, space.call_function, w_app_g3_i,
               w("foo"))
        raises(gateway.OperationError, space.call_function, w_app_g3_i, w(1.0))

        app_g3_s = gateway.interp2app_temp(g3_id,
                                           unwrap_spec=[gateway.ObjSpace, str])
        w_app_g3_s = space.wrap(app_g3_s)
        assert space.eq_w(space.call_function(w_app_g3_s, w("foo")), w("foo"))
        raises(gateway.OperationError, space.call_function, w_app_g3_s,
               w(None))
        raises(gateway.OperationError, space.call_function, w_app_g3_s, w(1))
        raises(gateway.OperationError, space.call_function, w_app_g3_s, w(1.0))

        app_g3_f = gateway.interp2app_temp(
            g3_id, unwrap_spec=[gateway.ObjSpace, float])
        w_app_g3_f = space.wrap(app_g3_f)
        assert space.eq_w(space.call_function(w_app_g3_f, w(1.0)), w(1.0))
        assert space.eq_w(space.call_function(w_app_g3_f, w(1)), w(1.0))
        assert space.eq_w(space.call_function(w_app_g3_f, w(1L)), w(1.0))
        raises(gateway.OperationError, space.call_function, w_app_g3_f,
               w(None))
        raises(gateway.OperationError, space.call_function, w_app_g3_f,
               w("foo"))
Beispiel #39
0
    def test_func_defaults(self):
        from pypy.interpreter import gateway

        def g(w_a=None):
            pass

        app_g = gateway.interp2app_temp(g)
        space = self.space
        w_g = space.wrap(app_g)
        w_defs = space.getattr(w_g, space.wrap("__defaults__"))
        assert space.is_w(w_defs, space.w_None)
        w_count = space.getattr(w_g, space.wrap("__defaults_count__"))
        assert space.unwrap(w_count) == 1
Beispiel #40
0
    def test_interp2app(self):
        space = self.space
        w = space.wrap

        def g3(space, w_a, w_b):
            return space.add(w_a, w_b)

        app_g3 = gateway.interp2app_temp(g3)
        w_app_g3 = space.wrap(app_g3)
        assert self.space.eq_w(
            space.call(w_app_g3, space.newtuple([w('foo'), w('bar')]),
                       space.newdict()), w('foobar'))
        assert self.space.eq_w(
            space.call_function(w_app_g3, w('foo'), w('bar')), w('foobar'))
Beispiel #41
0
    def test_interp2app_classmethod(self):
        space = self.space
        w = space.wrap
        def g_run(space, w_type):
            assert space.is_w(w_type, space.w_str)
            return w(42)

        app_g_run = gateway.interp2app_temp(g_run,
                                            unwrap_spec=[gateway.ObjSpace,
                                                         gateway.W_Root],
                                            as_classmethod=True)
        w_app_g_run = space.wrap(app_g_run)
        w_bound = space.get(w_app_g_run, w("hello"), space.w_str)
        assert space.eq_w(space.call_function(w_bound), w(42))
Beispiel #42
0
    def test_interp2app_unwrap_spec_unwrapper(self):
        space = self.space

        class Unwrapper(gateway.Unwrapper):
            def unwrap(self, space, w_value):
                return space.int_w(w_value)

        w = space.wrap

        def g3_u(space, value):
            return space.wrap(value + 1)

        app_g3_u = gateway.interp2app_temp(g3_u, unwrap_spec=[gateway.ObjSpace, Unwrapper])
        assert self.space.eq_w(space.call_function(w(app_g3_u), w(42)), w(43))
        raises(gateway.OperationError, space.call_function, w(app_g3_u), w(None))
Beispiel #43
0
 def test_interp2app(self):
     space = self.space
     w = space.wrap
     def g3(space, w_a, w_b):
         return space.add(w_a, w_b)
     app_g3 = gateway.interp2app_temp(g3)
     w_app_g3 = space.wrap(app_g3) 
     assert self.space.eq_w(
         space.call(w_app_g3, 
                    space.newtuple([w('foo'), w('bar')]),
                    space.newdict()),
         w('foobar'))
     assert self.space.eq_w(
         space.call_function(w_app_g3, w('foo'), w('bar')),
         w('foobar'))
Beispiel #44
0
    def test_interp2app_unwrap_spec_int_float(self):
        space = self.space
        w = space.wrap

        def g3_if(space, i0, f1):
            return space.wrap(i0 + f1)

        app_g3_if = gateway.interp2app_temp(
            g3_if, unwrap_spec=[gateway.ObjSpace, int, float])
        w_app_g3_if = space.wrap(app_g3_if)
        assert self.space.eq_w(
            space.call(w_app_g3_if, space.newtuple([w(1), w(1.0)]),
                       space.newdict()), w(2.0))
        assert self.space.eq_w(space.call_function(w_app_g3_if, w(1), w(1.0)),
                               w(2.0))
Beispiel #45
0
def build_pytest_assertion(space):
    def my_init(space, w_self, __args__):
        "Our new AssertionError.__init__()."
        w_parent_init = space.getattr(w_BuiltinAssertionError,
                                      space.wrap('__init__'))
        space.call_args(w_parent_init, __args__.prepend(w_self))
        framestack = space.getexecutioncontext().framestack
##        # Argh! we may see app-level helpers in the frame stack!
##        #       that's very probably very bad...
##        if frame.code.co_name == 'normalize_exception': 
##            frame = framestack.top(1)
        
        # if the assertion provided a message, don't do magic
        args_w, kwargs_w = __args__.unpack()
        if args_w: 
            w_msg = args_w[0]
        else:
            frame = framestack.top(0)
            runner = AppFrame(space, frame)
            try:
                source = runner.statement
                source = str(source).strip()
            except py.error.ENOENT: 
                source = None
            from pypy import conftest
            if source and not conftest.option.nomagic: 
                msg = exprinfo.interpret(source, runner, should_fail=True)
                space.setattr(w_self, space.wrap('args'),
                            space.newtuple([space.wrap(msg)]))
                w_msg = space.wrap(msg)
            else:
                w_msg = space.w_None
        space.setattr(w_self, space.wrap('msg'), w_msg)

    # build a new AssertionError class to replace the original one.
    w_BuiltinAssertionError = space.getitem(space.builtin.w_dict, 
                                            space.wrap('AssertionError'))
    w_metaclass = space.type(w_BuiltinAssertionError)
    w_init = space.wrap(gateway.interp2app_temp(my_init,
                                                unwrap_spec=[gateway.ObjSpace,
                                                             gateway.W_Root,
                                                             gateway.Arguments]))
    w_dict = space.newdict()
    space.setitem(w_dict, space.wrap('__init__'), w_init)
    return space.call_function(w_metaclass,
                               space.wrap('AssertionError'),
                               space.newtuple([w_BuiltinAssertionError]),
                               w_dict)
Beispiel #46
0
    def test_interp2app_unwrap_spec_unicode(self):
        space = self.space
        w = space.wrap

        def g3_u(space, uni):
            return space.wrap(len(uni))

        app_g3_u = gateway.interp2app_temp(
            g3_u, unwrap_spec=[gateway.ObjSpace, unicode])
        w_app_g3_u = space.wrap(app_g3_u)
        assert self.space.eq_w(space.call_function(w_app_g3_u, w(u"foo")),
                               w(3))
        assert self.space.eq_w(space.call_function(w_app_g3_u, w("baz")), w(3))
        raises(gateway.OperationError, space.call_function, w_app_g3_u,
               w(None))
        raises(gateway.OperationError, space.call_function, w_app_g3_u, w(42))
Beispiel #47
0
 def test_system_error(self):
     py.test.skip("we don't wrap a random exception inside SystemError "
                  "when untranslated, because it makes testing harder")
     class UnexpectedException(Exception):
         pass
     space = self.space
     def g(space):
         raise UnexpectedException
     w_g = space.wrap(gateway.interp2app_temp(g))
     e = py.test.raises(OperationError, space.appexec, [w_g], """(my_g):
         my_g()
     """)
     err = str(e.value)
     assert 'SystemError' in err
     assert ('unexpected internal exception (please '
             'report a bug): UnexpectedException') in err
Beispiel #48
0
    def test_interp2app_unwrap_spec_str(self):
        space = self.space
        w = space.wrap

        def g3_ss(space, s0, s1):
            return space.wrap(s0 + s1)

        app_g3_ss = gateway.interp2app_temp(
            g3_ss, unwrap_spec=[gateway.ObjSpace, str, str])
        w_app_g3_ss = space.wrap(app_g3_ss)
        assert self.space.eq_w(
            space.call(w_app_g3_ss, space.newtuple([w('foo'),
                                                    w('bar')]),
                       space.newdict()), w('foobar'))
        assert self.space.eq_w(
            space.call_function(w_app_g3_ss, w('foo'), w('bar')), w('foobar'))
Beispiel #49
0
    def test_interp2app_unwrap_spec_fsencode(self):
        import sys
        space = self.space
        w = space.wrap

        def f(filename):
            return space.newbytes(filename)

        app_f = gateway.interp2app_temp(f, unwrap_spec=['fsencode'])
        w_app_f = space.wrap(app_f)
        if sys.platform == 'win32':
            raises(gateway.OperationError, space.call_function, w_app_f,
                   w(u'\udc80'))
        else:
            assert space.eq_w(space.call_function(w_app_f, w(u'\udc80')),
                              space.newbytes('\x80'))
Beispiel #50
0
    def test_interp2app_unwrap_spec_r_longlong(self):
        space = self.space
        w = space.wrap

        def g3_ll(space, n):
            return space.wrap(n * 3)

        app_g3_ll = gateway.interp2app_temp(g3_ll, unwrap_spec=[gateway.ObjSpace, gateway.r_longlong])
        w_app_g3_ll = space.wrap(app_g3_ll)
        w_big = w(gateway.r_longlong(10 ** 10))
        assert space.eq_w(
            space.call(w_app_g3_ll, space.newtuple([w_big]), space.newdict()), w(gateway.r_longlong(3 * 10 ** 10))
        )
        assert space.eq_w(space.call_function(w_app_g3_ll, w_big), w(gateway.r_longlong(3 * 10 ** 10)))
        w_huge = w(10L ** 100)
        space.raises_w(space.w_OverflowError, space.call_function, w_app_g3_ll, w_huge)
Beispiel #51
0
    def test_interp2app_unwrap_spec_index(self):
        space = self.space
        w = space.wrap

        def g3_idx(space, idx0):
            return space.wrap(idx0 + 1)

        app_g3_idx = gateway.interp2app_temp(g3_idx, unwrap_spec=[gateway.ObjSpace, "index"])
        w_app_g3_idx = space.wrap(app_g3_idx)
        assert space.eq_w(space.call_function(w_app_g3_idx, w(123)), w(124))
        space.raises_w(
            space.w_OverflowError, space.call_function, w_app_g3_idx, space.mul(space.wrap(sys.maxint), space.wrap(7))
        )
        space.raises_w(
            space.w_OverflowError, space.call_function, w_app_g3_idx, space.mul(space.wrap(sys.maxint), space.wrap(-7))
        )
Beispiel #52
0
    def test_interp2app_unwrap_spec_unwrapper(self):
        space = self.space
        class Unwrapper(gateway.Unwrapper):
            def unwrap(self, space, w_value):
                return space.int_w(w_value)

        w = space.wrap
        def g3_u(space, value):
            return space.wrap(value + 1)
        app_g3_u = gateway.interp2app_temp(g3_u,
                                         unwrap_spec=[gateway.ObjSpace,
                                                      Unwrapper])
        assert self.space.eq_w(
            space.call_function(w(app_g3_u), w(42)), w(43))
        raises(gateway.OperationError, space.call_function,
               w(app_g3_u), w(None))
Beispiel #53
0
 def test_system_error(self):
     py.test.skip("we don't wrap a random exception inside SystemError "
                  "when untranslated, because it makes testing harder")
     class UnexpectedException(Exception):
         pass
     space = self.space
     def g(space):
         raise UnexpectedException
     w_g = space.wrap(gateway.interp2app_temp(g))
     e = py.test.raises(OperationError, space.appexec, [w_g], """(my_g):
         my_g()
     """)
     err = str(e.value)
     assert 'SystemError' in err
     assert ('unexpected internal exception (please '
             'report a bug): UnexpectedException') in err
Beispiel #54
0
def build_pytest_assertion(space):
    def my_init(space, w_self, __args__):
        "Our new AssertionError.__init__()."
        w_parent_init = space.getattr(w_BuiltinAssertionError,
                                      space.wrap('__init__'))
        space.call_args(w_parent_init, __args__.prepend(w_self))
##        # Argh! we may see app-level helpers in the frame stack!
##        #       that's very probably very bad...
##        ^^^the above comment may be outdated, but we are not sure

        # if the assertion provided a message, don't do magic
        args_w, kwargs_w = __args__.unpack()
        if args_w:
            w_msg = args_w[0]
        else:
            frame = space.getexecutioncontext().gettopframe()
            runner = AppFrame(space, frame)
            try:
                source = runner.statement
                source = str(source).strip()
            except py.error.ENOENT:
                source = None
            from pypy import conftest
            if source and py.test.config._assertstate.mode != "off":
                msg = interpret(source, runner, should_fail=True)
                space.setattr(w_self, space.wrap('args'),
                            space.newtuple([space.wrap(msg)]))
                w_msg = space.wrap(msg)
            else:
                w_msg = space.w_None
        space.setattr(w_self, space.wrap('msg'), w_msg)

    # build a new AssertionError class to replace the original one.
    w_BuiltinAssertionError = space.getitem(space.builtin.w_dict,
                                            space.wrap('AssertionError'))
    w_metaclass = space.type(w_BuiltinAssertionError)
    w_init = space.wrap(gateway.interp2app_temp(my_init))
    w_dict = space.getattr(w_BuiltinAssertionError, space.wrap('__dict__'))
    w_dict = space.call_method(w_dict, 'copy')
    # fixup __module__, since the new type will be is_heaptype() == True
    w_dict.setitem_str('__module__', space.getattr(w_BuiltinAssertionError,
                                                   space.wrap('__module__')))
    space.setitem(w_dict, space.wrap('__init__'), w_init)
    return space.call_function(w_metaclass,
                               space.wrap('AssertionError'),
                               space.newtuple([w_BuiltinAssertionError]),
                               w_dict)
Beispiel #55
0
 def test_unwrap_spec_default_applevel_2(self):
     space = self.space
     @gateway.unwrap_spec(w_x = (WrappedDefault(42)), y=int)
     def g(space, w_x, y=10):
         return space.add(w_x, space.wrap(y))
     w_g = space.wrap(gateway.interp2app_temp(g))
     args = argument.Arguments(space, [])
     w_res = space.call_args(w_g, args)
     assert space.eq_w(w_res, space.wrap(52))
     #
     args = argument.Arguments(space, [space.wrap(84)])
     w_res = space.call_args(w_g, args)
     assert space.eq_w(w_res, space.wrap(94))
     #
     args = argument.Arguments(space, [space.wrap(84), space.wrap(-1)])
     w_res = space.call_args(w_g, args)
     assert space.eq_w(w_res, space.wrap(83))
Beispiel #56
0
    def test_interp2app_unwrap_spec_func(self):
        space = self.space
        w = space.wrap
        def g_id(space, w_x):
            return w_x
        l =[]
        def checker(w_x):
            l.append(w_x)
            return w_x

        app_g_id = gateway.interp2app_temp(g_id,
                                           unwrap_spec=[gateway.ObjSpace,
                                                        (checker, gateway.W_Root)])
        w_app_g_id = space.wrap(app_g_id)
        assert space.eq_w(space.call_function(w_app_g_id,w("foo")),w("foo"))
        assert len(l) == 1
        assert space.eq_w(l[0], w("foo"))
Beispiel #57
0
 def test_interp2app_unwrap_spec_str(self):
     space = self.space
     w = space.wrap
     def g3_ss(space, s0, s1):
         return space.wrap(s0+s1)       
     app_g3_ss = gateway.interp2app_temp(g3_ss,
                                      unwrap_spec=[gateway.ObjSpace,
                                                   str,str])
     w_app_g3_ss = space.wrap(app_g3_ss) 
     assert self.space.eq_w(
         space.call(w_app_g3_ss, 
                    space.newtuple([w('foo'), w('bar')]),
                    space.newdict()),
         w('foobar'))
     assert self.space.eq_w(
         space.call_function(w_app_g3_ss, w('foo'), w('bar')),
         w('foobar'))