~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: Alexander Belchenko
  • Date: 2007-01-30 23:05:35 UTC
  • mto: This revision was merged to the branch mainline in revision 2259.
  • Revision ID: bialix@ukr.net-20070130230535-kx1rd478rtigyc3v
standalone installer: win98 support

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
    needed.
49
49
    """
50
50
 
51
 
    __slots__ = ('_scope', '_factory', '_name', '_real_obj')
52
 
 
53
 
    # Setting this to True will allow you to do x = y, and still access members
54
 
    # from both variables. This should not normally be enabled, but is useful
55
 
    # when building documentation.
56
 
    _should_proxy = False
 
51
    __slots__ = ('_scope', '_factory', '_name')
57
52
 
58
53
    def __init__(self, scope, factory, name):
59
54
        """Create a temporary object in the specified scope.
64
59
            It will be passed (self, scope, name)
65
60
        :param name: The variable name in the given scope.
66
61
        """
67
 
        object.__setattr__(self, '_scope', scope)
68
 
        object.__setattr__(self, '_factory', factory)
69
 
        object.__setattr__(self, '_name', name)
70
 
        object.__setattr__(self, '_real_obj', None)
 
62
        self._scope = scope
 
63
        self._factory = factory
 
64
        self._name = name
71
65
        scope[name] = self
72
66
 
73
67
    def _replace(self):
87
81
                          " to another variable?",
88
82
                extra=e)
89
83
        obj = factory(self, scope, name)
90
 
        if ScopeReplacer._should_proxy:
91
 
            object.__setattr__(self, '_real_obj', obj)
92
84
        scope[name] = obj
93
85
        return obj
94
86
 
100
92
        # del self._name
101
93
 
102
94
    def __getattribute__(self, attr):
103
 
        obj = object.__getattribute__(self, '_real_obj')
104
 
        if obj is None:
105
 
            _replace = object.__getattribute__(self, '_replace')
106
 
            obj = _replace()
107
 
            _cleanup = object.__getattribute__(self, '_cleanup')
108
 
            _cleanup()
 
95
        _replace = object.__getattribute__(self, '_replace')
 
96
        obj = _replace()
 
97
        _cleanup = object.__getattribute__(self, '_cleanup')
 
98
        _cleanup()
109
99
        return getattr(obj, attr)
110
100
 
111
 
    def __setattr__(self, attr, value):
112
 
        obj = object.__getattribute__(self, '_real_obj')
113
 
        if obj is None:
114
 
            _replace = object.__getattribute__(self, '_replace')
115
 
            obj = _replace()
116
 
            _cleanup = object.__getattribute__(self, '_cleanup')
117
 
            _cleanup()
118
 
        return setattr(obj, attr, value)
119
 
 
120
101
    def __call__(self, *args, **kwargs):
121
102
        _replace = object.__getattribute__(self, '_replace')
122
103
        obj = _replace()
174
155
            assert not children, \
175
156
                'Cannot supply both a member and children'
176
157
 
177
 
        object.__setattr__(self, '_import_replacer_children', children)
178
 
        object.__setattr__(self, '_member', member)
179
 
        object.__setattr__(self, '_module_path', module_path)
 
158
        self._import_replacer_children = children
 
159
        self._member = member
 
160
        self._module_path = module_path
180
161
 
181
162
        # Indirecting through __class__ so that children can
182
163
        # override _import (especially our instrumented version)