catch_warnings([record, module]) | A context manager that copies and restores the warnings filter upon exiting the context. |
warn(message[, category, stacklevel, quiet, ...]) | Replacement for warnings.warn that also sends the stack trace to sys.stderr. |
showwarning(message, category, filename, lineno) | Implementation of showwarnings which redirects to logging, |
formatwarning(message, category, filename, ...) | Function to format a warning the standard way. |
filterwarnings(action[, message, category, ...]) | Insert an entry into the list of warnings filters (at the front). |
resetwarnings() | Clear the list of warning filters, so that no filters are active. |
Enhanced warning facilities.
This may be used as a replacement for the standard warnings module.
Bases: object
A context manager that copies and restores the warnings filter upon exiting the context.
The ‘record’ argument specifies whether warnings should be captured by a custom implementation of warnings.showwarning() and be appended to a list returned by the context manager. Otherwise None is returned by the context manager. The objects appended to the list are arguments whose attributes mirror the arguments to showwarning().
The ‘module’ argument is to specify an alternative module to the module named ‘warnings’ and imported under that name. This argument is only useful when testing the warnings module itself.
Specify whether to record warnings and if an alternative module should be used other than sys.modules[‘warnings’].
For compatibility with Python 3.0, please consider all arguments to be keyword-only.
Specify whether to record warnings and if an alternative module should be used other than sys.modules[‘warnings’].
For compatibility with Python 3.0, please consider all arguments to be keyword-only.
Replacement for warnings.warn that also sends the stack trace to sys.stderr. Supports additional argument stdout_message which is sent to stdout rather than stderr every time the warning is called.
Example: >>> import mmf.utils.mmf_warnings as warnings >>> def f(): ... warnings.warn(“Warning in f!”,stdout_message=”Short warning!”) >>> def g(): ... f() >>> stderr = sys.stderr >>> sys.stderr = open(‘/dev/null’,’w’) # Suppress stderr output >>> f() Short warning! >>> sys.stderr.close() >>> sys.stderr = sys.stdout # Send all output to stdout for checking. >>> g() # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS Short warning! ...: UserWarning: Warning in f!
... File “<doctest mmf.utils.mmf_warnings.warn[8]>”, line 1, in <module>
g() # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
- File “<doctest mmf.utils.mmf_warnings.warn[2]>”, line 2, in g
- f()
- File “<doctest mmf.utils.mmf_warnings.warn[1]>”, line 2, in f
- warnings.warn(“Warning in f!”,stdout_message=”Short warning!”)
>>> warnings._warn_show_stack_trace = False # You can turn off the trace
>>> __warningregistry__ = {}
>>> warnings.__warningregistry__ = {}
>>> g()
Short warning!
...: UserWarning: Warning in f!
Implementation of showwarnings which redirects to logging, which will first check to see if the file parameter is None. If a file is specified, it will delegate to the original warnings implementation of showwarning. Otherwise, it will call warnings.formatwarning and will log the resulting string to a warnings logger named “py.warnings” with level logging.WARNING.
Function to format a warning the standard way.
Insert an entry into the list of warnings filters (at the front).
‘message’ – a regex that the warning message must match ‘category’ – a class that the warning must be a subclass of ‘module’ – a regex that the module name must match ‘lineno’ – an integer line number, 0 matches all warnings ‘append’ – if true, append to the list of filters
Clear the list of warning filters, so that no filters are active.