0

I'm trying to profile my Cython code using the line_profile package (The code runs but I want to know if I can optimize it..) but when I try to profile it I get an AttributeError: 'builtin_function_or_method' object has no attribute '__code__'. (I've tried to get some help from this and this)

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)
    ....

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. This is from my main_python.py file where I run the code:

...
from cython_agri import read_static_binary_data
read_static_binary_data = profile(read_static_binary_data)
data_row, nr_dlvs, nr_static = read_static_binary_data(data_row, read_point, binary_data, tlg_dict, self.dt, self.start_date)

When I try to run the profiler with: kernprof -l agriculture.py I get this error

  File "agriculture.py", line 301, in read_binaryfile
    read_static_binary_data = profile(read_static_binary_data)
  File "C:\dev\program\pyAgriculture11783\venv\lib\site-packages\line_profiler\line_profiler.py", line 53, in __call__
    elif is_generator(func):
  File "C:\dev\program\pyAgriculture11783\venv\lib\site-packages\line_profiler\line_profiler.py", line 38, in is_generator
    isgen = (f.__code__.co_flags & CO_GENERATOR) != 0
AttributeError: 'builtin_function_or_method' object has no attribute '__code__'. Did you mean: '__call__'?

Any ideas what I might have done wrong?

axel_ande
  • 359
  • 1
  • 4
  • 20
  • I don't have an answer but perhaps a couple of clues. The `builtin_function_or_method` is probably `pow`. The line `print(func.__code__)` in `line_profiler.py` that produces the error is not found in the most recent version of line_profiler here https://github.com/pyutils/line_profiler . Check what version you are using and if it's compatible with your the rest of your environment. – Nikolay Hüttenberend Mar 03 '22 at 09:51
  • Thanks for looking in to it @NikolayManolov, I hade made some adjustment to the `line_profiler.py` (thats why the error originated from `print(func.__code__)`, I've now updated my question with the "correct" `line_profiler.py` code. The `func` is the built version of `read_static_binary_data` so I don't think it has anything to do with the pow function(?) – axel_ande Mar 03 '22 at 11:53
  • Probably not the pow function, seems to be working for me. Then it must be one of your functions that doesn't have a __code__ attribute but my knowledge is lacking in that erea. As a quick workaround you could amend the line_profiler code to check if the attribute exists before accessing it. This happens when it is checking if your function is a generator. If it's not a generator then it doesn't matter. Of course it would be better to know why this is happening. Perhaps something is not fully initialized – Nikolay Hüttenberend Mar 04 '22 at 07:36
  • Yes, during the debugging I came to the conclusion that it is my `read_static_binary_data` function written in a .pyx file compiled to a .pyd file that doesn't have a `code` attribute, should I add a function (within the cpdef?) it manually? (if so what should it contain?) – axel_ande Mar 04 '22 at 08:01
  • _ _ code _ _ is not usually something that you would set manually. I guess you could amend your question to reflect the findings. This does not seem to be a problem with line_profiler, rather with cpdef and _ _ code _ _ attribute or so – Nikolay Hüttenberend Mar 04 '22 at 09:27
  • line_profiler has (had?) some issue with cpdef functions, e.g. see https://stackoverflow.com/q/57591990/5769463 In the linked question, the module was compiled slightly different (maybe that would be better way anyways) – ead Mar 04 '22 at 11:12
  • Thanks for the suggestion @ead, I've tried to add a dummy `def` function but that does not help :( . I'm not sure if the `read_static_binary_data = profile(read_static_binary_data)` line is necessary, if I don't include it I get no failure but the `lprof` file is then empyt :( – axel_ande Mar 04 '22 at 13:00

0 Answers0