# HG changeset patch # User Paul Boddie # Date 1479940709 -3600 # Node ID c3935cc7b5b6bf6234a98002ce2aa0ef680921e7 # Parent 3a4ba5788f0f5b3507b095e1ea07dbf973439bff Sketched out the attribute inspection support somewhat. diff -r 3a4ba5788f0f -r c3935cc7b5b6 lib/__builtins__/attribute.py --- a/lib/__builtins__/attribute.py Wed Nov 23 17:32:58 2016 +0100 +++ b/lib/__builtins__/attribute.py Wed Nov 23 23:38:29 2016 +0100 @@ -3,7 +3,7 @@ """ Attribute-related functions. -Copyright (C) 2015 Paul Boddie +Copyright (C) 2015, 2016 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,22 +19,44 @@ this program. If not, see . """ -_getattr_default=object() # a unique placeholder for a missing value +from native import _object_getattr + +_default=object() # a unique placeholder for a missing value + +def _getattr(obj, name, default=_default): -def getattr(obj, name, default=_getattr_default): + """ + Return for 'obj' the attribute having the given 'name', returning the given + 'default' if the attribute is not defined for 'obj'. + """ - "Implementation of getattr." + return _object_getattr(obj, name, default) + +def getattr(obj, name, default=_default): - try: - return _getattr(obj, name) - except AttributeError: - if default is not _getattr_default: - return default + """ + Return for 'obj' the attribute having the given 'name', returning the given + 'default' if the attribute is not defined for 'obj', raising an exception if + 'default' is not indicated and the attribute is not defined. + """ + + result = _getattr(obj, name, default) + + if result is _default: + if default is _default: + raise AttributeError(name) else: - raise + return default + else: + return result + +def hasattr(obj, name): -def hasattr(obj, name): pass + "Return whether 'obj' has an attribute called 'name'." + + result = _getattr(obj, name) + return result is not _default + def setattr(obj, name, value): pass -def _getattr(obj, name): pass # vim: tabstop=4 expandtab shiftwidth=4 diff -r 3a4ba5788f0f -r c3935cc7b5b6 lib/native.py --- a/lib/native.py Wed Nov 23 17:32:58 2016 +0100 +++ b/lib/native.py Wed Nov 23 23:38:29 2016 +0100 @@ -63,6 +63,8 @@ def _buffer_str(self): pass +def _object_getattr(obj, name, default): pass + def _isinstance(obj, cls): pass def _read(fd, n): pass diff -r 3a4ba5788f0f -r c3935cc7b5b6 templates/native.c --- a/templates/native.c Wed Nov 23 17:32:58 2016 +0100 +++ b/templates/native.c Wed Nov 23 23:38:29 2016 +0100 @@ -494,6 +494,16 @@ return __new_str(s); } +__attr __fn_native__object_getattr(__attr __args[]) +{ + __attr * const obj = &__args[1]; + __attr * const name = &__args[2]; + __attr * const _default = &__args[3]; + + /* NOTE: To be written. */ + return __builtins___none_None; +} + __attr __fn_native__isinstance(__attr __args[]) { __attr * const obj = &__args[1]; diff -r 3a4ba5788f0f -r c3935cc7b5b6 templates/native.h --- a/templates/native.h Wed Nov 23 17:32:58 2016 +0100 +++ b/templates/native.h Wed Nov 23 23:38:29 2016 +0100 @@ -52,6 +52,8 @@ __attr __fn_native__buffer_str(__attr __args[]); +__attr __fn_native__object_getattr(__attr __args[]); + __attr __fn_native__isinstance(__attr __args[]); __attr __fn_native__read(__attr __args[]);