0

I'm trying to use a line_profiler on my Cython code and asked this question, I now realize that the question was not clear. The problem is that the function that I import from Cython hasn't got any __code__ attribute.

Should Cython cpdef, cdef or def functions have this attribute? Any ideas why mine hasn't got one?

This is my cython_agri.pyx file:

# cython: linetrace=True
cimport cython
import numpy as np
from datetime import timedelta, datetime


@cython.binding(True)
cpdef list read_static_binary_data(data_row: list, read_point: int, binary_data: object, tlg_dict: dict, dt: np.dtype,
                                    start_date: datetime):
    """Function to obtain the position and time data """
    cdef int nr_d = 3
    cdef float ten_minus_seven = pow(10, -7)
    ....
    return [data_row, nr_dlvs, nr_d - 1]

This is my setup.py

import setuptools
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Compiler.Options import get_directive_defaults
directive_defaults = get_directive_defaults()
directive_defaults['linetrace'] = True
directive_defaults['binding'] = True

extensions = [
    Extension("pyAgriculture", ["pyAgriculture\cython_agri.pyx"], define_macros=[('CYTHON_TRACE', '1')])
]
setup(ext_modules=cythonize(extensions, language_level="3"))

I compile the code using this command: py pyAgriculture\setup.py build_ext --inplace.

axel_ande
  • 359
  • 1
  • 4
  • 20
  • Not quite answering your question but most of the time you should just pick either a regular `def` function, or a `cdef` function, instead of `cpdef` functions. `cpdef` functions are really only for functions that you need to call from Python but want to call efficiently from within Cython. They have all the limitations of `def` and `cdef` functions and so are usually the first choice (unless you have a very specific use-case) – DavidW Mar 05 '22 at 08:42
  • Unfortunately it didn't help switching to `cdef` or `def` :( – axel_ande Mar 06 '22 at 18:49

0 Answers0