diff options
author | Lynn Root <lroot@redhat.com> | 2012-11-08 10:06:35 -0500 |
---|---|---|
committer | Martin Kosek <mkosek@redhat.com> | 2012-12-11 10:52:06 +0100 |
commit | 173ee4d141958f198a706d65d4ee47c4a2ec31ed (patch) | |
tree | f03ad715dfa5f57ce676327f709831264d54612f | |
parent | 585b91df3b1fab9c865e00f522950003709ea23d (diff) | |
download | freeipa-173ee4d141958f198a706d65d4ee47c4a2ec31ed.tar.gz freeipa-173ee4d141958f198a706d65d4ee47c4a2ec31ed.tar.xz freeipa-173ee4d141958f198a706d65d4ee47c4a2ec31ed.zip |
Switch %r specifiers to '%s' in Public errors
This switch drops the preceding 'u' from strings within Public error messages.
This patch also addresses the related unfriendly 'u' from re-raising errors from netaddr.IPAddress by passing a bytestring through the function.
Also switched ValidationError to TypeError in validate_scalar per jcholast@redhat.com.
Ticket: https://fedorahosted.org/freeipa/ticket/3121
Ticket: https://fedorahosted.org/freeipa/ticket/2588
-rw-r--r-- | ipalib/constants.py | 2 | ||||
-rw-r--r-- | ipalib/errors.py | 36 | ||||
-rw-r--r-- | ipalib/parameters.py | 16 | ||||
-rw-r--r-- | ipalib/plugins/dns.py | 6 | ||||
-rw-r--r-- | ipalib/util.py | 2 | ||||
-rw-r--r-- | ipapython/ipautil.py | 6 | ||||
-rw-r--r-- | tests/test_ipalib/test_parameters.py | 18 | ||||
-rw-r--r-- | tests/test_xmlrpc/test_dns_plugin.py | 4 |
8 files changed, 43 insertions, 47 deletions
diff --git a/ipalib/constants.py b/ipalib/constants.py index bf49375ca..e6d951440 100644 --- a/ipalib/constants.py +++ b/ipalib/constants.py @@ -39,7 +39,7 @@ NULLS = (None, '', u'', tuple(), []) NAME_REGEX = r'^[a-z][_a-z0-9]*[a-z0-9]$|^[a-z]$' # Format for ValueError raised when name does not match above regex: -NAME_ERROR = 'name must match %r; got %r' +NAME_ERROR = "name must match '%s'; got '%s'" # Standard format for TypeError message: TYPE_ERROR = '%s: need a %r; got %r (a %r)' diff --git a/ipalib/errors.py b/ipalib/errors.py index a6c8e7683..a391ada67 100644 --- a/ipalib/errors.py +++ b/ipalib/errors.py @@ -166,7 +166,7 @@ class PluginSubclassError(PrivateError): """ - format = '%(plugin)r not subclass of any base in %(bases)r' + format = '%(plugin)r not subclass of any base in %(bases)r' class PluginDuplicateError(PrivateError): @@ -311,7 +311,7 @@ class VersionError(PublicError): """ errno = 901 - format = _('%(cver)s client incompatible with %(sver)s server at %(server)r') + format = _("%(cver)s client incompatible with %(sver)s server at '%(server)s'") class UnknownError(PublicError): @@ -367,7 +367,7 @@ class ServerInternalError(PublicError): """ errno = 904 - format = _('an internal error has occurred on server at %(server)r') + format = _("an internal error has occurred on server at '%(server)s'") class CommandError(PublicError): @@ -383,7 +383,7 @@ class CommandError(PublicError): """ errno = 905 - format = _('unknown command %(name)r') + format = _("unknown command '%(name)s'") class ServerCommandError(PublicError): @@ -400,7 +400,7 @@ class ServerCommandError(PublicError): """ errno = 906 - format = _('error on server %(server)r: %(error)s') + format = _("error on server '%(server)s': %(error)s") class NetworkError(PublicError): @@ -416,7 +416,7 @@ class NetworkError(PublicError): """ errno = 907 - format = _('cannot connect to %(uri)r: %(error)s') + format = _("cannot connect to '%(uri)s': %(error)s") class ServerNetworkError(PublicError): @@ -425,7 +425,7 @@ class ServerNetworkError(PublicError): """ errno = 908 - format = _('error on server %(server)r: %(error)s') + format = _("error on server '%(server)s': %(error)s") class JSONError(PublicError): @@ -526,7 +526,7 @@ class ServiceError(KerberosError): """ errno = 1102 - format = _('Service %(service)r not found in Kerberos database') + format = _("Service '%(service)s' not found in Kerberos database") class NoCCacheError(KerberosError): @@ -688,7 +688,7 @@ class ZeroArgumentError(InvocationError): """ errno = 3003 - format = _('command %(name)r takes no arguments') + format = _("command '%(name)s' takes no arguments") class MaxArgumentError(InvocationError): @@ -708,8 +708,8 @@ class MaxArgumentError(InvocationError): def __init__(self, message=None, **kw): if message is None: format = ungettext( - 'command %(name)r takes at most %(count)d argument', - 'command %(name)r takes at most %(count)d arguments', + "command '%(name)s' takes at most %(count)d argument", + "command '%(name)s' takes at most %(count)d arguments", kw['count'] ) else: @@ -738,7 +738,7 @@ class OverlapError(InvocationError): """ errno = 3006 - format = _('overlapping arguments and options: %(names)r') + format = _("overlapping arguments and options: %(names)s") class RequirementError(InvocationError): @@ -754,7 +754,7 @@ class RequirementError(InvocationError): """ errno = 3007 - format = _('%(name)r is required') + format = _("'%(name)s' is required") class ConversionError(InvocationError): @@ -770,7 +770,7 @@ class ConversionError(InvocationError): """ errno = 3008 - format = _('invalid %(name)r: %(error)s') + format = _("invalid '%(name)s': %(error)s") class ValidationError(InvocationError): @@ -786,7 +786,7 @@ class ValidationError(InvocationError): """ errno = 3009 - format = _('invalid %(name)r: %(error)s') + format = _("invalid '%(name)s': %(error)s") class NoSuchNamespaceError(InvocationError): @@ -802,7 +802,7 @@ class NoSuchNamespaceError(InvocationError): """ errno = 3010 - format = _('api has no such namespace: %(name)r') + format = _("api has no such namespace: '%(name)s'") class PasswordMismatch(InvocationError): @@ -978,7 +978,7 @@ class MalformedUserPrincipal(ExecutionError): """ errno = 4008 - format = _('Principal is not of the form user@REALM: %(principal)r') + format = _("Principal is not of the form user@REALM: '%(principal)s'") class AlreadyActive(ExecutionError): """ @@ -1357,7 +1357,7 @@ class HelpError(BuiltinError): """ errno = 4101 - format = _('no command nor help topic %(topic)r') + format = _("no command nor help topic '%(topic)s'") class LDAPError(ExecutionError): diff --git a/ipalib/parameters.py b/ipalib/parameters.py index b3a75f288..670e03605 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -879,10 +879,8 @@ class Param(ReadOnly): def _validate_scalar(self, value, index=None): if type(value) is not self.type: - raise ValidationError(name=self.name, - error='need a %r; got %r (a %r)' % ( - self.type, value, type(value) - ) + raise TypeError( + TYPE_ERROR % (self.name, self.type, value, type(value)) ) if index is not None and type(index) is not int: raise TypeError( @@ -1167,11 +1165,9 @@ class Int(Number): the exception that it allows both int and long types. The min/max rules handle size enforcement. """ - if type(value) not in (int, long): - raise ValidationError(name=self.name, - error='need a %r; got %r (a %r)' % ( - self.type, value, type(value) - ) + if type(value) not in (int, long): + raise TypeError( + TYPE_ERROR % (self.name, self.type, value, type(value)) ) if index is not None and type(index) is not int: raise TypeError( @@ -1553,7 +1549,7 @@ class IA5Str(Str): if ord(value[i]) > 127: raise ConversionError(name=self.get_param_name(), index=index, - error=_('The character \'%(char)r\' is not allowed.') % + error=_('The character %(char)r is not allowed.') % dict(char=value[i],) ) return super(IA5Str, self)._convert_scalar(value, index) diff --git a/ipalib/plugins/dns.py b/ipalib/plugins/dns.py index 17a794b53..ae72f3053 100644 --- a/ipalib/plugins/dns.py +++ b/ipalib/plugins/dns.py @@ -267,7 +267,7 @@ def _create_zone_serial(): def _reverse_zone_name(netstr): try: - netaddr.IPAddress(netstr) + netaddr.IPAddress(str(netstr)) except (netaddr.AddrFormatError, ValueError): pass else: @@ -285,7 +285,7 @@ def _reverse_zone_name(netstr): def _validate_ipaddr(ugettext, ipaddr, ip_version=None): try: - ip = netaddr.IPAddress(ipaddr, flags=netaddr.INET_PTON) + ip = netaddr.IPAddress(str(ipaddr), flags=netaddr.INET_PTON) if ip_version is not None: if ip.version != ip_version: @@ -454,7 +454,7 @@ def add_forward_record(zone, name, str_address): pass # the entry already exists and matches def get_reverse_zone(ipaddr, prefixlen=None): - ip = netaddr.IPAddress(ipaddr) + ip = netaddr.IPAddress(str(ipaddr)) revdns = unicode(ip.reverse_dns) if prefixlen is None: diff --git a/ipalib/util.py b/ipalib/util.py index c52d060b5..a92e68c4a 100644 --- a/ipalib/util.py +++ b/ipalib/util.py @@ -526,7 +526,7 @@ def zone_is_reverse(zone_name): return False def get_reverse_zone_default(ip_address): - ip = netaddr.IPAddress(ip_address) + ip = netaddr.IPAddress(str(ip_address)) items = ip.reverse_dns.split('.') if ip.version == 4: diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py index 1053fc665..fbb3c26d8 100644 --- a/ipapython/ipautil.py +++ b/ipapython/ipautil.py @@ -103,7 +103,7 @@ class CheckedIPAddress(netaddr.IPAddress): else: try: try: - addr = netaddr.IPAddress(addr, flags=self.netaddr_ip_flags) + addr = netaddr.IPAddress(str(addr), flags=self.netaddr_ip_flags) except netaddr.AddrFormatError: # netaddr.IPAddress doesn't handle zone indices in textual # IPv6 addresses. Try removing zone index and parse the @@ -113,11 +113,11 @@ class CheckedIPAddress(netaddr.IPAddress): addr, sep, foo = addr.partition('%') if sep != '%': raise - addr = netaddr.IPAddress(addr, flags=self.netaddr_ip_flags) + addr = netaddr.IPAddress(str(addr), flags=self.netaddr_ip_flags) if addr.version != 6: raise except ValueError: - net = netaddr.IPNetwork(addr, flags=self.netaddr_ip_flags) + net = netaddr.IPNetwork(str(addr), flags=self.netaddr_ip_flags) if not parse_netmask: raise ValueError("netmask and prefix length not allowed here") addr = net.ip diff --git a/tests/test_ipalib/test_parameters.py b/tests/test_ipalib/test_parameters.py index e6ac91db7..b30ae5ada 100644 --- a/tests/test_ipalib/test_parameters.py +++ b/tests/test_ipalib/test_parameters.py @@ -469,11 +469,11 @@ class test_Param(ClassChecker): assert str(e) == 'value: empty tuple must be converted to None' # Test with wrong (scalar) type: - e = raises(ValidationError, o.validate, (None, None, 42, None), 'cli') - assert str(e) == 'invalid %s' % (TYPE_ERROR % ('\'my_param\'', NoneType, 42, int)) + e = raises(TypeError, o.validate, (None, None, 42, None), 'cli') + assert str(e) == TYPE_ERROR % ('my_param', NoneType, 42, int) o = self.cls('my_param') - e = raises(ValidationError, o.validate, 'Hello', 'cli') - assert str(e) == 'invalid %s' % (TYPE_ERROR % ('\'my_param\'', NoneType, 'Hello', str)) + e = raises(TypeError, o.validate, 'Hello', 'cli') + assert str(e) == TYPE_ERROR % ('my_param', NoneType, 'Hello', str) class Example(self.cls): type = int @@ -535,10 +535,10 @@ class test_Param(ClassChecker): o = MyParam('my_param', okay) # Test that TypeError is appropriately raised: - e = raises(ValidationError, o._validate_scalar, 0) - assert str(e) == 'invalid %s' % (TYPE_ERROR % ('\'my_param\'', bool, 0, int)) - e = raises(ValidationError, o._validate_scalar, 'Hi', index=4) - assert str(e) == 'invalid %s' % (TYPE_ERROR % ('\'my_param\'', bool, 'Hi', str)) + e = raises(TypeError, o._validate_scalar, 0) + assert str(e) == TYPE_ERROR % ('my_param', bool, 0, int) + e = raises(TypeError, o._validate_scalar, 'Hi', index=4) + assert str(e) == TYPE_ERROR % ('my_param', bool, 'Hi', str) e = raises(TypeError, o._validate_scalar, True, index=3.0) assert str(e) == TYPE_ERROR % ('index', int, 3.0, float) @@ -1574,4 +1574,4 @@ class test_IA5Str(ClassChecker): e = raises(errors.ConversionError, mthd, value) assert e.name == 'my_str' assert e.index is None - assert_equal(e.error, "The character \''\\xc3'\' is not allowed.") + assert_equal(e.error, "The character '\\xc3' is not allowed.") diff --git a/tests/test_xmlrpc/test_dns_plugin.py b/tests/test_xmlrpc/test_dns_plugin.py index eb4356afb..190248494 100644 --- a/tests/test_xmlrpc/test_dns_plugin.py +++ b/tests/test_xmlrpc/test_dns_plugin.py @@ -1095,7 +1095,7 @@ class test_dns(Declarative): desc='Try to add invalid allow-query to zone %r' % dnszone1, command=('dnszone_mod', [dnszone1], {'idnsallowquery': u'foo'}), expected=errors.ValidationError(name='allow_query', - error=u"failed to detect a valid IP address from u'foo'"), + error=u"failed to detect a valid IP address from 'foo'"), ), dict( @@ -1128,7 +1128,7 @@ class test_dns(Declarative): desc='Try to add invalid allow-transfer to zone %r' % dnszone1, command=('dnszone_mod', [dnszone1], {'idnsallowtransfer': u'10.'}), expected=errors.ValidationError(name='allow_transfer', - error=u"failed to detect a valid IP address from u'10.'"), + error=u"failed to detect a valid IP address from '10.'"), ), dict( |