Cyhton 与 line_profiler 结合使用会导致 AttributeError: 'builtin_function_or_method'对象没有属性“__code__”

发布于 2025-01-11 07:40:20 字数 2458 浏览 1 评论 0原文

我正在尝试使用 line_profile 包来分析我的 Cython 代码(代码运行,但我想知道我是否可以优化它..),但是当我尝试分析它时,我得到一个 AttributeError: 'builtin_function_or_method' object has没有属性“__code__”。 (我试图从 this 获得一些帮助这个

这是我的 cython_agri.pyx文件:

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

这是我的 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"))

我使用以下命令编译代码:py pyAgriculture\setup.py build_ext --inplace。这是来自我运行代码的 main_python.py 文件:

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

当我尝试使用以下命令运行探查器时:kernprof -l Agriculture.py 我收到此错误

  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__'?

你知道我可能做错了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文