Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3.3
Changeset: r75025:0b478e4a57e3
Date: 2014-12-18 22:35 +0100
http://bitbucket.org/pypy/pypy/changeset/0b478e4a57e3/

Log:    unicode_internal codec accept both bytes and unicode for both
        functions

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
@@ -512,7 +512,7 @@
     wrap_decoder.func_name = rname
     globals()[name] = wrap_decoder
 
-for encoders in [
+for encoder in [
          "ascii_encode",
          "latin_1_encode",
          "utf_7_encode",
@@ -524,11 +524,10 @@
          "utf_32_le_encode",
          "unicode_escape_encode",
          "raw_unicode_escape_encode",
-         "unicode_internal_encode",
         ]:
-    make_encoder_wrapper(encoders)
+    make_encoder_wrapper(encoder)
 
-for decoders in [
+for decoder in [
          "ascii_decode",
          "latin_1_decode",
          "utf_7_decode",
@@ -539,7 +538,7 @@
          "utf_32_be_decode",
          "utf_32_le_decode",
          ]:
-    make_decoder_wrapper(decoders)
+    make_decoder_wrapper(decoder)
 
 if hasattr(runicode, 'str_decode_mbcs'):
     # mbcs functions are not regular, because we have to pass
@@ -810,6 +809,8 @@
 
 @unwrap_spec(errors='str_or_None')
 def unicode_internal_decode(space, w_string, errors="strict"):
+    space.warn(space.wrap("unicode_internal codec has been deprecated"),
+               space.w_DeprecationWarning)
     if errors is None:
         errors = 'strict'
     # special case for this codec: unicodes are returned as is
@@ -828,6 +829,23 @@
         final, state.decode_error_handler)
     return space.newtuple([space.wrap(result), space.wrap(consumed)])
 
+@unwrap_spec(errors='str_or_None')
+def unicode_internal_encode(space, w_uni, errors="strict"):
+    space.warn(space.wrap("unicode_internal codec has been deprecated"),
+               space.w_DeprecationWarning)
+    if errors is None:
+        errors = 'strict'
+    if space.isinstance_w(w_uni, space.w_unicode):
+        uni = space.unicode_w(w_uni)
+        state = space.fromcache(CodecState)
+        result = runicode.unicode_encode_unicode_internal(
+            uni, len(uni), errors, state.encode_error_handler)
+        return space.newtuple([space.wrapbytes(result), space.wrap(len(uni))])
+    else:
+        # special case for this codec: bytes are returned as is
+        string = space.readbuf_w(w_uni).as_str()
+        return space.newtuple([space.wrapbytes(string), 
space.wrap(len(string))])
+
 # ____________________________________________________________
 # support for the "string escape" translation
 # This is a bytes-to bytes transformation
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
@@ -295,6 +295,12 @@
         assert _codecs.unicode_internal_decode(array.array('b', bytes))[0] == 
u"a"
         assert _codecs.unicode_internal_decode(memoryview(bytes))[0] == u"a"
 
+        # This codec accepts bytes and unicode on both sides
+        _codecs.unicode_internal_decode(u'\0\0\0\0')
+        _codecs.unicode_internal_decode(b'\0\0\0\0')
+        _codecs.unicode_internal_encode(u'\0\0\0\0')
+        _codecs.unicode_internal_encode(b'\0\0\0\0')
+
     def test_raw_unicode_escape(self):
         import _codecs
         assert str(b"\u0663", "raw-unicode-escape") == "\u0663"
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to