paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Attribute-related functions. |
paul@6 | 5 | |
paul@228 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@360 | 22 | from __builtins__.types import check_string |
paul@354 | 23 | from native import object_getattr |
paul@228 | 24 | |
paul@228 | 25 | _default=object() # a unique placeholder for a missing value |
paul@228 | 26 | |
paul@228 | 27 | def getattr(obj, name, default=_default): |
paul@6 | 28 | |
paul@228 | 29 | """ |
paul@228 | 30 | Return for 'obj' the attribute having the given 'name', returning the given |
paul@228 | 31 | 'default' if the attribute is not defined for 'obj', raising an exception if |
paul@228 | 32 | 'default' is not indicated and the attribute is not defined. |
paul@228 | 33 | """ |
paul@228 | 34 | |
paul@360 | 35 | check_string(name) |
paul@360 | 36 | |
paul@360 | 37 | # Attempt to obtain the attribute. If the name is not recognised as an |
paul@360 | 38 | # attribute name, the default will be returned. Otherwise, an access |
paul@360 | 39 | # operation will be attempted. |
paul@360 | 40 | |
paul@360 | 41 | try: |
paul@360 | 42 | result = object_getattr(obj, name, default) |
paul@360 | 43 | |
paul@360 | 44 | # Handle exceptions when the access operation fails. |
paul@360 | 45 | |
paul@360 | 46 | except TypeError: |
paul@360 | 47 | result = _default |
paul@360 | 48 | |
paul@360 | 49 | # Check the result and, if it is the placeholder value, raise an exception. |
paul@228 | 50 | |
paul@228 | 51 | if result is _default: |
paul@360 | 52 | raise AttributeError(name) |
paul@360 | 53 | |
paul@360 | 54 | # Otherwise, return the obtained value or supplied default. |
paul@360 | 55 | |
paul@228 | 56 | else: |
paul@228 | 57 | return result |
paul@228 | 58 | |
paul@228 | 59 | def hasattr(obj, name): |
paul@6 | 60 | |
paul@228 | 61 | "Return whether 'obj' has an attribute called 'name'." |
paul@228 | 62 | |
paul@360 | 63 | try: |
paul@360 | 64 | getattr(obj, name) |
paul@360 | 65 | except AttributeError: |
paul@360 | 66 | return False |
paul@360 | 67 | else: |
paul@360 | 68 | return True |
paul@360 | 69 | |
paul@360 | 70 | # NOTE: setattr would probably only be supported on instances due to deductions |
paul@360 | 71 | # NOTE: applying to static objects being undermined by dynamic modifications. |
paul@228 | 72 | |
paul@468 | 73 | def setattr(obj, name, value): |
paul@468 | 74 | raise NotImplementedError, "setattr" |
paul@6 | 75 | |
paul@6 | 76 | # vim: tabstop=4 expandtab shiftwidth=4 |