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

« back to all changes in this revision

Viewing changes to pypy/objspace/std/transparent.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
""" transparent.py - Several transparent proxy helpers
 
3
"""
 
4
 
 
5
from pypy.interpreter import gateway
 
6
from pypy.interpreter.function import Function
 
7
from pypy.interpreter.error import OperationError
 
8
from pypy.objspace.std.proxyobject import *
 
9
from pypy.objspace.std.typeobject import W_TypeObject
 
10
 
 
11
def proxy(space, w_type, w_controller):
 
12
    """tproxy(typ, controller) -> obj
 
13
Return something that looks like it is of type typ. Its behaviour is
 
14
completely controlled by the controller."""
 
15
    from pypy.interpreter.typedef import Function, PyTraceback, PyFrame, \
 
16
        PyCode, GeneratorIterator
 
17
    
 
18
    if not space.is_true(space.callable(w_controller)):
 
19
        raise OperationError(space.w_TypeError, space.wrap("controller should be function"))
 
20
    
 
21
    if isinstance(w_type, W_TypeObject):
 
22
        if space.is_true(space.issubtype(w_type, space.w_list)):
 
23
            return W_TransparentList(space, w_type, w_controller)
 
24
        if space.is_true(space.issubtype(w_type, space.w_dict)):
 
25
            return W_TransparentDict(space, w_type, w_controller)
 
26
        if space.is_true(space.issubtype(w_type, space.gettypeobject(Function.typedef))):
 
27
            return W_TransparentFunction(space, w_type, w_controller)
 
28
        if space.is_true(space.issubtype(w_type, space.gettypeobject(PyTraceback.typedef))):
 
29
            return W_TransparentTraceback(space, w_type, w_controller)
 
30
        if space.is_true(space.issubtype(w_type, space.gettypeobject(PyFrame.typedef))):
 
31
            return W_TransparentFrame(space, w_type, w_controller)
 
32
        if space.is_true(space.issubtype(w_type, space.gettypeobject(GeneratorIterator.typedef))):
 
33
            return W_TransparentGenerator(space, w_type, w_controller)
 
34
        if space.is_true(space.issubtype(w_type, space.gettypeobject(PyCode.typedef))):
 
35
            return W_TransparentCode(space, w_type, w_controller)
 
36
        if w_type.instancetypedef is space.w_object.instancetypedef:
 
37
            return W_Transparent(space, w_type, w_controller)
 
38
    else:
 
39
        raise OperationError(space.w_TypeError, space.wrap("type expected as first argument"))
 
40
    #return type_cache[w_type or w_type.w_bestbase]
 
41
    raise OperationError(space.w_TypeError, space.wrap("Object type %s could not "\
 
42
          "be wrapped (YET)" % w_type.getname(space, "?")))
 
43
 
 
44
def proxy_controller(space, w_object):
 
45
    """get_tproxy_controller(obj) -> controller
 
46
If obj is really a transparent proxy, return its controller. Otherwise return
 
47
None."""
 
48
    if isinstance(w_object, W_Transparent):
 
49
        return w_object.w_controller
 
50
    if isinstance(w_object, W_TransparentObject):
 
51
        return w_object.w_controller
 
52
    return None
 
53
 
 
54
app_proxy = gateway.interp2app(proxy, unwrap_spec=[gateway.ObjSpace, gateway.W_Root, \
 
55
    gateway.W_Root])
 
56
app_proxy_controller = gateway.interp2app(proxy_controller, unwrap_spec=[gateway.ObjSpace, gateway.W_Root])