summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGodefroid Chapelle <gotcha@bubblenet.be>2007-06-06 09:23:18 +0000
committerGodefroid Chapelle <gotcha@bubblenet.be>2007-06-06 09:23:18 +0000
commitca1ae01e5b2ffe3ddd148cf6ad687c82b601f220 (patch)
tree712dd8cebae27c393faf5b0f1cefae9171cc3fc5
parentcc3822a32f681749725d59b50ecb4bbf0a4c0355 (diff)
downloadzope-tal-ca1ae01e5b2ffe3ddd148cf6ad687c82b601f220.tar.gz
small example used to test rpython patterns
-rw-r--r--src/zope/tal/pypy/test/__init__.py12
-rw-r--r--src/zope/tal/pypy/test/appzpt.py0
-rw-r--r--src/zope/tal/pypy/test/zpt.py73
3 files changed, 85 insertions, 0 deletions
diff --git a/src/zope/tal/pypy/test/__init__.py b/src/zope/tal/pypy/test/__init__.py
new file mode 100644
index 0000000..674a75a
--- /dev/null
+++ b/src/zope/tal/pypy/test/__init__.py
@@ -0,0 +1,12 @@
+from pypy.interpreter.mixedmodule import MixedModule
+
+class Module(MixedModule):
+ """A ZPT module."""
+
+ interpleveldefs = {
+ 'test' : 'zpt.test',
+ 'Interpreter' : 'zpt.Interpreter',
+ }
+
+ appleveldefs = {
+ }
diff --git a/src/zope/tal/pypy/test/appzpt.py b/src/zope/tal/pypy/test/appzpt.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/zope/tal/pypy/test/appzpt.py
diff --git a/src/zope/tal/pypy/test/zpt.py b/src/zope/tal/pypy/test/zpt.py
new file mode 100644
index 0000000..78c4453
--- /dev/null
+++ b/src/zope/tal/pypy/test/zpt.py
@@ -0,0 +1,73 @@
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root, Wrappable
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.typedef import TypeDef, GetSetProperty
+
+def importModule(space, name):
+ w_builtin = space.getbuiltinmodule('__builtin__')
+ w_import = space.getattr(w_builtin, space.wrap('__import__'))
+ w_module = space.call_function(w_import, space.wrap(name))
+ return w_module
+
+def test(space, n):
+ return space.wrap(n + 1)
+test.unwrap_spec = [ObjSpace, int]
+
+class Interpreter(Wrappable):
+ def __init__(self, space, w_instance, code=1):
+ self.space = space
+ self.code = code
+ self.w_instance = w_instance
+
+ def multiply(self, w_y):
+ space = self.space
+ y = space.int_w(w_y)
+ return space.wrap(self.code * y)
+
+ def fget_code(space, self):
+ return space.wrap(self.code)
+
+ def fset_code(space, self, w_value):
+ self.code = space.int_w(w_value)
+
+ def call(self):
+ space = self.space
+ w_random = importModule(space, 'random')
+ w_randrange = space.getattr(w_random, space.wrap('randrange'))
+ rand = space.call_function(w_randrange, space.wrap(self.code))
+ return rand
+
+ def power2(self):
+ p = Power2(self.code)
+ return self.space.wrap(p.compute())
+
+ def getInstance(self):
+ space = self.space
+ if space.is_w(self.w_instance, space.wrap(None)):
+ return
+ w_append =space.getattr(self.w_instance, 'append')
+ space.call_function(w_append, space.wrap(3))
+ return self.w_instance
+
+class Power2:
+ def __init__(self, value):
+ self.value = value
+
+ def compute(self):
+ return self.value * self.value
+
+
+
+def interpreter_new(space, w_subtype, w_instance, x):
+ return space.wrap(Interpreter(space, w_instance, x))
+interpreter_new.unwrap_spec = [ObjSpace, W_Root, W_Root, int]
+
+getset_code = GetSetProperty(Interpreter.fget_code, Interpreter.fset_code, cls=Interpreter)
+
+Interpreter.typedef = TypeDef('Interpreter',
+ __new__ = interp2app(interpreter_new),
+ code = getset_code,
+ multiply = interp2app(Interpreter.multiply),
+ __call__ = interp2app(Interpreter.call),
+ power2 = interp2app(Interpreter.power2),
+ getInstance = interp2app(Interpreter.getInstance),
+)