diff options
author | Steven Knight <knight@baldmt.com> | 2010-05-19 14:17:30 (GMT) |
---|---|---|
committer | Steven Knight <knight@baldmt.com> | 2010-05-19 14:17:30 (GMT) |
commit | 1ba573a94634f56d954280a1ca6828c4892c8e72 (patch) | |
tree | 713d94d4d3d514d8af713d974ff9520160912f70 /src | |
parent | 398fa063bac8a14c3e0c3a248e428730119ecc0e (diff) | |
download | SCons-1ba573a94634f56d954280a1ca6828c4892c8e72.zip SCons-1ba573a94634f56d954280a1ca6828c4892c8e72.tar.gz SCons-1ba573a94634f56d954280a1ca6828c4892c8e72.tar.bz2 |
Convert Util.Proxy from an old-style class to a new-style class.
Diffstat (limited to 'src')
-rw-r--r-- | src/engine/SCons/Builder.py | 2 | ||||
-rw-r--r-- | src/engine/SCons/Node/FS.py | 3 | ||||
-rw-r--r-- | src/engine/SCons/Util.py | 40 |
3 files changed, 38 insertions, 7 deletions
diff --git a/src/engine/SCons/Builder.py b/src/engine/SCons/Builder.py index 3d73a0c..b062678 100644 --- a/src/engine/SCons/Builder.py +++ b/src/engine/SCons/Builder.py @@ -854,6 +854,8 @@ class CompositeBuilder(SCons.Util.Proxy): self.cmdgen = cmdgen self.builder = builder + __call__ = SCons.Util.Delegate('__call__') + def add_action(self, suffix, action): self.cmdgen.add_action(suffix, action) self.set_src_suffix(self.cmdgen.src_suffixes()) diff --git a/src/engine/SCons/Node/FS.py b/src/engine/SCons/Node/FS.py index 9c73d70..4aeaff7 100644 --- a/src/engine/SCons/Node/FS.py +++ b/src/engine/SCons/Node/FS.py @@ -406,6 +406,9 @@ def diskcheck_types(): class EntryProxy(SCons.Util.Proxy): + + __str__ = SCons.Util.Delegate('__str__') + def __get_abspath(self): entry = self.get() return SCons.Subst.SpecialAttrWrapper(entry.get_abspath(), diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index 71fb4bd..54e738f 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -487,7 +487,7 @@ def semi_deepcopy(x): -class Proxy: +class Proxy(object): """A simple generic Proxy class, forwarding all calls to subject. So, for the benefit of the python newbie, what does this really mean? Well, it means that you can take an object, let's @@ -505,26 +505,52 @@ class Proxy: x = objA.var1 - Inherit from this class to create a Proxy.""" + Inherit from this class to create a Proxy. + + Note that, with new-style classes, this does *not* work transparently + for Proxy subclasses that use special .__*__() method names, because + those names are now bound to the class, not the individual instances. + You now need to know in advance which .__*__() method names you want + to pass on to the underlying Proxy object, and specifically delegate + their calls like this: + + class Foo(Proxy): + __str__ = Delegate('__str__') + """ def __init__(self, subject): """Wrap an object as a Proxy object""" - self.__subject = subject + self._subject = subject def __getattr__(self, name): """Retrieve an attribute from the wrapped object. If the named attribute doesn't exist, AttributeError is raised""" - return getattr(self.__subject, name) + return getattr(self._subject, name) def get(self): """Retrieve the entire wrapped object""" - return self.__subject + return self._subject def __cmp__(self, other): - if issubclass(other.__class__, self.__subject.__class__): - return cmp(self.__subject, other) + if issubclass(other.__class__, self._subject.__class__): + return cmp(self._subject, other) return cmp(self.__dict__, other.__dict__) +class Delegate(object): + """A Python Descriptor class that delegates attribute fetches + to an underlying wrapped subject of a Proxy. Typical use: + + class Foo(Proxy): + __str__ = Delegate('__str__') + """ + def __init__(self, attribute): + self.attribute = attribute + def __get__(self, obj, cls): + if isinstance(obj, cls): + return getattr(obj._subject, self.attribute) + else: + return self + # attempt to load the windows registry module: can_read_reg = 0 try: |