Author: Armin Rigo <[email protected]>
Branch:
Changeset: r1668:47d41a504ce6
Date: 2015-03-13 09:43 +0100
http://bitbucket.org/cffi/cffi/changeset/47d41a504ce6/
Log: Support for Python 3 too
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5178,6 +5178,7 @@
static int invalid_input_buffer_type(PyObject *x)
{
+#if PY_MAJOR_VERSION < 3
if (PyBuffer_Check(x)) {
/* XXX fish fish fish in an inofficial way */
typedef struct {
@@ -5190,11 +5191,17 @@
if (x == NULL)
return 0;
}
- else if (PyMemoryView_Check(x)) {
+ else
+#endif
+#if PY_MAJOR_VERSION > 2 || PY_MINOR_VERSION > 6
+ if (PyMemoryView_Check(x)) {
x = PyMemoryView_GET_BASE(x);
if (x == NULL)
return 0;
}
+ else
+#endif
+ ;
if (PyBytes_Check(x) || PyUnicode_Check(x))
return 1;
@@ -5470,6 +5477,7 @@
return PyLong_FromVoidPtr(f);
}
+#if PY_MAJOR_VERSION < 3
static Py_ssize_t _test_segcountproc(PyObject *o, Py_ssize_t *ignored)
{
return 1;
@@ -5492,6 +5500,7 @@
*r = buf;
return 3;
}
+#endif
static int _test_getbuf(PyObject *self, Py_buffer *view, int flags)
{
static char buf[] = "GTB";
@@ -5514,12 +5523,14 @@
assert(obj->tp_as_buffer != NULL);
+#if PY_MAJOR_VERSION < 3
obj->tp_as_buffer->bf_getsegcount = &_test_segcountproc;
obj->tp_flags |= Py_TPFLAGS_HAVE_GETCHARBUFFER;
obj->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
if (methods & 1) obj->tp_as_buffer->bf_getreadbuffer = &_test_getreadbuf;
if (methods & 2) obj->tp_as_buffer->bf_getwritebuffer =
&_test_getwritebuf;
if (methods & 4) obj->tp_as_buffer->bf_getcharbuffer = &_test_getcharbuf;
+#endif
if (methods & 8) obj->tp_as_buffer->bf_getbuffer = &_test_getbuf;
if (methods & 16) obj->tp_as_buffer->bf_getbuffer = &_test_getbuf_ro;
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -3259,18 +3259,29 @@
assert list(a) == [10000, 20500, 30000]
def test_from_buffer_not_str_unicode_bytearray():
- from __builtin__ import buffer
BChar = new_primitive_type("char")
BCharP = new_pointer_type(BChar)
BCharA = new_array_type(BCharP, None)
py.test.raises(TypeError, from_buffer, BCharA, b"foo")
py.test.raises(TypeError, from_buffer, BCharA, u"foo")
py.test.raises(TypeError, from_buffer, BCharA, bytearray(b"foo"))
- py.test.raises(TypeError, from_buffer, BCharA, buffer(b"foo"))
- py.test.raises(TypeError, from_buffer, BCharA, buffer(u"foo"))
- py.test.raises(TypeError, from_buffer, BCharA, buffer(bytearray(b"foo")))
- py.test.raises(TypeError, from_buffer, BCharA, memoryview(b"foo"))
- py.test.raises(TypeError, from_buffer, BCharA,
memoryview(bytearray(b"fo")))
+ try:
+ from __builtin__ import buffer
+ except ImportError:
+ pass
+ else:
+ py.test.raises(TypeError, from_buffer, BCharA, buffer(b"foo"))
+ py.test.raises(TypeError, from_buffer, BCharA, buffer(u"foo"))
+ py.test.raises(TypeError, from_buffer, BCharA,
+ buffer(bytearray(b"foo")))
+ try:
+ from __builtin__ import memoryview
+ except ImportError:
+ pass
+ else:
+ py.test.raises(TypeError, from_buffer, BCharA, memoryview(b"foo"))
+ py.test.raises(TypeError, from_buffer, BCharA,
+ memoryview(bytearray(b"foo")))
def test_from_buffer_more_cases():
try:
@@ -3284,27 +3295,34 @@
def check1(bufobj, expected):
c = from_buffer(BCharA, bufobj)
assert typeof(c) is BCharA
+ if sys.version_info >= (3,):
+ expected = [bytes(c, "ascii") for c in expected]
assert list(c) == list(expected)
#
- def check(methods, expected, expeted_for_memoryview=None):
- from __builtin__ import buffer, memoryview
+ def check(methods, expected, expected_for_memoryview=None):
+ if sys.version_info >= (3,):
+ if methods <= 7:
+ return
+ if expected_for_memoryview is not None:
+ expected = expected_for_memoryview
class X(object):
pass
_testbuff(X, methods)
bufobj = X()
check1(bufobj, expected)
try:
+ from __builtin__ import buffer
bufobjb = buffer(bufobj)
- except TypeError:
+ except (TypeError, ImportError):
pass
else:
check1(bufobjb, expected)
try:
bufobjm = memoryview(bufobj)
- except TypeError:
+ except (TypeError, NameError):
pass
else:
- check1(bufobjm, expeted_for_memoryview or expected)
+ check1(bufobjm, expected_for_memoryview or expected)
#
check(1, "RDB")
check(2, "WRB")
@@ -3312,14 +3330,14 @@
check(8, "GTB")
check(16, "ROB")
#
- check(1 | 2, "RDB")
- check(1 | 4, "RDB")
- check(2 | 4, "CHB")
- check(1 | 8, "RDB", "GTB")
+ check(1 | 2, "RDB")
+ check(1 | 4, "RDB")
+ check(2 | 4, "CHB")
+ check(1 | 8, "RDB", "GTB")
check(1 | 16, "RDB", "ROB")
- check(2 | 8, "WRB", "GTB")
+ check(2 | 8, "WRB", "GTB")
check(2 | 16, "WRB", "ROB")
- check(4 | 8, "CHB", "GTB")
+ check(4 | 8, "CHB", "GTB")
check(4 | 16, "CHB", "ROB")
def test_version():
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit