Nick Coghlan thought I should forward this to python-dev so people are aware of this change and why it occurred (plus it indirectly informs Guido I finally finished the work).<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>

From: <b class="gmail_sendername">brett.cannon</b> <span dir="ltr">&lt;<a href="mailto:python-checkins@python.org">python-checkins@python.org</a>&gt;</span><br>Date: Sat, Jan 9, 2010 at 18:56<br>Subject: [Python-checkins] r77402 - in python/trunk: Doc/library/warnings.rst Lib/test/test_ascii_formatd.py Lib/test/test_exceptions.py Lib/warnings.py Misc/NEWS Python/_warnings.c<br>

To: <a href="mailto:python-checkins@python.org">python-checkins@python.org</a><br><br><br>Author: brett.cannon<br>
Date: Sun Jan 10 03:56:19 2010<br>
New Revision: 77402<br>
<br>
Log:<br>
DeprecationWarning is now silent by default.<br>
<br>
This was originally suggested by Guido, discussed on the stdlib-sig mailing<br>
list, and given the OK by Guido directly to me. What this change essentially<br>
means is that Python has taken a policy of silencing warnings that are only<br>
of interest to developers by default. This should prevent users from seeing<br>
warnings which are triggered by an application being run against a new<br>
interpreter before the app developer has a chance to update their code.<br>
<br>
Closes issue #7319. Thanks to Antoine Pitrou, Ezio Melotti, and Brian Curtin<br>
for helping with the issue.<br>
<br>
<br>
Modified:<br>
   python/trunk/Doc/library/warnings.rst<br>
   python/trunk/Lib/test/test_ascii_formatd.py<br>
   python/trunk/Lib/test/test_exceptions.py<br>
   python/trunk/Lib/warnings.py<br>
   python/trunk/Misc/NEWS<br>
   python/trunk/Python/_warnings.c<br>
<br>
Modified: python/trunk/Doc/library/warnings.rst<br>
==============================================================================<br>
--- python/trunk/Doc/library/warnings.rst       (original)<br>
+++ python/trunk/Doc/library/warnings.rst       Sun Jan 10 03:56:19 2010<br>
@@ -59,7 +59,7 @@<br>
 | :exc:`UserWarning`               | The default category for :func:`warn`.        |<br>
 +----------------------------------+-----------------------------------------------+<br>
 | :exc:`DeprecationWarning`        | Base category for warnings about deprecated   |<br>
-|                                  | features.                                     |<br>
+|                                  | features (ignored by default).                |<br>
 +----------------------------------+-----------------------------------------------+<br>
 | :exc:`SyntaxWarning`             | Base category for warnings about dubious      |<br>
 |                                  | syntactic features.                           |<br>
@@ -89,6 +89,9 @@<br>
 standard warning categories.  A warning category must always be a subclass of<br>
 the :exc:`Warning` class.<br>
<br>
+.. versionchanged:: 2.7<br>
+   :exc:`DeprecationWarning` is ignored by default.<br>
+<br>
<br>
 .. _warning-filter:<br>
<br>
@@ -148,14 +151,6 @@<br>
 :mod:`warnings` module parses these when it is first imported (invalid options<br>
 are ignored, after printing a message to ``sys.stderr``).<br>
<br>
-The warnings that are ignored by default may be enabled by passing :option:`-Wd`<br>
-to the interpreter. This enables default handling for all warnings, including<br>
-those that are normally ignored by default. This is particular useful for<br>
-enabling ImportWarning when debugging problems importing a developed package.<br>
-ImportWarning can also be enabled explicitly in Python code using::<br>
-<br>
-   warnings.simplefilter(&#39;default&#39;, ImportWarning)<br>
-<br>
<br>
 .. _warning-suppress:<br>
<br>
@@ -226,6 +221,37 @@<br>
 entries from the warnings list before each new operation).<br>
