Ask Your Question

Why does Cython use PyObjects for very simple expressions?

asked 2012-04-03 16:09:19 -0500

chaesloc2 gravatar image

updated 2012-04-03 16:45:32 -0500


cimport cython
def f():
    cdef long a


  /* "_data_programfiles__sage_sage_notebook_sagenb_home_admin_1_code_sage119_spyx_0.pyx":12
 * def f():
 *     cdef long a
 *     a=long(1.5)             # <<<<<<<<<<<<<<
  __pyx_t_1 = PyFloat_FromDouble(1.5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)(&PyLong_Type))), ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __pyx_t_3 = __Pyx_PyInt_AsLong(__pyx_t_1); if (unlikely((__pyx_t_3 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_v_a = __pyx_t_3;

Why is the c code so complicated? I expected something like "__pyx_v_a=long(1.5);" Is there any way I can avoid all the PyObjects? (I don't need to, but I managed to do so in the rest of my code)

EDIT: So one way seems to be to 'cdef extern from "math.h" \n long floor(double x)'.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2012-04-03 20:25:38 -0500

Jason Grout gravatar image


cimport cython
def f():
    cdef long a

I think the problem is that you are calling the python long function. Instead, you can either do the above (or use your floor idea).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-04-03 16:09:19 -0500

Seen: 1,154 times

Last updated: Apr 03 '12