Verifying assertion failures 
Author Message
 Verifying assertion failures

Sez Gustavo Niemeyer:

Quote:
> Hello!!

> I was just wondering, is there anyway of knowing what assertion
> has failed?

> I mean, it'd be nice to have something like:

> >>> try:
> ...   assert 0 == 1
> ... except AssertionError, e:
> ...   print e
> ...
> 0 == 1
>>> try:

...   assert 0 == 1, "0 == 1"
... except AssertionError, e:
...   print e
...
0 == 1

http://www.*-*-*.com/

Peace,
  Kalle
--

Web: http://www.*-*-*.com/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
 [ Not signed due to lossage.  Blame Microsoft Outlook Express. ]



Wed, 05 Nov 2003 23:43:32 GMT  
 Verifying assertion failures
Hello!!

I was just wondering, is there anyway of knowing what assertion
has failed?

I mean, it'd be nice to have something like:

Quote:
>>> try:

...   assert 0 == 1
... except AssertionError, e:
...   print e
...
0 == 1

Yes, I know it's possible to achieve the same results with something
like:

Quote:
>>> def myassert(test):

...   try:
...     assert eval(test)
...   except AssertionError:
...     print test
...
Quote:
>>> myassert("0 == 1")

0 == 1

But wouldn't the first be a nice addition?

--
Gustavo Niemeyer

[ 2AAC 7928 0FBF 0299 5EB5  60E2 2253 B29A 6664 3A0C ]



Wed, 05 Nov 2003 23:01:55 GMT  
 Verifying assertion failures

Quote:
> I mean, it'd be nice to have something like:

> >>> try:
> ...   assert 0 == 1
> ... except AssertionError, e:
> ...   print e
> ...
> 0 == 1

Why do you want to do that?  Usually, you use assertions for things you
believe are necessarily true.  For other situations, you usually throw
some more pertinent exception.

Alex.



Thu, 06 Nov 2003 00:07:18 GMT  
 Verifying assertion failures

Quote:
>> >>> try:
>> ...   assert 0 == 1
>> ... except AssertionError, e:
>> ...   print e
>> ...
>> 0 == 1

>Why do you want to do that?  Usually, you use assertions for things you
>believe are necessarily true.

but-that-still-leaves-being-able-to-catch-the-assertion-when-it-fails ly-yrs C


Thu, 06 Nov 2003 00:27:16 GMT  
 Verifying assertion failures

Quote:
> http://www.python.org/doc/current/ref/assert.html

Thank you Kalle. But what I meant was to make the
second exception parameter default to the expression itself.

--
Gustavo Niemeyer

[ 2AAC 7928 0FBF 0299 5EB5  60E2 2253 B29A 6664 3A0C ]



Thu, 06 Nov 2003 00:37:28 GMT  
 Verifying assertion failures

Quote:
> but-that-still-leaves-being-able-to-catch-the-assertion-when-it-fails
> ly-yrs C

Can you give an example where you'd want to do that, rather than throw
an exception more pertinent to the failure?  I thought the point of
assertions was to halt the program when one of the assumptions your code
is relying on proves to be false.

Alex.



Thu, 06 Nov 2003 01:14:07 GMT  
 Verifying assertion failures

Quote:
>Can you give an example where you'd want to do that, rather than throw
>an exception more pertinent to the failure?  I thought the point of
>assertions was to halt the program when one of the assumptions your code
>is relying on proves to be false.

Assertions don't have points; the programmers who use them do.
Which is to say, whether or not catching some kind of assertion failure
is appropriate is up to the programmer in question. As to your question
about throwing a specific exception, that seems the right answer to me.

This is, of course, all very independent of the question as to whether
or not assert/catch ought to be a form in Python. Personally, I don't think
so.

C//



Thu, 06 Nov 2003 02:08:50 GMT  
 Verifying assertion failures

Quote:

>I was just wondering, is there anyway of knowing what assertion
>has failed?

>I mean, it'd be nice to have something like:

>>>> try:
>...   assert 0 == 1
>... except AssertionError, e:
>...   print e
>...
>0 == 1

No there isn't, unless you specify it.  Why would it
be nice (give an example from real code)?

Suppose it is implemented.  What is the content of that
exception when done in the context of

  a = 0
  b = 1
  assert a == b == (a == math.sqrt(a))
?

I can think of two possibilities

The first is to use the string
 "a == b == (a == math.sqrt(a))"

that is, to use the exact text representation of the
exception.  Since assertions should not occur in working
code, this would be used for debugging.  Since you have
the source code when debugging, this information comes
up in the stack trace, or you can use the same methods
to write a function to get that code.

The second is to somehow insert actual values for the
different part of the assertion, as in using the text
  0 == 1 == (0 == math.sqrt(0))

