[Pyrex] passing method's args to extern func

Robert Bradshaw robertwb at math.washington.edu
Fri Jun 12 07:31:23 CEST 2009


On Jun 10, 2009, at 1:02 PM, Bartosz SKOWRON wrote:

> On Wed, Jun 10, 2009 at 7:19 AM, Stefan Behnel<stefan_ml at behnel.de>  
> wrote:
>
>>> /home/xsx/__usrsrc__/pypcap/pcap.pyx:235:18: Cannot convert Python
>>> object to 'pcap_pkthdr *'
>> Maybe you could add some info about what kind of type  
>> 'pcap_pkthdr' is?
>> Most likely, you will have to convert the data manually in some way.
>
> Propably - yes. This is a struct:
> struct pcap_pkthdr {
> 	struct timeval ts;	/* time stamp */
> 	bpf_u_int32 caplen;	/* length of portion present */
> 	bpf_u_int32 len;	/* length this packet (off wire) */
> };
>
> I tried to define it as ctypedef, but..hmm maybe i should cast  
> something later?

The point is that there is no Python object that corresponds  
naturally to a pcap_pkthdr, so pcap_pkthdr <-> object isn't automatic.

What you'll have to do is set the fields manually, or write a  
conversion function yourself. Either that or never treat it as an  
object (i.e. it can't be used as an argument in a Python method).

>> You can cast the object to <char*> first to let Pyrex do the  
>> conversion,
>> and then re-cast that to <unsigned char*> to make the C compiler  
>> happy, i.e.
>>
>>        cdef unsigned char* c_val
>>        c_val = <unsigned char*><char*>some_python_byte_string
>>
>> Note that this will not work with unicode strings, just plain byte  
>> strings.
>
> There will be not a problem with unicode. It should works as you  
> shown. Thanks!

Note that <char*>some_python_byte_string works differently in Cython  
and Pyrex (in the later, unless things have changed, it is like  
<char*><PyObject*>some_python_byte_string).

- Robert




More information about the Pyrex mailing list