Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r60113:b67edd58b7c1
Date: 2013-01-16 16:08 -0800
http://bitbucket.org/pypy/pypy/changeset/b67edd58b7c1/

Log:    o add a bufferstr_or_u_w that acts like the 2.x bufferstr_w (accepts
        unicode and returns it encoded to the default encoding) mostly for
        use by codecs o fix readbuffer_encode to return bytes

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1298,6 +1298,17 @@
             buffer = self.buffer_w(w_obj)
             return buffer.as_str()
 
+    def bufferstr_or_u_w(self, w_obj):
+        """Returns an interp-level str, directly if possible.
+
+        Accepts unicode or any type supporting the buffer
+        interface. Unicode objects will be encoded to the default
+        encoding (UTF-8)
+        """
+        if self.isinstance_w(w_obj, self.w_unicode):
+            return w_obj.identifier_w(self)
+        return self.bufferstr_w(w_obj)
+
     def str_or_None_w(self, w_obj):
         if self.is_w(w_obj, self.w_None):
             return None
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -125,6 +125,9 @@
     def visit_bufferstr(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
+    def visit_bufferstr_or_u(self, el, app_sig):
+        self.checked_space_method(el, app_sig)
+
     def visit_str_or_None(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
@@ -243,6 +246,9 @@
     def visit_bufferstr(self, typ):
         self.run_args.append("space.bufferstr_w(%s)" % (self.scopenext(),))
 
+    def visit_bufferstr_or_u(self, typ):
+        self.run_args.append("space.bufferstr_or_u_w(%s)" % 
(self.scopenext(),))
+
     def visit_str_or_None(self, typ):
         self.run_args.append("space.str_or_None_w(%s)" % (self.scopenext(),))
 
@@ -380,6 +386,9 @@
     def visit_bufferstr(self, typ):
         self.unwrap.append("space.bufferstr_w(%s)" % (self.nextarg(),))
 
+    def visit_bufferstr_or_u(self, typ):
+        self.unwrap.append("space.bufferstr_or_u_w(%s)" % (self.nextarg(),))
+
     def visit_str_or_None(self, typ):
         self.unwrap.append("space.str_or_None_w(%s)" % (self.nextarg(),))
 
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
@@ -402,9 +402,9 @@
     w_res = space.call_function(w_encoder, w_obj, space.wrap(errors))
     return space.getitem(w_res, space.wrap(0))
 
-@unwrap_spec(s='bufferstr', errors='str_or_None')
+@unwrap_spec(s='bufferstr_or_u', errors='str_or_None')
 def buffer_encode(space, s, errors='strict'):
-    return space.newtuple([space.wrap(s), space.wrap(len(s))])
+    return space.newtuple([space.wrapbytes(s), space.wrap(len(s))])
 
 @unwrap_spec(errors=str)
 def decode(space, w_obj, w_encoding=None, errors='strict'):
@@ -749,7 +749,7 @@
             return -1
         return space.int_w(w_code)
 
-@unwrap_spec(string='bufferstr', errors='str_or_None',
+@unwrap_spec(string='bufferstr_or_u', errors='str_or_None',
              w_final=WrappedDefault(False))
 def unicode_escape_decode(space, string, errors="strict", w_final=None):
     if errors is None:
@@ -799,7 +799,7 @@
     result = string_escape_encode(data, False)
     return space.newtuple([space.wrapbytes(result), space.wrap(len(data))])
 
-@unwrap_spec(data="bufferstr", errors='str_or_None')
+@unwrap_spec(data='bufferstr_or_u', errors='str_or_None')
 def escape_decode(space, data, errors='strict'):
     from pypy.interpreter.pyparser.parsestring import PyString_DecodeEscape
     result = PyString_DecodeEscape(space, data, None)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to