summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/ctypes/test/test_strings.py11
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_ctypes/_ctypes.c17
3 files changed, 25 insertions, 6 deletions
diff --git a/Lib/ctypes/test/test_strings.py b/Lib/ctypes/test/test_strings.py
index 324848e..8945d0c 100644
--- a/Lib/ctypes/test/test_strings.py
+++ b/Lib/ctypes/test/test_strings.py
@@ -1,5 +1,6 @@
import unittest
from ctypes import *
+from test import test_support
class StringArrayTestCase(unittest.TestCase):
def test(self):
@@ -24,7 +25,7 @@ class StringArrayTestCase(unittest.TestCase):
self.assertRaises(ValueError, setattr, buf, "value", "aaaaaaaa")
self.assertRaises(TypeError, setattr, buf, "value", 42)
- def test_c_buffer_value(self):
+ def test_c_buffer_value(self, memoryview=memoryview):
buf = c_buffer(32)
buf.value = "Hello, World"
@@ -34,7 +35,7 @@ class StringArrayTestCase(unittest.TestCase):
self.assertRaises(TypeError, setattr, buf, "value", memoryview("abc"))
self.assertRaises(ValueError, setattr, buf, "raw", memoryview("x" * 100))
- def test_c_buffer_raw(self):
+ def test_c_buffer_raw(self, memoryview=memoryview):
buf = c_buffer(32)
buf.raw = memoryview("Hello, World")
@@ -42,6 +43,12 @@ class StringArrayTestCase(unittest.TestCase):
self.assertRaises(TypeError, setattr, buf, "value", memoryview("abc"))
self.assertRaises(ValueError, setattr, buf, "raw", memoryview("x" * 100))
+ def test_c_buffer_deprecated(self):
+ # Compatibility with 2.x
+ with test_support.check_py3k_warnings():
+ self.test_c_buffer_value(buffer)
+ self.test_c_buffer_raw(buffer)
+
def test_param_1(self):
BUF = c_char * 4
buf = BUF()
diff --git a/Misc/NEWS b/Misc/NEWS
index c5424c8..1824f5f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
Library
-------
+- Issue #7703: ctypes supports both buffer() and memoryview(). The former is
+ deprecated.
+
- Issue #7860: platform.uname now reports the correct 'machine' type
when Python is running in WOW64 mode on 64 bit Windows.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 4d2302d..d1b436d 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -1076,22 +1076,31 @@ CharArray_set_raw(CDataObject *self, PyObject *value)
{
char *ptr;
Py_ssize_t size;
+ Py_buffer view = { 0 };
if (PyBuffer_Check(value)) {
size = Py_TYPE(value)->tp_as_buffer->bf_getreadbuffer(value, 0, (void *)&ptr);
if (size < 0)
- return -1;
- } else if (-1 == PyString_AsStringAndSize(value, &ptr, &size)) {
- return -1;
+ goto fail;
+ } else {
+ if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
+ goto fail;
+ size = view.len;
+ ptr = view.buf;
}
if (size > self->b_size) {
PyErr_SetString(PyExc_ValueError,
"string too long");
- return -1;
+ goto fail;
}
memcpy(self->b_ptr, ptr, size);
+ PyBuffer_Release(&view);
return 0;
+ fail:
+
+ PyBuffer_Release(&view);
+ return -1;
}
static PyObject *