1.1 --- a/lib/__builtins__/attribute.py Wed Nov 23 17:32:58 2016 +0100
1.2 +++ b/lib/__builtins__/attribute.py Wed Nov 23 23:38:29 2016 +0100
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Attribute-related functions.
1.6
1.7 -Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -19,22 +19,44 @@
1.13 this program. If not, see <http://www.gnu.org/licenses/>.
1.14 """
1.15
1.16 -_getattr_default=object() # a unique placeholder for a missing value
1.17 +from native import _object_getattr
1.18 +
1.19 +_default=object() # a unique placeholder for a missing value
1.20 +
1.21 +def _getattr(obj, name, default=_default):
1.22
1.23 -def getattr(obj, name, default=_getattr_default):
1.24 + """
1.25 + Return for 'obj' the attribute having the given 'name', returning the given
1.26 + 'default' if the attribute is not defined for 'obj'.
1.27 + """
1.28
1.29 - "Implementation of getattr."
1.30 + return _object_getattr(obj, name, default)
1.31 +
1.32 +def getattr(obj, name, default=_default):
1.33
1.34 - try:
1.35 - return _getattr(obj, name)
1.36 - except AttributeError:
1.37 - if default is not _getattr_default:
1.38 - return default
1.39 + """
1.40 + Return for 'obj' the attribute having the given 'name', returning the given
1.41 + 'default' if the attribute is not defined for 'obj', raising an exception if
1.42 + 'default' is not indicated and the attribute is not defined.
1.43 + """
1.44 +
1.45 + result = _getattr(obj, name, default)
1.46 +
1.47 + if result is _default:
1.48 + if default is _default:
1.49 + raise AttributeError(name)
1.50 else:
1.51 - raise
1.52 + return default
1.53 + else:
1.54 + return result
1.55 +
1.56 +def hasattr(obj, name):
1.57
1.58 -def hasattr(obj, name): pass
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 +
1.64 def setattr(obj, name, value): pass
1.65 -def _getattr(obj, name): pass
1.66
1.67 # vim: tabstop=4 expandtab shiftwidth=4