Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mobiusklein opened this issue Jul 28, 2017 · 1 comment
Milestone

Comments

@mobiusklein
Copy link

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

@scoder scoder closed this as completed in 4b61de6 Jul 31, 2017
@scoder
Copy link
Contributor

scoder commented Jul 31, 2017

Thank you for the very detailed report.

@scoder scoder added this to the 0.26.1 milestone Aug 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants