diff -r b4ccf8e1fdba Lib/ctypes/test/test_bitfields.py --- a/Lib/ctypes/test/test_bitfields.py Tue Aug 16 22:26:48 2011 -0500 +++ b/Lib/ctypes/test/test_bitfields.py Wed Aug 17 09:00:46 2011 -0700 @@ -240,5 +240,13 @@ _anonymous_ = ["_"] _fields_ = [("_", X)] + def test_invalid_name(self): + # field name must be string + def declare_with_name(name): + class S(Structure): + _fields_ = [(name, c_int)] + + self.assertRaises(TypeError, declare_with_name, u"x\xe9") + if __name__ == "__main__": unittest.main() diff -r b4ccf8e1fdba Modules/_ctypes/stgdict.c --- a/Modules/_ctypes/stgdict.c Tue Aug 16 22:26:48 2011 -0500 +++ b/Modules/_ctypes/stgdict.c Wed Aug 17 09:00:46 2011 -0700 @@ -492,10 +492,23 @@ bitsize = 0; if (isStruct && !isPacked) { char *fieldfmt = dict->format ? dict->format : "B"; + char *ptr = NULL; + Py_ssize_t len = 0; + char *buf = NULL; char *fieldname = PyString_AsString(name); - char *ptr; - Py_ssize_t len = strlen(fieldname) + strlen(fieldfmt); - char *buf = alloca(len + 2 + 1); + + if (fieldname == NULL) + { + PyErr_Format(PyExc_TypeError, + "structure field name must be string not %s", + name->ob_type->tp_name); + + Py_DECREF(pair); + return -1; + } + + len = strlen(fieldname) + strlen(fieldfmt); + buf = alloca(len + 2 + 1); sprintf(buf, "%s:%s:", fieldfmt, fieldname);