python - 当从 QObject 派生的类的属性中引发 AttributeError 时出现误导性错误消息

标签 python properties pyqt pyqt5 attributeerror

假设我创建了一个具有属性的常规 Python 类,并且在该属性的实现中我犯了一个错误,导致了 AttributeError。一个MVCE如下:

class MyClass():

    @property
    def myProp(self):
        raise AttributeError("my mistake")

def main():
    # Gives a 'expected-error-message' as expected
    myObject = MyClass()
    print("Regular object property: {}".format(myObject.myProp))


if __name__ == "__main__":
    main()

这会产生以下错误,正如预期的那样:

Traceback (most recent call last):
  File "prop_regular.py", line 14, in <module>
    main()
  File "prop_regular.py", line 10, in main
    print("Regular object property: {}".format(myObject.myProp))
  File "prop_regular.py", line 5, in myProp
    raise AttributeError("my mistake")
AttributeError: my mistake

但是,如果我让该类继承自 QObject,则错误会令人困惑。例如运行以下代码

from PyQt5 import QtCore

class MyQtClass(QtCore.QObject):

    @property
    def myProp(self):
        raise AttributeError("my-mistake")


def main():
    app = QtCore.QCoreApplication([])

    # Gives confusing error message: 'MyQtClass' object has no attribute 'myProp'
    qc = MyQtClass()
    print("Qt object property: {}".format(qc.myProp))


if __name__ == "__main__":
    main()

给出

Traceback (most recent call last):
  File "prop_qt.py", line 19, in <module>
    main()
  File "prop_qt.py", line 15, in main
    print("Qt object property: {}".format(qc.myProp))
AttributeError: 'MyQtClass' object has no attribute 'myProp'

但是MyQtClass确实有一个myProp属性,它只是包含一个错误!这花了我一些时间在实际应用程序中进行调试。

所以我的问题是:这里发生了什么?这是 PyQt 中的错误吗?还是我做错了什么?

编辑:

Ekhumoro 的回答促使我查看 PyQt (5.6) 源代码。看来错误源自QtCore/qpycore_qobject_getattr.cpp,它定义了以下函数

// See if we can find an attribute in the Qt meta-type system.  This is
// primarily to support access to JavaScript (e.g. QQuickItem) so we don't
// support overloads.
PyObject *qpycore_qobject_getattr(const QObject *qobj, PyObject *py_qobj,
        const char *name)
{
    const QMetaObject *mo = qobj->metaObject();

    // Try and find a method with the name.
    QMetaMethod method;
    int method_index = -1;

    // Count down to allow overrides (assuming they are possible).
    for (int m = mo->methodCount() - 1; m >= 0; --m)
    {
        method = mo->method(m);

        if (method.methodType() == QMetaMethod::Constructor)
            continue;

        // Get the method name.
        QByteArray mname(method.methodSignature());
        int idx = mname.indexOf('(');

        if (idx >= 0)
            mname.truncate(idx);

        if (mname == name)
        {
            method_index = m;
            break;
        }
    }

    if (method_index >= 0)
    {
        // Get the value to return.  Note that this is recreated each time.  We
        // could put a descriptor in the type dictionary to satisfy the request
        // in future but the typical use case is getting a value from a C++
        // proxy (e.g. QDeclarativeItem) and we can't assume that what is being
        // proxied is the same each time.
        if (method.methodType() == QMetaMethod::Signal)
        {
            // We need to keep explicit references to the unbound signals
            // (because we don't use the type dictionary to do so) because they
            // own the parsed signature which may be needed by a PyQtSlotProxy
            // at some point.
            typedef QHash<QByteArray, PyObject *> SignalHash;

            static SignalHash *sig_hash = 0;

            // For crappy compilers.
            if (!sig_hash)
                sig_hash = new SignalHash;

            PyObject *sig_obj;

            QByteArray sig_str = method.methodSignature();

            SignalHash::const_iterator it = sig_hash->find(sig_str);

            if (it == sig_hash->end())
            {
                sig_obj = (PyObject *)qpycore_pyqtSignal_New(
                        sig_str.constData());

                if (!sig_obj)
                    return 0;

                sig_hash->insert(sig_str, sig_obj);
            }
            else
            {
                sig_obj = it.value();
            }

            return qpycore_pyqtBoundSignal_New((qpycore_pyqtSignal *)sig_obj,
                    py_qobj, const_cast<QObject *>(qobj));
        }

        // Respect the 'private' nature of __ names.
        if (name[0] != '_' || name[1] != '_')
        {
            QByteArray py_name(Py_TYPE(py_qobj)->tp_name);
            py_name.append('.');
            py_name.append(name);

            return qpycore_pyqtMethodProxy_New(const_cast<QObject *>(qobj),
                    method_index, py_name);
        }
    }

    // Replicate the standard Python exception.
    PyErr_Format(PyExc_AttributeError, "'%s' object has no attribute '%s'",
            Py_TYPE(py_qobj)->tp_name, name);

    return 0;
}

如果在 Qt 元类型系统中找不到该名称的方法,则会引发该错误消息。我想确实很难做其他事情。

最佳答案

这是normal python behaviour当类定义 __getattr__ 时,因为每当引发 AttributeError 时都必须调用它:

>>> class MyClass():
...     @property
...     def myProp(self):
...         raise AttributeError("my mistake")
...     def __getattr__(self, name):
...         raise AttributeError("no attribute %r" % name)
...
>>> x = MyClass()
>>> x.myProp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in __getattr__
AttributeError: no attribute 'myProp'

如果没有定义__getattr__,则传播原始异常;否则,原始内容将被吞掉,而由 __getattr__ 引发的异常将被传播。

这意味着从 QObject 派生的所有 PyQt 类都必须定义 __getattr__

关于python - 当从 QObject 派生的类的属性中引发 AttributeError 时出现误导性错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45016491/

相关文章:

python - 在 Python 中与 Pandas 进行大型合并时出现 MemoryError

java - 以编程方式加载 Spring 3.1 中的属性

iphone - 为什么在非 ARC 环境中的属性上使用关键字 "strong"?

c# - 应用程序设置与应用程序设置。应用程序设置已过时?

python - 如何将一行编辑的焦点更改为另一行编辑

从模块导入函数而从另一个模块导入类时出现 Python 导入错误 (ModuleNotFoundError)

python - 如何在Python中基于x,y和z轴旋转cv2.putText?

python - PyQt Qthread自动重启

python - QWizard:更改标题字段的高度/尺寸

python - 如何在 Python 应用程序中建立与 Kerberos 的连接?