Ticket #19923: 19923-1.diff

File 19923-1.diff, 2.8 KB (added by claudep, 2 years ago)
  • 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))
  • 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