Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16397 -- Respected the --traceback flag in BaseCommand. This s…

…hould make import loops easier to debug. Refs #11667, #17369.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17197 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 694bc0f22bdb30a472de6fec05a44adf5c0f1f2c 1 parent affca13
Aymeric Augustin authored December 11, 2011
15  django/core/management/base.py
@@ -7,6 +7,7 @@
7 7
 import os
8 8
 import sys
9 9
 from optparse import make_option, OptionParser
  10
+import traceback
10 11
 
11 12
 import django
12 13
 from django.core.exceptions import ImproperlyConfigured
@@ -197,8 +198,9 @@ def execute(self, *args, **options):
197 198
         ``self.requires_model_validation``). If the command raises a
198 199
         ``CommandError``, intercept it and print it sensibly to
199 200
         stderr.
200  
-
201 201
         """
  202
+        show_traceback = options.get('traceback', False)
  203
+
202 204
         # Switch to English, because django-admin.py creates database content
203 205
         # like permissions, and those shouldn't contain any translations.
204 206
         # But only do this if we can assume we have a working settings file,
@@ -212,8 +214,12 @@ def execute(self, *args, **options):
212 214
             except ImportError, e:
213 215
                 # If settings should be available, but aren't,
214 216
                 # raise the error and quit.
215  
-                sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
  217
+                if show_traceback:
  218
+                    traceback.print_exc()
  219
+                else:
  220
+                    sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
216 221
                 sys.exit(1)
  222
+
217 223
         try:
218 224
             self.stdout = options.get('stdout', sys.stdout)
219 225
             self.stderr = options.get('stderr', sys.stderr)
@@ -232,7 +238,10 @@ def execute(self, *args, **options):
232 238
                 if self.output_transaction:
233 239
                     self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;") + '\n')
234 240
         except CommandError, e:
235  
-            self.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
  241
+            if show_traceback:
  242
+                traceback.print_exc()
  243
+            else:
  244
+                self.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
236 245
             sys.exit(1)
237 246
         if saved_lang is not None:
238 247
             translation.activate(saved_lang)
2  django/core/management/commands/loaddata.py
@@ -7,6 +7,7 @@
7 7
 import gzip
8 8
 import zipfile
9 9
 from optparse import make_option
  10
+import traceback
10 11
 
11 12
 from django.conf import settings
12 13
 from django.core import serializers
@@ -211,7 +212,6 @@ def read(self):
211 212
                             except (SystemExit, KeyboardInterrupt):
212 213
                                 raise
213 214
                             except Exception:
214  
-                                import traceback
215 215
                                 fixture.close()
216 216
                                 if commit:
217 217
                                     transaction.rollback(using=using)
2  django/core/management/commands/syncdb.py
... ...
@@ -1,5 +1,6 @@
1 1
 from optparse import make_option
2 2
 import sys
  3
+import traceback
3 4
 
4 5
 from django.conf import settings
5 6
 from django.core.management.base import NoArgsCommand
@@ -129,7 +130,6 @@ def model_installed(model):
129 130
                             sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \
130 131
                                                 (app_name, model._meta.object_name, e))
131 132
                             if show_traceback:
132  
-                                import traceback
133 133
                                 traceback.print_exc()
134 134
                             transaction.rollback_unless_managed(using=db)
135 135
                         else:

0 notes on commit 694bc0f

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