Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

args attribute of an exception subclass's type changes from string to tuple

Tags:

python

This happens on both python2.6 and python3:

class Error(Exception):
    def __init__(self, args):
            print(type(args))
            print(type(self.args)) # From BaseException
            self.args = args
            print(type(self.args))

Error("foo")

This results in:

<type 'str'>
<type 'tuple'>
<type 'tuple'>
Error('f', 'o', 'o')

For some reason, the args attribute is coerced into a tuple. Might the fact that it's defined in C have something to do with it? https://github.com/python/cpython/blob/master/Objects/exceptions.c

The name of the args argument is unrelated. Changing it to 'a' results in the same behavior, as long as it is assigned to self.args.

like image 986
AI0867 Avatar asked Oct 06 '22 22:10

AI0867


1 Answers

Looking at the code you link to, there is a setter defined for the "args" attribute. Look for BaseException_set_args - it is being set (elsewhere in the linked code) as the setter for args. So when you write self.args = args, you're really calling the function BaseException_set_args, with args as the argument.

If you then look in BaseException_set_args, it is coercing the argument into a tuple. If you give try to set self.args to something that can't be converted to a tuple (e.g. try Error(23)), you'll get a TypeError.

like image 116
happydave Avatar answered Oct 10 '22 03:10

happydave