[issue12769] String with NUL characters truncated by ctypes when assigning to a char array

2019-12-05 Thread Vinay Sajip


Change by Vinay Sajip :


--
versions: +Python 3.7, Python 3.8, Python 3.9 -Python 2.7, Python 3.3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12769] String with NUL characters truncated by ctypes when assigning to a char array

2011-08-29 Thread Vinay Sajip

Vinay Sajip  added the comment:

Seems related: #8161

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12769] String with NUL characters truncated by ctypes when assigning to a char array

2011-08-29 Thread Vinay Sajip

Vinay Sajip  added the comment:

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.

--
nosy: +vinay.sajip
versions: +Python 3.3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12769] String with NUL characters truncated by ctypes when assigning to a char array

2011-08-17 Thread Rafał Dowgird

Rafał Dowgird  added the comment:

The buffer output of the script suggests that the part after the '\000' has not 
been copied into the array at all. If that's the case, then the 'raw' output 
wouldn't print it anyway.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12769] String with NUL characters truncated by ctypes when assigning to a char array

2011-08-17 Thread STINNER Victor

STINNER Victor  added the comment:

I don't think that it's a bug, but a feature.

Example:

buffer=ctypes.create_string_buffer(4)
buffer.value='a\0bc'
print("buffer.value=%r" % buffer.value)
print("buffer.raw=%r" % buffer.raw)

displays

buffer.value='a'
buffer.raw='a\x00bc'

Sorry, I don't know how to get the raw value of a c_char array in a structure. 
You should maybe use another type.

--
nosy: +haypo

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12769] String with NUL characters truncated by ctypes when assigning to a char array

2011-08-17 Thread Rafał Dowgird

Rafał Dowgird  added the comment:

Attaching output of the script. 'x\000y\000' becomes 'x' after assigning to a 
char array.

--
Added file: http://bugs.python.org/file22921/output.txt

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12769] String with NUL characters truncated by ctypes when assigning to a char array

2011-08-17 Thread Rafał Dowgird

New submission from Rafał Dowgird :

The ctypes module seems to truncate NUL-containing strings when assigning to 
structure fields of type c_char*1024. Reproduced on a 2.7.2 compiled from 
tarball. Script to reproduce attached.

--
components: ctypes
files: reproduce.py
messages: 142274
nosy: Rafal.Dowgird
priority: normal
severity: normal
status: open
title: String with NUL characters truncated by ctypes when assigning to a char 
array
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file22920/reproduce.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com