[pypy-svn] r72486 - in pypy/trunk/pypy/module/cpyext: . include test
xoraxax at codespeak.net
xoraxax at codespeak.net
Sun Mar 21 04:58:13 CET 2010
Author: xoraxax
Date: Sun Mar 21 04:58:11 2010
New Revision: 72486
Modified:
pypy/trunk/pypy/module/cpyext/api.py
pypy/trunk/pypy/module/cpyext/dictobject.py
pypy/trunk/pypy/module/cpyext/include/dictobject.h
pypy/trunk/pypy/module/cpyext/include/modsupport.h
pypy/trunk/pypy/module/cpyext/include/object.h
pypy/trunk/pypy/module/cpyext/include/tupleobject.h
pypy/trunk/pypy/module/cpyext/include/typeobject.c
pypy/trunk/pypy/module/cpyext/include/varargwrapper.c
pypy/trunk/pypy/module/cpyext/modsupport.py
pypy/trunk/pypy/module/cpyext/object.py
pypy/trunk/pypy/module/cpyext/pyerrors.py
pypy/trunk/pypy/module/cpyext/state.py
pypy/trunk/pypy/module/cpyext/test/foo.c
pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
pypy/trunk/pypy/module/cpyext/tupleobject.py
pypy/trunk/pypy/module/cpyext/typeobject.py
Log:
General progress, not sure how to implement the object and type.
Modified: pypy/trunk/pypy/module/cpyext/api.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/api.py (original)
+++ pypy/trunk/pypy/module/cpyext/api.py Sun Mar 21 04:58:11 2010
@@ -1,4 +1,5 @@
import ctypes
+import sys
import py
@@ -180,6 +181,7 @@
PyObject *PyPy_True = NULL;
PyObject *PyPy_False = NULL;
PyObject *PyPyExc_Exception = NULL;
+ PyObject *PyPyExc_TypeError = NULL;
PyTypeObject *PyPyType_Type = NULL;
PyTypeObject *PyPyBaseObject_Type = NULL;
"""
@@ -193,8 +195,8 @@
eci = ExternalCompilationInfo(
include_dirs=include_dirs,
separate_module_sources=[code],
- #separate_module_files=[include_dir / "typeobject.c",
- # include_dir / "varargwrapper.c"],
+ separate_module_files=[include_dir / "typeobject.c",
+ include_dir / "varargwrapper.c"],
export_symbols=['pypyAPI'] + export_symbols,
)
eci = eci.convert_sources_to_files()
@@ -212,6 +214,7 @@
("PyPy_True", space.w_True),
("PyPy_False", space.w_False),
("PyPyExc_Exception", space.w_Exception),
+ ("PyPyExc_TypeError", space.w_TypeError),
("PyPyType_Type", space.w_type),
("PyPyBaseObject_Type", space.w_object),
]:
@@ -223,26 +226,36 @@
def wrapper(*args):
boxed_args = []
# XXX use unrolling_iterable here
+ print >>sys.stderr, callable
for i, typ in enumerate(callable.api_func.argtypes):
arg = args[i]
if typ is PyObject:
arg = from_ref(space, arg)
boxed_args.append(arg)
+ state = space.fromcache(State)
try:
retval = callable(space, *boxed_args)
+ print "Callable worked"
except OperationError, e:
e.normalize_exception(space)
- state = space.fromcache(State)
state.exc_type = e.w_type
state.exc_value = e.get_w_value(space)
+ except BaseException, e:
+ state.exc_type = space.w_SystemError
+ state.exc_value = space.wrap(str(e))
+ import traceback
+ traceback.print_exc()
+
+ if state.exc_value is not None:
restype = callable.api_func.restype
if restype is lltype.Void:
return
if restype is PyObject:
- return lltype.nullptr(PyObject)
- if restype is lltype.Signed:
+ return lltype.nullptr(PyObject.TO)
+ if restype in (lltype.Signed, rffi.INT):
return -1
assert False, "Unknown return type"
+
if callable.api_func.restype is PyObject:
retval = make_ref(space, retval, borrowed=callable.api_func.borrowed)
return retval
Modified: pypy/trunk/pypy/module/cpyext/dictobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/dictobject.py (original)
+++ pypy/trunk/pypy/module/cpyext/dictobject.py Sun Mar 21 04:58:11 2010
@@ -1,4 +1,25 @@
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import cpython_api, PyObject
+from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
+ at cpython_api([], PyObject)
+def PyDict_New(space):
+ return space.newdict()
+ at cpython_api([PyObject], rffi.INT)
+def PyDict_Check(space, w_obj):
+ w_type = space.w_dict
+ w_obj_type = space.type(w_obj)
+ return space.is_w(w_obj_type, w_type) or space.is_true(space.issubtype(w_obj_type, w_type))
+
+
+ at cpython_api([PyObject, rffi.CCHARP, PyObject], rffi.INT)
+def PyDict_SetItemString(space, w_dict, key_ptr, w_obj):
+ if PyDict_Check(space, w_dict):
+ key = rffi.charp2str(key_ptr)
+ # our dicts dont have a standardized interface, so we need
+ # to go through the space
+ space.setitem(w_dict, space.wrap(key), w_obj)
+ return 0
+ else:
+ PyErr_BadInternalCall()
Modified: pypy/trunk/pypy/module/cpyext/include/dictobject.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/dictobject.h (original)
+++ pypy/trunk/pypy/module/cpyext/include/dictobject.h Sun Mar 21 04:58:11 2010
@@ -7,6 +7,8 @@
extern "C" {
#endif
+PyObject * PyDict_New(void);
+int PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
#ifdef __cplusplus
}
Modified: pypy/trunk/pypy/module/cpyext/include/modsupport.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/modsupport.h (original)
+++ pypy/trunk/pypy/module/cpyext/include/modsupport.h Sun Mar 21 04:58:11 2010
@@ -8,6 +8,8 @@
#endif
PyObject *Py_InitModule(const char* name, PyMethodDef* methods);
+PyObject * PyModule_GetDict(PyObject *);
+
#ifdef __cplusplus
}
Modified: pypy/trunk/pypy/module/cpyext/include/object.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/object.h (original)
+++ pypy/trunk/pypy/module/cpyext/include/object.h Sun Mar 21 04:58:11 2010
@@ -387,7 +387,7 @@
/* objimpl.h ----------------------------------------------*/
-PyObject * _PyObject_New(PyTypeObject *);
+PyObject * _PyObject_New(PyObject *);
// PyVarObject * _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
#define PyObject_New(type, typeobj) \
@@ -397,4 +397,7 @@
void PyObject_Del(void *);
+/* PyPy internal ----------------------------------- */
+int PyPyType_Register(PyTypeObject *);
+
#endif
Modified: pypy/trunk/pypy/module/cpyext/include/tupleobject.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/tupleobject.h (original)
+++ pypy/trunk/pypy/module/cpyext/include/tupleobject.h Sun Mar 21 04:58:11 2010
@@ -9,6 +9,8 @@
PyObject * PyTuple_New(Py_ssize_t size);
PyObject * PyTuple_Pack(Py_ssize_t, ...);
+PyTuple_SetItem(PyObject *, Py_ssize_t, PyObject *);
+#define PyTuple_Pack PyPyTuple_Pack
#ifdef __cplusplus
}
Modified: pypy/trunk/pypy/module/cpyext/include/typeobject.c
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/typeobject.c (original)
+++ pypy/trunk/pypy/module/cpyext/include/typeobject.c Sun Mar 21 04:58:11 2010
@@ -75,7 +75,7 @@
goto error;
type->tp_dict = dict;
}
-
+#if 0
/* Add type-specific descriptors to tp_dict */
if (add_operators(type) < 0)
goto error;
@@ -163,9 +163,14 @@
add_subclass((PyTypeObject *)b, type) < 0)
goto error;
}
-
+#endif
/* All done -- set the ready flag */
assert(type->tp_dict != NULL);
+
+ /* PYPY ADDITION */
+ if (PyPyType_Register(type) < 0)
+ goto error;
+
type->tp_flags =
(type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
return 0;
Modified: pypy/trunk/pypy/module/cpyext/include/varargwrapper.c
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/varargwrapper.c (original)
+++ pypy/trunk/pypy/module/cpyext/include/varargwrapper.c Sun Mar 21 04:58:11 2010
@@ -2,7 +2,7 @@
#include <Python.h>
#include <stdarg.h>
-PyObject * PyTuple_Pack(Py_ssize_t size, ...)
+PyObject * PyPyTuple_Pack(Py_ssize_t size, ...)
{
va_list ap;
PyObject *cur, *tuple;
@@ -10,9 +10,10 @@
tuple = PyTuple_New(size);
va_start(ap, size);
- for (i = 0; i < size; cur = va_arg(ap, PyObject*)) {
+ for (i = 0; i < size; cur = va_arg(ap, PyObject*), i++) {
Py_INCREF(cur);
- PyTuple_SetItem(tuple, i, cur);
+ if (PyTuple_SetItem(tuple, i, cur) < 0)
+ return NULL;
}
va_end(ap);
return tuple;
Modified: pypy/trunk/pypy/module/cpyext/modsupport.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/modsupport.py (original)
+++ pypy/trunk/pypy/module/cpyext/modsupport.py Sun Mar 21 04:58:11 2010
@@ -2,6 +2,7 @@
from pypy.module.cpyext.api import cpython_api, cpython_struct, PyObject
from pypy.interpreter.module import Module
from pypy.module.cpyext.methodobject import PyCFunction_NewEx
+from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
PyCFunction = lltype.Ptr(lltype.FuncType([PyObject, PyObject], PyObject))
@@ -39,3 +40,17 @@
w_function)
i = i + 1
return w_mod
+
+ at cpython_api([PyObject], rffi.INT)
+def PyModule_Check(space, w_obj):
+ w_type = space.gettypeobject(Module.typedef)
+ w_obj_type = space.type(w_obj)
+ return space.is_w(w_obj_type, w_type) or space.is_true(space.issubtype(w_obj_type, w_type))
+
+ at cpython_api([PyObject], PyObject)
+def PyModule_GetDict(space, w_mod):
+ if PyModule_Check(space, w_mod):
+ assert isinstance(w_mod, Module)
+ return w_mod.getdict()
+ else:
+ PyErr_BadInternalCall()
Modified: pypy/trunk/pypy/module/cpyext/object.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/object.py (original)
+++ pypy/trunk/pypy/module/cpyext/object.py Sun Mar 21 04:58:11 2010
@@ -1,10 +1,17 @@
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import cpython_api, PyObject, make_ref
from pypy.module.cpyext.typeobject import PyTypeObjectPtr
+from pypy.objspace.std.objectobject import W_ObjectObject
- at cpython_api([PyTypeObjectPtr], PyObject)
-def _PyObject_New(space, pto):
- return space.wrap(42) # XXX
+def get_cls_for_type_object(space, w_type):
+ if space.is_w(w_type, space.w_object):
+ return W_ObjectObject
+ assert False, "Please add more cases!"
+
+ at cpython_api([PyObject], PyObject)
+def _PyObject_New(space, w_type):
+ cls = get_cls_for_type_object(space, w_type)
+ return space.allocate_instance(cls, w_type)
@cpython_api([rffi.VOIDP_real], lltype.Void)
def PyObject_Del(space, w_obj):
Modified: pypy/trunk/pypy/module/cpyext/pyerrors.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/pyerrors.py (original)
+++ pypy/trunk/pypy/module/cpyext/pyerrors.py Sun Mar 21 04:58:11 2010
@@ -2,9 +2,15 @@
from pypy.interpreter.error import OperationError
from pypy.module.cpyext.api import cpython_api, PyObject, make_ref
+
@cpython_api([PyObject, rffi.CCHARP], lltype.Void)
def PyErr_SetString(space, w_type, message_ptr):
message = rffi.charp2str(message_ptr)
w_obj = space.call_function(w_type, space.wrap(message))
raise OperationError(w_type, w_obj)
+
+ at cpython_api([], lltype.Void)
+def PyErr_BadInternalCall(space):
+ raise OperationError(space.w_SystemError, space.wrap("Bad internal call!"))
+
Modified: pypy/trunk/pypy/module/cpyext/state.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/state.py (original)
+++ pypy/trunk/pypy/module/cpyext/state.py Sun Mar 21 04:58:11 2010
@@ -15,5 +15,6 @@
if exc_type is not None or exc_value is not None:
self.exc_value = None
self.exc_type = None
- raise OperationError(exc_type, exc_value)
+ op_err = OperationError(exc_type, exc_value)
+ raise op_err
Modified: pypy/trunk/pypy/module/cpyext/test/foo.c
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/foo.c (original)
+++ pypy/trunk/pypy/module/cpyext/test/foo.c Sun Mar 21 04:58:11 2010
@@ -139,5 +139,5 @@
return;
d = PyModule_GetDict(m);
PyDict_SetItemString(d, "fooType", (PyObject *)&footype);
- /* No need to check the error here, the caller will do that */
+ /* No need to check the error here, the caller will do that */
}
Modified: pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_typeobject.py (original)
+++ pypy/trunk/pypy/module/cpyext/test/test_typeobject.py Sun Mar 21 04:58:11 2010
@@ -9,4 +9,5 @@
import sys
module = self.import_module(name='foo')
assert 'foo' in sys.modules
+ print module.fooType
assert module.new().name == "Foo Example"
Modified: pypy/trunk/pypy/module/cpyext/tupleobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/tupleobject.py (original)
+++ pypy/trunk/pypy/module/cpyext/tupleobject.py Sun Mar 21 04:58:11 2010
@@ -11,3 +11,4 @@
def PyTuple_SetItem(space, w_t, pos, w_obj):
assert isinstance(w_t, W_TupleObject)
w_t.wrappeditems[pos] = w_obj
+ return 0
Modified: pypy/trunk/pypy/module/cpyext/typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/typeobject.py (original)
+++ pypy/trunk/pypy/module/cpyext/typeobject.py Sun Mar 21 04:58:11 2010
@@ -1,10 +1,16 @@
+import ctypes
+
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.rpython.lltypesystem.lltype import Ptr, FuncType, Void
-
+from pypy.interpreter.gateway import ObjSpace, W_Root
+from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.baseobjspace import Wrappable
+from pypy.interpreter.typedef import TypeDef
from pypy.module.cpyext.api import cpython_api, cpython_struct, PyObject,\
PyObjectFields, Py_ssize_t, Py_TPFLAGS_READYING, Py_TPFLAGS_READY
from pypy.interpreter.module import Module
from pypy.module.cpyext.modsupport import PyMethodDef
+from pypy.module.cpyext.state import State
PyTypeObject = lltype.ForwardReference()
PyTypeObjectPtr = lltype.Ptr(PyTypeObject)
@@ -133,8 +139,41 @@
"PyTypeObject",
PyTypeObjectFields, PyTypeObject)
+
+class W_PyCTypeObject(Wrappable):
+ pass
+
+class W_PyCObject(Wrappable):
+ pass
+
+ at unwrap_spec(ObjSpace, W_Root, W_Root)
+def cobject_descr_getattr(space, w_obj, w_name):
+ name = space.str_w(w_name)
+ return w_name
+
+
def allocate_type_obj(space, w_obj):
py_obj = lltype.malloc(PyTypeObject, None, flavor="raw")
return py_obj
+def create_type_object(space, pto):
+ return space.gettypeobject(W_PyCTypeObject)
+ at cpython_api([PyTypeObjectPtr], rffi.INT)
+def PyPyType_Register(space, pto):
+ state = space.fromcache(State)
+ ptr = ctypes.addressof(pto._obj._storage)
+ if ptr not in state.py_objects_r2w:
+ w_obj = create_type_object(space, pto)
+ state.py_objects_r2w[ptr] = w_obj
+ state.py_objects_w2r[w_obj] = pto
+ return 1
+
+W_PyCObject.typedef = TypeDef(
+ 'C_object',
+ __getattr__ = interp2app(cobject_descr_getattr),
+ )
+
+W_PyCTypeObject.typedef = TypeDef(
+ 'C_type'
+ )
More information about the Pypy-commit
mailing list