Author: Armin Rigo <[email protected]>
Branch: kill-someobject
Changeset: r57981:2fee47ecd40e
Date: 2012-10-10 18:44 +0200
http://bitbucket.org/pypy/pypy/changeset/2fee47ecd40e/

Log:    Fix fix fix in-progress

diff --git a/pypy/conftest.py b/pypy/conftest.py
--- a/pypy/conftest.py
+++ b/pypy/conftest.py
@@ -101,15 +101,7 @@
 def maketestobjspace(config=None):
     if config is None:
         config = make_config(option)
-    try:
-        space = make_objspace(config)
-    except OperationError, e:
-        check_keyboard_interrupt(e)
-        if option.verbose:
-            import traceback
-            traceback.print_exc()
-        py.test.fail("fatal: cannot initialize objspace: %r" %
-                         (config.objspace.name,))
+    space = make_objspace(config)
     space.startup() # Initialize all builtin modules
     space.setitem(space.builtin.w_dict, space.wrap('AssertionError'),
                   appsupport.build_pytest_assertion(space))
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -448,6 +448,7 @@
     - positional arguments must be as many as the function parameters
     - keywords arguments allow to change only some parameter specs
     """
+    assert spec or kwargs
     def decorator(func):
         if kwargs:
             if spec:
@@ -829,18 +830,28 @@
         self.__name__ = f.func_name
         self.name = app_name
         self.as_classmethod = as_classmethod
-        self._staticdefs = list(f.func_defaults or ())
+
+        if not f.func_defaults:
+            self._staticdefs = []
+        else:
+            from pypy.interpreter import pycode
+            defaults = f.func_defaults
+            argnames, _, _ = pycode.cpython_code_signature(f.func_code)
+            self._staticdefs = zip(argnames[-len(defaults):], defaults)
         return self
 
     def _getdefaults(self, space):
         "NOT_RPYTHON"
-        import pdb; pdb.set_trace()
         defs_w = []
-        for val in self._staticdefs:
-            if val is NoneNotWrapped:
+        for name, defaultval in self._staticdefs:
+            if name.startswith('w_'):
+                assert defaultval is None, (
+                    "%s: default value for '%s' can only be None; "
+                    "use unwrap_spec(...=(W_Root, 'default_expr'))" % (
+                    self._code.identifier, name))
                 defs_w.append(None)
             else:
-                defs_w.append(space.wrap(val))
+                defs_w.append(space.wrap(defaultval))
         return defs_w
 
     # lazy binding to space
diff --git a/pypy/interpreter/test/test_gateway.py 
b/pypy/interpreter/test/test_gateway.py
--- a/pypy/interpreter/test/test_gateway.py
+++ b/pypy/interpreter/test/test_gateway.py
@@ -571,6 +571,46 @@
         unwrap_spec = gateway.BuiltinCode(f)._unwrap_spec
         assert unwrap_spec == [ObjSpace, W_Root, int]
 
+    def test_unwrap_spec_default_applevel(self):
+        space = self.space
+        @gateway.unwrap_spec(w_x = (W_Root, 'space.wrap(42)'))
+        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.wrap(42))
+        #
+        args = argument.Arguments(space, [space.wrap(84)])
+        w_res = space.call_args(w_g, args)
+        assert space.eq_w(w_res, space.wrap(84))
+
+    def test_unwrap_spec_default_applevel_2(self):
+        space = self.space
+        @gateway.unwrap_spec(w_x = (W_Root, 'space.wrap(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))
+
+    def test_unwrap_spec_default_applevel_bogus(self):
+        space = self.space
+        @gateway.unwrap_spec(w_x = (W_Root, 'space.wrap(42)'), y=int)
+        def g(space, w_x, y):
+            never_called
+        py.test.raises(AssertionError, space.wrap, gateway.interp2app_temp(g))
+
+
 class AppTestPyTestMark:
     @py.test.mark.unlikely_to_exist
     def test_anything(self):
diff --git a/pypy/module/__builtin__/operation.py 
b/pypy/module/__builtin__/operation.py
--- a/pypy/module/__builtin__/operation.py
+++ b/pypy/module/__builtin__/operation.py
@@ -9,7 +9,6 @@
 from pypy.rlib.rfloat import isnan, isinf, round_double
 from pypy.rlib import rfloat
 import __builtin__
-NoneNotWrapped = gateway.NoneNotWrapped
 
 def abs(space, w_val):
     "abs(number) -> number\n\nReturn the absolute value of the argument."
diff --git a/pypy/module/sys/vm.py b/pypy/module/sys/vm.py
--- a/pypy/module/sys/vm.py
+++ b/pypy/module/sys/vm.py
@@ -5,7 +5,7 @@
 
 from pypy.interpreter import gateway
 from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import unwrap_spec, NoneNotWrapped
+from pypy.interpreter.gateway import unwrap_spec
 from pypy.rlib import jit
 from pypy.rlib.runicode import MAXUNICODE
 
@@ -252,7 +252,7 @@
     cdll = RawCDLL(handle)
     return space.wrap(W_CDLL(space, "python api", cdll))
 
-def getsizeof(space, w_object, w_default=NoneNotWrapped):
+def getsizeof(space, w_object, w_default=None):
     """Not implemented on PyPy."""
     if w_default is None:
         raise OperationError(space.w_TypeError,
diff --git a/pypy/objspace/std/floattype.py b/pypy/objspace/std/floattype.py
--- a/pypy/objspace/std/floattype.py
+++ b/pypy/objspace/std/floattype.py
@@ -2,7 +2,8 @@
 import sys
 from pypy.rlib.unroll import unrolling_iterable
 from pypy.rlib import rfloat, rarithmetic
-from pypy.interpreter import gateway, typedef
+from pypy.interpreter import typedef
+from pypy.interpreter.gateway import interp2app, unwrap_spec, W_Root
 from pypy.interpreter.baseobjspace import ObjSpace, W_Root
 from pypy.interpreter.error import OperationError
 from pypy.objspace.std.register_all import register_all
@@ -21,7 +22,8 @@
 register_all(vars(), globals())
 
 
-def descr__new__(space, w_floattype, w_x=0.0):
+@unwrap_spec(w_x = (W_Root, 'space.wrap(0.0)'))
+def descr__new__(space, w_floattype, w_x):
     from pypy.objspace.std.floatobject import W_FloatObject
     w_value = w_x     # 'x' is the keyword argument name in CPython
     w_special = space.lookup(w_value, "__float__")
@@ -86,7 +88,7 @@
 
 _double_format, _float_format = detect_floatformat()
 
[email protected]_spec(kind=str)
+@unwrap_spec(kind=str)
 def descr___getformat__(space, w_cls, kind):
     if kind == "float":
         return space.wrap(_float_format)
@@ -111,7 +113,7 @@
         i = co_end - 1 - j
     return _hex_from_char(s[i])
 
[email protected]_spec(s=str)
+@unwrap_spec(s=str)
 def descr_fromhex(space, w_cls, s):
     length = len(s)
     i = 0
@@ -274,12 +276,10 @@
     __doc__ = '''float(x) -> floating point number
 
 Convert a string or number to a floating point number, if possible.''',
-    __new__ = gateway.interp2app(descr__new__),
-    __getformat__ = gateway.interp2app(descr___getformat__,
-                                       as_classmethod=True),
-    fromhex = gateway.interp2app(descr_fromhex,
-                                 as_classmethod=True),
-    conjugate = gateway.interp2app(descr_conjugate),
+    __new__ = interp2app(descr__new__),
+    __getformat__ = interp2app(descr___getformat__, as_classmethod=True),
+    fromhex = interp2app(descr_fromhex, as_classmethod=True),
+    conjugate = interp2app(descr_conjugate),
     real = typedef.GetSetProperty(descr_get_real),
     imag = typedef.GetSetProperty(descr_get_imag),
 )
diff --git a/pypy/objspace/std/stdtypedef.py b/pypy/objspace/std/stdtypedef.py
--- a/pypy/objspace/std/stdtypedef.py
+++ b/pypy/objspace/std/stdtypedef.py
@@ -186,10 +186,11 @@
     app_defaults = multimethod.extras.get('defaults', ())
     i = len(argnames) - len(app_defaults)
     wrapper_signature = wrapper_arglist[:]
+    unwrap_spec_kwds = {}
     for app_default in app_defaults:
         name = wrapper_signature[i]
-        wrapper_signature[i] = '%s=%s' % (name, name)
-        miniglobals[name] = app_default
+        unwrap_spec_kwds[name] = (gateway.W_Root,
+                                  'space.wrap(%r)' % (app_default,))
         i += 1
 
     wrapper_signature.insert(0, wrapper_signature.pop(selfindex))
@@ -239,7 +240,10 @@
 """        % (prefix, wrapper_sig, renaming, expr,
               multimethod.operatorsymbol, ', '.join(solid_arglist))
     exec compile2(code, '', 'exec') in miniglobals 
-    return miniglobals["%s_perform_call" % prefix]
+    func = miniglobals["%s_perform_call" % prefix]
+    if unwrap_spec_kwds:
+        func = gateway.unwrap_spec(**unwrap_spec_kwds)(func)
+    return func
 
 def wrap_trampoline_in_gateway(func, methname, multimethod):
     """NOT_RPYTHON"""
diff --git a/pypy/objspace/std/stringtype.py b/pypy/objspace/std/stringtype.py
--- a/pypy/objspace/std/stringtype.py
+++ b/pypy/objspace/std/stringtype.py
@@ -1,4 +1,4 @@
-from pypy.interpreter import gateway
+from pypy.interpreter.gateway import interp2app, unwrap_spec, W_Root
 from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
 from pypy.objspace.std.basestringtype import basestring_typedef
 from pypy.objspace.std.register_all import register_all
@@ -290,7 +290,8 @@
 
 # ____________________________________________________________
 
-def descr__new__(space, w_stringtype, w_object=''):
+@unwrap_spec(w_object = (W_Root, 'space.wrap("")'))
+def descr__new__(space, w_stringtype, w_object):
     # NB. the default value of w_object is really a *wrapped* empty string:
     #     there is gateway magic at work
     from pypy.objspace.std.stringobject import W_StringObject
@@ -311,7 +312,7 @@
 # ____________________________________________________________
 
 str_typedef = StdTypeDef("str", basestring_typedef,
-    __new__ = gateway.interp2app(descr__new__),
+    __new__ = interp2app(descr__new__),
     __doc__ = '''str(object) -> string
 
 Return a nice string representation of the object.
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to