[Numpy-discussion] ANN: NumPy 1.2.0
Francesc Alted
faltet@pytables....
Tue Sep 30 04:27:29 CDT 2008
A Monday 29 September 2008, Anne Archibald escrigué:
> 2008/9/29 Francesc Alted <faltet@pytables.org>:
> > But I agree with Robert Kern that tests *should* never be run under
> > -OO because they are *tests*, not production code, and using the
> > -OO for running the test units is simply a wrong practice.
>
> I have to disagree with this. What if some piece of code misbehaves
> when optimization is turned on? This frequently happens with C code,
> and I would not be too surprised if it occurred with python code. The
> easiest way, which I think is what Chuck was referring to as "abuse
> of asserts" is for someone to write something like
>
> try:
> while True:
> do_something()
> assert np.all(a<17)
> except AssertionError:
> pass
>
> This is of course a bug, though it may work, more or less, in
> non-optimized code. But if you can't run your test suite in optimized
> mode, it's going to be useless finding this bug.
It is a bug, and it reveals a poor knowledge of the assert statement
indeed. But the sad truth is that this sort of things *can* be done.
> Perhaps more plausibly, someone might use assert to check the
> validity of user input. Then you turn on optimization and use input
> becomes unchecked...
Using assert for validating user input is a programming error. The
official library reference [1]_ is clear about the usage of the assert
statement:
"""
Assert statements are a convenient way to insert *debugging* assertions
into a program.
"""
(emphasis is mine)
.. [1] http://docs.python.org/ref/assert.html
Unfortunately, Python will not warn about the problem (even pylint
neither issues a warning on that!).
> I support getting rid of assert statements in tests. How hard is it
> to create a function assert_ in numpy.Testing?
>
> assert np.all(a<17), "oops"
>
> becomes
>
> assert_(np.all(a<17), "oops")
>
> It could even be done automatically, or nearly.
This is already provided by the unitest module (and most surely by nose
too), through its self.assert_() function:
self.assert_(np.all(a<17), "oops")
so, changing this is probably quite easy to be done (although it might
admittedly require quite a few keystrokes to be issued).
Cheers,
--
Francesc Alted
More information about the Numpy-discussion
mailing list