~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: Aaron Bentley
  • Date: 2007-11-09 01:55:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2976.
  • Revision ID: aaron.bentley@utoronto.ca-20071109015537-q83rlmzstfggjebb
Fix exception when ScopeReplacer is assigned to before retrieving any members

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
            It will be passed (self, scope, name)
65
65
        :param name: The variable name in the given scope.
66
66
        """
67
 
        self._scope = scope
68
 
        self._factory = factory
69
 
        self._name = name
70
 
        self._real_obj = None
 
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)
71
71
        scope[name] = self
72
72
 
73
73
    def _replace(self):
88
88
                extra=e)
89
89
        obj = factory(self, scope, name)
90
90
        if ScopeReplacer._should_proxy:
91
 
            self._real_obj = obj
 
91
            object.__setattr__(self, '_real_obj', obj)
92
92
        scope[name] = obj
93
93
        return obj
94
94
 
108
108
            _cleanup()
109
109
        return getattr(obj, attr)
110
110
 
 
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
 
111
120
    def __call__(self, *args, **kwargs):
112
121
        _replace = object.__getattribute__(self, '_replace')
113
122
        obj = _replace()
165
174
            assert not children, \
166
175
                'Cannot supply both a member and children'
167
176
 
168
 
        self._import_replacer_children = children
169
 
        self._member = member
170
 
        self._module_path = module_path
 
177
        object.__setattr__(self, '_import_replacer_children', children)
 
178
        object.__setattr__(self, '_member', member)
 
179
        object.__setattr__(self, '_module_path', module_path)
171
180
 
172
181
        # Indirecting through __class__ so that children can
173
182
        # override _import (especially our instrumented version)