Python abc module: Extending both an abstract base class and an exception-derived class leads to surprising behavior

我怕爱的太早我们不能终老 提交于 2019-12-05 04:17:23

It looks like this is because the __new__ method for BaseException doesn't care about abstract methods/properties.

When you try to instantiate myConcreteClass_1, it ends up calling __new__ from the Exception class. When want to instantiate myConcreteClass_2, it calls the __new__ from object:

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

The Exception class doesn't provide a __new__ method, but it's parent, BaseException, does:

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;
}

Compare this to the __new__ implementation for object:

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);
}

As you can see object.__new__ has code to throw an error when there are abstract methods that aren't overridden, but BaseException.__new__ does not.

Dano's answer is accurate but is missing a workaround. You can reproduce the object code in your own __new__ method:

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

Which yields the two expected exceptions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!