~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/objspace/cpy/property.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Support to turn GetSetProperty objects into W_Objects containing a
 
3
property object of CPython.
 
4
"""
 
5
 
 
6
from pypy.interpreter.baseobjspace import SpaceCache
 
7
from pypy.interpreter.gateway import ObjSpace, W_Root, interp2app
 
8
from pypy.interpreter.function import BuiltinFunction
 
9
from pypy.objspace.cpy.objspace import W_Object
 
10
 
 
11
 
 
12
class PropertyCache(SpaceCache):
 
13
    def build(cache, prop):
 
14
        space = cache.space
 
15
 
 
16
        reslist = []
 
17
        for f, arity in [(prop.fget, 1),
 
18
                         (prop.fset, 2),
 
19
                         (prop.fdel, 1)]:
 
20
            if f is None:
 
21
                res = None
 
22
            else:
 
23
                unwrap_spec = [ObjSpace] + [W_Root]*arity
 
24
                func = interp2app(f, unwrap_spec=unwrap_spec)
 
25
                func = func.__spacebind__(space)
 
26
                bltin = BuiltinFunction(func)
 
27
                res = space.wrap(bltin).value
 
28
            reslist.append(res)
 
29
 
 
30
        # make a CPython property
 
31
        p = property(reslist[0], reslist[1], reslist[2], prop.doc)
 
32
 
 
33
        w_result = W_Object(p)
 
34
        space.wrap_cache[id(w_result)] = w_result, p, follow_annotations
 
35
        return w_result
 
36
 
 
37
 
 
38
def follow_annotations(bookkeeper, w_property):
 
39
    from pypy.annotation import model as annmodel
 
40
    p = w_property.value
 
41
    for f, arity, key in [(p.fget, 1, 'get'),
 
42
                          (p.fset, 2, 'set'),
 
43
                          (p.fdel, 1, 'del')]:
 
44
        if f is not None:
 
45
            s_func = bookkeeper.immutablevalue(f)
 
46
            args_s = [annmodel.SomeObject()] * arity
 
47
            uniquekey = p, key
 
48
            bookkeeper.emulate_pbc_call(uniquekey, s_func, args_s)