◐ Shell
clean mode source ↗

bpo-38205: Convert Py_UNREACHABLE() macro to a function by vstinner · Pull Request #16280 · python/cpython

You can test this change by (1) applying this change:

diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index a40eb421f5..a85c3fbaae 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1552,6 +1552,7 @@ builtin_len(PyObject *module, PyObject *obj)
         assert(PyErr_Occurred());
         return NULL;
     }
+    if (res == 123456789) { Py_UNREACHABLE(); }
     return PyLong_FromSsize_t(res);
 }
 

(2) run this script:

class HaveLength:
    def __len__(self):
        return 123456789

def func(obj):
    print("len", len(obj))

def main():
    func(HaveLength())

main()

Output with this change (in debug mode):

$ ./python x.py 
Fatal Python error: We've reached an unreachable state. Anything is possible.
The limits were in our heads all along. Follow your dreams.
https://xkcd.com/2200
Python runtime state: initialized

Current thread 0x00007f81b54ec740 (most recent call first):
  File "/home/vstinner/python/master/x.py", line 6 in func
  File "/home/vstinner/python/master/x.py", line 9 in main
  File "/home/vstinner/python/master/x.py", line 11 in <module>
Aborted (core dumped)

Output without this change (in debug mode):

$ ./python x.py
ERROR:

We've reached an unreachable state. Anything is possible.
The limits were in our heads all along. Follow your dreams.

https://xkcd.com/2200
Aborted (core dumped)

This main enhancement is that you now get the Python traceback where the bug occurred.