Storing unsafe C derivative of temporary Python in malloc'ed arrays

608 views
Skip to first unread message

Hiroaki Nakamura

unread,
Jul 6, 2015, 2:43:15 AM7/6/15
to cython...@googlegroups.com
Hi, all.

I tried to store C strings in malloc'ed arrays, but I got
"Storing unsafe C derivative of temporary Python" errors.
Could you help me work around these errors please?
Thanks!

Hiroaki

Toni Barth

unread,
Jul 6, 2015, 5:42:34 AM7/6/15
to cython...@googlegroups.com
Hi Hiroaki,

the problem seems clear to me, as far as I read your code.
You loop through strings and put them into a variable, let's call it
temp, and then want to store temp.encode('UTF-8') somewhere else.
The problem seems to be that the .encode() method just creates a
reference which points back to temp, but temp gets overridden the next
time the loop iterates, therefore being unstable, which causes the code
checker to cancel the compilation.
I can't give you a solution to solve your problem, I didn't read that
much that I could fix it, but maybe you can find a solution on your own
if you know the reason.
Hope I could help.

Best Regards.

Toni

Hiroaki Nakamura

unread,
Jul 6, 2015, 12:20:25 PM7/6/15
to cython...@googlegroups.com
Hi Toni,

Thanks for your advice.

I fixed my code and it compiles successfully now!
https://github.com/hnakamur/cygroonga/commit/f8df4ddea7bc0231f0765dc94a0ec48950055822#diff-62f32e0c44239a4ac1ff41461c1a5796R766

The cause of the previous error was that I just forgot to store the result of x.encode('UTF-8') to a python variable like:
http://docs.cython.org/src/tutorial/strings.html#encoding-text-to-bytes

Thanks for your help!

Robert Bradshaw

unread,
Jul 6, 2015, 12:30:34 PM7/6/15
to cython...@googlegroups.com
On Mon, Jul 6, 2015 at 8:59 AM, Hiroaki Nakamura <hnak...@gmail.com> wrote:
> Hi Toni,
>
> Thanks for your advice.
>
> I fixed my code and it compiles successfully now!
> https://github.com/hnakamur/cygroonga/commit/f8df4ddea7bc0231f0765dc94a0ec48950055822#diff-62f32e0c44239a4ac1ff41461c1a5796R766
>
> The cause of the previous error was that I just forgot to store the result
> of x.encode('UTF-8') to a python variable like:
> http://docs.cython.org/src/tutorial/strings.html#encoding-text-to-bytes
>
> Thanks for your help!

Just be sure to keep that Python reference around as long as you need
your char* to be valid.

> 2015-07-06 18:42 UTC+9 Toni Barth wrote:
>>
>> the problem seems clear to me, as far as I read your code.
>> You loop through strings and put them into a variable, let's call it
>> temp, and then want to store temp.encode('UTF-8') somewhere else.
>> The problem seems to be that the .encode() method just creates a
>> reference which points back to temp, but temp gets overridden the next
>> time the loop iterates, therefore being unstable, which causes the code
>> checker to cancel the compilation.
>> I can't give you a solution to solve your problem, I didn't read that
>> much that I could fix it, but maybe you can find a solution on your own
>> if you know the reason.
>> Hope I could help.
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Hiroaki Nakamura

unread,
Jul 6, 2015, 1:40:14 PM7/6/15
to cython...@googlegroups.com
Hi Robert,


2015-07-07 01:30:34 UTC+9 Robert Bradshaw wrote:
Just be sure to keep that Python reference around as long as you need
your char* to be valid.

Thanks for your reminder!
I fixed my code like:
https://github.com/hnakamur/cygroonga/commit/beb622d11d7ff3fea3ee66e100d4fea6ec0fd1c0#diff-62f32e0c44239a4ac1ff41461c1a5796R764

Without this fix, I confirmed values are overwritten.

Thanks for your help!

Best regards,
Hiroaki
Reply all
Reply to author
Forward
0 new messages