Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue14656] Add a macro for unreachable code

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Apr 23, 2012, 2:52 PM

Post #1 of 20 (250 views)
Permalink
[issue14656] Add a macro for unreachable code

New submission from Benjamin Peterson <benjamin [at] python>:

It would be nice to have a macro Py_UNREACHABLE to keep compiler warnings away in unreachable code. This is can call __builtin_unreachable on gcc and abort elsewhere.

----------
components: Interpreter Core
keywords: easy
messages: 159088
nosy: benjamin.peterson
priority: normal
severity: normal
stage: needs patch
status: open
title: Add a macro for unreachable code
type: enhancement
versions: Python 3.3

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 23, 2012, 3:04 PM

Post #2 of 20 (238 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Changes by STINNER Victor <victor.stinner [at] gmail>:


----------
nosy: +haypo

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 23, 2012, 3:12 PM

Post #3 of 20 (239 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

What's the specific warning that you want to eliminate?

----------
nosy: +loewis

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 23, 2012, 3:19 PM

Post #4 of 20 (242 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Benjamin Peterson <benjamin [at] python> added the comment:

2012/4/23 Martin v. Löwis <report [at] bugs>:
>
> Martin v. Löwis <martin [at] v> added the comment:
>
> What's the specific warning that you want to eliminate?

"control reaches end of non-void function without return"

Basically, whenever you see "assert(0)" today.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 23, 2012, 3:45 PM

Post #5 of 20 (243 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

>> What's the specific warning that you want to eliminate?
>
> "control reaches end of non-void function without return"
>
> Basically, whenever you see "assert(0)" today.

Sorry, what I meant is: what specific line (in what specific
source file) is that warning on?

If there is an assert(0) somewhere, there should be no
subsequent code, so no such warning should be generated.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 23, 2012, 4:06 PM

Post #6 of 20 (242 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Benjamin Peterson <benjamin [at] python> added the comment:

2012/4/23 Martin v. Löwis <report [at] bugs>:
>
> Martin v. Löwis <martin [at] v> added the comment:
>
>>> What's the specific warning that you want to eliminate?
>>
>> "control reaches end of non-void function without return"
>>
>> Basically, whenever you see "assert(0)" today.
>
> Sorry, what I meant is: what specific line (in what specific
> source file) is that warning on?
>
> If there is an assert(0) somewhere, there should be no
> subsequent code, so no such warning should be generated.

Right, we currently avoid this problem by writing "assert(0)" for
example in lookdict_split in dictobject.c. What I'm saying is that
instead writing "assert(0)", we could use Py_UNREACHABLE.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 23, 2012, 4:45 PM

Post #7 of 20 (236 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

> Right, we currently avoid this problem by writing "assert(0)" for
> example in lookdict_split in dictobject.c. What I'm saying is that
> instead writing "assert(0)", we could use Py_UNREACHABLE.

I don't understand. The assert(0) is not there to silence any compiler
warnings, is it? Instead, ISTM that it is there to truly assert that
the code is not reached, which isn't actually obvious at all (IIUC, it's
not reached because there must be some empty slot eventually unless the
key is in there already).

Instead, ISTM that is actually the "return 0;" that should silence the
compiler warning about not having a return statement.

If that's all true, I fail to see what __builtin_unreachable would achieve:
we certainly have to preserve the return statement, for compilers not
supporting
such a declaration.

Wouldn't this actually have the undesirable effect of complaining about
the return statement when compiling with -Wunreachable-code?

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 24, 2012, 12:10 PM

Post #8 of 20 (227 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Benjamin Peterson <benjamin [at] python> added the comment:

Let me explain what I meant with code.

----------
keywords: +patch
Added file: http://bugs.python.org/file25346/unreachable.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 27, 2012, 12:05 AM

Post #9 of 20 (228 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Changes by Ezio Melotti <ezio.melotti [at] gmail>:


----------
nosy: +ezio.melotti

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 30, 2012, 2:11 AM

Post #10 of 20 (226 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

Please, go ahead, explain :-)

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 30, 2012, 6:08 AM

Post #11 of 20 (226 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Benjamin Peterson <benjamin [at] python> added the comment:

Did you see the sample patch I posted?

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 1, 2012, 11:56 PM

Post #12 of 20 (216 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

