Cannot convert 'int8_t *' to Python object

2,556 views
Skip to first unread message

Bottiger

unread,
Sep 5, 2010, 1:30:41 AM9/5/10
to cython-users
I am using "from libc.stdint cimport *" to get fixed width integer
types.

However, it seems that Cython can only convert python objects to
"char*", even when the typedef is the same type as char*.

.....
cdef extern from "header.h":
void prototype(int8_t*)

def test(pystring):
prototype(pystring)

Cannot convert 'int8_t *' to Python Object
...
cdef extern from "header.h":
ctypedef char int8_t
void prototype(int8_t*)

def test(pystring):
prototype(pystring)

Cannot convert 'int8_t *' to Python Object


What am I doing wrong?

Stefan Behnel

unread,
Sep 5, 2010, 4:46:31 AM9/5/10
to cython...@googlegroups.com
Bottiger, 05.09.2010 07:30:

> I am using "from libc.stdint cimport *" to get fixed width integer
> types.

Regarding this bit, I just noticed that all integer types are simply
ctypedef'ed to int in that file:

ctypedef signed int int8_t
ctypedef signed int int16_t
ctypedef signed int int32_t
ctypedef signed int int64_t
ctypedef unsigned int uint8_t
ctypedef unsigned int uint16_t
ctypedef unsigned int uint32_t
ctypedef unsigned int uint64_t

Wouldn't it make more sense to typedef them to something with a closer
match to the integer types that Cython knows, i.e. byte/8, short/16,
int/32, long/64?


> However, it seems that Cython can only convert python objects to
> "char*", even when the typedef is the same type as char*.

Well, it won't convert *most* pointer types to Python objects
automatically, with (s/u)char* being the only special cased exception. But
you can use an explicit cast.


> cdef extern from "header.h":
> ctypedef char int8_t
> void prototype(int8_t*)
>
> def test(pystring):
> prototype(pystring)
>
> Cannot convert 'int8_t *' to Python Object

Try

prototype(<char*>pystring)

instead to get automatic conversion from a byte string to a char*, which
Cython should then be able to assign to a ctypedef'ed int8_t*.

BTW, note that this only handles byte strings, unicode strings require
explicit encoding.

Stefan

Bottiger

unread,
Sep 5, 2010, 5:51:26 PM9/5/10
to cython-users
Ok it works that way.

But now I have another library which has

prototype(uint8_t*)
prototype(<char*>pystring)

and when I use that trick, it says

Cannot assign type 'char *' to 'uint8_t *'

Is there any way to fix this?

Lisandro Dalcin

unread,
Sep 5, 2010, 9:12:40 PM9/5/10
to cython-users
On 5 September 2010 18:51, Bottiger <bott...@gmail.com> wrote:
> Ok it works that way.
>
> But now I have another library which has
>
> prototype(uint8_t*)
> prototype(<char*>pystring)
>
> and when I use that trick, it says
>
> Cannot assign type 'char *' to  'uint8_t *'
>
> Is there any way to fix this?
>

prototype(<int8_t*>(<char*>pystring))

--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

Stefan Behnel

unread,
Sep 6, 2010, 2:13:19 AM9/6/10
to cython...@googlegroups.com
Lisandro Dalcin, 06.09.2010 03:12:

> On 5 September 2010 18:51, Bottiger wrote:
>> Ok it works that way.
>>
>> But now I have another library which has
>>
>> prototype(uint8_t*)
>> prototype(<char*>pystring)
>>
>> and when I use that trick, it says
>>
>> Cannot assign type 'char *' to 'uint8_t *'
>>
>> Is there any way to fix this?
>>
>
> prototype(<int8_t*>(<char*>pystring))

I'd try this first:

prototype(<unsigned char*>pystring)

Stefan

Lisandro Dalcin

unread,
Sep 6, 2010, 10:02:11 AM9/6/10
to cython...@googlegroups.com

Cython has stricter typing than C, more or less like to C++, you have to:

prototype(<int8_t*>(<unsigned char*>pystring))

Robert Bradshaw

unread,
Sep 6, 2010, 11:55:19 AM9/6/10
to cython...@googlegroups.com
On Sun, Sep 5, 2010 at 1:46 AM, Stefan Behnel <stef...@behnel.de> wrote:
> Bottiger, 05.09.2010 07:30:
>>
>> I am using "from libc.stdint cimport *" to get fixed width integer
>> types.
>
> Regarding this bit, I just noticed that all integer types are simply
> ctypedef'ed to int in that file:
>
>    ctypedef   signed int  int8_t
>    ctypedef   signed int  int16_t
>    ctypedef   signed int  int32_t
>    ctypedef   signed int  int64_t
>    ctypedef unsigned int uint8_t
>    ctypedef unsigned int uint16_t
>    ctypedef unsigned int uint32_t
>    ctypedef unsigned int uint64_t
>
> Wouldn't it make more sense to typedef them to something with a closer match
> to the integer types that Cython knows, i.e. byte/8, short/16, int/32,
> long/64?

It really doesn't matter, as it completely ignores the defined
longness and generates code based on the actual longness.

- Robert

Lisandro Dalcin

unread,
Sep 6, 2010, 1:04:30 PM9/6/10
to cython...@googlegroups.com

Well, for integer promotion rules it does matter, right?

Robert Bradshaw

unread,
Sep 6, 2010, 1:13:29 PM9/6/10
to cython...@googlegroups.com
On Mon, Sep 6, 2010 at 10:04 AM, Lisandro Dalcin <dal...@gmail.com> wrote:
> On 6 September 2010 12:55, Robert Bradshaw <robe...@math.washington.edu> wrote:
>> On Sun, Sep 5, 2010 at 1:46 AM, Stefan Behnel <stef...@behnel.de> wrote:
>>> Bottiger, 05.09.2010 07:30:
>>>>
>>>> I am using "from libc.stdint cimport *" to get fixed width integer
>>>> types.
>>>
>>> Regarding this bit, I just noticed that all integer types are simply
>>> ctypedef'ed to int in that file:
>>>
>>>    ctypedef   signed int  int8_t
>>>    ctypedef   signed int  int16_t
>>>    ctypedef   signed int  int32_t
>>>    ctypedef   signed int  int64_t
>>>    ctypedef unsigned int uint8_t
>>>    ctypedef unsigned int uint16_t
>>>    ctypedef unsigned int uint32_t
>>>    ctypedef unsigned int uint64_t
>>>
>>> Wouldn't it make more sense to typedef them to something with a closer match
>>> to the integer types that Cython knows, i.e. byte/8, short/16, int/32,
>>> long/64?
>>
>> It really doesn't matter, as it completely ignores the defined
>> longness and generates code based on the actual longness.
>>
>
> Well, for integer promotion rules it does matter, right?

Sorry, yes, it does there. I was only thinking about conversion, so
yes, we should fix that (and bite the bullet that we can't be
right...).

- Robert

Lisandro Dalcin

unread,
Sep 6, 2010, 1:47:17 PM9/6/10
to cython...@googlegroups.com

Should we follow LP64?

Robert Bradshaw

unread,
Sep 6, 2010, 8:08:55 PM9/6/10
to cython...@googlegroups.com

Yes, that model makes the most sense to use.

- Robert

Reply all
Reply to author
Forward
0 new messages