Author: Antonio Cuni <[email protected]>
Branch: cpyext-avoid-roundtrip
Changeset: r92668:ed10b210f7b9
Date: 2017-10-09 08:56 +0200
http://bitbucket.org/pypy/pypy/changeset/ed10b210f7b9/
Log: port PyObject_Init{,Var} to C
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -628,6 +628,7 @@
'_PyTraceMalloc_Track', '_PyTraceMalloc_Untrack', 'PyMem_Malloc',
'Py_IncRef', 'Py_DecRef', 'PyObject_Free', 'PyObject_GC_Del',
'PyType_GenericAlloc',
'_PyObject_New', '_PyObject_NewVar', '_PyObject_GC_New',
+ 'PyObject_Init', 'PyObject_InitVar',
]
TYPES = {}
FORWARD_DECLS = []
diff --git a/pypy/module/cpyext/include/object.h
b/pypy/module/cpyext/include/object.h
--- a/pypy/module/cpyext/include/object.h
+++ b/pypy/module/cpyext/include/object.h
@@ -346,6 +346,10 @@
PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
+PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
+PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
+ PyTypeObject *, Py_ssize_t);
+
/* PyPy internal ----------------------------------- */
PyAPI_FUNC(int) PyPyType_Register(PyTypeObject *);
diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -161,31 +161,6 @@
space.delitem(w_obj, w_key)
return 0
-# CCC port to C
-@cpython_api([PyObject, PyTypeObjectPtr], PyObject, result_is_ll=True)
-def PyObject_Init(space, obj, type):
- """Initialize a newly-allocated object op with its type and initial
- reference. Returns the initialized object. If type indicates that the
- object participates in the cyclic garbage detector, it is added to the
- detector's set of observed objects. Other fields of the object are not
- affected."""
- if not obj:
- PyErr_NoMemory(space)
- obj.c_ob_type = type
- obj.c_ob_pypy_link = 0
- obj.c_ob_refcnt = 1
- return obj
-
-# CCC port to C
-@cpython_api([PyVarObject, PyTypeObjectPtr, Py_ssize_t], PyObject,
result_is_ll=True)
-def PyObject_InitVar(space, py_obj, type, size):
- """This does everything PyObject_Init() does, and also initializes the
- length information for a variable-size object."""
- if not py_obj:
- PyErr_NoMemory(space)
- py_obj.c_ob_size = size
- return PyObject_Init(space, rffi.cast(PyObject, py_obj), type)
-
@cpython_api([PyObject], PyObject)
def PyObject_Type(space, w_obj):
"""When o is non-NULL, returns a type object corresponding to the object
type
diff --git a/pypy/module/cpyext/src/object.c b/pypy/module/cpyext/src/object.c
--- a/pypy/module/cpyext/src/object.c
+++ b/pypy/module/cpyext/src/object.c
@@ -128,3 +128,23 @@
else
return PyObject_InitVar((PyVarObject*)py_obj, type, nitems);
}
+
+PyObject *
+PyObject_Init(PyObject *obj, PyTypeObject *type)
+{
+ if (!obj)
+ PyErr_NoMemory();
+ obj->ob_type = type;
+ obj->ob_pypy_link = 0;
+ obj->ob_refcnt = 1;
+ return obj;
+}
+
+PyVarObject *
+PyObject_InitVar(PyVarObject *obj, PyTypeObject *type, Py_ssize_t size)
+{
+ if (!obj)
+ PyErr_NoMemory();
+ obj->ob_size = size;
+ return PyObject_Init((PyObject*)obj, type);
+}
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit