Commits

Amaury Forgeot d'Arc  committed 2dd5a00

cpyext: implement PyInstance_New (for oldstyle classes)

  • Participants
  • Parent commits 0a0a789

Comments (0)

Files changed (3)

File pypy/module/cpyext/classobject.py

         w_result.setdict(space, w_dict)
     return w_result
 
+@cpython_api([PyObject, PyObject, PyObject], PyObject)
+def PyInstance_New(space, w_cls, w_arg, w_kw):
+    """Create a new instance of a specific class.  The parameters arg and kw are
+    used as the positional and keyword parameters to the object's constructor."""
+    return space.call(w_cls, w_arg, w_kw)
+
 @cpython_api([PyObject, PyObject], PyObject, error=CANNOT_FAIL)
 def _PyInstance_Lookup(space, w_instance, w_name):
     name = space.str_w(w_name)

File pypy/module/cpyext/stubs.py

     """Return true if klass is a subclass of base. Return false in all other cases."""
     raise NotImplementedError
 
-@cpython_api([PyObject, PyObject, PyObject], PyObject)
-def PyInstance_New(space, cls, arg, kw):
-    """Create a new instance of a specific class.  The parameters arg and kw are
-    used as the positional and keyword parameters to the object's constructor."""
-    raise NotImplementedError
-
 @cpython_api([PyObject], rffi.INT_real, error=-1)
 def PyCodec_Register(space, search_function):
     """Register a new codec search function.

File pypy/module/cpyext/test/test_classobject.py

         w_class = space.appexec([], """():
             class C:
                 x = None
-                def __init__(self):
+                def __init__(self, *args, **kwargs):
                     self.x = 1
+                    self.args = args
+                    self.__dict__.update(kwargs)
             return C
         """)
 
         assert space.getattr(w_instance, space.wrap('x')) is space.w_None
         assert space.unwrap(space.getattr(w_instance, space.wrap('a'))) == 3
 
+        w_instance = api.PyInstance_New(w_class,
+                                        space.wrap((3,)), space.wrap(dict(y=2)))
+        assert space.unwrap(space.getattr(w_instance, space.wrap('x'))) == 1
+        assert space.unwrap(space.getattr(w_instance, space.wrap('y'))) == 2
+        assert space.unwrap(space.getattr(w_instance, space.wrap('args'))) == (3,)
+        
+
     def test_lookup(self, space, api):
         w_instance = space.appexec([], """():
             class C: