This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vladris
Recipients nullnil, theller, vladris
Date 2011-07-11.00:21:37
SpamBayes Score 1.2151851e-09
Marked as misclassified No
Message-id <1310343699.44.0.116451882452.issue8161@psf.upfronthosting.co.za>
In-reply-to
Content
Looks like this was implemented by design at some point. In cfield.c, we have specific code to treat character array fields as strings:

    /*  Field descriptors for 'c_char * n' are be scpecial cased to
        return a Python string instead of an Array object instance...
    */
    if (PyCArrayTypeObject_Check(proto)) {
        StgDictObject *adict = PyType_stgdict(proto);
        StgDictObject *idict;
        if (adict && adict->proto) {
            idict = PyType_stgdict(adict->proto);
            if (!idict) {
                PyErr_SetString(PyExc_TypeError,
                                "has no _stginfo_");
                Py_DECREF(self);
                return NULL;
            }
            if (idict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
                struct fielddesc *fd = _ctypes_get_fielddesc("s");
                getfunc = fd->getfunc;
                setfunc = fd->setfunc;
            }
#ifdef CTYPES_UNICODE
            if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
                struct fielddesc *fd = _ctypes_get_fielddesc("U");
                getfunc = fd->getfunc;
                setfunc = fd->setfunc;
            }
#endif
        }
    }

Simple fix would be to just remove this whole section though this might break code which relied on string assignment to such fields. For example:

class T(ctypes.Structure):
	_fields_ = (
		('member', ctypes.c_char * 16),
	)

x = T()
x.member = bytes('Spam', 'ascii')

Above works now but will fail if change is made. There is a high chance this would break existing code as I imagine people using this due to convenience. An alternative would be to keep string setfunc but don't change getfunc, though that is also pretty inconsistent as you will be able to set a string but not get one back.

If we are willing to take the risk, fix is easy.
History
Date User Action Args
2011-07-11 00:21:39vladrissetrecipients: + vladris, theller, nullnil
2011-07-11 00:21:39vladrissetmessageid: <1310343699.44.0.116451882452.issue8161@psf.upfronthosting.co.za>
2011-07-11 00:21:38vladrislinkissue8161 messages
2011-07-11 00:21:37vladriscreate