Python exception - how does the args attribute get automatically set?

百般思念 提交于 2020-12-26 06:38:10

问题


Suppose I define the following exception:

>>> class MyError(Exception):
...     def __init__(self, arg1):
...         pass

Then I instantiate the class to create an exception object:

>>> e = MyError('abc')
>>> e.args
('abc',)

Here how is the args attribute getting set? (In the __init__, I am doing nothing.)


回答1:


args is implemented as a data descriptor with __get__ and __set__ methods.

This takes place inside BaseException.__new__ like @bakatrouble mentioned. Among other things, what happens inside BaseException.__new__ is roughly like the Python code below:

class BaseException:
    def __new__(cls, *args): 
        # self = create object of type cls
        self.args = args  # This calls: BaseException.args.__set__(self, args) 
        ...
        return self

In C code of Python 3.7.0 alpha 1, the above Python code looks like this (inspect Python's C code for any past or future differences):

BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    # other things omitted... 
    self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
    # many things follow... 
    if (args) {
        self->args = args;
        Py_INCREF(args);
        return (PyObject *)self;

    }
    # many more things follow
}

Experimenting interactively:

>>> e = Exception('aaa')
>>> e
Exception('aaa',)

>>> BaseException.args.__set__(e, ('bbb',))
>>> e
Exception('bbb',)
>>> BaseException.args.__get__(e)
('bbb',)

Hence, the magical inspiration of args that makes your eyes look heavenward takes place in BaseException.__new__, when an object of BaseException or any of its sub-classes is created.




回答2:


It is being set in the BaseException.__new__() method, which could be seen here: source code

Note: in Python 2.7 it is being set in the BaseException.__init__() method, so the override makes .args dict always empty (not sure if pointing to the correct line): source code



来源:https://stackoverflow.com/questions/44799976/python-exception-how-does-the-args-attribute-get-automatically-set

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