You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use abort() for code we never expect to hit #75519
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/warsaw'closed_at=<Date2017-09-15.01:13:40.432>created_at=<Date2017-09-04.19:18:14.110>labels= ['interpreter-core', '3.7']
title='Use abort() for code we never expect to hit'updated_at=<Date2017-11-08.14:06:26.211>user='https://github.com/warsaw'
Over in bpo-31337 the observation was made that we often use the following pattern in situations we never expect to hit:
assert(0);
return NULL;
but this isn't strictly optimal. First, the asserts can be compiled away. Second, it's possible that our assumptions about a particular condition are incorrect. Third, the intent of non-reachability isn't as clear as it could be.
instead (although @skrah says "The only drawback is that in the case of libraries, sometimes distribution package lint tools complain." so it would be useful to understand that in more detail.
@serhiy.storchaka says "I have counted 48 occurrences of assert(0), 11 assert(0 && "message") and 2 assert(!"message"). If fix one occurrence, why not fix all others?" We should! This issue tracks that.
The fact that assert() is compiled out in release build looks as an advantage to me. This allows the compiler to generate smaller code. I'm in favor of using assert(!"message"), but this form is rarely used in CPython sources.
I think it would be nice to ask on Python-Dev first. Maybe there are other concerns.
I'm thinking there are two aspects to this. One would involve updates to PEP-7 to include a section on "Unreachable code". The other would be a PR that updates the current C code to the PEP-7 standard.
I'll work on a PEP update as a separate PR, then we can discuss further on python-dev. If the PEP changes are accepted, then we can manage the code changes as a PR against this issue.
@skrah - quick question. Is /* NOT REACHED */ a common convention? Do any compilers or IDEs recognize it? Is it documented anywhere? I like the idea of adding that comment on the abort(), but I'm trying to find some prior art or references for that.
Can we have a Py_UNREACHABLE() macro for that, then?
First, it makes the intent extra clear without needing any additional comment. Second, it can be defined however we need in order to get along with the various tools around.
Can we have a Py_UNREACHABLE() macro for that, then?
First, it makes the intent extra clear without needing any additional comment. Second, it can be defined however we need in order to get along with the various tools around.
+1
skrah commented on Sep 5, 2017
skrahmannequin
Mannequin
More actions
Is /* NOT REACHED */ a common convention?
I think it's often used in BSD, I first saw it in OpenBSD.
Please don't use abort(), but add a new Py_UNREACHABLE() macro to allow to use a different code depending on the compiler/platform and compiler option (like release vs debug build).
Can we use compiler-specific code like GCC's __builtin_unreachable()? This would help the optimizer.
That's a good example of better implementation for Py_UNREACHABLE().
The tricky part is to make compiler warnings quiet. For example, you cannot replace "abort(); return NULL;" with "abort()", because a function without return would emit a warning.
Maybe the default implementation of the macro should be:
On Tue, Sep 05, 2017 at 05:10:24PM +0000, STINNER Victor wrote:
That's a good example of better implementation for Py_UNREACHABLE().
The tricky part is to make compiler warnings quiet. For example, you cannot replace "abort(); return NULL;" with "abort()", because a function without return would emit a warning.
Which compiler needs more than "abort();" in a default statement? gcc and clang
don't.
Another use for __builtin_unreachable is following a call a function that never returns but that is not declared __attribute__((noreturn)), as in this example:
> Neither gcc (macOS, Ubuntu), nor clang (Ubuntu) complain.
Ok, cool. In that case, go ahead.
I checked with @steve.dower and I think abort() will work on MSVC too. He did have the idea to #define it to Py_FatalError(“some message”); abort(); but since the former calls the latter we could get warnings that the second abort() isn’t reachable.
I say we start with abort() as the expansion and go from there. Since it’s a macro it’s easy to redefine if you want to crank up debugging, add a breakpoint, add __LINE__ and __FILE__ or need something special for some particular compiler.
Activity
warsaw commentedon Sep 4, 2017
Over in bpo-31337 the observation was made that we often use the following pattern in situations we never expect to hit:
assert(0);
return NULL;
but this isn't strictly optimal. First, the asserts can be compiled away. Second, it's possible that our assumptions about a particular condition are incorrect. Third, the intent of non-reachability isn't as clear as it could be.
As suggested in http://bugs.python.org/issue31337#msg301229 it would be better to use
abort() /* NOT REACHED */
instead (although @skrah says "The only drawback is that in the case of libraries, sometimes distribution package lint tools complain." so it would be useful to understand that in more detail.
@serhiy.storchaka says "I have counted 48 occurrences of assert(0), 11 assert(0 && "message") and 2 assert(!"message"). If fix one occurrence, why not fix all others?" We should! This issue tracks that.
skrah commentedon Sep 4, 2017
Regarding lint warnings, I may have confused abort() with exit().
Lintian has the shlib-calls-exit tag, somehow I thought there was
a similar one for abort(), but I can't find it now.
serhiy-storchaka commentedon Sep 5, 2017
The fact that assert() is compiled out in release build looks as an advantage to me. This allows the compiler to generate smaller code. I'm in favor of using assert(!"message"), but this form is rarely used in CPython sources.
I think it would be nice to ask on Python-Dev first. Maybe there are other concerns.
warsaw commentedon Sep 5, 2017
I'm thinking there are two aspects to this. One would involve updates to PEP-7 to include a section on "Unreachable code". The other would be a PR that updates the current C code to the PEP-7 standard.
I'll work on a PEP update as a separate PR, then we can discuss further on python-dev. If the PEP changes are accepted, then we can manage the code changes as a PR against this issue.
warsaw commentedon Sep 5, 2017
@skrah - quick question. Is /* NOT REACHED */ a common convention? Do any compilers or IDEs recognize it? Is it documented anywhere? I like the idea of adding that comment on the abort(), but I'm trying to find some prior art or references for that.
pitrou commentedon Sep 5, 2017
Can we have a Py_UNREACHABLE() macro for that, then?
First, it makes the intent extra clear without needing any additional comment. Second, it can be defined however we need in order to get along with the various tools around.
warsaw commentedon Sep 5, 2017
+1
skrah commentedon Sep 5, 2017
I think it's often used in BSD, I first saw it in OpenBSD.
A macro is fine of course.
skrah commentedon Sep 5, 2017
And Solaris lint recognizes it:
https://docs.oracle.com/cd/E60778_01/pdf/E60745.pdf
Actually it could be that the convention comes right from K&R, but I
can't find my copy right now.
serhiy-storchaka commentedon Sep 5, 2017
Can we use compiler-specific code like GCC's __builtin_unreachable()? This would help the optimizer.
vstinner commentedon Sep 5, 2017
Please don't use abort(), but add a new Py_UNREACHABLE() macro to allow to use a different code depending on the compiler/platform and compiler option (like release vs debug build).
That's a good example of better implementation for Py_UNREACHABLE().
The tricky part is to make compiler warnings quiet. For example, you cannot replace "abort(); return NULL;" with "abort()", because a function without return would emit a warning.
Maybe the default implementation of the macro should be:
I don't know if it would work.
pitrou commentedon Sep 5, 2017
Le 05/09/2017 à 19:10, STINNER Victor a écrit :
Or simply you would write:
vstinner commentedon Sep 5, 2017
I recall that I had to fix warnings when using clang on:
See bpo-28152.
I don't know if it's related to this issue.
skrah commentedon Sep 5, 2017
On Tue, Sep 05, 2017 at 05:10:24PM +0000, STINNER Victor wrote:
Which compiler needs more than "abort();" in a default statement? gcc and clang
don't.
vstinner commentedon Sep 5, 2017
Again, I'm not sure that bpo-28152 is directly related to this issue, but here an example:
Parser/grammar.c: In function '_Py_findlabel':
Parser/grammar.c:142:1: warning: control reaches end of non-void function [-Wreturn-type]
warsaw commentedon Sep 5, 2017
So with this diff:
Neither gcc (macOS, Ubuntu), nor clang (Ubuntu) complain.
vstinner commentedon Sep 5, 2017
+#define Py_UNREACHABLE() abort()
Using such macro, I don't think that __builtin_unreachable() is useful.
https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
Another use for __builtin_unreachable is following a call a function that never returns but that is not declared __attribute__((noreturn)), as in this example:
I expect abort() to be annotated with __attribute__((noreturn)) on the C library used GCC.
vstinner commentedon Sep 5, 2017
Ok, cool. In that case, go ahead.
warsaw commentedon Sep 5, 2017
On Sep 5, 2017, at 16:07, STINNER Victor <report@bugs.python.org> wrote:
I checked with @steve.dower and I think abort() will work on MSVC too. He did have the idea to #define it to
Py_FatalError(“some message”); abort();but since the former calls the latter we could get warnings that the second abort() isn’t reachable.I say we start with abort() as the expansion and go from there. Since it’s a macro it’s easy to redefine if you want to crank up debugging, add a breakpoint, add __LINE__ and __FILE__ or need something special for some particular compiler.
warsaw commentedon Sep 15, 2017
New changeset b2e5794 by Barry Warsaw in branch 'master':
bpo-31338 (bpo-3374)
b2e5794
serhiy-storchaka commentedon Nov 8, 2017
New changeset 8bf288e by Serhiy Storchaka (Petr Viktorin) in branch 'master':
Docs: Mention that Py_UNREACHABLE was added in 3.7 (bpo-4337)
8bf288e
vstinner commentedon Nov 8, 2017
New changeset 54cc0c0 by Victor Stinner in branch 'master':
bpo-31338: C API intro: add missing versionadded (bpo-4339)
54cc0c0