Re: [ctypes-users] Strings with null characters
Brought to you by:
theller
From: Dominik G. <de...@na...> - 2011-06-26 14:10:58
|
Hi, here is an example code that breaks: from ctypes import * class a(Structure): _fields_ = [("aaa", c_char * 14)] mya = a() mya.aaa = "a\0b" print mya.aaa 'a' I found two workarounds at http://stackoverflow.com/questions/5082753/how-do-i-build-a-python-string-from-a-ctype-struct , but those solutions look somehow nasty ... -nik Am 26.06.2011 15:17, schrieb Dominik George: > Hi, > > I am using ctypes while porting parts of glibc to pure Python (i.e. to > be able to do ifconfig from within Python). The complete code can be > found in my Redmine under > http://git.proparity.net/redmine/projects/pyverkko/repository/revisions/master/show/src/pyverkko/ifconfig > . > > In structs.py, I have defined the structs used by glibc to do ioctl(), > which works perfectly in most cases. But, this breaks when I have to > cope with null bytes. For example, set_ip_address("eth0", "192.168.0.1") > breaks, because: > > I create an isntance of sockaddr, which is a ctypes Structure (or, a > PackableStructure, which is a ctypes Structure to which I added a pack() > method as described in the FAQ). Now, assigning the binary form of > 192.168.0.1 to a member field of sockaddr, breaks because it is > expecting a null-termianted string, so the string I assign is only the > first two bytes because the third octet, 0, terminates the string. > > I am not sure if Python does this while assigning, because the field > sa_data I am assigning to is declared as a char[14]. > > How can I take care that *all* bytes of a string are stored in the > Structure and recognized when I run pack(), i.e. strings aren't cut off > at the first null byte? > > Regards, > Nik > > ------------------------------------------------------------------------------ > All of the data generated in your IT infrastructure is seriously valuable. > Why? It contains a definitive record of application performance, security > threats, fraudulent activity, and more. Splunk takes this data and makes > sense of it. IT sense. And common sense. > http://p.sf.net/sfu/splunk-d2d-c2 > _______________________________________________ > ctypes-users mailing list > cty...@li... > https://lists.sourceforge.net/lists/listinfo/ctypes-users |