But that isn't possible because:
  - math.sqrt could be turned into <built-in function sqrt>
  - perhaps you want to see the results of math.sqrt
     0 == 1 == (0 == 0.0)
  - suppose 'a' was really an instance which overrides __cmp__
    in which case you may want to see the results of the ___cmp__
     0 == 1 == 1
  - to be done correctly, the expression cannot be reevaluated
     to get the text.  For example,
         assert f(1) != 1
     should not require calling f twice.  A good implementation
     only needs to do it once (by saving the intermediate results
     of the expression) so there is no expectation for a double
     call.
The only general solution I know of is to show the whole
expression tree as output and the values for each node.  This
is complicated code to implement and hard to understand.

On the other hand, with the current assert format I can do

  assert a == b == (a == math.sqrt(a)), \
     "%s == %s == (%s == math.sqrt(%s))" % (a, b, a, a)

or
  assert a == b == (a == math.sqrt(a)), \
     "%s == %s == %s" % (a, b, a == math.sqrt(a))

and get exactly the output I want and know exactly how each
term will be used and when.

                    Andrew



Thu, 06 Nov 2003 03:16:42 GMT  
 Verifying assertion failures
Hi Andrew!

Quote:
> No there isn't, unless you specify it.  Why would it
> be nice (give an example from real code)?

I'm working on a test framework. I'd like to use assertions
as one of the ways to know that something has failed in a
test. It'd be nice to put in the logs *what* exactly went
wrong, so that the guy checking the problems may solve the
case as fast as possible. I don't think this is abusive
use of assertions, since that's exactly what they exist
for... checking that something that *should* be in some
way is really that way, and failing otherwise. Logging the
assertion that has failed is useful for me and probably
will be for others as well.

Quote:
> Suppose it is implemented.  What is the content of that
> exception when done in the context of

>   a = 0
>   b = 1
>   assert a == b == (a == math.sqrt(a))
> ?

> I can think of two possibilities

> The first is to use the string
>  "a == b == (a == math.sqrt(a))"
> [...]

> that is, to use the exact text representation of the
> exception.  Since assertions should not occur in working
> code, this would be used for debugging.  Since you have
> the source code when debugging, this information comes
> up in the stack trace, or you can use the same methods
> to write a function to get that code.

Yes that's the implementation I was thinking of. Notice that,
specifically in my case, the guy doing the debuggings will
also look for the bug in the environment the framework is
testing.

Quote:
> The second is to somehow insert actual values for the
> different part of the assertion, as in using the text
>   0 == 1 == (0 == math.sqrt(0))

[...]

Hummm.. no, that's not what I had in mind. This wouldn't
help much. Think about something like:

assert a == b
assert c == d

Then... Oops! "assert 0 == 1" has failed!! :-)

And, as you mentioned, there's other reasons of why to not
do this.

Thanks for your thoughts Andrew!

--
Gustavo Niemeyer

[ 2AAC 7928 0FBF 0299 5EB5  60E2 2253 B29A 6664 3A0C ]



Thu, 06 Nov 2003 11:38:52 GMT  
 Verifying assertion failures
Me:

Quote:
>> ... content of that exception when done in the context of
>>   a = 0
>>   b = 1
>>   assert a == b == (a == math.sqrt(a))
 ...
>> The first is to use the string
>>  "a == b == (a == math.sqrt(a))"

Gustavo Niemeyer wrote

Quote:
>Yes that's the implementation I was thinking of. Notice that,
>specifically in my case, the guy doing the debuggings will
>also look for the bug in the environment the framework is
>testing.

In that case I've found it useful to know the actual values
of the interesting parts of the assertion, as in

 assert a == b == (a == math.sqrt(a)), "%s %s %s" % (a, b, math.sqrt(a))

because this helps in debugging much more than seeing the
string.  I really want to know which part of the expression
failed, and when I write the test I usually have a good
enough idea of what might fail to put that data in the assertion
text string.

                    Andrew



Thu, 06 Nov 2003 21:01:12 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Runtime Failure: Failed Assertion.

2. Runtime Failure: Failed Assertion?

3. help assertion failure problem

4. VCS Assertion failure (and the code that causes it)

5. Failed Runtime Assertion: GNULLI failure---pthread_mutex_destroy

6. Tcl_SplitList & MSVC++ 4.0 Debug assertion failure

7. Free-ing result from Tcl_SplitList causes assertion failure on VC++ 4.2/Window NT 3.51

8. Is IT failure a symptom of general failure?

9. Verify CICS or MVS environment @ runtime

10. fun project/ help me verify my graphics project!

11. Possiple Bug in GNU awk, please verify this

12. /etc/passwd file verify parse script, PLEASE!

 

 
Powered by phpBB® Forum Software