为什么 swig 在使用 __getattribute__ 时无法正确处理 AttributeError?

发布于 2024-12-23 03:04:31 字数 1278 浏览 1 评论 0原文

我有一个类在 SWIG 中从 C++ 导出到 Python。一切正常。现在我想定义 getattribute 来处理对 C++ 代码中内置脚本语言中定义的变量和函数的虚拟化访问。但是,当我使用 %pythoncode 定义 getattribute 函数时,它无法按预期工作。如果我找不到变量或函数,我应该引发名为 AttributeError 的异常。然而,SWIG 函数 getattr 对此感到窒息。

%pythoncode %{
    def __getattribute__(self, attribute):
        raise AttributeError(attribute)

因此

现在,如果我这样做,那就没问题了:

%pythoncode %{
    def __getattribute__(self, attribute):
        return object.__getattribute__(self, attribute)
%}

,当我引发 AttributeError 时,SWIG 生成的 getattr 的行为无法正常工作,就像我在未找到属性时应该做的那样。因此,出于我的目的,我将使用第二个示例并在例程之前插入我自己的代码以确定虚拟化函数是否存在。如果没有,我将让默认对象 getattribute 函数处理它。

有更好的方法来解决这个问题吗?

现在我看看这个,我发现它在常规 Python 2.7 中也不起作用:

class testmethods(object):
    def __init__(self):
        self.nofunc1 = None
        self.nofunc2 = "nofunc2"
    def func1(self):
        print "func1"
    def func2(self):
        print "func2"

    def __getattribute__(self, attribute):
        print "Attribute:",attribute
        raise AttributeError(attribute)

这会引发异常,但不会将责任切换到“getattr”函数。那么应该如何处理这个问题呢?

好吧,打击那个。如果 getattr 存在于引发异常的对象中,则该异常确实有效。所以喝水的行为是不正确的。

I have a class that is getting exported from C++ to Python in SWIG. Everything works fine. Now I want to define getattribute to handle virtualize access to variables and functions that are defined in a scripting language built into the C++ code. However when I define the getattribute function using %pythoncode it is not working as expected. If I cannot find the variables or functions I am supposed to raise and exception called AttributeError. However the SWIG function getattr chokes on this.

%pythoncode %{
    def __getattribute__(self, attribute):
        raise AttributeError(attribute)

%}

Now if I do this it is okay:

%pythoncode %{
    def __getattribute__(self, attribute):
        return object.__getattribute__(self, attribute)
%}

So, the behaviour of the SWIG produced getattr does not work properly when I raise an AttributeError like I am supposed to do when and attribute is not found. So, for my purposes I will be using the second example and inserting my own code before the routine to determine if a virtualized function exists. If not I will let the default object getattribute function handle it.

Is there a better way to approach this?

Now that I look at this i am finding it does not work in regular Python 2.7 either:

class testmethods(object):
    def __init__(self):
        self.nofunc1 = None
        self.nofunc2 = "nofunc2"
    def func1(self):
        print "func1"
    def func2(self):
        print "func2"

    def __getattribute__(self, attribute):
        print "Attribute:",attribute
        raise AttributeError(attribute)

This raises the exception, but does not switch responsibility to "getattr" function. So how is one supposed to handle this?

Okay, strike that. If getattr is present in the object raising the exception does work. So swig behaviour is not correct.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-12-30 03:04:31

现在,我想我明白了:

def __getattribute__(self, attribute): 
    try: 
        defattrib = object.__getattribute__(self, attribute) 
    except AttributeError,e: 
        defattrib = None 
    if defattrib is not None: 
        return defattrib             

    # find attributes in user defined functions 
            ... 


    # if an attribute cannot be found 
    raise AttributeError(attribute) 

这似乎运作良好。 swig getattr 似乎可以正确处理异常。所以这只是我的代码不起作用。

Now, I think I figured this out:

def __getattribute__(self, attribute): 
    try: 
        defattrib = object.__getattribute__(self, attribute) 
    except AttributeError,e: 
        defattrib = None 
    if defattrib is not None: 
        return defattrib             

    # find attributes in user defined functions 
            ... 


    # if an attribute cannot be found 
    raise AttributeError(attribute) 

This seems to work fine. swig getattr seems to handle the exception properly. So it is just my code that was not working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文