编程语言
首页 > 编程语言> > Python代码找不到cython函数,尽管它甚至不应该尝试找到它.为什么?

Python代码找不到cython函数,尽管它甚至不应该尝试找到它.为什么?

作者:互联网

底部是两个文件,一个应该执行的超级最小python文件和一个cython文件.如果将它们保存为文件,则将cython命名为“cycode.pyx”,一旦执行另一个文件(例如“start.py”),它将自动编译并运行

问题

如果你执行纯python文件/.start.py,你将从Cython中获得属性错误.

Exception AttributeError: “‘cycode.Item’ object has no attribute
‘export'” in ‘cycode.insertItem’

根据我的经验,这意味着Python函数或对象试图访问未声明为public的cython代码(或cpdef,readonly,def等).但我从来没有打算从Python访问这个功能.据我所知,这不应该发生. cython和python之间应该有一个干净的分离. Python只获得一个包含简单dicts的列表.

问题是为什么会这样?我的目标不是让它工作,这可以用一个简单的cpdef来完成.但要理解为什么会发生这种情况,并最终如何以干净和受控的方式将数据从cython发送到python,而不必为python领域声明任何cython对象公开.

start.py

    #! /usr/bin/env python3
    # -*- coding: utf-8 -*-
    import pyximport; pyximport.install()
    import cycode
    #Register a callback function with the cython module.
    #In this case just attempt to print the data.
    cycode.callbacksDatabase.update.append(print)


    #Call an insert function to create and insert a cython object.
    #We should have nothing to do with this object,
    #we just receive a simple list of dict(s) via the callback.
    cycode.new1() 

cycode.pyx

# cython: language_level=3
cdef class Block:
    """A container class for Items"""
    cdef list content
    cdef void insert(self, Item item)
    cdef list export(self)    

    def __cinit__(self):                
        self.content = []

    cdef void insert(self, Item item):        
        self.content.append(item)           

    cdef list export(self):
        """The frontend should just receive simple data types to
        vizualize them. Create export dicts of all items"""
        cdef list result        
        result = []
        for item in self.content:            
            result.append(item.export())  #THIS is the problem. item.export() cannot be found.
        return result        

cdef class Item:
    cdef int value
    cdef dict export(self)
    def __cinit__(self, int value):
        self.value = value

    cdef dict export(self):        
        return {
            "id" : id(self),
            "value" : self.value,
        }

########API#############
class Callbacks():    
    def __init__(self):
        self.update = []        

    def _update(self):
        ex = block.export()
        for func in self.update:            
            func(ex)

cdef void insertItem(int value):        
    cdef Item item
    item = Item(value)  #this should create a cython object, not a python one.
    block.insert(item)            
    callbacksDatabase._update()        

def new1():    
    insertItem(1)

#######Actual Data on module level#######
cdef Block block
block = Block() #this is just for the cython code. No direct access from python allowed.
callbacksDatabase = Callbacks()  #this should be accesable from python

解决方法:

典型的IRC效果……一旦你详细解决了问题,解决方案会在几分钟后弹出(虽然这次通过Facebook聊天……).

我忘了在Python / Cython中,for循环不会自动创建一个新的范围,并且循环变量没有什么特别之处.它也需要声明为cdef.

    cdef Item item
    for item in self.content:            
        result.append(item.export())

标签:python,scope,c-3,cython,pyrex
来源: https://codeday.me/bug/20190703/1366295.html