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
2.1 --- a/lib/native.py Wed Nov 23 17:32:58 2016 +0100
2.2 +++ b/lib/native.py Wed Nov 23 23:38:29 2016 +0100
2.3 @@ -63,6 +63,8 @@
2.4
2.5 def _buffer_str(self): pass
2.6
2.7 +def _object_getattr(obj, name, default): pass
2.8 +
2.9 def _isinstance(obj, cls): pass
2.10
2.11 def _read(fd, n): pass
3.1 --- a/templates/native.c Wed Nov 23 17:32:58 2016 +0100
3.2 +++ b/templates/native.c Wed Nov 23 23:38:29 2016 +0100
3.3 @@ -494,6 +494,16 @@
3.4 return __new_str(s);
3.5 }
3.6
3.7 +__attr __fn_native__object_getattr(__attr __args[])
3.8 +{
3.9 + __attr * const obj = &__args[1];
3.10 + __attr * const name = &__args[2];
3.11 + __attr * const _default = &__args[3];
3.12 +
3.13 + /* NOTE: To be written. */
3.14 + return __builtins___none_None;
3.15 +}
3.16 +
3.17 __attr __fn_native__isinstance(__attr __args[])
3.18 {
3.19 __attr * const obj = &__args[1];
4.1 --- a/templates/native.h Wed Nov 23 17:32:58 2016 +0100
4.2 +++ b/templates/native.h Wed Nov 23 23:38:29 2016 +0100
4.3 @@ -52,6 +52,8 @@
4.4
4.5 __attr __fn_native__buffer_str(__attr __args[]);
4.6
4.7 +__attr __fn_native__object_getattr(__attr __args[]);
4.8 +
4.9 __attr __fn_native__isinstance(__attr __args[]);
4.10
4.11 __attr __fn_native__read(__attr __args[]);