Dieter Maurer | 7 Jun 2012 10:44
Picon

[Cython] Bug: bad C code generated for (some) "... and ... or ..." expressions

"cython 0.13" generates bad C code for the attached "pyx" file.

"cython" itself recognizes that it did something wrong and emits "<error>;"
to the generated file:

...
static  __pyx_t_12cybug_and_or_pointer __pyx_f_12cybug_and_or_bug(PyObject *__pyx_v_o) {
  __pyx_t_12cybug_and_or_pointer __pyx_r;
  int __pyx_t_1;
  __pyx_t_12cybug_and_or_pointer __pyx_t_2;
  <error>;
  <error>;
...

Attachment (cybug_and_or.pyx): text/x-cython, 164 bytes

The error probably happens because it is difficult for "cython" to
determine the type for "and" and "or" expressions (if the operand types
differ). In an "cond and t or f" expression, however, the result type
is "type(t)" if "type(t) == type(f)", independent of "type(cond)".

It might not be worse to special case this type of expressions.
It would however be more friendly to output an instructive
error message instead of generating bad C code.

--
Dieter
(Continue reading)

Stefan Behnel | 8 Jun 2012 08:50
Picon
Favicon

Re: [Cython] Bug: bad C code generated for (some) "... and ... or ..." expressions

Hi,

thanks for the report.

Dieter Maurer, 07.06.2012 10:44:
> "cython 0.13" generates bad C code for the attached "pyx" file.

Could you try the latest release? I would at least expect an error instead
of actually generating code.

> "cython" itself recognizes that it did something wrong and emits "<error>;"
> to the generated file:
> 
> ...
> static  __pyx_t_12cybug_and_or_pointer __pyx_f_12cybug_and_or_bug(PyObject *__pyx_v_o) {
>   __pyx_t_12cybug_and_or_pointer __pyx_r;
>   int __pyx_t_1;
>   __pyx_t_12cybug_and_or_pointer __pyx_t_2;
>   <error>;
>   <error>;
> ...
> 

This is generated from this Cython code:

> cdef pointer bug(o):
>   return o is not None and to_pointer(o) or NULL

The right way to implement this is:

(Continue reading)

Dieter Maurer | 8 Jun 2012 13:38
Picon

Re: [Cython] Bug: bad C code generated for (some) "... and ... or ..." expressions

Stefan Behnel wrote at 2012-6-8 08:50 +0200:
>thanks for the report.
>
>Dieter Maurer, 07.06.2012 10:44:
>> "cython 0.13" generates bad C code for the attached "pyx" file.
>
>Could you try the latest release? I would at least expect an error instead
>of actually generating code.

The latest release on PyPI is "0.16". It behaves identical to version
"0.13": no error message; just wrongly generated C code (C code
containing "<error>;" "statements".

--
Dieter

Gmane