Author: Brian Kearns <[email protected]>
Branch: stdlib-2.7.6
Changeset: r69603:76668a98dac6
Date: 2014-03-02 03:31 -0500
http://bitbucket.org/pypy/pypy/changeset/76668a98dac6/
Log: improve type checking in charmap_encode also
diff --git a/pypy/module/_codecs/interp_codecs.py
b/pypy/module/_codecs/interp_codecs.py
--- a/pypy/module/_codecs/interp_codecs.py
+++ b/pypy/module/_codecs/interp_codecs.py
@@ -558,33 +558,22 @@
raise
return errorchar
- # Charmap may return a string
- try:
- x = space.realstr_w(w_ch)
- except OperationError, e:
- if not e.match(space, space.w_TypeError):
- raise
- else:
- return x
-
- # Charmap may return a number
- try:
+ if space.isinstance_w(w_ch, space.w_str):
+ # Charmap may return a string
+ return space.str_w(w_ch)
+ elif space.isinstance_w(w_ch, space.w_int):
+ # Charmap may return a number
x = space.int_w(w_ch)
- except OperationError:
- if not e.match(space, space.w_TypeError):
- raise
- else:
- if 0 <= x < 256:
- return chr(x)
- else:
- raise OperationError(space.w_TypeError, space.wrap(
- "character mapping must be in range(256)"))
-
- # Charmap may return None
- if space.is_w(w_ch, space.w_None):
+ if not 0 <= x < 256:
+ raise oefmt(space.w_TypeError,
+ "character mapping must be in range(256)")
+ return chr(x)
+ elif space.is_w(w_ch, space.w_None):
+ # Charmap may return None
return errorchar
- raise OperationError(space.w_TypeError, space.wrap("invalid mapping"))
+ raise oefmt(space.w_TypeError,
+ "character mapping must return integer, None or str")
@unwrap_spec(string=str, errors='str_or_None')
diff --git a/pypy/module/_codecs/test/test_codecs.py
b/pypy/module/_codecs/test/test_codecs.py
--- a/pypy/module/_codecs/test/test_codecs.py
+++ b/pypy/module/_codecs/test/test_codecs.py
@@ -572,7 +572,10 @@
assert 'xxx'.encode('charmap') == 'xxx'
import codecs
- raises(TypeError, codecs.charmap_encode, u'\xff', "replace", {0xff:
300})
+ exc = raises(TypeError, codecs.charmap_encode, u'\xff', "replace",
{0xff: 300})
+ assert exc.value[0] == 'character mapping must be in range(256)'
+ exc = raises(TypeError, codecs.charmap_encode, u'\xff', "replace",
{0xff: u'a'})
+ assert exc.value[0] == 'character mapping must return integer, None or
str'
raises(UnicodeError, codecs.charmap_encode, u"\xff", "replace", {0xff:
None})
def test_charmap_encode_replace(self):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit