[Numpy-discussion] numpy migration

Duncan Smith buzzard at contactbox.co.uk
Wed Apr 25 15:22:54 EDT 2007



Timothy Hochberg wrote:
> On 4/24/07, Duncan Smith <buzzard at contactbox.co.uk> wrote:
> 
>>
>>
>>
>> Timothy Hochberg wrote:
> 
> 
> 
> [SNIP]
> 
> 
>>
>> >
>> > You should be getting a warning here. Did one disappear in the cut and
>> > paste? Or are you using a nonstandard shell that eats warnings? Or an
>> old
>> > version of numpy?
>> >
>>
>> Python 2.5.1 / numpy 1.0.1 / IDLE (under Windows 2000).  No warning.
> 
> 
> 
> I don't use Idle, but I expect that it is just eating everything that goes
> to stderr. It'd be easy enough to check with something like:
> 
>>>> import sys
>>>> print>>sys.stderr, "Does this get printed?"
> 
> Does this get printed?
> 

Yes.  Maybe it just suppresses warnings by default; but I can't find
anything relevant to that.

> If this doesn't print anything, then that explains why you aren't seeing a
> warning.
> 
>> In any event if you want an error rather than a warning for zero
>> division,
>>
>> > use:
>> >
>> >>>> 1/i
>> >
>> > Warning: divide by zero encountered in long_scalars
>> > 0
>> > SyntaxError: invalid syntax
>> >
>> >>>> N.seterr(divide='raise')
>> >
>> > {'over': 'print', 'divide': 'print', 'invalid': 'print', 'under':
>> 'ignore'}
>> >
>> >>>> 1/i
>> >
>> > Traceback (most recent call last):
>> >  File "<stdin>", line 1, in <module>
>> > FloatingPointError: divide by zero encountered in long_scalars
>> >
>> > Although I'm not sure why that's a floating point error.
>> >
>>
>> True, bug or feature?, I'm really looking for a ZeroDivisionError.
> 
> 
> 
> I'm guessing that setting this to raise just blindly converts all of the
> warnings to FloatingPointErrors. I imagine if someone with some spare
> cycles
> submitted a patch to be more specific it would be accepted. I don't have
> time to dive into that right now though.
> 
> You can hack around this for the time being by using seterr(divide='call')
> and installing an appropriate handler (see code below).
> 

[snip]

Excellent, that does the trick.  It will keep me going for now.  I'll
have to come back to my classes and rethink the design when I have more
time.  Many thanks.

Duncan



More information about the NumPy-Discussion mailing list