Skip to content
4 changes: 4 additions & 0 deletions Include/pymacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
"We've reached an unreachable state. Anything is possible.\n" \
"The limits were in our heads all along. Follow your dreams.\n" \
"https://xkcd.com/2200")
#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
#define Py_UNREACHABLE() __builtin_unreachable()
#elif defined(_MSC_VER)
#define Py_UNREACHABLE() __assume(0)
#else
#define Py_UNREACHABLE() \
Py_FatalError("Unreachable C code path reached")
Expand Down
2 changes: 1 addition & 1 deletion Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)

The GIL and the table lock ensures that only one thread is
allocating memory. */
Py_UNREACHABLE();
Py_FatalError("Memory allocation failed.");
}
TABLES_UNLOCK();
}
Expand Down
15 changes: 6 additions & 9 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1551,18 +1551,15 @@ PyNumber_Float(PyObject *o)
PyObject *
PyNumber_ToBase(PyObject *n, int base)
{
PyObject *res = NULL;
if (!(base == 2 || base == 8 || base == 10 || base == 16)) {
PyErr_SetString(PyExc_SystemError,
"PyNumber_ToBase: base must be 2, 8, 10 or 16");
return NULL;
}
PyObject *index = PyNumber_Index(n);

if (!index)
return NULL;
if (PyLong_Check(index))
res = _PyLong_Format(index, base);
else
/* It should not be possible to get here, as
PyNumber_Index already has a check for the same
condition */
PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not int");
PyObject *res = _PyLong_Format(index, base);
Py_DECREF(index);
return res;
}
Expand Down
9 changes: 5 additions & 4 deletions Objects/stringlib/eq.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
Py_LOCAL_INLINE(int)
unicode_eq(PyObject *aa, PyObject *bb)
{
assert(PyUnicode_Check(aa));
assert(PyUnicode_Check(bb));
assert(PyUnicode_IS_READY(aa));
assert(PyUnicode_IS_READY(bb));

PyUnicodeObject *a = (PyUnicodeObject *)aa;
PyUnicodeObject *b = (PyUnicodeObject *)bb;

if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
Py_UNREACHABLE();
}

if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
return 0;
if (PyUnicode_GET_LENGTH(a) == 0)
Expand Down
2 changes: 1 addition & 1 deletion Python/formatter_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
spec->n_lpadding = n_padding;
break;
default:
/* Shouldn't get here, but treat it as '>' */
/* Shouldn't get here */
Py_UNREACHABLE();
}
}
Expand Down
6 changes: 5 additions & 1 deletion Python/peephole.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,12 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
if (instrsize(j) > ilen) {
goto exitUnchanged;
}
assert(ilen <= INT_MAX);
/* If instrsize(j) < ilen, we'll emit EXTENDED_ARG 0 */
if (ilen > 4) {
/* Can only happen when PyCode_Optimize() is called with
malformed bytecode. */
goto exitUnchanged;
}
write_op_arg(codestr + h, opcode, j, (int)ilen);
h += ilen;
}
Expand Down
9 changes: 4 additions & 5 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ _PyTime_GetSystemClock(void)
_PyTime_t t;
if (pygettimeofday(&t, NULL, 0) < 0) {
/* should not happen, _PyTime_Init() checked the clock at startup */
Py_UNREACHABLE();
Py_FatalError("pygettimeofday() failed.");
}
return t;
}
Expand Down Expand Up @@ -775,8 +775,7 @@ pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
_PyTime_overflow();
return -1;
}
/* Hello, time traveler! */
Py_UNREACHABLE();
Py_FatalError("Hello, time traveler!");
}
*tp = t * MS_TO_NS;

Expand Down Expand Up @@ -918,7 +917,7 @@ _PyTime_GetMonotonicClock(void)
if (pymonotonic(&t, NULL, 0) < 0) {
/* should not happen, _PyTime_Init() checked that monotonic clock at
startup */
Py_UNREACHABLE();
Py_FatalError("pymonotonic() failed.");
}
return t;
}
Expand Down Expand Up @@ -1019,7 +1018,7 @@ _PyTime_GetPerfCounter(void)
{
_PyTime_t t;
if (_PyTime_GetPerfCounterWithInfo(&t, NULL)) {
Py_UNREACHABLE();
Py_FatalError("_PyTime_GetPerfCounterWithInfo() failed.");
}
return t;
}
Expand Down