Commits

Philip Jenvey  committed b67edd5

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

  • Participants
  • Parent commits c1245fc
  • Branches py3k

Comments (0)

Files changed (3)

File pypy/interpreter/baseobjspace.py

             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

File pypy/interpreter/gateway.py

     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)
 
     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(),))
 
     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(),))
 

File pypy/module/_codecs/interp_codecs.py

     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'):
             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:
     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)