Ticket #19923: 19923-2.diff

File 19923-2.diff, 3.5 KB (added by Claude Paroz, 11 years ago)

Same patch including updated docs

  • django/core/management/base.py

    diff --git a/django/core/management/base.py b/django/core/management/base.py
    index bdaa5fa..e6a968b 100644
    a b class BaseCommand(object):  
    241241        except Exception as e:
    242242            # self.stderr is not guaranteed to be set here
    243243            stderr = getattr(self, 'stderr', OutputWrapper(sys.stderr, self.style.ERROR))
    244             if options.traceback:
     244            if options.traceback or not isinstance(e, CommandError):
    245245                stderr.write(traceback.format_exc())
    246246            else:
    247247                stderr.write('%s: %s' % (e.__class__.__name__, e))
  • docs/ref/django-admin.txt

    diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
    index c2034a8..81877c7 100644
    a b Example usage::  
    13421342    django-admin.py syncdb --traceback
    13431343
    13441344By default, ``django-admin.py`` will show a simple error message whenever an
    1345 error occurs. If you specify ``--traceback``, ``django-admin.py``  will
    1346 output a full stack trace whenever an exception is raised.
     1345:class:`~django.core.management.CommandError` occurs, but a full stack trace
     1346for any other exception. If you specify ``--traceback``, ``django-admin.py``
     1347will also output a full stack trace when a ``CommandError`` is raised.
    13471348
    13481349.. django-admin-option:: --verbosity
    13491350
  • tests/admin_scripts/tests.py

    diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py
    index 4259598..90f7720 100644
    a b import codecs  
    1616
    1717from django import conf, bin, get_version
    1818from django.conf import settings
    19 from django.core.management import BaseCommand
     19from django.core.management import BaseCommand, CommandError
    2020from django.db import connection
    2121from django.test.simple import DjangoTestSuiteRunner
    2222from django.utils import unittest
    class CommandTypes(AdminScriptTestCase):  
    12971297        Also test proper traceback display.
    12981298        """
    12991299        command = BaseCommand()
    1300         command.execute = lambda args: args  # This will trigger TypeError
     1300        def raise_command_error(*args, **kwargs):
     1301            raise CommandError("Custom error")
    13011302
    13021303        old_stderr = sys.stderr
    13031304        sys.stderr = err = StringIO()
    13041305        try:
     1306            command.execute = lambda args: args  # This will trigger TypeError
    13051307            with self.assertRaises(SystemExit):
    13061308                command.run_from_argv(['', ''])
    13071309            err_message = err.getvalue()
    1308             self.assertNotIn("Traceback", err_message)
     1310            # Exceptions other than CommandError automatically output the traceback
     1311            self.assertIn("Traceback", err_message)
    13091312            self.assertIn("TypeError", err_message)
    13101313
     1314            command.execute = raise_command_error
     1315            err.truncate(0)
     1316            with self.assertRaises(SystemExit):
     1317                command.run_from_argv(['', ''])
     1318            err_message = err.getvalue()
     1319            self.assertNotIn("Traceback", err_message)
     1320            self.assertIn("CommandError", err_message)
     1321
     1322            err.truncate(0)
    13111323            with self.assertRaises(SystemExit):
    13121324                command.run_from_argv(['', '', '--traceback'])
    13131325            err_message = err.getvalue()
    13141326            self.assertIn("Traceback (most recent call last)", err_message)
    1315             self.assertIn("TypeError", err_message)
     1327            self.assertIn("CommandError", err_message)
    13161328        finally:
    13171329            sys.stderr = old_stderr
    13181330
Back to Top