[Python-checkins] r79295 - in python/trunk: Lib/ctypes/test/test_strings.py Misc/NEWS Modules/_ctypes/_ctypes.c

florent.xicluna python-checkins at python.org
Mon Mar 22 17:07:38 CET 2010


Author: florent.xicluna
Date: Mon Mar 22 17:07:38 2010
New Revision: 79295

Log:
Issue #7703: ctypes supports both buffer() and memoryview().  The former is deprecated.
Complement of r79288.


Modified:
   python/trunk/Lib/ctypes/test/test_strings.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/_ctypes/_ctypes.c

Modified: python/trunk/Lib/ctypes/test/test_strings.py
==============================================================================
--- python/trunk/Lib/ctypes/test/test_strings.py	(original)
+++ python/trunk/Lib/ctypes/test/test_strings.py	Mon Mar 22 17:07:38 2010
@@ -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 @@
         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 @@
         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 @@
         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()

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Mar 22 17:07:38 2010
@@ -29,6 +29,9 @@
 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.
 

Modified: python/trunk/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/trunk/Modules/_ctypes/_ctypes.c	(original)
+++ python/trunk/Modules/_ctypes/_ctypes.c	Mon Mar 22 17:07:38 2010
@@ -1076,22 +1076,31 @@
 {
 	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 *


More information about the Python-checkins mailing list