Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Attributes
warnings.catch_warnings Class Reference
Inheritance diagram for warnings.catch_warnings:
Inheritance graph
[legend]
Collaboration diagram for warnings.catch_warnings:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __repr__
def __enter__
def __exit__

Public Attributes

_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Private Attributes

 _record
 _module
 _entered
 _filters
 _showwarning

Detailed Description

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.

Definition at line 289 of file warnings.py.


Constructor & Destructor Documentation

def warnings.catch_warnings.__init__ (   self,
  record = False,
  module = None 
)
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.

Definition at line 306 of file warnings.py.

00306 
00307     def __init__(self, *, record=False, module=None):
00308         """Specify whether to record warnings and if an alternative module
00309         should be used other than sys.modules['warnings'].
00310 
00311         For compatibility with Python 3.0, please consider all arguments to be
00312         keyword-only.
00313 
00314         """
00315         self._record = record
00316         self._module = sys.modules['warnings'] if module is None else module
00317         self._entered = False

Here is the caller graph for this function:


Member Function Documentation

Definition at line 327 of file warnings.py.

00327 
00328     def __enter__(self):
00329         if self._entered:
00330             raise RuntimeError("Cannot enter %r twice" % self)
00331         self._entered = True
00332         self._filters = self._module.filters
00333         self._module.filters = self._filters[:]
00334         self._showwarning = self._module.showwarning
00335         if self._record:
00336             log = []
00337             def showwarning(*args, **kwargs):
00338                 log.append(WarningMessage(*args, **kwargs))
00339             self._module.showwarning = showwarning
00340             return log
00341         else:
00342             return None

def warnings.catch_warnings.__exit__ (   self,
  exc_info 
)

Definition at line 343 of file warnings.py.

00343 
00344     def __exit__(self, *exc_info):
00345         if not self._entered:
00346             raise RuntimeError("Cannot exit %r without entering first" % self)
00347         self._module.filters = self._filters
00348         self._module.showwarning = self._showwarning
00349 
00350 
00351 # filters contains a sequence of filter 5-tuples
00352 # The components of the 5-tuple are:
00353 # - an action: error, ignore, always, default, module, or once
00354 # - a compiled regex that must match the warning message
00355 # - a class representing the warning category
00356 # - a compiled regex that must match the module that is being warned
00357 # - a line number for the line being warning, or 0 to mean any line
# If either if the compiled regexs are None, match anything.

Definition at line 318 of file warnings.py.

00318 
00319     def __repr__(self):
00320         args = []
00321         if self._record:
00322             args.append("record=True")
00323         if self._module is not sys.modules['warnings']:
00324             args.append("module=%r" % self._module)
00325         name = type(self).__name__
00326         return "%s(%s)" % (name, ", ".join(args))


Member Data Documentation

Definition at line 316 of file warnings.py.

Definition at line 331 of file warnings.py.

Definition at line 315 of file warnings.py.

Definition at line 314 of file warnings.py.

Definition at line 333 of file warnings.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.


The documentation for this class was generated from the following file: