Author: Armin Rigo <[email protected]>
Branch:
Changeset: r44059:c65231305af3
Date: 2011-05-10 22:45 +0200
http://bitbucket.org/pypy/pypy/changeset/c65231305af3/
Log: Encode interface.
diff --git a/pypy/module/_multibytecodec/interp_multibytecodec.py
b/pypy/module/_multibytecodec/interp_multibytecodec.py
--- a/pypy/module/_multibytecodec/interp_multibytecodec.py
+++ b/pypy/module/_multibytecodec/interp_multibytecodec.py
@@ -35,8 +35,29 @@
space.wrap(len(input))])
decode.unwrap_spec = ['self', ObjSpace, str, 'str_or_None']
- def encode(self):
- xxx
+ def encode(self, space, input, errors=None):
+ if errors is not None and errors != 'strict':
+ raise OperationError(space.w_NotImplementedError, # XXX
+ space.wrap("errors='%s' in _multibytecodec"
+ % errors))
+ #
+ try:
+ output = c_codecs.encode(self.codec, input)
+ except c_codecs.EncodeDecodeError, e:
+ raise OperationError(
+ space.w_UnicodeEncodeError,
+ space.newtuple([
+ space.wrap(self.name),
+ space.wrap(input),
+ space.wrap(e.start),
+ space.wrap(e.end),
+ space.wrap(e.reason)]))
+ except RuntimeError:
+ raise OperationError(space.w_RuntimeError,
+ space.wrap("internal codec error"))
+ return space.newtuple([space.wrap(output),
+ space.wrap(len(input))])
+ encode.unwrap_spec = ['self', ObjSpace, unicode, 'str_or_None']
MultibyteCodec.typedef = TypeDef(
diff --git a/pypy/module/_multibytecodec/test/test_app_codecs.py
b/pypy/module/_multibytecodec/test/test_app_codecs.py
--- a/pypy/module/_multibytecodec/test/test_app_codecs.py
+++ b/pypy/module/_multibytecodec/test/test_app_codecs.py
@@ -20,6 +20,7 @@
codec = _codecs_cn.getcodec("hz")
r = codec.decode("~{abc}", "strict")
assert r == (u'\u5f95\u6cef', 6)
+ assert type(r[0]) is unicode
def test_decode_hz_error(self):
import _codecs_cn
@@ -27,10 +28,29 @@
e = raises(UnicodeDecodeError, codec.decode, "~{}").value
assert e.args == ('hz', '~{}', 2, 3, 'incomplete multibyte sequence')
assert e.encoding == 'hz'
- assert e.object == '~{}'
+ assert e.object == '~{}' and type(e.object) is str
assert e.start == 2
assert e.end == 3
assert e.reason == "incomplete multibyte sequence"
#
e = raises(UnicodeDecodeError, codec.decode, "~{xyz}").value
assert e.args == ('hz', '~{xyz}', 2, 4, 'illegal multibyte sequence')
+
+ def test_encode_hz(self):
+ import _codecs_cn
+ codec = _codecs_cn.getcodec("hz")
+ r = codec.encode(u'\u5f95\u6cef')
+ assert r == ('~{abc}~}', 2)
+ assert type(r[0]) is str
+
+ def test_encode_hz_error(self):
+ import _codecs_cn
+ codec = _codecs_cn.getcodec("hz")
+ u = u'abc\u1234def'
+ e = raises(UnicodeEncodeError, codec.encode, u).value
+ assert e.args == ('hz', u, 3, 4, 'illegal multibyte sequence')
+ assert e.encoding == 'hz'
+ assert e.object == u and type(e.object) is unicode
+ assert e.start == 3
+ assert e.end == 4
+ assert e.reason == 'illegal multibyte sequence'
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit