Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Clean up errstate handling in our tests #23813

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions numpy/core/_add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4808,7 +4808,8 @@

Examples
--------
>>> np.geterrobj() # first get the defaults
>>> orig_errobj = np.geterrobj()[:] # get a copy of the errobj
>>> orig_errobj
[8192, 521, None]

>>> def err_handler(type, flag):
Expand All @@ -4818,7 +4819,7 @@
>>> old_err = np.seterr(divide='raise')
>>> old_handler = np.seterrcall(err_handler)
>>> np.geterrobj()
[8192, 521, <function err_handler at 0x91dcaac>]
[20000, 522, <function err_handler at 0x...>]

>>> old_err = np.seterr(all='ignore')
>>> np.base_repr(np.geterrobj()[1], 8)
Expand All @@ -4827,6 +4828,7 @@
... invalid='print')
>>> np.base_repr(np.geterrobj()[1], 8)
'4351'
>>> old_errobj = np.seterrobj(orig_errobj) # restore the error state

""")

Expand Down Expand Up @@ -4871,8 +4873,8 @@

Examples
--------
>>> old_errobj = np.geterrobj() # first get the defaults
>>> old_errobj
>>> orig_errobj = np.geterrobj()[:] # get a copy of the errobj
>>> orig_errobj
[8192, 521, None]

>>> def err_handler(type, flag):
Expand All @@ -4883,9 +4885,10 @@
>>> np.base_repr(12, 8) # int for divide=4 ('print') and over=1 ('warn')
'14'
>>> np.geterr()
{'over': 'warn', 'divide': 'print', 'invalid': 'ignore', 'under': 'ignore'}
{'divide': 'print', 'over': 'warn', 'under': 'ignore', 'invalid': 'ignore'}
>>> np.geterrcall() is err_handler
True
>>> old_errobj = np.seterrobj(orig_errobj) # restore the original state

""")

Expand Down
46 changes: 26 additions & 20 deletions numpy/core/_ufunc_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,11 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None):

Examples
--------
>>> old_settings = np.seterr(all='ignore') #seterr to known value
>>> np.seterr(over='raise')
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
>>> np.seterr(**old_settings) # reset to default
{'divide': 'ignore', 'over': 'raise', 'under': 'ignore', 'invalid': 'ignore'}

>>> orig_settings = np.seterr(all='ignore') # seterr to known value
>>> np.int16(32000) * np.int16(3)
30464
>>> np.seterr(over='raise')
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird that this doesn't change the value in the returned dict, but indeed that is the behavior (the change is only reflected after calling geterr.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a normal pattern for getters: you get back the old value so that you can reset it.

Honestly, we should maybe just remove this API in a follow-up and force users to use the errstate as only real api...

>>> old_settings = np.seterr(all='warn', over='raise')
>>> np.int16(32000) * np.int16(3)
Traceback (most recent call last):
Expand All @@ -104,6 +101,8 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None):
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}
>>> np.int16(32000) * np.int16(3)
30464
>>> np.seterr(**orig_settings) # restore original
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}

"""

Expand Down Expand Up @@ -155,14 +154,18 @@ def geterr():
--------
>>> np.geterr()
{'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}
>>> np.arange(3.) / np.arange(3.)
>>> np.arange(3.) / np.arange(3.) # doctest: +SHOW_WARNINGS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHOW_WARNINGS seems to be invalid and breaks the refguide checks.

array([nan, 1., 1.])
RuntimeWarning: invalid value encountered in divide

>>> oldsettings = np.seterr(all='warn', over='raise')
>>> oldsettings = np.seterr(all='warn', invalid='raise')
>>> np.geterr()
{'divide': 'warn', 'over': 'raise', 'under': 'warn', 'invalid': 'warn'}
{'divide': 'warn', 'over': 'warn', 'under': 'warn', 'invalid': 'raise'}
>>> np.arange(3.) / np.arange(3.)
array([nan, 1., 1.])
Traceback (most recent call last):
...
FloatingPointError: invalid value encountered in divide
>>> oldsettings = np.seterr(**oldsettings) # restore original

"""
maskvalue = umath.geterrobj()[1]
Expand Down Expand Up @@ -267,16 +270,16 @@ def seterrcall(func):
... print("Floating point error (%s), with flag %s" % (type, flag))
...

>>> saved_handler = np.seterrcall(err_handler)
>>> save_err = np.seterr(all='call')
>>> orig_handler = np.seterrcall(err_handler)
>>> orig_err = np.seterr(all='call')

>>> np.array([1, 2, 3]) / 0.0
Floating point error (divide by zero), with flag 1
array([inf, inf, inf])

>>> np.seterrcall(saved_handler)
>>> np.seterrcall(orig_handler)
<function err_handler at 0x...>
>>> np.seterr(**save_err)
>>> np.seterr(**orig_err)
{'divide': 'call', 'over': 'call', 'under': 'call', 'invalid': 'call'}

Log error message:
Expand All @@ -294,9 +297,9 @@ def seterrcall(func):
LOG: Warning: divide by zero encountered in divide
array([inf, inf, inf])

>>> np.seterrcall(saved_handler)
<numpy.core.numeric.Log object at 0x...>
>>> np.seterr(**save_err)
>>> np.seterrcall(orig_handler)
<numpy.Log object at 0x...>
>>> np.seterr(**orig_err)
{'divide': 'log', 'over': 'log', 'under': 'log', 'invalid': 'log'}

"""
Expand Down Expand Up @@ -341,17 +344,19 @@ def geterrcall():
--------
>>> np.geterrcall() # we did not yet set a handler, returns None

>>> oldsettings = np.seterr(all='call')
>>> orig_settings = np.seterr(all='call')
>>> def err_handler(type, flag):
... print("Floating point error (%s), with flag %s" % (type, flag))
>>> oldhandler = np.seterrcall(err_handler)
>>> old_handler = np.seterrcall(err_handler)
>>> np.array([1, 2, 3]) / 0.0
Floating point error (divide by zero), with flag 1
array([inf, inf, inf])

>>> cur_handler = np.geterrcall()
>>> cur_handler is err_handler
True
>>> old_settings = np.seterr(**orig_settings) # restore original
>>> old_handler = np.seterrcall(None) # restore original

"""
return umath.geterrobj()[2]
Expand Down Expand Up @@ -404,7 +409,7 @@ class errstate(contextlib.ContextDecorator):

>>> np.arange(3) / 0.
array([nan, inf, inf])
>>> with np.errstate(divide='warn'):
>>> with np.errstate(divide='ignore'):
... np.arange(3) / 0.
array([nan, inf, inf])

Expand All @@ -420,6 +425,7 @@ class errstate(contextlib.ContextDecorator):

>>> np.geterr()
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
>>> olderr = np.seterr(**olderr) # restore original state

"""

Expand Down
Loading