How to write partial of a buffer which was returned from a C function to a file?

Jach Fong jfong at ms4.hinet.net
Sat Apr 14 21:51:39 EDT 2018


Gregory Ewing at 2018/4/15 PM 08:20 wrote:
> Jach Fong wrote:
>>  >>> pvoid = ctypes.c_void_p(ctypes.addressof(buf0))
>>  >>> pvoid.contents
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> AttributeError: 'c_void_p' object has no attribute 'contents'
> 
> I think the 'contents' attribute only applies to pointers that are
> pointing at part of another Python object. Your c_void_p instance
> is not that kind of pointer.
> 
> I must say the ctypes documentation is rather confusing when it
> comes to these kinds of details. It doesn't help that the "Pointers
> and Arrays" section is marked as "Not yet written". Does anyone
> have any plans to finish it?

 >>> pt = ctypes.cast(buf0, ctypes.POINTER(ctypes.c_void_p))
 >>> type(pt)
<class '__main__.LP_c_void_p'>
 >>> pt.contents
c_void_p(1835102323)

 From the API point of view, I think it's better not to dive into the 
ocean of "type" too deeply, especially when foreign language was 
involved. You may get totally confused. (unless you want to write code 
like in the IOCCC contest:-)

I have to admit that the ctypes' document was written pretty well. Most 
of my problems were caused by that I didn't read it thoughtfully:-(

--Jach

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus




More information about the Python-list mailing list