Change SystemError to NOT inherit from Exception

A SystemError is typically raised from C to indicate serious bugs in the application which shouldn't normally be caught and handled. It's used for example for NULL arguments where a Python object is expected. So in some sense, SystemError is the Python equivalent of a segmentation fault. Since these exceptions should typically not be handled in a try/except Exeption block, I suggest to make SystemError inherit directly from BaseException instead of Exception.

On 01.07.2019 12:25, Jeroen Demeyer wrote:
A SystemError is typically raised from C to indicate serious bugs in the application which shouldn't normally be caught and handled. It's used for example for NULL arguments where a Python object is expected. So in some sense, SystemError is the Python equivalent of a segmentation fault.
Since these exceptions should typically not be handled in a try/except Exeption block, I suggest to make SystemError inherit directly from BaseException instead of Exception.
https://docs.python.org/3/library/exceptions.html#SystemError:
Raised when the interpreter finds an internal error, but the situation does not look so serious to cause it to abandon all hope. <...>
You should report this to the author or maintainer of your Python interpreter. For cases where the interpreter deems it too dangerous to continue, there's Py_FatalError().
And if it's safe to continue, the exception can be handled -- and since no-one specifically expecs SystemError, will be either logged, or ignored if the author explicitly doesn't mind if the particular code fails -- whether for this reason or any other. -- Regards, Ivan

There's two issues with this idea. One, backwards-compatibility, especially since the only good way to handle this would be to modify the exception-handling code to recognize this specific case during deprecation. But two, this would be a semantic shift of what classes directly inherit from `BaseException`. If you look at https://docs.python.org/3/library/exceptions.html#exception-hierarchy you will see that the only classes that inherit directly are ones that represent control flow. Adding `SystemError` to that list would make it a unique error condition that doesn't inherit from `Exception`.

On 2019-07-02 23:29, Brett Cannon wrote:
But two, this would be a semantic shift of what classes directly inherit from `BaseException`.
It depends how you interpret that. I always interpreted classes inheriting directly from BaseException as exceptions that you almost never want to catch in an "except Exception" block.
Adding `SystemError` to that list would make it a unique error condition that doesn't inherit from `Exception`.
I would argue that the various exception classes inheriting from BaseException are already quite unique: a KeyboardInterrupt is very different from a GeneratorExit for example.

Jeroen Demeyer wrote:
On 2019-07-02 23:29, Brett Cannon wrote:
But two, this would be a semantic shift of what classes directly inherit from BaseException. It depends how you interpret that. I always interpreted classes inheriting directly from BaseException as exceptions that you almost never want to catch in an "except Exception" block.
As co-author of PEP 352 I can tell you the intent is the one I laid out. ;) (See https://www.python.org/dev/peps/pep-0352/#exception-hierarchy-changes.)
Adding SystemError to that list would make it a unique error condition that doesn't inherit from Exception. I would argue that the various exception classes inheriting from BaseException are already quite unique: a KeyboardInterrupt is very different from a GeneratorExit for example.
Depends on your view. To me they are both control flow; one is for generators, one is for whole applications, but the point of both is to bubble up a control flow shift as appropriate and not be caught by `except Exception` blocks.
participants (3)
-
Brett Cannon
-
Ivan Pozdeev
-
Jeroen Demeyer