I love assert

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Nov 30 03:18:17 EST 2014


Ethan Furman wrote:

> On 11/28/2014 05:30 PM, Steven D'Aprano wrote:
>> alister wrote:
>> 
>>> And as may wiser people than me have already highlighted Assertions can
>>> be switched off in python which means they cannot be relied upon in
>>> production code invalidating the authors suggestion that they can
>>> automate bug reports & "Extend testing into the lifetime of the product"
>> 
>> I'm afraid that you appear to have missed the point of assertions as
>> tests. Since they should never be used to *enforce* correct behaviour,
>> but only to *verify* behaviour is correct (i.e. as a form of testing), it
>> doesn't matter if you disable them. If I turn them off, all that happens
>> is that the tests won't run *for me*. The software will still be just as
>> correct, or just as buggy, regardless of the assertions.
> 
> And you have missed the point that building an intricate error-reporting,
> program restarting framework on top of 'assert' is ridiculous.

You are absolutely correct, I did miss that point. I didn't even notice
anyone suggest building an intricate error-reporting, program restarting
framework, let alone that it should be built on top of assert. Who made
that suggestion?

To be frank, I don't even know what that would mean -- how do you build such
a framework on top of assert? That's like saying "on top of print", only
even less useful. print() at least can redirect output to a file. assert
can only do to things: nothing, or raise AssertionError.


Ah, wait, I see the reference. You're talking about the quote from James O
Coplien:

    "An even more professional approach is to leave the assertions 
    in the code when you ship, and to automatically file a bug report
    on behalf of the end user and perhaps to try to re-start the 
    application every time an assertion fails."


Sounds reasonable to me (modulo privacy considerations). Many modern
software already does this: Firefox, Windows, KDE all have forms of crash
reporting that will automatically submit bug reports to the developers for
you. And Firefox at least will try (and generally succeed) to automatically
restart after a crash.

I'm sure that this crash reporting software isn't built on top of assert.
That would be absurd. But whatever it is built from, once it exists, then
it ought to be entirely agnostic whether exceptions are raised by assert or
raise. Python already supports half of this: the logging module can
intercept unexpected exceptions (including AssertionError), log them, email
them, display them in a GUI window, or any other output method you should
like. Having an application catch AssertionError and then phone home to
automatically submit diagnostic information to the developer shouldn't be
terribly hard, although the privacy issues need to be considered.

The other part, restarting the application, may be appropriate for some apps
but not others. How you would technically do this, I'm not sure. You
probably need some sort of daemon process which respawns your app if it
dies.

Which reminds me of this:

http://www.csd.uwo.ca/~magi/personal/humour/Computer_Folklore/Robin%20Hood%20And%20Friar%20Tuck.html



-- 
Steven




More information about the Python-list mailing list