Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Deprecated undocumented warnings manipulation testing tools.

  • Loading branch information...
commit 7947c9e3a6bd8c1dfe7fc209fb2256a528149bb6 1 parent 9a4a1ce
Ramiro Morales authored January 31, 2013
21  django/test/testcases.py
... ...
@@ -1,12 +1,13 @@
1 1
 from __future__ import unicode_literals
2 2
 
  3
+from copy import copy
3 4
 import difflib
  5
+import errno
  6
+from functools import wraps
4 7
 import json
5 8
 import os
6 9
 import re
7 10
 import sys
8  
-from copy import copy
9  
-from functools import wraps
10 11
 try:
11 12
     from urllib.parse import urlsplit, urlunsplit
12 13
 except ImportError:     # Python 2
@@ -14,7 +15,7 @@
14 15
 import select
15 16
 import socket
16 17
 import threading
17  
-import errno
  18
+import warnings
18 19
 
19 20
 from django.conf import settings
20 21
 from django.contrib.staticfiles.handlers import StaticFilesHandler
@@ -36,8 +37,7 @@
36 37
 from django.test.client import Client
37 38
 from django.test.html import HTMLParseError, parse_html
38 39
 from django.test.signals import template_rendered
39  
-from django.test.utils import (get_warnings_state, restore_warnings_state,
40  
-    override_settings, compare_xml, strip_quotes)
  40
+from django.test.utils import (override_settings, compare_xml, strip_quotes)
41 41
 from django.test.utils import ContextList
42 42
 from django.utils import unittest as ut2
43 43
 from django.utils.encoding import force_text
@@ -241,6 +241,11 @@ def message(self):
241 241
 
242 242
 
243 243
 class SimpleTestCase(ut2.TestCase):
  244
+
  245
+    _warn_txt = ("save_warnings_state/restore_warnings_state "
  246
+        "django.test.*TestCase methods are deprecated. Use Python's "
  247
+        "warnings.catch_warnings context manager instead.")
  248
+
244 249
     def __call__(self, result=None):
245 250
         """
246 251
         Wrapper around default __call__ method to perform common Django test
@@ -279,14 +284,16 @@ def save_warnings_state(self):
279 284
         """
280 285
         Saves the state of the warnings module
281 286
         """
282  
-        self._warnings_state = get_warnings_state()
  287
+        warnings.warn(self._warn_txt, DeprecationWarning, stacklevel=2)
  288
+        self._warnings_state = warnings.filters[:]
283 289
 
284 290
     def restore_warnings_state(self):
285 291
         """
286 292
         Restores the state of the warnings module to the state
287 293
         saved by save_warnings_state()
288 294
         """
289  
-        restore_warnings_state(self._warnings_state)
  295
+        warnings.warn(self._warn_txt, DeprecationWarning, stacklevel=2)
  296
+        warnings.filters = self._warnings_state[:]
290 297
 
291 298
     def settings(self, **kwargs):
292 299
         """
7  django/test/utils.py
@@ -98,6 +98,11 @@ def teardown_test_environment():
98 98
     del mail.outbox
99 99
 
100 100
 
  101
+warn_txt = ("get_warnings_state/restore_warnings_state functions from "
  102
+    "django.test.utils are deprecated. Use Python's warnings.catch_warnings() "
  103
+    "context manager instead.")
  104
+
  105
+
101 106
 def get_warnings_state():
102 107
     """
103 108
     Returns an object containing the state of the warnings module
@@ -105,6 +110,7 @@ def get_warnings_state():
105 110
     # There is no public interface for doing this, but this implementation of
106 111
     # get_warnings_state and restore_warnings_state appears to work on Python
107 112
     # 2.4 to 2.7.
  113
+    warnings.warn(warn_txt, DeprecationWarning, stacklevel=2)
108 114
     return warnings.filters[:]
109 115
 
110 116
 
@@ -113,6 +119,7 @@ def restore_warnings_state(state):
113 119
     Restores the state of the warnings module when passed an object that was
114 120
     returned by get_warnings_state()
115 121
     """
  122
+    warnings.warn(warn_txt, DeprecationWarning, stacklevel=2)
116 123
     warnings.filters = state[:]
117 124
 
118 125
 
8  docs/internals/deprecation.txt
@@ -267,7 +267,6 @@ these changes.
267 267
   in 1.4. The backward compatibility will be removed --
268 268
   ``HttpRequest.raw_post_data`` will no longer work.
269 269
 
270  
-
271 270
 * The value for the ``post_url_continue`` parameter in
272 271
   ``ModelAdmin.response_add()`` will have to be either ``None`` (to redirect
273 272
   to the newly created object's edit page) or a pre-formatted url. String
@@ -314,6 +313,13 @@ these changes.
314 313
 * The ``depth`` keyword argument will be removed from
315 314
   :meth:`~django.db.models.query.QuerySet.select_related`.
316 315
 
  316
+* The undocumented ``get_warnings_state()``/``restore_warnings_state()``
  317
+  functions from :mod:`django.test.utils` and the ``save_warnings_state()``/
  318
+  ``restore_warnings_state()``
  319
+  :ref:`django.test.*TestCase <django-testcase-subclasses>` methods are
  320
+  deprecated. Use the :class:`warnings.catch_warnings` context manager
  321
+  available starting with Python 2.6 instead.
  322
+
317 323
 1.8
318 324
 ---
319 325
 
2  docs/topics/testing/overview.txt
@@ -835,6 +835,8 @@ The following is a simple unit test using the test client::
835 835
 
836 836
     :class:`django.test.client.RequestFactory`
837 837
 
  838
+.. _django-testcase-subclasses:
  839
+
838 840
 Provided test case classes
839 841
 --------------------------
840 842
 
31  tests/regressiontests/test_utils/tests.py
@@ -220,24 +220,27 @@ def test_save_restore_warnings_state(self):
220 220
         # of save_warnings_state/restore_warnings_state (e.g. just
221 221
         # warnings.resetwarnings()) , but it is difficult to test more.
222 222
         import warnings
223  
-        self.save_warnings_state()
  223
+        with warnings.catch_warnings():
  224
+            warnings.simplefilter("ignore", DeprecationWarning)
224 225
 
225  
-        class MyWarning(Warning):
226  
-            pass
  226
+            self.save_warnings_state()
  227
+
  228
+            class MyWarning(Warning):
  229
+                pass
227 230
 
228  
-        # Add a filter that causes an exception to be thrown, so we can catch it
229  
-        warnings.simplefilter("error", MyWarning)
230  
-        self.assertRaises(Warning, lambda: warnings.warn("warn", MyWarning))
  231
+            # Add a filter that causes an exception to be thrown, so we can catch it
  232
+            warnings.simplefilter("error", MyWarning)
  233
+            self.assertRaises(Warning, lambda: warnings.warn("warn", MyWarning))
231 234
 
232  
-        # Now restore.
233  
-        self.restore_warnings_state()
234  
-        # After restoring, we shouldn't get an exception. But we don't want a
235  
-        # warning printed either, so we have to silence the warning.
236  
-        warnings.simplefilter("ignore", MyWarning)
237  
-        warnings.warn("warn", MyWarning)
  235
+            # Now restore.
  236
+            self.restore_warnings_state()
  237
+            # After restoring, we shouldn't get an exception. But we don't want a
  238
+            # warning printed either, so we have to silence the warning.
  239
+            warnings.simplefilter("ignore", MyWarning)
  240
+            warnings.warn("warn", MyWarning)
238 241
 
239  
-        # Remove the filter we just added.
240  
-        self.restore_warnings_state()
  242
+            # Remove the filter we just added.
  243
+            self.restore_warnings_state()
241 244
 
242 245
 
243 246
 class HTMLEqualTests(TestCase):

0 notes on commit 7947c9e

Please sign in to comment.
Something went wrong with that request. Please try again.