Sorry, I missed the patch. I still fail to see the problem that this solves: what compiler produces "control reaches end of non-void function without return" for the current code? ISTM that your patch has the potential of *introducing* such a warning on some compilers, since you are removing the return statement (and the compiler may not be smart enough to determine that Py_FatalError does not return).

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 2, 2012, 4:35 AM

Post #13 of 20 (213 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

STINNER Victor <victor.stinner [at] gmail> added the comment:

I think you misuse __builtin_unreachable(): the code *is* reachable,
it is just very unlikely.

I really prefer to return something looking valid and continue the
execution of the program, instead of calling the evil Py_FatalError()
in release mode which stops immediatly the program in an insane
manner. For example, -1 is a valid result for findchar(), so the
program execution will continue, even if the kind is invalid.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 2, 2012, 4:37 AM

Post #14 of 20 (215 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Antoine Pitrou <pitrou [at] free> added the comment:

> I really prefer to return something looking valid and continue the
> execution of the program

How would that be better? Should Python become more like PHP?

----------
nosy: +pitrou

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 2, 2012, 4:50 AM

Post #15 of 20 (214 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

STINNER Victor <victor.stinner [at] gmail> added the comment:

Oh, I read again the patch. There are two different cases:

* The code is really unreachable. Ex: the loop of lookdict() does never stop, return is used in the loop. Py_UNREACHABLE can be used in this case *to avoid a compiler warning*. __builtin_unreachable() helps if you have GCC, but if you don't have GCC, using return with a dummy value would be better than a Py_FatalError(). I don't think that calling Py_FatalError() does make the warning quiet.

* The code is reachable, but if it is reached, it's a bug. Ex: switch/case on the Unicode kinde of a string. I like the current solution: assert(0) + return a almost valid value, but Antoine and Benjamin don't like the "almost valid value" (and so prefer a fatal error?). We may issue a warning instead of a fatal error (e.g. write a message into stderr with fprintf ?) in release mode.

--

Using return, it gives something like:

+#ifdef NDEBUG
+#ifdef __GNUC__
+#define Py_UNREACHABLE(value) __builtin_unreachable()
+#else
+#define Py_UNREACHABLE(value) return (value);
+#endif
+#else
+#define Py_UNREACHABLE(value) do { assert(0); return (value); } while (0)
+#endif

It cannot be used if the function has no result value (void), but quite all Python functions have a result.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 2, 2012, 6:26 AM

Post #16 of 20 (217 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Antoine Pitrou <pitrou [at] free> added the comment:

> I like the current
> solution: assert(0) + return a almost valid value, but Antoine and
> Benjamin don't like the "almost valid value" (and so prefer a fatal
> error?). We may issue a warning instead of a fatal error (e.g. write a
> message into stderr with fprintf ?) in release mode.

If there's a bug, either an exception should be raised, or a fatal error.
We should discourage warnings on stderr (the PHP approach).

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 2, 2012, 8:33 AM

Post #17 of 20 (213 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Georg Brandl <georg [at] python> added the comment:

> If there's a bug, either an exception should be raised, or a fatal error.
> We should discourage warnings on stderr (the PHP approach).

Agreed.

That said, I'm with Martin in that don't see how the patch in this issue helps.

----------
nosy: +georg.brandl

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 2, 2012, 8:39 AM

Post #18 of 20 (217 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

> Py_UNREACHABLE can be used in this case *to avoid a compiler warning*.

What is the specific warning that you want to avoid, and what specific compiler version produces it currently on what specific code?

It's not "control reaches end of non-void function without return", is it? (since if the compiler correctly determines that the code is unreachable, it shouldn't complain that control reaches the end of the function, as it doesn't reach the end of the function).

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 2, 2012, 8:43 AM

Post #19 of 20 (217 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Benjamin Peterson <benjamin [at] python> added the comment:

It does not avoid any warning because we have the "assert(0); return X" pattern. I was just attempting to provide a standard way of marking unreachable code.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 2, 2012, 8:54 AM

Post #20 of 20 (213 views)
Permalink
[issue14656] Add a macro for unreachable code [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

> I was just attempting to provide a standard way of marking unreachable code.

I'm -1 for the proposed patch (and probably -0 on the general idea). I think the patch has the potential *introducing* new warnings, as compilers might warn that a return is lacking in these functions.

I'm -0 on the general idea, as I think the status quo is just fine.

As for Victor's second use case (run-time checking that supposedly-unreachable code is indeed not reached, in release mode), I'm -0 also: we check that in debug mode; this looks sufficient to me.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.