Author: Amaury Forgeot d'Arc <[email protected]>
Branch:
Changeset: r60328:2dd5a00fac80
Date: 2013-01-21 23:39 +0100
http://bitbucket.org/pypy/pypy/changeset/2dd5a00fac80/
Log: cpyext: implement PyInstance_New (for oldstyle classes)
diff --git a/pypy/module/cpyext/classobject.py
b/pypy/module/cpyext/classobject.py
--- a/pypy/module/cpyext/classobject.py
+++ b/pypy/module/cpyext/classobject.py
@@ -22,6 +22,12 @@
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)
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -176,12 +176,6 @@
"""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.
diff --git a/pypy/module/cpyext/test/test_classobject.py
b/pypy/module/cpyext/test/test_classobject.py
--- a/pypy/module/cpyext/test/test_classobject.py
+++ b/pypy/module/cpyext/test/test_classobject.py
@@ -7,8 +7,10 @@
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
""")
@@ -22,6 +24,13 @@
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:
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit