def __getattr__(self, name): """Retrieves the 'value' attribute of the flag --name.""" fl = self.FlagDict() if name not in fl: raise AttributeError(name) if name in self.__dict__['__hiddenflags']: raise AttributeError(name) if self.__dict__['__flags_parsed'] or fl[name].present: return fl[name].value else: error_message = ( 'Trying to access flag %s before flags were parsed.' % name) if self._IsUnparsedFlagAccessAllowed(name): # Print warning to stderr. Messages in logs are often ignored/unnoticed. warnings.warn(error_message + ' This will raise an exception in the future.', RuntimeWarning, stacklevel=2) # Force logging.exception() to behave realistically, but don't propagate # exception up. Allow flag value to be returned (for now). try: raise exceptions.UnparsedFlagAccessError(error_message) except exceptions.UnparsedFlagAccessError: logging.exception(error_message) return fl[name].value else: raise exceptions.UnparsedFlagAccessError(error_message)
def __getattr__(self, name): """Retrieves the 'value' attribute of the flag --name.""" fl = self.FlagDict() if name not in fl: raise AttributeError(name) if name in self.__dict__['__hiddenflags']: raise AttributeError(name) if self.__dict__['__flags_parsed'] or fl[name].present: return fl[name].value else: # Trying to use the flag before FlagValues object got a chance to parse # arguments. # Doing that results in __getattr__ returning default value of the flag, # no matter what was given in the arguments. # Note: if you are using hasattr() and seeing this error, please use # 'flag_name not in FLAGS.FlagDict()' instead of 'hasattr(flag_name)'. # Unfortunately hasattr() implemented via calling getattr and there's no # reliable way to determine hasattr() vs getattr(). try: error_message = ( 'Trying to access flag %s before flags were parsed.' % name) warnings.warn(error_message + ' This will raise an exception in the future.', RuntimeWarning, stacklevel=2) traceback.print_stack() raise exceptions.UnparsedFlagAccessError(error_message) except exceptions.UnparsedFlagAccessError: if self.__dict__['__reset_called']: # Raise exception if .Reset() was called. This mostly happens in tests # and very hard to fix via automated test, so we rather make those # tests fail. raise elif os.getenv('GFLAGS_ALLOW_UNPARSED_FLAG_ACCESS', '1') == '1': logging.exception(error_message) return fl[name].value else: raise