Skip to content

Cython 0.26 generates invalid C in extension type __iter__ methods for MSVC and Python 2.7 #1801

@mobiusklein

Description

@mobiusklein

Given the following extension class

cdef class PolynomialParameters(object):
    cdef:
        public list elementary_symmetric_polynomial
        public list power_sum
    def __init__(self, elementary_symmetric_polynomial, power_sum):
        self.elementary_symmetric_polynomial = elementary_symmetric_polynomial
        self.power_sum = power_sum

    def __iter__(self):
        yield self.power_sum
        yield self.elementary_symmetric_polynomial

Cython 0.26 generates the following code for the class's __iter__ method:

static PyObject *__pyx_gb_7brainpy_8_speedup_20PolynomialParameters_4generator(__pyx_CoroutineObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
{
  struct __pyx_obj_7brainpy_8_speedup___pyx_scope_struct____iter__ *__pyx_cur_scope = ((struct __pyx_obj_7brainpy_8_speedup___pyx_scope_struct____iter__ *)__pyx_generator->closure);
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("None", 0);
  __Pyx_TraceDeclarations
  __Pyx_TraceCall("__iter__", __pyx_f[0], 105, 0, __PYX_ERR(0, 105, __pyx_L1_error));
  switch (__pyx_generator->resume_label) {
    case 0: goto __pyx_L3_first_run;
    case 1: goto __pyx_L4_resume_from_yield;
    case 2: goto __pyx_L5_resume_from_yield;
    default: /* CPython raises the right error here */
    __Pyx_TraceReturn(Py_None, 0);
    __Pyx_RefNannyFinishContext();
    return NULL;
  }
 //gotos....

The line __Pyx_TraceDeclarations looks like it was meant to be a macro, given that it has no trailing semi-colon, but MSVC appears to be seeing it literally and throws the error error C2143: syntax error : missing ';' before 'type'.

Cython 0.25.2 generated the following code for the same method

static PyObject *__pyx_gb_7brainpy_8_speedup_20PolynomialParameters_4generator(__pyx_CoroutineObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */
{
  struct __pyx_obj_7brainpy_8_speedup___pyx_scope_struct____iter__ *__pyx_cur_scope = ((struct __pyx_obj_7brainpy_8_speedup___pyx_scope_struct____iter__ *)__pyx_generator->closure);
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("None", 0);
  switch (__pyx_generator->resume_label) {
    case 0: goto __pyx_L3_first_run;
    case 1: goto __pyx_L4_resume_from_yield;
    case 2: goto __pyx_L5_resume_from_yield;
    default: /* CPython raises the right error here */
    __Pyx_RefNannyFinishContext();
    return NULL;
  }
 //gotos...

which compiles successfully with the same setup.

There is no issue on Linux. Could it be that __Pyx_TraceDeclarations is only defined inside a Unix-specific preprocessor block?

The repository this code is from is https://github.com/mobiusklein/brainpy, and the file being compiled is brainpy/_speedup.pyx in the course of the standard python setup.py install process

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions