[Python-Dev] Fwd: [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

Brett Cannon brett at python.org
Sun Jan 10 19:51:26 CET 2010


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).

---------- Forwarded message ----------
From: brett.cannon <python-checkins at python.org>
Date: Sat, Jan 9, 2010 at 18:56
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
To: python-checkins at python.org


Author: brett.cannon
Date: Sun Jan 10 03:56:19 2010
New Revision: 77402

Log:
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.


Modified:
  python/trunk/Doc/library/warnings.rst
  python/trunk/Lib/test/test_ascii_formatd.py
  python/trunk/Lib/test/test_exceptions.py
  python/trunk/Lib/warnings.py
  python/trunk/Misc/NEWS
  python/trunk/Python/_warnings.c

Modified: python/trunk/Doc/library/warnings.rst
==============================================================================
--- python/trunk/Doc/library/warnings.rst       (original)
+++ python/trunk/Doc/library/warnings.rst       Sun Jan 10 03:56:19 2010
@@ -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,6 +89,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:

@@ -148,14 +151,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:

@@ -226,6 +221,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/trunk/Lib/test/test_ascii_formatd.py
==============================================================================
--- python/trunk/Lib/test/test_ascii_formatd.py (original)
+++ python/trunk/Lib/test/test_ascii_formatd.py Sun Jan 10 03:56:19 2010
@@ -4,6 +4,7 @@

 import unittest
 from test_support import check_warnings, run_unittest, cpython_only
+import warnings


 class FormatDeprecationTests(unittest.TestCase):
@@ -17,6 +18,7 @@
        buf = create_string_buffer(' ' * 100)

        with check_warnings() as w:
+            warnings.simplefilter('default')
            PyOS_ascii_formatd(byref(buf), sizeof(buf), '%+.10f',
                               c_double(10.0))
            self.assertEqual(buf.value, '+10.0000000000')

Modified: python/trunk/Lib/test/test_exceptions.py
==============================================================================
--- python/trunk/Lib/test/test_exceptions.py    (original)
+++ python/trunk/Lib/test/test_exceptions.py    Sun Jan 10 03:56:19 2010
@@ -309,6 +309,7 @@
        # BaseException.__init__ triggers a deprecation warning.
        exc = BaseException("foo")
        with warnings.catch_warnings(record=True) as w:
+            warnings.simplefilter('default')
            self.assertEquals(exc.message, "foo")
        self.assertEquals(len(w), 1)
        self.assertEquals(w[0].category, DeprecationWarning)

Modified: python/trunk/Lib/warnings.py
==============================================================================
--- python/trunk/Lib/warnings.py        (original)
+++ python/trunk/Lib/warnings.py        Sun Jan 10 03:56:19 2010
@@ -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=True)
    bytes_warning = sys.flags.bytes_warning
    if bytes_warning > 1:
        bytes_action = "error"

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS      (original)
+++ python/trunk/Misc/NEWS      Sun Jan 10 03:56:19 2010
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------

+- Issue #7319: Silence DeprecationWarning by default.
+
 - Issue #2335: Backport set literals syntax from Python 3.x.

 Library

Modified: python/trunk/Python/_warnings.c
==============================================================================
--- python/trunk/Python/_warnings.c     (original)
+++ python/trunk/Python/_warnings.c     Sun Jan 10 03:56:19 2010
@@ -85,10 +85,10 @@

    default_action = get_warnings_attr("defaultaction");
    if (default_action == NULL) {
-       if (PyErr_Occurred()) {
-           return NULL;
-       }
-       return _default_action;
+        if (PyErr_Occurred()) {
+            return NULL;
+        }
+        return _default_action;
    }

    Py_DECREF(_default_action);
@@ -202,12 +202,12 @@

    mod_str = PyString_AsString(filename);
    if (mod_str == NULL)
-           return NULL;
+        return NULL;
    len = PyString_Size(filename);
    if (len < 0)
        return NULL;
    if (len >= 3 &&
-       strncmp(mod_str + (len - 3), ".py", 3) == 0) {
+            strncmp(mod_str + (len - 3), ".py", 3) == 0) {
        module = PyString_FromStringAndSize(mod_str, len-3);
    }
    else {
@@ -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) {
@@ -341,7 +341,7 @@
        rc = already_warned(registry, key, 0);
        if (rc == -1)
            goto cleanup;
-       else if (rc == 1)
+        else if (rc == 1)
            goto return_none;
        /* Else this warning hasn't been generated before. */
    }
@@ -492,9 +492,9 @@
    /* Setup filename. */
    *filename = PyDict_GetItemString(globals, "__file__");
    if (*filename != NULL) {
-           Py_ssize_t len = PyString_Size(*filename);
+            Py_ssize_t len = PyString_Size(*filename);
        const char *file_str = PyString_AsString(*filename);
-           if (file_str == NULL || (len < 0 && PyErr_Occurred()))
+            if (file_str == NULL || (len < 0 && PyErr_Occurred()))
            goto handle_error;

        /* if filename.lower().endswith((".pyc", ".pyo")): */
@@ -506,10 +506,10 @@
                tolower(file_str[len-1]) == 'o'))
        {
            *filename = PyString_FromStringAndSize(file_str, len-1);
-               if (*filename == NULL)
-                       goto handle_error;
-           }
-           else
+            if (*filename == NULL)
+                goto handle_error;
+        }
+        else
            Py_INCREF(*filename);
    }
    else {
@@ -536,8 +536,8 @@
            else {
                /* embedded interpreters don't have sys.argv, see bug
#839151 */
                *filename = PyString_FromString("__main__");
-                   if (*filename == NULL)
-                       goto handle_error;
+                if (*filename == NULL)
+                    goto handle_error;
            }
        }
        if (*filename == NULL) {
@@ -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;
     }
_______________________________________________
Python-checkins mailing list
Python-checkins at python.org
http://mail.python.org/mailman/listinfo/python-checkins
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20100110/db6da5fd/attachment-0007.htm>


More information about the Python-Dev mailing list