Univention Bugzilla – Bug 28276
configRegistry[key] does not raise KeyError
Last modified: 2020-07-13 20:06:45 CEST
univention.config_registry.ConfigRegistry.__getitem__(self, key) is just a wrapper for get(self, key, default=None), but does not raise a KeyError, which a Python dictionary should do; to quote from <http://docs.python.org/reference/datamodel.html#object.__getitem__>: ... For mapping types, if key is missing (not in the container), KeyError should be raised. This is unexpected and complicates differenciating unset values from empty values using Exceptions. Some code even defines its own wrapper to get the default dictionary behaviour: def ucr(key, default=None): """Wrapper around configRegistry throwing KeyErrors on undefined keys.""" if default is None: default = ucr.UNIQUE value = configRegistry.get(key, default) if value is ucr.UNIQUE: raise KeyError(key) return value ucr.UNIQUE = object() IMHO the behaviour of UCR should be changed to throw Exceptions. See Bug #27465 for a similar problem in the command line tool.
Within univention.config_registry.interfaces VengefulConfigRegistry is used to wrap the ConfigRegistry instance to throw KeyErrors instead of returning '' for unset keys. This fixes the error of ipv6_gateway() always returning an error even is the value is unset. svn34995, univention-config-registry_8.0.2-4.394.201208221410 But the decision to change the default behaviour to generally throw a KeyError remains.
(In reply to comment #1) > But the decision to change the default behaviour to generally throw a KeyError > remains. Yes, it makes sense. It is an API change, so UCS 4 would be the next version for this issue.
(In reply to Stefan Gohmann from comment #2) > (In reply to comment #1) > > But the decision to change the default behaviour to generally throw a KeyError > > remains. > > Yes, it makes sense. It is an API change, so UCS 4 would be the next version > for this issue. In general it would be useful. But I think the benefit is less than the cost. It has to be changed in our and in all external code and we have to support both for a time. Removed UCS 4 target milestone.
This issue has been filed against UCS 4.2. UCS 4.2 is out of maintenance and many UCS components have changed in later releases. Thus, this issue is now being closed. If this issue still occurs in newer UCS versions, please use "Clone this bug" or reopen it and update the UCS version. In this case please provide detailed information on how this issue is affecting you.