Code

Ticket #19665: 19665-1.diff

File 19665-1.diff, 2.3 KB (added by claudep, 18 months ago)
Line 
1diff --git a/django/core/management/base.py b/django/core/management/base.py
2index 895753e..fb3f22c 100644
3--- a/django/core/management/base.py
4+++ b/django/core/management/base.py
5@@ -221,9 +221,10 @@ class BaseCommand(object):
6         try:
7             self.execute(*args, **options.__dict__)
8         except Exception as e:
9+            stderr = getattr(self, 'stderr', OutputWrapper(sys.stderr, self.style.ERROR))
10             if options.traceback:
11-                self.stderr.write(traceback.format_exc())
12-            self.stderr.write('%s: %s' % (e.__class__.__name__, e))
13+                stderr.write(traceback.format_exc())
14+            stderr.write('%s: %s' % (e.__class__.__name__, e))
15             sys.exit(1)
16 
17     def execute(self, *args, **options):
18diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
19index df2326e..8be687a 100644
20--- a/tests/regressiontests/admin_scripts/tests.py
21+++ b/tests/regressiontests/admin_scripts/tests.py
22@@ -16,11 +16,13 @@ import codecs
23 
24 from django import conf, bin, get_version
25 from django.conf import settings
26+from django.core.management import BaseCommand
27 from django.db import connection
28 from django.test.simple import DjangoTestSuiteRunner
29 from django.utils import unittest
30 from django.utils.encoding import force_str, force_text
31 from django.utils._os import upath
32+from django.utils.six import StringIO
33 from django.test import LiveServerTestCase
34 
35 test_dir = os.path.dirname(os.path.dirname(upath(__file__)))
36@@ -1279,6 +1281,19 @@ class CommandTypes(AdminScriptTestCase):
37         self.assertNoOutput(err)
38         self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
39 
40+    def test_base_run_from_argv(self):
41+        "Test run_from_argv properly terminates even with custom execute() (#19665)"
42+        command = BaseCommand()
43+        command.execute = lambda *args: args
44+
45+        old_stderr = sys.stderr
46+        sys.stderr = StringIO()
47+        try:
48+            with self.assertRaises(SystemExit):
49+                command.run_from_argv(['', ''])
50+        finally:
51+            sys.stderr = old_stderr
52+
53     def test_noargs(self):
54         "NoArg Commands can be executed"
55         args = ['noargs_command']