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 vinay.sajip
Recipients Rafal.Dowgird, vinay.sajip, vstinner
Date 2011-08-29.18:05:11
SpamBayes Score 1.8596236e-14
Marked as misclassified No
Message-id <1314641112.18.0.728476024012.issue12769@psf.upfronthosting.co.za>
In-reply-to
Content
This behaviour also occurs in 3.3, where this does appear to be a bug. In Modules/_ctypes/cfield.c, the setting code does a strlen(), which is in fact questioned in a comment. In function s_set():

size = strlen(data); /* XXX Why not Py_SIZE(value)? */

Why not, indeed? value is the bytes object passed in, and using Py_SIZE does indeed copy all the bytes. However, it's operating in string rather than buffer mode: for example, it adds a byte for a terminating NUL, so if the 5-byte value b'x\x00y\x00z' were passed, 6 bytes are actually copied. This doesn't seem right.

Even after changing s_set to use Py_SIZE, you can't see the copied bytes when you access the attribute, since the code in s_get() skips out at the first NUL byte and then constructs using PyBytes_FromStringAndSize and the truncated size. One can see the convenience of avoiding the display of lots of NUL chars, but it doesn't seem correct to do this.

On 2.x it's a bit muddier, as arrays of c_char could be using ASCII strings, where a NUL terminator might be appropriate to consider.
History
Date User Action Args
2011-08-29 18:05:12vinay.sajipsetrecipients: + vinay.sajip, vstinner, Rafal.Dowgird
2011-08-29 18:05:12vinay.sajipsetmessageid: <1314641112.18.0.728476024012.issue12769@psf.upfronthosting.co.za>
2011-08-29 18:05:11vinay.sajiplinkissue12769 messages
2011-08-29 18:05:11vinay.sajipcreate