[Python-checkins] r59550 - in python/branches/release25-maint: Lib/ctypes/test/test_delattr.py Misc/NEWS Modules/_ctypes/_ctypes.c Modules/_ctypes/cfield.c

thomas.heller python-checkins at python.org
Tue Dec 18 20:01:00 CET 2007


Author: thomas.heller
Date: Tue Dec 18 20:00:59 2007
New Revision: 59550

Added:
   python/branches/release25-maint/Lib/ctypes/test/test_delattr.py   (contents, props changed)
Modified:
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Modules/_ctypes/_ctypes.c
   python/branches/release25-maint/Modules/_ctypes/cfield.c
Log:
Issue #1642: Fix segfault in ctypes when trying to delete attributes.


Added: python/branches/release25-maint/Lib/ctypes/test/test_delattr.py
==============================================================================
--- (empty file)
+++ python/branches/release25-maint/Lib/ctypes/test/test_delattr.py	Tue Dec 18 20:00:59 2007
@@ -0,0 +1,21 @@
+import unittest
+from ctypes import *
+
+class X(Structure):
+    _fields_ = [("foo", c_int)]
+
+class TestCase(unittest.TestCase):
+    def test_simple(self):
+        self.assertRaises(TypeError,
+                          delattr, c_int(42), "value")
+
+    def test_chararray(self):
+        self.assertRaises(TypeError,
+                          delattr, (c_char * 5)(), "value")
+
+    def test_struct(self):
+        self.assertRaises(TypeError,
+                          delattr, X(), "foo")
+
+if __name__ == "__main__":
+    unittest.main()

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Tue Dec 18 20:00:59 2007
@@ -49,6 +49,8 @@
 Library
 -------
 
+- Issue #1642: Fix segfault in ctypes when trying to delete attributes.
+
 - os.access now returns True on Windows for any existing directory.
 
 - Issue #1531: tarfile.py: Read fileobj from the current offset, do not

Modified: python/branches/release25-maint/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/branches/release25-maint/Modules/_ctypes/_ctypes.c	(original)
+++ python/branches/release25-maint/Modules/_ctypes/_ctypes.c	Tue Dec 18 20:00:59 2007
@@ -791,6 +791,12 @@
 	char *ptr;
 	int size;
 
+	if (value == NULL) {
+		PyErr_SetString(PyExc_TypeError,
+				"can't delete attribute");
+		return -1;
+	}
+
 	if (PyUnicode_Check(value)) {
 		value = PyUnicode_AsEncodedString(value,
 						  conversion_mode_encoding,
@@ -846,6 +852,11 @@
 {
 	int result = 0;
 
+	if (value == NULL) {
+		PyErr_SetString(PyExc_TypeError,
+				"can't delete attribute");
+		return -1;
+	}
 	if (PyString_Check(value)) {
 		value = PyUnicode_FromEncodedObject(value,
 						    conversion_mode_encoding,
@@ -3969,6 +3980,11 @@
 	PyObject *result;
 	StgDictObject *dict = PyObject_stgdict((PyObject *)self);
 
+	if (value == NULL) {
+		PyErr_SetString(PyExc_TypeError,
+				"can't delete attribute");
+		return -1;
+	}
 	assert(dict); /* Cannot be NULL for CDataObject instances */
 	assert(dict->setfunc);
 	result = dict->setfunc(self->b_ptr, value, dict->size);

Modified: python/branches/release25-maint/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/release25-maint/Modules/_ctypes/cfield.c	(original)
+++ python/branches/release25-maint/Modules/_ctypes/cfield.c	Tue Dec 18 20:00:59 2007
@@ -199,6 +199,11 @@
 	assert(CDataObject_Check(inst));
 	dst = (CDataObject *)inst;
 	ptr = dst->b_ptr + self->offset;
+	if (value == NULL) {
+		PyErr_SetString(PyExc_TypeError,
+				"can't delete attribute");
+		return -1;
+	}
 	return CData_set(inst, self->proto, self->setfunc, value,
 			 self->index, self->size, ptr);
 }


More information about the Python-checkins mailing list