[pypy-svn] r28725 - in pypy/dist/pypy/objspace/cpy: . test
arigo at codespeak.net
arigo at codespeak.net
Mon Jun 12 19:40:53 CEST 2006
Author: arigo
Date: Mon Jun 12 19:40:52 2006
New Revision: 28725
Modified:
pypy/dist/pypy/objspace/cpy/function.py
pypy/dist/pypy/objspace/cpy/test/test_typedef.py
Log:
Hey, attaching (non-special) methods to exported types just works! Cool.
Modified: pypy/dist/pypy/objspace/cpy/function.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/function.py (original)
+++ pypy/dist/pypy/objspace/cpy/function.py Mon Jun 12 19:40:52 2006
@@ -28,6 +28,18 @@
tramp.wrappings.append('%s = ___W_Object(%s)' % (argname, basename))
tramp.passedargs.append(argname)
+ def visit__Wrappable(self, el, orig_sig, tramp):
+ clsname = el.__name__ # XXX name clashes, but in gateway.py too
+ tramp.miniglobals[clsname] = el
+ argname = orig_sig.next_arg()
+ assert not argname.startswith('w_')
+ tramp.inputargs.append(argname)
+ tramp.wrappings.append('%s = ___space.interp_w(%s, ___W_Object(%s))'
+ % (argname,
+ clsname,
+ argname))
+ tramp.passedargs.append(argname)
+
def visit__object(self, el, orig_sig, tramp):
convertermap = {int: '___PyInt_AsLong',
str: '___PyString_AsString',
@@ -47,6 +59,7 @@
self.inputargs = []
self.wrappings = []
self.passedargs = []
+ self.miniglobals = {}
def reraise(e):
@@ -72,6 +85,15 @@
unwrap_spec = factory.unwrap_spec
tramp = TrampolineSignature()
+ tramp.miniglobals = {
+ '___space': space,
+ '___W_Object': CPyObjSpace.W_Object,
+ '___PyInt_AsLong': PyInt_AsLong,
+ '___PyString_AsString': PyString_AsString,
+ '___bltin': bltin,
+ '___OperationError': OperationError,
+ '___reraise': reraise,
+ }
from pypy.interpreter import pycode
argnames, varargname, kwargname = pycode.cpython_code_signature(
@@ -98,15 +120,7 @@
sourcelines.append(' return w_result.value')
sourcelines.append('')
- miniglobals = {
- '___space': space,
- '___W_Object': CPyObjSpace.W_Object,
- '___PyInt_AsLong': PyInt_AsLong,
- '___PyString_AsString': PyString_AsString,
- '___bltin': bltin,
- '___OperationError': OperationError,
- '___reraise': reraise,
- }
+ miniglobals = tramp.miniglobals
exec py.code.Source('\n'.join(sourcelines)).compile() in miniglobals
trampoline = miniglobals['trampoline']
Modified: pypy/dist/pypy/objspace/cpy/test/test_typedef.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/test/test_typedef.py (original)
+++ pypy/dist/pypy/objspace/cpy/test/test_typedef.py Mon Jun 12 19:40:52 2006
@@ -10,8 +10,14 @@
class W_MyType(Wrappable):
- def __init__(self, space):
+ def __init__(self, space, x=1):
self.space = space
+ self.x = x
+
+ def multiply(self, w_y):
+ space = self.space
+ y = space.int_w(w_y)
+ return space.wrap(self.x * y)
def test_direct():
@@ -93,3 +99,19 @@
assert type(res).__name__ == 'MyType'
assert res.hello == 7
assert type(res).hello == 7
+
+
+def test_method():
+ W_MyType.typedef = TypeDef("MyType",
+ multiply = interp2app(W_MyType.multiply))
+ space = CPyObjSpace()
+ assert space.int_w(W_MyType(space, 6).multiply(space.wrap(7))) == 42
+
+ def make_mytype():
+ return space.wrap(W_MyType(space, 123))
+ fn = compile(make_mytype, [],
+ annotatorpolicy = CPyAnnotatorPolicy(space))
+
+ res = fn(expected_extra_mallocs=1)
+ assert type(res).__name__ == 'MyType'
+ assert res.multiply(3) == 369
More information about the Pypy-commit
mailing list