Issue #1642: Fix segfault in ctypes when trying to delete attributes.

This commit is contained in:
Thomas Heller 2007-12-18 19:00:34 +00:00
parent effde12f5f
commit 153038efa4
4 changed files with 44 additions and 0 deletions

View File

@ -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()

View File

@ -325,6 +325,8 @@ Core and builtins
Library
-------
- Issue #1642: Fix segfault in ctypes when trying to delete attributes.
- Issue #1727780: Support loading pickles of random.Random objects created
on 32-bit systems on 64-bit systems, and vice versa. As a consequence
of the change, Random pickles created by Python 2.6 cannot be loaded

View File

@ -788,6 +788,12 @@ CharArray_set_value(CDataObject *self, PyObject *value)
char *ptr;
Py_ssize_t 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,
@ -843,6 +849,11 @@ WCharArray_set_value(CDataObject *self, PyObject *value)
{
Py_ssize_t 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,
@ -4139,6 +4150,11 @@ Simple_set_value(CDataObject *self, PyObject *value)
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);

View File

@ -199,6 +199,11 @@ CField_set(CFieldObject *self, PyObject *inst, PyObject *value)
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);
}