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

binding=True in .pxd generates global too late #1658

Open
jdemeyer opened this Issue Apr 3, 2017 · 0 comments

Comments

Projects
None yet
1 participant
@jdemeyer
Contributor

jdemeyer commented Apr 3, 2017

Consider test.pxd:

cimport cython

@cython.binding(True)
cpdef inline bint fun():
    return 1

and test.pyx:

x = repr(fun)   # Or anything else accessing "fun"

This compiles but fails at runtime with a NameError saying that fun is not defined. The reason is that Cython generates code for x = repr(fun) before the code which assigns fun in the module global dict:

  /* "test.pyx":1
 * x = repr(fun)             # <<<<<<<<<<<<<<
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_fun); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 1, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_Repr(__pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 1, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_x, __pyx_t_2) < 0) __PYX_ERR(1, 1, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 1, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_2);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_2) < 0) __PYX_ERR(1, 1, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;

  /* "test.pxd":4
 * 
 * @cython.binding(True)
 * cpdef inline bint fun():             # <<<<<<<<<<<<<<
 *     return 1
 */
  __pyx_t_2 = __Pyx_CyFunction_NewEx(&__pyx_mdef_4test_1fun, 0, __pyx_n_s_fun, NULL, __pyx_n_s_test_2, __pyx_d, ((PyObject *)__pyx_codeobj_)); if (unlikely(!__pyx_t_2))
  __Pyx_GOTREF(__pyx_t_2);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_fun, __pyx_t_2) < 0) __PYX_ERR(0, 4, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;

The problem does not occur when everything is in one .pyx file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment