Opened 15 years ago

Closed 15 years ago

#12593 closed (duplicate)

"manage.py loaddata" exits with system exist status 0 when there are errors

Reported by: bruce_s Owned by: nobody
Component: Uncategorized Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

"manage.py loaddata" returns with system exit status 0 on error. it should return nonzero on error,
so that (for example) it plays nice with ant, make, expect, etc.

Example:

% ./manage.py loaddata gamedata-test.json
Installing json fixture 'gamedata-test' from absolute path.
Problem installing fixture 'gamedata-test.json': Traceback (most recent call last):
  File "/usr/lib64/python2.6/site-packages/django/core/management/commands/loaddata.py", line 150, in handle
    for obj in objects:
  File "/usr/lib64/python2.6/site-packages/django/core/serializers/json.py", line 41, in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream)):
  File "/usr/lib64/python2.6/json/__init__.py", line 267, in load
    parse_constant=parse_constant, **kw)
  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/usr/lib64/python2.6/json/decoder.py", line 219, in JSONArray
    raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 2 column 5 (char 6)
% echo $?
0

The code at fault is in /django/core/management/commands/loaddata.py, around line 170 (and in several other places),
where there are try/except blocks which return when they should either sys.exit(1) or raise, so that the outermost program
knows to exit with error:

except Exception:
    import traceback
    fixture.close()
    transaction.rollback()
    transaction.leave_transaction_management()
    if show_traceback:
        traceback.print_exc()
    else:
        sys.stderr.write(
            self.style.ERROR("Problem installing fixture '%s': %s\n" %
                 (full_path, ''.join(traceback.format_exception(sys.exc_type,
                     sys.exc_value, sys.exc_traceback)))))
    return

That last "return" is the bug. Replacing it with "raise" fixes the problem but makes the output quite a bit messier:

% ./manage.py loaddata gamedata-test.json
Installing json fixture 'gamedata-test' from absolute path.
Problem installing fixture 'gamedata-test.json': Traceback (most recent call last):
  File "/usr/lib64/python2.6/site-packages/django/core/management/commands/loaddata.py", line 150, in handle
    for obj in objects:
  File "/usr/lib64/python2.6/site-packages/django/core/serializers/json.py", line 41, in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream)):
  File "/usr/lib64/python2.6/json/__init__.py", line 267, in load
    parse_constant=parse_constant, **kw)
  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/usr/lib64/python2.6/json/decoder.py", line 219, in JSONArray
    raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 2 column 5 (char 6)

Traceback (most recent call last):
  File "./manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/lib64/python2.6/site-packages/django/core/management/__init__.py", line 439, in execute_manager
    utility.execute()
  File "/usr/lib64/python2.6/site-packages/django/core/management/__init__.py", line 380, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib64/python2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib64/python2.6/site-packages/django/core/management/base.py", line 222, in execute
    output = self.handle(*args, **options)
  File "/usr/lib64/python2.6/site-packages/django/core/management/commands/loaddata.py", line 200, in handle
    transaction.leave_transaction_management()
  File "/usr/lib64/python2.6/site-packages/django/db/transaction.py", line 74, in leave_transaction_management
    raise TransactionManagementError("This code isn't under transaction management")
django.db.transaction.TransactionManagementError: This code isn't under transaction management
% echo $?
1

The other option is to change the return to sys.exit(1):

% ./manage.py loaddata gamedata-test.json
Installing json fixture 'gamedata-test' from absolute path.
Problem installing fixture 'gamedata-test.json': Traceback (most recent call last):
  File "/usr/lib64/python2.6/site-packages/django/core/management/commands/loaddata.py", line 150, in handle
    for obj in objects:
  File "/usr/lib64/python2.6/site-packages/django/core/serializers/json.py", line 41, in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream)):
  File "/usr/lib64/python2.6/json/__init__.py", line 267, in load
    parse_constant=parse_constant, **kw)
  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/usr/lib64/python2.6/json/decoder.py", line 219, in JSONArray
    raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 2 column 5 (char 6)

% echo $?
1


Attachments (1)

foo (2.0 KB ) - added by bruce_s 15 years ago.
patch to loaddata.py using sys.exit(1)

Download all attachments as: .zip

Change History (3)

comment:1 by bruce_s, 15 years ago

Patch to fix with sys.exit(1):

--- loaddata.py 2010-01-12 10:54:47.000000000 -0800
+++ /usr/lib64/python2.6/site-packages/django/core/management/commands/loaddata.py 2010-01-12 10:54:17.000000000 -0800
@@ -105,7 +105,7 @@ class Command(BaseCommand):

(fixture_name, format)))

transaction.rollback()
transaction.leave_transaction_management()

  • sys.exit(1)

+ return

if os.path.isabs(fixture_name):

fixture_dirs = [fixture_name]

@@ -138,7 +138,7 @@ class Command(BaseCommand):

(fixture_name, humanize(fixture_dir)))

transaction.rollback()
transaction.leave_transaction_management()

  • sys.exit(1)

+ return

else:

fixture_count += 1
objects_in_fixture = 0

@@ -167,7 +167,7 @@ class Command(BaseCommand):

self.style.ERROR("Problem installing fixture '%s': %s\n" %

(full_path, .join(traceback.format_exception(sys.exc_type,

sys.exc_value, sys.exc_traceback)))))

  • sys.exit(1)

+ return

fixture.close()


# If the fixture we loaded contains 0 objects, assume that an

@@ -178,7 +178,7 @@ class Command(BaseCommand):

(fixture_name)))

transaction.rollback()
transaction.leave_transaction_management()

  • sys.exit(1)

+ return

except Exception, e:

if verbosity > 1:

by bruce_s, 15 years ago

Attachment: foo added

patch to loaddata.py using sys.exit(1)

comment:2 by Ramiro Morales, 15 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #11481.

Note: See TracTickets for help on using tickets.
Back to Top