<br>
<br>
+Updating Code For New Versions of Python<br>
+----------------------------------------<br>
+<br>
+Warnings that are only of interest to the developer are ignored by default. As<br>
+such you should make sure to test your code with typically ignored warnings<br>
+made visible. You can do this from the command-line by passing :option:`-Wd`<br>
+to the interpreter (this is shorthand for :option:`-W default`).  This enables<br>
+default handling for all warnings, including those that are ignored by default.<br>
+To change what action is taken for encountered warnings you simply change what<br>
+argument is passed to :option:`-W`, e.g. :option:`-W error`. See the<br>
+:option:`-W` flag for more details on what is possible.<br>
+<br>
+To programmatically do the same as :option:`-Wd`, use::<br>
+<br>
+  warnings.simplefilter(&#39;default&#39;)<br>
+<br>
+Make sure to execute this code as soon as possible. This prevents the<br>
+registering of what warnings have been raised from unexpectedly influencing how<br>
+future warnings are treated.<br>
+<br>
+Having certain warnings ignored by default is done to prevent a user from<br>
+seeing warnings that are only of interest to the developer. As you do not<br>
+necessarily have control over what interpreter a user uses to run their code,<br>
+it is possible that a new version of Python will be released between your<br>
+release cycles.  The new interpreter release could trigger new warnings in your<br>
+code that were not there in an older interpreter, e.g.<br>
+:exc:`DeprecationWarning` for a module that you are using. While you as a<br>
+developer want to be notified that your code is using a deprecated module, to a<br>
+user this information is essentially noise and provides no benefit to them.<br>
+<br>
+<br>
 .. _warning-functions:<br>
<br>
 Available Functions<br>
<br>
Modified: python/trunk/Lib/test/test_ascii_formatd.py<br>
==============================================================================<br>
--- python/trunk/Lib/test/test_ascii_formatd.py (original)<br>
+++ python/trunk/Lib/test/test_ascii_formatd.py Sun Jan 10 03:56:19 2010<br>
@@ -4,6 +4,7 @@<br>
<br>
 import unittest<br>
 from test_support import check_warnings, run_unittest, cpython_only<br>
+import warnings<br>
<br>
<br>
 class FormatDeprecationTests(unittest.TestCase):<br>
@@ -17,6 +18,7 @@<br>
         buf = create_string_buffer(&#39; &#39; * 100)<br>
<br>
         with check_warnings() as w:<br>
+            warnings.simplefilter(&#39;default&#39;)<br>
             PyOS_ascii_formatd(byref(buf), sizeof(buf), &#39;%+.10f&#39;,<br>
                                c_double(10.0))<br>
             self.assertEqual(buf.value, &#39;+10.0000000000&#39;)<br>
<br>
Modified: python/trunk/Lib/test/test_exceptions.py<br>
==============================================================================<br>
--- python/trunk/Lib/test/test_exceptions.py    (original)<br>
+++ python/trunk/Lib/test/test_exceptions.py    Sun Jan 10 03:56:19 2010<br>
@@ -309,6 +309,7 @@<br>
         # BaseException.__init__ triggers a deprecation warning.<br>
         exc = BaseException(&quot;foo&quot;)<br>
         with warnings.catch_warnings(record=True) as w:<br>
+            warnings.simplefilter(&#39;default&#39;)<br>
             self.assertEquals(exc.message, &quot;foo&quot;)<br>
         self.assertEquals(len(w), 1)<br>
         self.assertEquals(w[0].category, DeprecationWarning)<br>
<br>
Modified: python/trunk/Lib/warnings.py<br>
==============================================================================<br>
--- python/trunk/Lib/warnings.py        (original)<br>
+++ python/trunk/Lib/warnings.py        Sun Jan 10 03:56:19 2010<br>
@@ -383,8 +383,8 @@<br>
 # Module initialization<br>
 _processoptions(sys.warnoptions)<br>
 if not _warnings_defaults:<br>
-    simplefilter(&quot;ignore&quot;, category=PendingDeprecationWarning, append=1)<br>
-    simplefilter(&quot;ignore&quot;, category=ImportWarning, append=1)<br>
+    for cls in (DeprecationWarning, PendingDeprecationWarning, ImportWarning):<br>
+        simplefilter(&quot;ignore&quot;, category=cls, append=True)<br>
     bytes_warning = sys.flags.bytes_warning<br>
     if bytes_warning &gt; 1:<br>
         bytes_action = &quot;error&quot;<br>
<br>
Modified: python/trunk/Misc/NEWS<br>
==============================================================================<br>
--- python/trunk/Misc/NEWS      (original)<br>
+++ python/trunk/Misc/NEWS      Sun Jan 10 03:56:19 2010<br>
@@ -12,6 +12,8 @@<br>
 Core and Builtins<br>
 -----------------<br>
<br>
+- Issue #7319: Silence DeprecationWarning by default.<br>
+<br>
 - Issue #2335: Backport set literals syntax from Python 3.x.<br>
<br>
 Library<br>
<br>
Modified: python/trunk/Python/_warnings.c<br>
==============================================================================<br>
--- python/trunk/Python/_warnings.c     (original)<br>
+++ python/trunk/Python/_warnings.c     Sun Jan 10 03:56:19 2010<br>
@@ -85,10 +85,10 @@<br>
<br>
     default_action = get_warnings_attr(&quot;defaultaction&quot;);<br>
     if (default_action == NULL) {<br>
-       if (PyErr_Occurred()) {<br>
-           return NULL;<br>
-       }<br>
-       return _default_action;<br>
+        if (PyErr_Occurred()) {<br>
+            return NULL;<br>
+        }<br>
+        return _default_action;<br>
     }<br>
<br>
     Py_DECREF(_default_action);<br>
@@ -202,12 +202,12 @@<br>
<br>
     mod_str = PyString_AsString(filename);<br>
     if (mod_str == NULL)<br>
-           return NULL;<br>
+        return NULL;<br>
     len = PyString_Size(filename);<br>
     if (len &lt; 0)<br>
         return NULL;<br>
     if (len &gt;= 3 &amp;&amp;<br>
-       strncmp(mod_str + (len - 3), &quot;.py&quot;, 3) == 0) {<br>
+            strncmp(mod_str + (len - 3), &quot;.py&quot;, 3) == 0) {<br>
         module = PyString_FromStringAndSize(mod_str, len-3);<br>
     }<br>
     else {<br>
@@ -251,7 +251,7 @@<br>
<br>
     name = PyObject_GetAttrString(category, &quot;__name__&quot;);<br>
     if (name == NULL)  /* XXX Can an object lack a &#39;__name__&#39; attribute? */<br>
-           return;<br>
+        return;<br>
<br>
     f_stderr = PySys_GetObject(&quot;stderr&quot;);<br>
     if (f_stderr == NULL) {<br>
@@ -341,7 +341,7 @@<br>
         rc = already_warned(registry, key, 0);<br>
         if (rc == -1)<br>
             goto cleanup;<br>
-       else if (rc == 1)<br>
+        else if (rc == 1)<br>
             goto return_none;<br>
         /* Else this warning hasn&#39;t been generated before. */<br>
     }<br>
@@ -492,9 +492,9 @@<br>
     /* Setup filename. */<br>
     *filename = PyDict_GetItemString(globals, &quot;__file__&quot;);<br>
     if (*filename != NULL) {<br>
-           Py_ssize_t len = PyString_Size(*filename);<br>
+            Py_ssize_t len = PyString_Size(*filename);<br>
         const char *file_str = PyString_AsString(*filename);<br>
-           if (file_str == NULL || (len &lt; 0 &amp;&amp; PyErr_Occurred()))<br>
+            if (file_str == NULL || (len &lt; 0 &amp;&amp; PyErr_Occurred()))<br>
             goto handle_error;<br>
<br>
         /* if filename.lower().endswith((&quot;.pyc&quot;, &quot;.pyo&quot;)): */<br>
@@ -506,10 +506,10 @@<br>
                 tolower(file_str[len-1]) == &#39;o&#39;))<br>
         {<br>
             *filename = PyString_FromStringAndSize(file_str, len-1);<br>
-               if (*filename == NULL)<br>
-                       goto handle_error;<br>
-           }<br>
-           else<br>
+            if (*filename == NULL)<br>
+                goto handle_error;<br>
+        }<br>
+        else<br>
             Py_INCREF(*filename);<br>
     }<br>
     else {<br>
@@ -536,8 +536,8 @@<br>
             else {<br>
                 /* embedded interpreters don&#39;t have sys.argv, see bug #839151 */<br>
                 *filename = PyString_FromString(&quot;__main__&quot;);<br>
-                   if (*filename == NULL)<br>
-                       goto handle_error;<br>
+                if (*filename == NULL)<br>
+                    goto handle_error;<br>
             }<br>
         }<br>
         if (*filename == NULL) {<br>
@@ -839,26 +839,29 @@<br>
 static PyObject *<br>
 init_filters(void)<br>
 {<br>
-    PyObject *filters = PyList_New(3);<br>
+    PyObject *filters = PyList_New(4);<br>
     const char *bytes_action;<br>
     if (filters == NULL)<br>
         return NULL;<br>
<br>
     PyList_SET_ITEM(filters, 0,<br>
+                    create_filter(PyExc_DeprecationWarning, &quot;ignore&quot;));<br>
+    PyList_SET_ITEM(filters, 1,<br>
                     create_filter(PyExc_PendingDeprecationWarning, &quot;ignore&quot;));<br>
-    PyList_SET_ITEM(filters, 1, create_filter(PyExc_ImportWarning, &quot;ignore&quot;));<br>
+    PyList_SET_ITEM(filters, 2, create_filter(PyExc_ImportWarning, &quot;ignore&quot;));<br>
     if (Py_BytesWarningFlag &gt; 1)<br>
         bytes_action = &quot;error&quot;;<br>
     else if (Py_BytesWarningFlag)<br>
         bytes_action = &quot;default&quot;;<br>
     else<br>
         bytes_action = &quot;ignore&quot;;<br>
-    PyList_SET_ITEM(filters, 2, create_filter(PyExc_BytesWarning,<br>
+    PyList_SET_ITEM(filters, 3, create_filter(PyExc_BytesWarning,<br>
                     bytes_action));<br>
<br>
     if (PyList_GET_ITEM(filters, 0) == NULL ||<br>
         PyList_GET_ITEM(filters, 1) == NULL ||<br>
-        PyList_GET_ITEM(filters, 2) == NULL) {<br>
+        PyList_GET_ITEM(filters, 2) == NULL ||<br>
+        PyList_GET_ITEM(filters, 3) == NULL) {<br>
         Py_DECREF(filters);<br>
         return NULL;<br>
<div><div></div><div class="h5">     }<br>
_______________________________________________<br>
Python-checkins mailing list<br>
<a href="mailto:Python-checkins@python.org">Python-checkins@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-checkins" target="_blank">http://mail.python.org/mailman/listinfo/python-checkins</a><br>
</div></div></div><br>