Commits

Brian Kearns  committed 1569cea

correct _codecs.{char,read}buffer_encode

  • Participants
  • Parent commits bd46658
  • Branches refactor-buffer-api

Comments (0)

Files changed (6)

File pypy/interpreter/baseobjspace.py

         if self.is_none(w_obj):
             name = "None"
         else:
-            name = self.type(w_obj).getname(self)
+            name = self.type(w_obj).get_module_type_name()
         raise oefmt(self.w_TypeError, "must be %s, not %s", expected, name)
 
     @specialize.arg(1)
                 return w_obj.readbuf_w(self)
             except TypeError:
                 self._getarg_error("string or buffer", w_obj)
+        elif code == 's#':
+            if self.isinstance_w(w_obj, self.w_str):
+                return w_obj.str_w(self)
+            if self.isinstance_w(w_obj, self.w_unicode):
+                return self.str(w_obj).str_w(self)
+            try:
+                return w_obj.readbuf_w(self).as_str()
+            except TypeError:
+                self._getarg_error("string or read-only buffer")
         elif code == 'w*':
             try:
                 try:
                 return w_obj.writebuf_w(self)
             except TypeError:
                 self._getarg_error("read-write buffer", w_obj)
+        elif code == 't#':
+            try:
+                return w_obj.charbuf_w(self)
+            except TypeError:
+                self._getarg_error("string or read-only character buffer", w_obj)
         else:
             assert False
 

File pypy/module/_codecs/__init__.py

          'utf_32_le_decode' : 'interp_codecs.utf_32_le_decode',
          'utf_32_le_encode' : 'interp_codecs.utf_32_le_encode',
          'utf_32_ex_decode' : 'interp_codecs.utf_32_ex_decode',
-         'charbuffer_encode': 'interp_codecs.buffer_encode',
-         'readbuffer_encode': 'interp_codecs.buffer_encode',
+         'charbuffer_encode': 'interp_codecs.charbuffer_encode',
+         'readbuffer_encode': 'interp_codecs.readbuffer_encode',
          'charmap_decode'   : 'interp_codecs.charmap_decode',
          'charmap_encode'   : 'interp_codecs.charmap_encode',
          'escape_encode'    : 'interp_codecs.escape_encode',

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')
-def buffer_encode(space, s, errors='strict'):
+@unwrap_spec(errors='str_or_None')
+def readbuffer_encode(space, w_data, errors='strict'):
+    s = space.getarg_w('s#', w_data)
+    return space.newtuple([space.wrap(s), space.wrap(len(s))])
+
+@unwrap_spec(errors='str_or_None')
+def charbuffer_encode(space, w_data, errors='strict'):
+    s = space.getarg_w('t#', w_data)
     return space.newtuple([space.wrap(s), space.wrap(len(s))])
 
 @unwrap_spec(errors=str)

File pypy/module/_codecs/test/test_codecs.py

         for (i, line) in enumerate(reader):
             assert line == s[i]
 
-    def test_array(self):
+    def test_buffer_encode(self):
         import _codecs, array
-        _codecs.readbuffer_encode(array.array('c', 'spam')) == ('spam', 4)
+        assert _codecs.readbuffer_encode(array.array('c', 'spam')) == ('spam', 4)
+        exc = raises(TypeError, _codecs.charbuffer_encode, array.array('c', 'spam'))
+        assert str(exc.value) == "must be string or read-only character buffer, not array.array"
+        assert _codecs.readbuffer_encode(u"test") == ('test', 4)
+        assert _codecs.charbuffer_encode(u"test") == ('test', 4)
 
     def test_utf8sig(self):
         import codecs

File pypy/objspace/fake/objspace.py

     def get_module(self):
         return w_some_obj()
 
+    def get_module_type_name(self):
+        return self.name
+
 def w_some_obj():
     if NonConstant(False):
         return W_Root()

File pypy/objspace/std/unicodeobject.py

         raise OperationError(space.w_TypeError, space.wrap(
             "cannot use unicode as modifiable buffer"))
 
+    charbuf_w = str_w
+
     def listview_unicode(w_self):
         return _create_list_from_unicode(w_self._value)