[Python-checkins] cpython: Issue 14814: The new systematic tests aren't just about error reporting any
Terry Reedy
tjreedy at udel.edu
Sun Jul 8 19:22:43 CEST 2012
On 7/8/2012 9:14 AM, nick.coghlan wrote:
> if not self._DECIMAL_DIGITS.issuperset(octet_str):
> - raise ValueError("Only decimal digits permitted in %r" % octet_str)
> + msg = "Only decimal digits permitted in %r"
> + raise ValueError(msg % octet_str)
> + # We do the length check second, since the invalid character error
> + # is likely to be more informative for the user
> + if len(octet_str) > 3:
> + msg = "At most 3 characters permitted in %r"
> + raise ValueError(msg % octet_str)
If you want to report both errors, when present:
msg = ''
if not self._DECIMAL_DIGITS.issuperset(octet_str):
msg = "Only decimal digits permitted in %r" % octet_str
if len(octet_str) > 3:
msg = msg + ("; " if msg else '') +
"At most 3 characters permitted in %r" % octet_str
if msg:
raise ValueError(msg)
8 lines either way ;-)
> if not self._HEX_DIGITS.issuperset(hextet_str):
> raise ValueError("Only hex digits permitted in %r" % hextet_str)
> + # We do the length check second, since the invalid character error
> + # is likely to be more informative for the user
> if len(hextet_str) > 4:
> msg = "At most 4 characters permitted in %r"
> raise ValueError(msg % hextet_str)
Same thing for hextet string
msg = ''
if not self._HEX_DIGITS.issuperset(hextet_str):
msg = "Only hex digits permitted in %r" % hextet_str
if len(hextet_str) > 4:
msg = msg + ("; " if msg else '') +
"At most 4 characters permitted in %r" % hextet_str
if msg:
raise ValueError(msg)
(These *could* be factored into one private function with 5 params, but
probably not worth it for just 2 calls.)
I guess these would require another change to the tests ;-).
> +class BaseTestCase(unittest.TestCase):
> # One big change in ipaddress over the original ipaddr module is
> # error reporting that tries to assume users *don't know the rules*
> # for what constitutes an RFC compliant IP address
Good assumption. There *will* be people who learn by trial and error.
---
Terry Jan Reedy
More information about the Python-checkins
mailing list