~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: Robert Collins
  • Date: 2007-04-23 02:29:35 UTC
  • mfrom: (2441 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2442.
  • Revision ID: robertc@robertcollins.net-20070423022935-9hhongamvk6bfdso
Resolve conflicts with bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
    needed.
49
49
    """
50
50
 
51
 
    __slots__ = ('_scope', '_factory', '_name')
 
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
52
57
 
53
58
    def __init__(self, scope, factory, name):
54
59
        """Create a temporary object in the specified scope.
62
67
        self._scope = scope
63
68
        self._factory = factory
64
69
        self._name = name
 
70
        self._real_obj = None
65
71
        scope[name] = self
66
72
 
67
73
    def _replace(self):
81
87
                          " to another variable?",
82
88
                extra=e)
83
89
        obj = factory(self, scope, name)
 
90
        if ScopeReplacer._should_proxy:
 
91
            self._real_obj = obj
84
92
        scope[name] = obj
85
93
        return obj
86
94
 
92
100
        # del self._name
93
101
 
94
102
    def __getattribute__(self, attr):
95
 
        _replace = object.__getattribute__(self, '_replace')
96
 
        obj = _replace()
97
 
        _cleanup = object.__getattribute__(self, '_cleanup')
98
 
        _cleanup()
 
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()
99
109
        return getattr(obj, attr)
100
110
 
101
111
    def __call__(self, *args, **kwargs):