Message145986
| Author |
ncoghlan |
| Recipients |
belopolsky, benjamin.peterson, cben, eric.araujo, flox, georg.brandl, gvanrossum, lemburg, loewis, ncoghlan, ssbarnea, vstinner |
| Date |
2011年10月19日.22:54:41 |
| SpamBayes Score |
1.9769963e-11 |
| Marked as misclassified |
No |
| Message-id |
<CADiSq7fZnur9+7jW9AH_zRbcmua2OUnrqAV+RsDp1ryfxjFsuA@mail.gmail.com> |
| In-reply-to |
<1319063688.95.0.0614650932312.issue7475@psf.upfronthosting.co.za> |
| Content |
On Thu, Oct 20, 2011 at 8:34 AM, STINNER Victor <report@bugs.python.org> wrote:
>> str.transform('bz2') ==> CodecLookupError
>
> A lookup error is surprising here. It may be a TypeError instead. The bz2 can be used with .transform, but not on str. So:
No, it's the same concept as the other cases - we found a codec with
the requested name, but it's not the kind of codec we wanted in the
current context (i.e. str.transform). It may be that the problem is
the user has a str when they expected to have a bytearray or a bytes
object, but there's no way for the codec lookup process to know that.
> - Lookup error if the codec cannot be used with encode/decode or transform/untransform
> - Type error if the value type is invalid
There's no way for str.transform to tell the difference between "I
asked for the wrong codec" and "I expected to have a bytes object
here, not a str object". That's why I think we need to think in terms
of format checks rather than type checks.
> (CodecLookupError doesn't exist, you propose to define a new exception who inherits from LookupError?)
Yeah, and I'd get that to handle the process of creating the nice
error messages. I think it may even make sense to build the filtering
options into codecs.lookup() itself:
def lookup(encoding, decoded_format=None, encoded_format=None):
info = _lookup(encoding) # The existing codec lookup algorithm
if ((decoded_format is not None and decoded_format !=
info.decoded_format) or
(encoded_format is not None and encoded_format !=
info.encoded_format)):
raise CodecLookupError(info, decoded_format, encoded_format)
Then the various encode, decode and transform methods can just pass
the appropriate arguments to 'codecs.lookup' without all having to
reimplement the format checking logic. |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2011年10月19日 22:54:42 | ncoghlan | set | recipients:
+ ncoghlan, lemburg, gvanrossum, loewis, georg.brandl, cben, belopolsky, vstinner, benjamin.peterson, eric.araujo, ssbarnea, flox |
| 2011年10月19日 22:54:41 | ncoghlan | link | issue7475 messages |
| 2011年10月19日 22:54:41 | ncoghlan | create |
|