# HG changeset patch # User Mads Kiilerich # Date 1580492895 -3600 # Node ID 24e1099e4f29d55716f84b6d1f4ec79d0d061d84 # Parent 193138922d5604a667a7bb1d7bc5ddeea30af9bb py3: make get_current_authuser handle missing tg context consistently and explicitly tg context handling ends up using tg.support.registry.StackedObjectProxy._current_obj for attribute access ... which if no context has been pushed will end up in: raise TypeError( 'No object (name: %s) has been registered for this ' 'thread' % self.____name__) utils2.get_current_authuser used code like: if hasattr(tg.tmpl_context, 'authuser'): Python 2 hasattr will call __getattr__ and return False if it throws any exception. (It would thus catch the TypeError and silently fall through to use the default user None.) This hasattr behavior is confusing and hard to use correctly. Here, it was used incorrectly. It has been common practice to work around by using something like: getattr(x, y, None) is not None Python 3 hasattr fixed this flaw and only catches AttributeError. The TypeError would thus (rightfully) be propagated. That is a change that must be handled when introducing py3 support. The get_current_authuser code could more clearly and simple and py3-compatible be written as: return getattr(tmpl_context, 'authuser', None) - but then we also have to handle the TypeError explicitly ... which we are happy to do. diff -r 193138922d56 -r 24e1099e4f29 kallithea/lib/utils2.py --- a/kallithea/lib/utils2.py Sat Jan 25 20:18:59 2020 +0100 +++ b/kallithea/lib/utils2.py Fri Jan 31 18:48:15 2020 +0100 @@ -485,10 +485,10 @@ defined, else returns None. """ from tg import tmpl_context - if hasattr(tmpl_context, 'authuser'): - return tmpl_context.authuser - - return None + try: + return getattr(tmpl_context, 'authuser', None) + except TypeError: # No object (name: context) has been registered for this thread + return None class OptionalAttr(object):