Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.5.x] Fixed #18985 -- ensure module level deprecations are displayed

Also don't compete with -W CLI option.

Thanks to Aymeric Augustin for the catch, and Claude Paroz for the patch.

Backport of e79b857 from master.
  • Loading branch information...
commit 572a300e56bc0192a9c82565a1d39b96f18bebee 1 parent 456d6c1
Preston Holmes ptone authored jacobian committed
18 django/conf/__init__.py
View
@@ -8,6 +8,7 @@
import logging
import os
+import sys
import time # Needed for Windows
import warnings
@@ -56,14 +57,15 @@ def _configure_logging(self):
"""
Setup logging from LOGGING_CONFIG and LOGGING settings.
"""
- try:
- # Route warnings through python logging
- logging.captureWarnings(True)
- # Allow DeprecationWarnings through the warnings filters
- warnings.simplefilter("default", DeprecationWarning)
- except AttributeError:
- # No captureWarnings on Python 2.6, DeprecationWarnings are on anyway
- pass
+ if not sys.warnoptions:
+ try:
+ # Route warnings through python logging
+ logging.captureWarnings(True)
+ # Allow DeprecationWarnings through the warnings filters
+ warnings.simplefilter("default", DeprecationWarning)
+ except AttributeError:
+ # No captureWarnings on Python 2.6, DeprecationWarnings are on anyway
+ pass
if self.LOGGING_CONFIG:
from django.utils.log import DEFAULT_LOGGING
17 django/core/management/commands/test.py
View
@@ -1,3 +1,4 @@
+import logging
import sys
import os
from optparse import make_option, OptionParser
@@ -6,6 +7,7 @@
from django.core.management.base import BaseCommand
from django.test.utils import get_runner
+
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--noinput',
@@ -57,6 +59,21 @@ def create_parser(self, prog_name, subcommand):
version=self.get_version(),
option_list=options)
+ def execute(self, *args, **options):
+ if int(options['verbosity']) > 0:
+ # ensure that deprecation warnings are displayed during testing
+ # the following state is assumed:
+ # logging.capturewarnings is true
+ # a "default" level warnings filter has been added for
+ # DeprecationWarning. See django.conf.LazySettings._configure_logging
+ logger = logging.getLogger('py.warnings')
+ handler = logging.StreamHandler()
+ logger.addHandler(handler)
+ super(Command, self).execute(*args, **options)
+ if int(options['verbosity']) > 0:
+ # remove the testing-specific handler
+ logger.removeHandler(handler)
+
def handle(self, *test_labels, **options):
from django.conf import settings
from django.test.utils import get_runner
13 django/test/simple.py
View
@@ -1,4 +1,3 @@
-import logging
import unittest as real_unittest
from django.conf import settings
@@ -366,19 +365,7 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
self.setup_test_environment()
suite = self.build_suite(test_labels, extra_tests)
old_config = self.setup_databases()
- if self.verbosity > 0:
- # ensure that deprecation warnings are displayed during testing
- # the following state is assumed:
- # logging.capturewarnings is true
- # a "default" level warnings filter has been added for
- # DeprecationWarning. See django.conf.LazySettings._configure_logging
- logger = logging.getLogger('py.warnings')
- handler = logging.StreamHandler()
- logger.addHandler(handler)
result = self.run_suite(suite)
- if self.verbosity > 0:
- # remove the testing-specific handler
- logger.removeHandler(handler)
self.teardown_databases(old_config)
self.teardown_test_environment()
return self.suite_result(suite, result)
2  tests/regressiontests/test_runner/deprecation_app/tests.py
View
@@ -2,6 +2,8 @@
from django.test import TestCase
+warnings.warn("module-level warning from deprecation_app", DeprecationWarning)
+
class DummyTest(TestCase):
def test_warn(self):
warnings.warn("warning from test", DeprecationWarning)
3  tests/regressiontests/test_runner/tests.py
View
@@ -298,7 +298,8 @@ def tearDown(self):
def test_runner_deprecation_verbosity_default(self):
args = ['test', '--settings=regressiontests.settings']
out, err = self.run_django_admin(args)
- self.assertTrue("DeprecationWarning: warning from test" in err)
+ self.assertIn("DeprecationWarning: warning from test", err)
+ self.assertIn("DeprecationWarning: module-level warning from deprecation_app", err)
@unittest.skipIf(sys.version_info[:2] == (2, 6),
"On Python 2.6, DeprecationWarnings are visible anyway")
9 tests/runtests.py
View
@@ -1,4 +1,5 @@
#!/usr/bin/env python
+import logging
import os
import shutil
import subprocess
@@ -106,6 +107,14 @@ def setup(verbosity, test_labels):
# in our tests.
settings.MANAGERS = ("admin@djangoproject.com",)
+ if verbosity > 0:
+ # Ensure any warnings captured to logging are piped through a verbose
+ # logging handler. If any -W options were passed explicitly on command
+ # line, warnings are not captured, and this has no effect.
+ logger = logging.getLogger('py.warnings')
+ handler = logging.StreamHandler()
+ logger.addHandler(handler)
+
# Load all the ALWAYS_INSTALLED_APPS.
# (This import statement is intentionally delayed until after we
# access settings because of the USE_I18N dependency.)
Please sign in to comment.
Something went wrong with that request. Please try again.