Skip to content

Commit

Permalink
Add the default warning filters in a python --with-pydebug build
Browse files Browse the repository at this point in the history
Starting with Python3, debug builds do not have the default warning
filters which breaks numerous doctests. Also, collect warning filters
at the top so they get installed before Sage stuff is imported that
might throw warnings.
  • Loading branch information
vbraun committed Jan 19, 2020
1 parent b8f53f7 commit 49df509
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/sage/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@
import operator
import math

############ setup warning filters before importing Sage stuff ####
import warnings

__with_pydebug = hasattr(sys, 'gettotalrefcount') # This is a Python debug build (--with-pydebug)
if __with_pydebug:
# a debug build does not install the default warning filters. Sadly, this breaks doctests so we
# have to re-add them:
warnings.filterwarnings('ignore', category=PendingDeprecationWarning)
warnings.filterwarnings('ignore', category=ImportWarning)
warnings.filterwarnings('ignore', category=ResourceWarning)
else:
warnings.filters.remove(('ignore', None, DeprecationWarning, None, 0))

# The psutil swap_memory() function tries to collect some statistics
# that may not be available and that we don't need. Hide the warnings
# that are emitted if the stats aren't available (Trac #28329). That
# function is called in two places, so let's install this filter
# before the first one is imported from sage.misc.all below.
warnings.filterwarnings('ignore', category=RuntimeWarning,
message=r"'sin' and 'sout' swap memory stats couldn't be determined")

# Ignore all deprecations from IPython etc.
warnings.filterwarnings('ignore', category=DeprecationWarning,
module='.*(IPython|ipykernel|jupyter_client|jupyter_core|nbformat|notebook|ipywidgets|storemagic)')
# Ignore collections.abc warnings, there are a lot of them but they are
# harmless.
warnings.filterwarnings('ignore', category=DeprecationWarning,
message='.*collections[.]abc.*')
# However, be sure to keep OUR deprecation warnings
warnings.filterwarnings('default', category=DeprecationWarning,
message=r'[\s\S]*See https\?://trac\.sagemath\.org/[0-9]* for details.')
################ end setup warnings ###############################


from sage.env import SAGE_ROOT, SAGE_SRC, SAGE_DOC_SRC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV


Expand All @@ -82,14 +116,6 @@

import sage.misc.lazy_import

# The psutil swap_memory() function tries to collect some statistics
# that may not be available and that we don't need. Hide the warnings
# that are emitted if the stats aren't available (Trac #28329). That
# function is called in two places, so let's install this filter
# before the first one is imported from sage.misc.all below.
import warnings
warnings.filterwarnings('ignore', category=RuntimeWarning,
message=r"'sin' and 'sout' swap memory stats couldn't be determined")
from sage.misc.all import * # takes a while
from sage.typeset.all import *
from sage.repl.all import *
Expand Down Expand Up @@ -310,21 +336,6 @@ def _write_started_file():
O.close()


try:
warnings.filters.remove(('ignore', None, DeprecationWarning, None, 0))
except ValueError:
pass # SAGE_DEBUG=yes builds do not install default warning filters, ignore
# Ignore all deprecations from IPython etc.
warnings.filterwarnings('ignore', category=DeprecationWarning,
module='.*(IPython|ipykernel|jupyter_client|jupyter_core|nbformat|notebook|ipywidgets|storemagic)')
# Ignore collections.abc warnings, there are a lot of them but they are
# harmless.
warnings.filterwarnings('ignore', category=DeprecationWarning,
message='.*collections[.]abc.*')
# However, be sure to keep OUR deprecation warnings
warnings.filterwarnings('default', category=DeprecationWarning,
message=r'[\s\S]*See https\?://trac\.sagemath\.org/[0-9]* for details.')

# Set a new random number seed as the very last thing
# (so that printing initial_seed() and using that seed
# in set_random_seed() will result in the same sequence you got at
Expand Down

0 comments on commit 49df509

Please sign in to comment.