Python abc模块:扩展抽象基类和异常派生类导致意外行为

7 投票
2 回答
2045 浏览
提问于 2025-04-18 13:37

扩展一个抽象基类和一个从“object”派生的类时,结果是你预期的那样:如果你没有实现所有的抽象方法和属性,就会出现错误。

奇怪的是,如果把从“object”派生的类换成一个扩展了“Exception”的类,你就可以创建那些没有实现所有必需的抽象方法和属性的类的实例。

举个例子:

import abc

# The superclasses
class myABC( object ):
    __metaclass__ = abc.ABCMeta

    @abc.abstractproperty
    def foo(self):
        pass

class myCustomException( Exception ):
    pass

class myObjectDerivedClass( object ):
    pass

# Mix them in different ways
class myConcreteClass_1(myCustomException, myABC):
    pass

class myConcreteClass_2(myObjectDerivedClass, myABC):
    pass

# Get surprising results
if __name__=='__main__':
    a = myConcreteClass_1()
    print "First instantiation done. We shouldn't get this far, but we do."
    b = myConcreteClass_2()
    print "Second instantiation done. We never reach here, which is good."

...会得到...

First instantiation done. We shouldn't get this far, but we do.
Traceback (most recent call last):
  File "C:/Users/grahamf/PycharmProjects/mss/Modules/mssdevice/sutter/sutter/test.py", line 28, in <module>
    b = myConcreteClass_2()
TypeError: Can't instantiate abstract class myConcreteClass_2 with abstract methods foo

我知道“Exception”以及“myCustomException”没有“foo”这个属性,那我为什么还能创建“myCustomException”的实例呢?

编辑:为了记录,这就是我最终采用的一个变通方法。虽然不完全等价,但对我来说是有效的。

# "abstract" base class
class MyBaseClass( Exception ):
    def __init__(self):
        if not hasattr(self, 'foo'):
            raise NotImplementedError("Please implement abstract property foo")


class MyConcreteClass( MyBaseClass ):
    pass

if __name__=='__main__':
    a = MyConcreteClass()
    print "We never reach here, which is good."

2 个回答

0

Dano的回答是正确的,但缺少一个解决办法。你可以在自己的 __new__ 方法中重现对象代码:

import abc, traceback

# The superclasses
class MyABC(abc.ABC):

    @property
    @abc.abstractmethod
    def foo(self):
        pass

class MyCustomException( Exception ):
    pass

class MyObjectDerivedClass( object ):
    pass

# Mix them in different ways
class MyConcreteClass_1(MyCustomException, MyABC):

    def __new__(cls, *args, **kwargs):
        ''' Same abstract checks than in object.__new__ '''
        res = super().__new__(cls, *args, **kwargs)
        if cls.__abstractmethods__:
            raise TypeError(f"Can't instantiate abstract class {cls.__name__} with abstract methods {','.join(sorted(cls.__abstractmethods__))}")
        return res

class MyConcreteClass_2(MyObjectDerivedClass, MyABC):
    pass

# No longer get surprising results
if __name__=='__main__':
    try:
        a = MyConcreteClass_1()
    except TypeError:
        traceback.print_exc()
    try:
        b = MyConcreteClass_2()
    except TypeError:
        traceback.print_exc()

这样就会产生两个预期的异常。

6

看起来这是因为 BaseException__new__ 方法不关心抽象方法或属性。

当你尝试创建 myConcreteClass_1 的实例时,它实际上调用了 Exception 类的 __new__ 方法。而当你想创建 myConcreteClass_2 的实例时,它则调用了 object__new__ 方法:

>>> what.myConcreteClass_1.__new__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: exceptions.Exception.__new__(): not enough arguments
>>> what.myConcreteClass_2.__new__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object.__new__(): not enough arguments

Exception 类并没有提供 __new__ 方法,但它的父类 BaseException 是有的

static PyObject *
BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyBaseExceptionObject *self;

    self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
    if (!self)
        return NULL;
    /* the dict is created on the fly in PyObject_GenericSetAttr */
    self->dict = NULL;
    self->traceback = self->cause = self->context = NULL;
    self->suppress_context = 0;

    if (args) {
        self->args = args;
        Py_INCREF(args);
        return (PyObject *)self;
    }

    self->args = PyTuple_New(0);
    if (!self->args) {
        Py_DECREF(self);
        return NULL;
    }

    return (PyObject *)self;
}

对比一下 object__new__ 实现

static PyObject *
object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    if (excess_args(args, kwds) &&
        (type->tp_init == object_init || type->tp_new != object_new)) {
        PyErr_SetString(PyExc_TypeError, "object() takes no parameters");
        return NULL;
    }

    if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
        PyObject *abstract_methods = NULL;
        PyObject *builtins;
        PyObject *sorted;
        PyObject *sorted_methods = NULL;
        PyObject *joined = NULL;
        PyObject *comma;
        _Py_static_string(comma_id, ", ");
        _Py_IDENTIFIER(sorted);

        /* Compute ", ".join(sorted(type.__abstractmethods__))
           into joined. */
        abstract_methods = type_abstractmethods(type, NULL);
        if (abstract_methods == NULL)
            goto error;
        builtins = PyEval_GetBuiltins();
        if (builtins == NULL)
            goto error;
        sorted = _PyDict_GetItemId(builtins, &PyId_sorted);
        if (sorted == NULL)
            goto error;
        sorted_methods = PyObject_CallFunctionObjArgs(sorted,
                                                      abstract_methods,
                                                      NULL);
        if (sorted_methods == NULL)
            goto error;
        comma = _PyUnicode_FromId(&comma_id);
        if (comma == NULL)
            goto error;
        joined = PyUnicode_Join(comma, sorted_methods);
        if (joined == NULL)
            goto error;

        PyErr_Format(PyExc_TypeError,
                     "Can't instantiate abstract class %s "
                     "with abstract methods %U",
                     type->tp_name,
                     joined);
    error:
        Py_XDECREF(joined);
        Py_XDECREF(sorted_methods);
        Py_XDECREF(abstract_methods);
        return NULL;
    }
    return type->tp_alloc(type, 0);
}

你可以看到,object.__new__ 有代码会在有未被重写的抽象方法时抛出错误,但 BaseException.__new__ 则没有这样的检查。

撰写回答