Rick Wotnaz wrote.
it's an instance of the exception type, of course.
:::
if you do
raise SomeError, value
Python will actually do
raise SomeError(value)
Depending on what you mean by "actually" I guess ...
(I'm sure you know this and more ;-)
1 0 LOAD_NAME 0 (SomeError)
3 LOAD_NAME 1 (value)
6 RAISE_VARARGS 2
9 LOAD_CONST 0 (None)
12 RETURN_VALUE 1 0 LOAD_NAME 0 (SomeError)
3 LOAD_NAME 1 (value)
6 CALL_FUNCTION 1
9 RAISE_VARARGS 1
12 LOAD_CONST 0 (None)
15 RETURN_VALUE
I guess that comes from the grammar of the raise statement, i.e.,
raise_stmt: 'raise' [test [',' test [',' test]]]
which allows up to three arguments for raise, apparently all general
expressions, but with some specific run-time requirements for what
combinations of argument expression values are allowable.
I.e., it seems (I haven't looked in ceval.c(?)) that RAISE_VARARGS must
look at the first item in its count of args on the stack, and in the
SomeError, value case find an exception class, and decide to instantiate it
and throw the instance, but if it finds the instance ready made, as in
SomeError(value), it must skip the instantiation (and disallow further args BTW).
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Exception: arg
Those looks the same, but sensitivity to the type of the first arg is revealed by
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Exception: ('arg', 'what now?')
(that is, create a SomeError exception and pass the value as its
first argument).
you can use either form in your code (I prefer the latter myself).
Just to drive home the general expression allowability in raise,
and instance vs class as the first arg:
>>> extab = [StopIteration('stop stop ;-)'), ValueError('wrong value')]
>>> raise extab[1]
Traceback (most recent call last):
Traceback (most recent call last):
>>> extab = [StopIteration, 'stop stop ;-)', ValueError, 'wrong value']
>>> raise extab[2], extab[3]
Traceback (most recent call last):
>>> raise extab[0], extab[1]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration: stop stop ;-)
Ok, I'll stop ;-)
Regards,
Bengt Richter