~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-12 17:33:23 UTC
  • mto: This revision was merged to the branch mainline in revision 2004.
  • Revision ID: john@arbash-meinel.com-20060912173323-112d28652516fd94
Raise an exception when ScopeReplacer has been misused

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import re
23
23
import sys
24
24
 
 
25
from bzrlib import (
 
26
    errors,
 
27
    )
 
28
 
25
29
 
26
30
class ScopeReplacer(object):
27
31
    """A lazy object that will replace itself in the appropriate scope.
48
52
 
49
53
    def _replace(self):
50
54
        """Actually replace self with other in the given scope"""
51
 
        factory = object.__getattribute__(self, '_factory')
52
 
        scope = object.__getattribute__(self, '_scope')
53
55
        name = object.__getattribute__(self, '_name')
 
56
        try:
 
57
            factory = object.__getattribute__(self, '_factory')
 
58
            scope = object.__getattribute__(self, '_scope')
 
59
        except AttributeError, e:
 
60
            # Because ScopeReplacer objects only replace a single
 
61
            # item, passing them to another variable before they are
 
62
            # replaced would cause them to keep getting replaced
 
63
            # (only they are replacing the wrong variable). So we
 
64
            # make it forbidden, and try to give a good error.
 
65
            raise errors.IllegalUseOfScopeReplacer(
 
66
                name, msg="Object already cleaned up, did you assign it"
 
67
                          "to another variable?",
 
68
                extra=e)
54
69
        obj = factory(self, scope, name)
55
70
        scope[name] = obj
56
71
        return obj
59
74
        """Stop holding on to all the extra stuff"""
60
75
        del self._factory
61
76
        del self._scope
62
 
        del self._name
 
77
        # We keep _name, so that we can report errors
 
78
        # del self._name
63
79
 
64
80
    def __getattribute__(self, attr):
65
81
        obj = object.__getattribute__(self, '_replace')()