Add InheritablePropery class for mutable, inheritable descriptors.

* This allows us to define immutable x.getter and x.setter methods in a
   parent class while still inheriting the ability to alter the descriptor in
   a child class without needing to set something like:
       @property
       def x(self):
           super(self.__class__, self)._get_x()
   as a property override for every descriptor.
testing/mmn/mktime_takes_localtime_not_gmtime
Isis Lovecruft 2013-05-18 14:49:09 +00:00
parent 4ea1884239
commit a4630e1cfb
No known key found for this signature in database
GPG Key ID: A3ADB67A2CDB8B35
2 changed files with 37 additions and 0 deletions

View File

@ -354,3 +354,39 @@ def _write_passphrase(stream, passphrase, encoding):
passphrase = passphrase.encode(encoding)
stream.write(passphrase)
log.debug("Wrote passphrase on stdin.")
class InheritableProperty(object):
"""Based on the emulation of PyProperty_Type() in Objects/descrobject.c"""
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError, "unreadable attribute"
if self.fget.__name__ == '<lambda>' or not self.fget.__name__:
return self.fget(obj)
else:
return getattr(obj, self.fget.__name__)()
def __set__(self, obj, value):
if self.fset is None:
raise AttributeError, "can't set attribute"
if self.fset.__name__ == '<lambda>' or not self.fset.__name__:
self.fset(obj, value)
else:
getattr(obj, self.fset.__name__)(value)
def __delete__(self, obj):
if self.fdel is None:
raise AttributeError, "can't delete attribute"
if self.fdel.__name__ == '<lambda>' or not self.fdel.__name__:
self.fdel(obj)
else:
getattr(obj, self.fdel.__name__)()

View File

@ -96,6 +96,7 @@ import threading
from _parsers import _fix_unsafe, _sanitise, _is_allowed, _sanitise_list
from _parsers import _check_preferences
from _util import InheritableProperty
from _util import _conf, _is_list_or_tuple, _is_stream
from _util import _make_binary_stream
from _util import log