1.1 --- a/lib/__builtins__/attribute.py Fri Dec 09 23:30:41 2016 +0100
1.2 +++ b/lib/__builtins__/attribute.py Fri Dec 09 23:51:47 2016 +0100
1.3 @@ -19,19 +19,11 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +from __builtins__.types import check_string
1.8 from native import object_getattr
1.9
1.10 _default=object() # a unique placeholder for a missing value
1.11
1.12 -def _getattr(obj, name, default=_default):
1.13 -
1.14 - """
1.15 - Return for 'obj' the attribute having the given 'name', returning the given
1.16 - 'default' if the attribute is not defined for 'obj'.
1.17 - """
1.18 -
1.19 - return object_getattr(obj, name, default)
1.20 -
1.21 def getattr(obj, name, default=_default):
1.22
1.23 """
1.24 @@ -40,13 +32,27 @@
1.25 'default' is not indicated and the attribute is not defined.
1.26 """
1.27
1.28 - result = _getattr(obj, name, default)
1.29 + check_string(name)
1.30 +
1.31 + # Attempt to obtain the attribute. If the name is not recognised as an
1.32 + # attribute name, the default will be returned. Otherwise, an access
1.33 + # operation will be attempted.
1.34 +
1.35 + try:
1.36 + result = object_getattr(obj, name, default)
1.37 +
1.38 + # Handle exceptions when the access operation fails.
1.39 +
1.40 + except TypeError:
1.41 + result = _default
1.42 +
1.43 + # Check the result and, if it is the placeholder value, raise an exception.
1.44
1.45 if result is _default:
1.46 - if default is _default:
1.47 - raise AttributeError(name)
1.48 - else:
1.49 - return default
1.50 + raise AttributeError(name)
1.51 +
1.52 + # Otherwise, return the obtained value or supplied default.
1.53 +
1.54 else:
1.55 return result
1.56
1.57 @@ -54,8 +60,15 @@
1.58
1.59 "Return whether 'obj' has an attribute called 'name'."
1.60
1.61 - result = _getattr(obj, name)
1.62 - return result is not _default
1.63 + try:
1.64 + getattr(obj, name)
1.65 + except AttributeError:
1.66 + return False
1.67 + else:
1.68 + return True
1.69 +
1.70 +# NOTE: setattr would probably only be supported on instances due to deductions
1.71 +# NOTE: applying to static objects being undermined by dynamic modifications.
1.72
1.73 def setattr(obj, name, value): pass
1.74