* Add Py_UNREACHABLE() as an alias to abort().
* Use Py_UNREACHABLE() instead of assert(0)
* Convert more unreachable code to use Py_UNREACHABLE()
* Document Py_UNREACHABLE() and a few other macros.
This commit is contained in:
Barry Warsaw 2017-09-14 18:13:16 -07:00 committed by GitHub
parent d384a81f55
commit b2e5794870
22 changed files with 128 additions and 111 deletions

View file

@ -100,8 +100,7 @@ expr_context_name(expr_context_ty ctx)
case Param:
return "Param";
default:
assert(0);
return "(unknown)";
Py_UNREACHABLE();
}
}
@ -759,8 +758,7 @@ num_stmts(const node *n)
Py_FatalError(buf);
}
}
assert(0);
return 0;
Py_UNREACHABLE();
}
/* Transform the CST rooted at node * to the appropriate AST

View file

@ -274,7 +274,7 @@ PyEval_RestoreThread(PyThreadState *tstate)
if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
drop_gil(tstate);
PyThread_exit_thread();
assert(0); /* unreachable */
Py_UNREACHABLE();
}
errno = err;
}
@ -3430,7 +3430,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
/* This should never be reached. Every opcode should end with DISPATCH()
or goto error. */
assert(0);
Py_UNREACHABLE();
error:

View file

@ -1350,8 +1350,7 @@ get_const_value(expr_ty e)
case NameConstant_kind:
return e->v.NameConstant.value;
default:
assert(!is_const(e));
return NULL;
Py_UNREACHABLE();
}
}

View file

@ -351,8 +351,7 @@ calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align,
*n_lpadding = 0;
else {
/* We should never have an unspecified alignment. */
*n_lpadding = 0;
assert(0);
Py_UNREACHABLE();
}
*n_rpadding = *n_total - nchars - *n_lpadding;
@ -569,9 +568,7 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
break;
default:
/* Shouldn't get here, but treat it as '>' */
spec->n_lpadding = n_padding;
assert(0);
break;
Py_UNREACHABLE();
}
}

View file

@ -175,7 +175,7 @@ _Py_HashBytes(const void *src, Py_ssize_t len)
case 2: hash = ((hash << 5) + hash) + *p++; /* fallthrough */
case 1: hash = ((hash << 5) + hash) + *p++; break;
default:
assert(0);
Py_UNREACHABLE();
}
hash ^= len;
hash ^= (Py_uhash_t) _Py_HashSecret.djbx33a.suffix;

View file

@ -431,8 +431,8 @@ _Py_string_to_number_with_underscores(
error:
PyMem_Free(dup);
PyErr_Format(PyExc_ValueError,
"could not convert string to %s: "
"%R", what, obj);
"could not convert string to %s: "
"%R", what, obj);
return NULL;
}
@ -1061,7 +1061,7 @@ format_float_short(double d, char format_code,
something starting with a digit, an 'I', or 'N' */
strncpy(p, "ERR", 3);
/* p += 3; */
assert(0);
Py_UNREACHABLE();
}
goto exit;
}

View file

@ -630,10 +630,7 @@ _PyTime_GetSystemClock(void)
_PyTime_t t;
if (pygettimeofday(&t, NULL, 0) < 0) {
/* should not happen, _PyTime_Init() checked the clock at startup */
assert(0);
/* use a fixed value instead of a random value from the stack */
t = 0;
Py_UNREACHABLE();
}
return t;
}
@ -663,7 +660,7 @@ pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
return -1;
}
/* Hello, time traveler! */
assert(0);
Py_UNREACHABLE();
}
*tp = t * MS_TO_NS;
@ -771,10 +768,7 @@ _PyTime_GetMonotonicClock(void)
if (pymonotonic(&t, NULL, 0) < 0) {
/* should not happen, _PyTime_Init() checked that monotonic clock at
startup */
assert(0);
/* use a fixed value instead of a random value from the stack */
t = 0;
Py_UNREACHABLE();
}
return t;
}

View file

@ -39,6 +39,6 @@ write_op_arg(_Py_CODEUNIT *codestr, unsigned char opcode,
*codestr++ = PACKOPARG(opcode, oparg & 0xff);
break;
default:
assert(0);
Py_UNREACHABLE();
}
}