[pypy-svn] r26172 - in pypy/dist/pypy: interpreter objspace/cpy objspace/cpy/test
arigo at codespeak.net
arigo at codespeak.net
Sun Apr 23 11:33:49 CEST 2006
Author: arigo
Date: Sun Apr 23 11:33:47 2006
New Revision: 26172
Added:
pypy/dist/pypy/objspace/cpy/test/test_wrappable.py (contents, props changed)
pypy/dist/pypy/objspace/cpy/wrappable.py (contents, props changed)
Modified:
pypy/dist/pypy/interpreter/gateway.py
pypy/dist/pypy/objspace/cpy/capi.py
pypy/dist/pypy/objspace/cpy/objspace.py
Log:
Started work on exposing built-in functions in the CPyObjSpace.
Modified: pypy/dist/pypy/interpreter/gateway.py
==============================================================================
--- pypy/dist/pypy/interpreter/gateway.py (original)
+++ pypy/dist/pypy/interpreter/gateway.py Sun Apr 23 11:33:47 2006
@@ -374,6 +374,8 @@
frame_cls = type("BuiltinFrame_UwS_%s" % label, (BuiltinFrame,), d)
class MyBuiltinFrameFactory(BuiltinFrameFactory):
+ # export 'unwrap_spec' for inspection from outside gateway.py
+ unwrap_spec = self.unwrap_spec
def create(self, space, code, w_globals):
newframe = frame_cls(space, code, w_globals)
Modified: pypy/dist/pypy/objspace/cpy/capi.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/capi.py (original)
+++ pypy/dist/pypy/objspace/cpy/capi.py Sun Apr 23 11:33:47 2006
@@ -32,6 +32,13 @@
Py_GT = ctypes_platform.ConstantInteger('Py_GT')
Py_GE = ctypes_platform.ConstantInteger('Py_GE')
+ PyMethodDef = ctypes_platform.Struct('PyMethodDef',
+ [('ml_name', c_char_p),
+ ('ml_meth', c_void_p),
+ ('ml_flags', c_int),
+ ('ml_doc', c_char_p)])
+ METH_VARARGS = ctypes_platform.ConstantInteger('METH_VARARGS')
+
globals().update(ctypes_platform.configure(CConfig))
del CConfig
@@ -83,6 +90,10 @@
PyInt_FromLong.argtypes = [c_long]
PyInt_FromLong.restype = W_Object
+PyInt_AsLong = pythonapi.PyInt_AsLong
+PyInt_AsLong.argtypes = [W_Object]
+PyInt_AsLong.restype = c_long
+
###################################################
# ____________________ Strings ____________________
@@ -134,3 +145,20 @@
PyImport_ImportModule = pythonapi.PyImport_ImportModule
PyImport_ImportModule.argtypes = [c_char_p]
PyImport_ImportModule.restype = W_Object
+
+
+##############################################################
+# ____________________ Built-in functions ____________________
+
+PyArg_ParseTuple = pythonapi.PyArg_ParseTuple
+PyArg_ParseTuple.restype = c_int
+#PyArg_ParseTuple.argtypes = [W_Object, c_char_p, ...]
+
+PyArg_ParseTupleAndKeywords = pythonapi.PyArg_ParseTupleAndKeywords
+PyArg_ParseTupleAndKeywords.restype = c_int
+#PyArg_ParseTupleAndKeywords.argtypes = [W_Object, W_Object,
+# c_char_p, POINTER(c_char_p), ...]
+
+PyCFunction_NewEx = pythonapi.PyCFunction_NewEx
+PyCFunction_NewEx.argtypes = [POINTER(PyMethodDef), W_Object, W_Object]
+PyCFunction_NewEx.restype = W_Object
Modified: pypy/dist/pypy/objspace/cpy/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/objspace.py (original)
+++ pypy/dist/pypy/objspace/cpy/objspace.py Sun Apr 23 11:33:47 2006
@@ -1,4 +1,5 @@
from pypy.objspace.cpy.capi import *
+from pypy.annotation.pairtype import pair
from pypy.interpreter import baseobjspace
@@ -6,11 +7,12 @@
from pypy.objspace.cpy.capi import W_Object
def __init__(self):
- #self.fromcache = baseobjspace.InternalSpaceCache(self).getorbuild
+ self.fromcache = baseobjspace.InternalSpaceCache(self).getorbuild
self.w_int = W_Object(int)
self.w_None = W_Object(None)
self.w_False = W_Object(False)
self.w_True = W_Object(True)
+ self.wrap_cache = {}
def enter_cache_building_mode(self):
pass
@@ -22,6 +24,16 @@
return PyImport_ImportModule(name)
def wrap(self, x):
+ if isinstance(x, baseobjspace.Wrappable):
+ x = x.__spacebind__(self)
+ if isinstance(x, baseobjspace.Wrappable):
+ try:
+ return self.wrap_cache[x]
+ except KeyError:
+ import pypy.objspace.cpy.wrappable
+ result = pair(self, x).wrap()
+ self.wrap_cache[x] = result
+ return result
if x is None:
return self.w_None
if isinstance(x, int):
@@ -31,9 +43,14 @@
raise TypeError("wrap(%r)" % (x,))
wrap._annspecialcase_ = "specialize:wrap"
+ def unwrap(self, w_obj):
+ assert isinstance(w_obj, W_Object)
+ return w_obj.value
+
getattr = staticmethod(PyObject_GetAttr)
getitem = staticmethod(PyObject_GetItem)
setitem = staticmethod(PyObject_SetItem)
+ int_w = staticmethod(PyInt_AsLong)
def call_function(self, w_callable, *args_w):
w_args = self.newtuple(list(args_w)) # XXX not very efficient
Added: pypy/dist/pypy/objspace/cpy/test/test_wrappable.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cpy/test/test_wrappable.py Sun Apr 23 11:33:47 2006
@@ -0,0 +1,19 @@
+from pypy.objspace.cpy.objspace import CPyObjSpace
+from pypy.interpreter.function import BuiltinFunction
+from pypy.interpreter.gateway import interp2app, ObjSpace, W_Root
+
+
+def test_builtin_function():
+ def entrypoint(space, w_x):
+ x = space.int_w(w_x)
+ result = x * 7
+ return space.wrap(result)
+ entrypoint.unwrap_spec = [ObjSpace, W_Root]
+
+ space = CPyObjSpace()
+ func = interp2app(entrypoint).__spacebind__(space)
+ bltin = BuiltinFunction(func)
+ w_entrypoint = space.wrap(bltin)
+ w_result = space.call_function(w_entrypoint, space.wrap(-2))
+ result = space.int_w(w_result)
+ assert result == -14
Added: pypy/dist/pypy/objspace/cpy/wrappable.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cpy/wrappable.py Sun Apr 23 11:33:47 2006
@@ -0,0 +1,32 @@
+"""
+Support to turn interpreter objects (subclasses of Wrappable)
+into CPython objects (subclasses of W_Object).
+"""
+
+import py
+from pypy.annotation.pairtype import pair, pairtype
+from pypy.objspace.cpy.capi import *
+from pypy.objspace.cpy.objspace import CPyObjSpace
+from pypy.interpreter.function import BuiltinFunction
+from pypy.interpreter.gateway import BuiltinCode, ObjSpace, W_Root
+
+
+class __extend__(pairtype(CPyObjSpace, BuiltinFunction)):
+
+ def wrap((space, func)):
+ # make a built-in function
+ assert isinstance(func.code, BuiltinCode)
+ factory = func.code.framefactory
+ bltin = factory.behavior
+ unwrap_spec = factory.unwrap_spec
+
+ assert unwrap_spec[0] == ObjSpace
+ for spec in unwrap_spec[1:]:
+ assert spec == W_Root # XXX
+
+ def trampoline(*args):
+ args_w = [space.wrap(a) for a in args]
+ w_result = bltin(space, *args_w)
+ return space.unwrap(w_result)
+
+ return W_Object(trampoline)
More information about the Pypy-commit
mailing list