Ask Your Question
0

Why does Cython use PyObjects for very simple expressions?

asked 2012-04-03 23:09:19 +0100

chaesloc2 gravatar image

updated 2012-04-03 23:45:32 +0100

Before:

%cython
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def f():
    cdef long a
    a=long(1.5)

After:

  /* "_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_GOTREF(__pyx_t_1);
  __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_GIVEREF(__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_GOTREF(__pyx_t_1);
  __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
0

answered 2012-04-04 03:25:38 +0100

Jason Grout gravatar image

Try:

%cython
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def f():
    cdef long a
    a=int(1.5)

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

Stats

Asked: 2012-04-03 23:09:19 +0100

Seen: 1,866 times

Last updated: Apr 04 '12