~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

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()
161
170
            from foo import bar, baz would get translated into 2 import
162
171
            requests. On for 'name=bar' and one for 'name=baz'
163
172
        """
164
 
        if member is not None:
165
 
            assert not children, \
166
 
                'Cannot supply both a member and children'
 
173
        if (member is not None) and children:
 
174
            raise ValueError('Cannot supply both a member and children')
167
175
 
168
 
        self._import_replacer_children = children
169
 
        self._member = member
170
 
        self._module_path = module_path
 
176
        object.__setattr__(self, '_import_replacer_children', children)
 
177
        object.__setattr__(self, '_member', member)
 
178
        object.__setattr__(self, '_module_path', module_path)
171
179
 
172
180
        # Indirecting through __class__ so that children can
173
181
        # override _import (especially our instrumented version)
251
259
 
252
260
        :param import_str: The import string to process
253
261
        """
254
 
        assert import_str.startswith('import ')
 
262
        if not import_str.startswith('import '):
 
263
            raise ValueError('bad import string %r' % (import_str,))
255
264
        import_str = import_str[len('import '):]
256
265
 
257
266
        for path in import_str.split(','):
296
305
 
297
306
        :param from_str: The import string to process
298
307
        """
299
 
        assert from_str.startswith('from ')
 
308
        if not from_str.startswith('from '):
 
309
            raise ValueError('bad from/import %r' % from_str)
300
310
        from_str = from_str[len('from '):]
301
311
 
302
312
        from_module, import_list = from_str.split(' import ')