[Python-checkins] r82314 - in python/branches/py3k: Doc/library/warnings.rst Lib/warnings.py Python/_warnings.c

benjamin.peterson python-checkins at python.org
Mon Jun 28 02:01:59 CEST 2010


Author: benjamin.peterson
Date: Mon Jun 28 02:01:59 2010
New Revision: 82314

Log:
Merged revisions 77402,77505,77510 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r77402 | brett.cannon | 2010-01-09 20:56:19 -0600 (Sat, 09 Jan 2010) | 12 lines
  
  DeprecationWarning is now silent by default.
  
  This was originally suggested by Guido, discussed on the stdlib-sig mailing
  list, and given the OK by Guido directly to me. What this change essentially
  means is that Python has taken a policy of silencing warnings that are only
  of interest to developers by default. This should prevent users from seeing
  warnings which are triggered by an application being run against a new
  interpreter before the app developer has a chance to update their code.
  
  Closes issue #7319. Thanks to Antoine Pitrou, Ezio Melotti, and Brian Curtin
  for helping with the issue.
........
  r77505 | brett.cannon | 2010-01-14 14:00:28 -0600 (Thu, 14 Jan 2010) | 7 lines
  
  The silencing of DeprecationWarning was not taking -3 into consideration. Since
  Py3K warnings are DeprecationWarning by default this was causing -3 to
  essentially be a no-op. Now DeprecationWarning is only silenced if -3 is not
  used.
  
  Closes issue #7700. Thanks Ezio Melotti and Florent Xicluna for patch help.
........
  r77510 | brett.cannon | 2010-01-14 19:31:45 -0600 (Thu, 14 Jan 2010) | 1 line
  
  Remove C++/C99-style comments.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Doc/library/warnings.rst
   python/branches/py3k/Lib/warnings.py
   python/branches/py3k/Python/_warnings.c

Modified: python/branches/py3k/Doc/library/warnings.rst
==============================================================================
--- python/branches/py3k/Doc/library/warnings.rst	(original)
+++ python/branches/py3k/Doc/library/warnings.rst	Mon Jun 28 02:01:59 2010
@@ -57,7 +57,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.                           |
@@ -91,6 +91,9 @@
 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:
 
@@ -150,14 +153,6 @@
 :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:
 
@@ -233,6 +228,37 @@
 entries from the warnings list before each new operation).
 
 
+Updating Code For New Versions of Python
+----------------------------------------
+
+Warnings that are only of interest to the developer are ignored by default. As
+such you should make sure to test your code with typically ignored warnings
+made visible. You can do this from the command-line 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 for encountered warnings 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.
+
+Having certain warnings ignored by default is done to prevent a user from
+seeing warnings that are only of interest to the developer. As you do 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.  The new interpreter release could trigger new warnings in your
+code that were not there in an older interpreter, e.g.
+:exc:`DeprecationWarning` for a module that you are using. While you as a
+developer want to be notified that your code is using a deprecated module, to a
+user this information is essentially noise and provides no benefit to them.
+
+
 .. _warning-functions:
 
 Available Functions

Modified: python/branches/py3k/Lib/warnings.py
==============================================================================
--- python/branches/py3k/Lib/warnings.py	(original)
+++ python/branches/py3k/Lib/warnings.py	Mon Jun 28 02:01:59 2010
@@ -371,8 +371,10 @@
 # Module initialization
 _processoptions(sys.warnoptions)
 if not _warnings_defaults:
-    simplefilter("ignore", category=PendingDeprecationWarning, append=1)
-    simplefilter("ignore", category=ImportWarning, append=1)
+    silence = [ImportWarning, PendingDeprecationWarning]
+    silence.append(DeprecationWarning)
+    for cls in silence:
+        simplefilter("ignore", category=cls)
     bytes_warning = sys.flags.bytes_warning
     if bytes_warning > 1:
         bytes_action = "error"

Modified: python/branches/py3k/Python/_warnings.c
==============================================================================
--- python/branches/py3k/Python/_warnings.c	(original)
+++ python/branches/py3k/Python/_warnings.c	Mon Jun 28 02:01:59 2010
@@ -251,7 +251,7 @@
 
     name = PyObject_GetAttrString(category, "__name__");
     if (name == NULL)  /* XXX Can an object lack a '__name__' attribute? */
-            return;
+        return;
 
     f_stderr = PySys_GetObject("stderr");
     if (f_stderr == NULL) {
@@ -846,28 +846,35 @@
 static PyObject *
 init_filters(void)
 {
-    PyObject *filters = PyList_New(3);
+    /* Don't silence DeprecationWarning if -3 was used. */
+    PyObject *filters = PyList_New(4);
+    unsigned int pos = 0;  /* Post-incremented in each use. */
+    unsigned int x;
     const char *bytes_action;
+
     if (filters == NULL)
         return NULL;
 
-    PyList_SET_ITEM(filters, 0,
+    PyList_SET_ITEM(filters, pos++,
+                    create_filter(PyExc_DeprecationWarning, "ignore"));
+    PyList_SET_ITEM(filters, pos++,
                     create_filter(PyExc_PendingDeprecationWarning, "ignore"));
-    PyList_SET_ITEM(filters, 1, create_filter(PyExc_ImportWarning, "ignore"));
+    PyList_SET_ITEM(filters, pos++,
+                    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, pos++, 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) {
-        Py_DECREF(filters);
-        return NULL;
+    for (x = 0; x < pos; x += 1) {
+        if (PyList_GET_ITEM(filters, x) == NULL) {
+            Py_DECREF(filters);
+            return NULL;
+        }
     }
 
     return filters;


More information about the Python-checkins mailing list