Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r59057:8cf90ccf4a24
Date: 2012-11-22 22:46 +0100
http://bitbucket.org/pypy/pypy/changeset/8cf90ccf4a24/
Log: cpyext: implement PyBytes_FromObject. Need to rename all of
PyString* to PyBytes*
diff --git a/pypy/module/cpyext/__init__.py b/pypy/module/cpyext/__init__.py
--- a/pypy/module/cpyext/__init__.py
+++ b/pypy/module/cpyext/__init__.py
@@ -37,6 +37,7 @@
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
diff --git a/pypy/module/cpyext/bytesobject.py
b/pypy/module/cpyext/bytesobject.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/bytesobject.py
@@ -0,0 +1,13 @@
+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())
+
+
diff --git a/pypy/module/cpyext/test/test_bytesobject.py
b/pypy/module/cpyext/test/test_bytesobject.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -0,0 +1,11 @@
+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()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit