diff options
author | Barry Warsaw <barry@python.org> | 2017-09-15 01:13:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-15 01:13:16 (GMT) |
commit | b2e5794870eb4728ddfaafc0f79a40299576434f (patch) | |
tree | b625687bc81fd33c04fd83820e1276db92d9fa1a /Objects | |
parent | d384a81f557dab0b142bfcc9850bc68df46496ef (diff) | |
download | cpython-b2e5794870eb4728ddfaafc0f79a40299576434f.zip cpython-b2e5794870eb4728ddfaafc0f79a40299576434f.tar.gz cpython-b2e5794870eb4728ddfaafc0f79a40299576434f.tar.bz2 |
bpo-31338 (#3374)
* 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.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 2 | ||||
-rw-r--r-- | Objects/bytesobject.c | 2 | ||||
-rw-r--r-- | Objects/dictobject.c | 18 | ||||
-rw-r--r-- | Objects/longobject.c | 7 | ||||
-rw-r--r-- | Objects/stringlib/eq.h | 3 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 84 |
6 files changed, 46 insertions, 70 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 998bcb1..38484b7 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1984,7 +1984,7 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation) goto Done; default: - assert(!"unknown operation"); + Py_UNREACHABLE(); } } diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index d91cb7d..6a4eb67 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -868,7 +868,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len, switch(c) { default: - assert(0 && "'type' not in [diuoxX]"); + Py_UNREACHABLE(); case 'd': case 'i': case 'u': diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 81c7f7f..6ba2cc9 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -643,8 +643,7 @@ lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index) perturb >>= PERTURB_SHIFT; i = mask & (i*5 + perturb + 1); } - assert(0); /* NOT REACHED */ - return DKIX_ERROR; + Py_UNREACHABLE(); } /* @@ -723,8 +722,7 @@ top: perturb >>= PERTURB_SHIFT; i = (i*5 + perturb + 1) & mask; } - assert(0); /* NOT REACHED */ - return 0; + Py_UNREACHABLE(); } /* Specialized version for string-only keys */ @@ -766,9 +764,7 @@ lookdict_unicode(PyDictObject *mp, PyObject *key, perturb >>= PERTURB_SHIFT; i = mask & (i*5 + perturb + 1); } - - assert(0); /* NOT REACHED */ - return 0; + Py_UNREACHABLE(); } /* Faster version of lookdict_unicode when it is known that no <dummy> keys @@ -810,8 +806,7 @@ lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key, perturb >>= PERTURB_SHIFT; i = mask & (i*5 + perturb + 1); } - assert(0); /* NOT REACHED */ - return 0; + Py_UNREACHABLE(); } /* Version of lookdict for split tables. @@ -856,8 +851,7 @@ lookdict_split(PyDictObject *mp, PyObject *key, perturb >>= PERTURB_SHIFT; i = mask & (i*5 + perturb + 1); } - assert(0); /* NOT REACHED */ - return 0; + Py_UNREACHABLE(); } int @@ -3603,7 +3597,7 @@ dictiter_reduce(dictiterobject *di) else if (Py_TYPE(di) == &PyDictIterValue_Type) element = dictiter_iternextvalue(&tmp); else - assert(0); + Py_UNREACHABLE(); if (element) { if (PyList_Append(list, element)) { Py_DECREF(element); diff --git a/Objects/longobject.c b/Objects/longobject.c index 4862b76..3b07585 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -1806,8 +1806,7 @@ long_format_binary(PyObject *aa, int base, int alternate, bits = 1; break; default: - assert(0); /* shouldn't ever get here */ - bits = 0; /* to silence gcc warning */ + Py_UNREACHABLE(); } /* Compute exact length 'sz' of output string. */ @@ -2169,8 +2168,8 @@ PyLong_FromString(const char *str, char **pend, int base) } } if (str[0] == '_') { - /* May not start with underscores. */ - goto onError; + /* May not start with underscores. */ + goto onError; } start = str; diff --git a/Objects/stringlib/eq.h b/Objects/stringlib/eq.h index f8fd384..ff22f91 100644 --- a/Objects/stringlib/eq.h +++ b/Objects/stringlib/eq.h @@ -10,8 +10,7 @@ unicode_eq(PyObject *aa, PyObject *bb) PyUnicodeObject *b = (PyUnicodeObject *)bb; if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) { - assert(0 && "unicode_eq ready fail"); - return 0; + Py_UNREACHABLE(); } if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b)) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index c4d93fc..bb1c083 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -768,7 +768,7 @@ make_bloom_mask(int kind, void* ptr, Py_ssize_t len) BLOOM_UPDATE(Py_UCS4, mask, ptr, len); break; default: - assert(0); + Py_UNREACHABLE(); } return mask; @@ -869,8 +869,7 @@ findchar(const void *s, int kind, else return ucs4lib_rfind_char((Py_UCS4 *) s, size, ch); default: - assert(0); - return -1; + Py_UNREACHABLE(); } } @@ -1520,8 +1519,7 @@ _copy_characters(PyObject *to, Py_ssize_t to_start, ); } else { - assert(0); - return -1; + Py_UNREACHABLE(); } } else { @@ -2079,7 +2077,7 @@ PyUnicode_FromWideChar(const wchar_t *u, Py_ssize_t size) #endif break; default: - assert(0 && "Impossible state"); + Py_UNREACHABLE(); } return unicode_result(unicode); @@ -2172,8 +2170,7 @@ kind_maxchar_limit(unsigned int kind) case PyUnicode_4BYTE_KIND: return 0x10000; default: - assert(0 && "invalid kind"); - return MAX_UNICODE; + Py_UNREACHABLE(); } } @@ -2317,8 +2314,7 @@ _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) case PyUnicode_4BYTE_KIND: return ucs4lib_find_max_char(startptr, endptr); default: - assert(0); - return 0; + Py_UNREACHABLE(); } } @@ -4068,7 +4064,7 @@ PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) *w = *four_bytes; if (w > wchar_end) { - assert(0 && "Miscalculated string end"); + Py_UNREACHABLE(); } } *w = 0; @@ -4120,7 +4116,7 @@ PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) #endif } else { - assert(0 && "This should never happen."); + Py_UNREACHABLE(); } } } @@ -5134,7 +5130,7 @@ _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) #endif if (ch > 0xFF) { #if SIZEOF_WCHAR_T == 4 - assert(0); + Py_UNREACHABLE(); #else assert(ch > 0xFFFF && ch <= MAX_UNICODE); /* compute and append the two surrogates: */ @@ -5187,7 +5183,7 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) switch (kind) { default: - assert(0); + Py_UNREACHABLE(); case PyUnicode_1BYTE_KIND: /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ assert(!PyUnicode_IS_ASCII(unicode)); @@ -9294,7 +9290,7 @@ any_find_slice(PyObject* s1, PyObject* s2, result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); break; default: - assert(0); result = -2; + Py_UNREACHABLE(); } } else { @@ -9312,7 +9308,7 @@ any_find_slice(PyObject* s1, PyObject* s2, result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); break; default: - assert(0); result = -2; + Py_UNREACHABLE(); } } @@ -9386,8 +9382,7 @@ _PyUnicode_InsertThousandsGrouping( (Py_UCS4 *) thousands_sep_data, thousands_sep_len); break; default: - assert(0); - return -1; + Py_UNREACHABLE(); } if (unicode != NULL && thousands_sep_kind != kind) { if (thousands_sep_kind < kind) @@ -9465,7 +9460,7 @@ PyUnicode_Count(PyObject *str, ); break; default: - assert(0); result = 0; + Py_UNREACHABLE(); } if (kind2 != kind1) @@ -9881,8 +9876,7 @@ case_operation(PyObject *self, memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); break; default: - assert(0); - break; + Py_UNREACHABLE(); } leave: PyMem_FREE(tmp); @@ -10105,7 +10099,7 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject **items, Py_ssize_t seqlen) for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ break; \ } \ - default: assert(0); \ + default: Py_UNREACHABLE(); \ } \ } while (0) @@ -10229,8 +10223,7 @@ PyUnicode_Splitlines(PyObject *string, int keepends) PyUnicode_GET_LENGTH(string), keepends); break; default: - assert(0); - list = 0; + Py_UNREACHABLE(); } return list; } @@ -10275,8 +10268,7 @@ split(PyObject *self, PyUnicode_GET_LENGTH(self), maxcount ); default: - assert(0); - return NULL; + Py_UNREACHABLE(); } if (PyUnicode_READY(substring) == -1) @@ -10367,8 +10359,7 @@ rsplit(PyObject *self, PyUnicode_GET_LENGTH(self), maxcount ); default: - assert(0); - return NULL; + Py_UNREACHABLE(); } if (PyUnicode_READY(substring) == -1) @@ -10434,8 +10425,7 @@ anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, case PyUnicode_4BYTE_KIND: return ucs4lib_find(buf1, len1, buf2, len2, offset); } - assert(0); - return -1; + Py_UNREACHABLE(); } static Py_ssize_t @@ -10453,8 +10443,7 @@ anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, case PyUnicode_4BYTE_KIND: return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); } - assert(0); - return 0; + Py_UNREACHABLE(); } static void @@ -10909,7 +10898,7 @@ unicode_compare(PyObject *str1, PyObject *str2) COMPARE(Py_UCS1, Py_UCS4); break; default: - assert(0); + Py_UNREACHABLE(); } break; } @@ -10928,7 +10917,7 @@ unicode_compare(PyObject *str1, PyObject *str2) COMPARE(Py_UCS2, Py_UCS4); break; default: - assert(0); + Py_UNREACHABLE(); } break; } @@ -10956,12 +10945,12 @@ unicode_compare(PyObject *str1, PyObject *str2) break; } default: - assert(0); + Py_UNREACHABLE(); } break; } default: - assert(0); + Py_UNREACHABLE(); } if (len1 == len2) @@ -11285,8 +11274,7 @@ PyUnicode_Contains(PyObject *str, PyObject *substr) result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; break; default: - result = -1; - assert(0); + Py_UNREACHABLE(); } if (kind2 != kind1) @@ -11511,7 +11499,7 @@ unicode_count(PyObject *self, PyObject *args) ); break; default: - assert(0); iresult = 0; + Py_UNREACHABLE(); } result = PyLong_FromSsize_t(iresult); @@ -12985,8 +12973,7 @@ PyUnicode_Partition(PyObject *str_obj, PyObject *sep_obj) out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); break; default: - assert(0); - out = 0; + Py_UNREACHABLE(); } if (kind2 != kind1) @@ -13043,8 +13030,7 @@ PyUnicode_RPartition(PyObject *str_obj, PyObject *sep_obj) out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); break; default: - assert(0); - out = 0; + Py_UNREACHABLE(); } if (kind2 != kind1) @@ -13627,8 +13613,7 @@ _PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer, case PyUnicode_2BYTE_KIND: maxchar = 0xffff; break; case PyUnicode_4BYTE_KIND: maxchar = 0x10ffff; break; default: - assert(0 && "invalid kind"); - return -1; + Py_UNREACHABLE(); } return _PyUnicodeWriter_PrepareInternal(writer, 0, maxchar); @@ -13770,7 +13755,7 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, break; } default: - assert(0); + Py_UNREACHABLE(); } writer->pos += len; @@ -14204,7 +14189,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) switch (type) { default: - assert(!"'type' not in [diuoxX]"); + Py_UNREACHABLE(); case 'd': case 'i': case 'u': @@ -14362,7 +14347,7 @@ mainformatlong(PyObject *v, switch(type) { default: - assert(0 && "'type' not in [diuoxX]"); + Py_UNREACHABLE(); case 'd': case 'i': case 'u': @@ -15373,8 +15358,7 @@ _Py_ReleaseInternedUnicodeStrings(void) for (i = 0; i < n; i++) { s = PyList_GET_ITEM(keys, i); if (PyUnicode_READY(s) == -1) { - assert(0 && "could not ready string"); - fprintf(stderr, "could not ready string\n"); + Py_UNREACHABLE(); } switch (PyUnicode_CHECK_INTERNED(s)) { case SSTATE_NOT_INTERNED: |