Index: Python/_warnings.c =================================================================== --- Python/_warnings.c (revision 77294) +++ Python/_warnings.c (working copy) @@ -839,26 +839,29 @@ static PyObject * init_filters(void) { - PyObject *filters = PyList_New(3); + PyObject *filters = PyList_New(4); const char *bytes_action; if (filters == NULL) return NULL; PyList_SET_ITEM(filters, 0, + create_filter(PyExc_DeprecationWarning, "ignore")); + PyList_SET_ITEM(filters, 1, create_filter(PyExc_PendingDeprecationWarning, "ignore")); - PyList_SET_ITEM(filters, 1, create_filter(PyExc_ImportWarning, "ignore")); + PyList_SET_ITEM(filters, 2, create_filter(PyExc_ImportWarning, "ignore")); if (Py_BytesWarningFlag > 1) bytes_action = "error"; else if (Py_BytesWarningFlag) bytes_action = "default"; else bytes_action = "ignore"; - PyList_SET_ITEM(filters, 2, create_filter(PyExc_BytesWarning, + PyList_SET_ITEM(filters, 3, create_filter(PyExc_BytesWarning, bytes_action)); if (PyList_GET_ITEM(filters, 0) == NULL || PyList_GET_ITEM(filters, 1) == NULL || - PyList_GET_ITEM(filters, 2) == NULL) { + PyList_GET_ITEM(filters, 2) == NULL || + PyList_GET_ITEM(filters, 3) == NULL) { Py_DECREF(filters); return NULL; } Index: Doc/library/warnings.rst =================================================================== --- Doc/library/warnings.rst (revision 77294) +++ Doc/library/warnings.rst (working copy) @@ -59,7 +59,7 @@ | :exc:`UserWarning` | The default category for :func:`warn`. | +----------------------------------+-----------------------------------------------+ | :exc:`DeprecationWarning` | Base category for warnings about deprecated | -| | features. | +| | features (ignored by default). | +----------------------------------+-----------------------------------------------+ | :exc:`SyntaxWarning` | Base category for warnings about dubious | | | syntactic features. | @@ -89,7 +89,10 @@ standard warning categories. A warning category must always be a subclass of the :exc:`Warning` class. +.. versionchanged:: 2.7 + :exc:`DeprecationWarning` is ignored by default. + .. _warning-filter: The Warnings Filter @@ -148,15 +151,7 @@ :mod:`warnings` module parses these when it is first imported (invalid options are ignored, after printing a message to ``sys.stderr``). -The warnings that are ignored by default may be enabled by passing :option:`-Wd` -to the interpreter. This enables default handling for all warnings, including -those that are normally ignored by default. This is particular useful for -enabling ImportWarning when debugging problems importing a developed package. -ImportWarning can also be enabled explicitly in Python code using:: - warnings.simplefilter('default', ImportWarning) - - .. _warning-suppress: Temporarily Suppressing Warnings @@ -226,6 +221,44 @@ entries from the warnings list before each new operation). +Updating Code For New Versions of Python +---------------------------------------- + +By default, the Python interpreter ignores warnings that are only of interest +to developers. This is for two reasons. One is that a developer does not +necessarily have control over what interpreter a user uses to run their code. It is +possible that a new version of Python will be released between your release +cycles that introduces new warnings that your code triggers, e.g. :exc:`DeprecationWarning` +for a module that you are using. While you as a developer care that your code +is using a deprecated module, a user does not and thus has no need to see +this warning simply because they chose to run your code with a new interpreter. + +There is also the issue that warnings in Python are triggered during every +execution. For compiled languages certain warnings only show up once when code +is compiled into a binary. But because Python is an interpreted language, code +is essentially "compiled" every time it is run, leading to warnings that a +compiled language would emit only once to be emitted every time. +Once again, these types of warnings are of no interest to a user and thus are +silent by default. + +But as these warnings are of interest to developers, you +should make sure to test your code without ignoring warnings. From the +command-line you can enable warnings that are ignored by default by passing :option:`-Wd` +to the interpreter (this is shorthand for :option:`-W default`). +This enables default handling for all warnings, including those that are ignored by default. +To change what action is taken you simply change what argument is passed to +:option:`-W`, e.g. :option:`-W error`. See the :option:`-W` flag for more +details on what is possible. + +To programmatically do the same as :option:`-Wd`, use:: + + warnings.simplefilter('default') + +Make sure to execute this code as soon as possible. This prevents the +registering of what warnings have been raised from unexpectedly influencing how +future warnings are treated. + + .. _warning-functions: Available Functions Index: Lib/warnings.py =================================================================== --- Lib/warnings.py (revision 77294) +++ Lib/warnings.py (working copy) @@ -383,8 +383,8 @@ # Module initialization _processoptions(sys.warnoptions) if not _warnings_defaults: - simplefilter("ignore", category=PendingDeprecationWarning, append=1) - simplefilter("ignore", category=ImportWarning, append=1) + for cls in (DeprecationWarning, PendingDeprecationWarning, ImportWarning): + simplefilter("ignore", category=cls, append=1) bytes_warning = sys.flags.bytes_warning if bytes_warning > 1: bytes_action = "error"