Commits

Amaury Forgeot d'Arc committed 8cf90cc

cpyext: implement PyBytes_FromObject.
Need to rename all of PyString* to PyBytes*

  • Participants
  • Parent commits 040de36
  • Branches py3k

Comments (0)

Files changed (3)

pypy/module/cpyext/__init__.py

 import pypy.module.cpyext.typeobject
 import pypy.module.cpyext.object
 import pypy.module.cpyext.stringobject
+import pypy.module.cpyext.bytesobject
 import pypy.module.cpyext.tupleobject
 import pypy.module.cpyext.setobject
 import pypy.module.cpyext.dictobject

pypy/module/cpyext/bytesobject.py

+from pypy.module.cpyext.pyobject import PyObject
+from pypy.module.cpyext.api import cpython_api
+
+@cpython_api([PyObject], PyObject)
+def PyBytes_FromObject(space, w_obj):
+    """Return the bytes representation of object obj that implements
+    the buffer protocol."""
+    if space.is_w(space.type(w_obj), space.w_bytes):
+        return w_obj
+    buffer = space.buffer_w(w_obj)
+    return space.wrapbytes(buffer.as_str())
+    
+

pypy/module/cpyext/test/test_bytesobject.py

+from pypy.module.cpyext.test.test_api import BaseApiTest
+
+class TestBytes(BaseApiTest):
+    def test_FromObject(self, space, api):
+        w_obj = space.wrapbytes("test")
+        assert space.eq_w(w_obj, api.PyBytes_FromObject(w_obj))
+        w_obj = space.call_function(space.w_bytearray, w_obj)
+        assert space.eq_w(w_obj, api.PyBytes_FromObject(w_obj))
+        w_obj = space.wrap(u"test")
+        assert api.PyBytes_FromObject(w_obj) is None
+        api.PyErr_Clear()