Ticket #16926: 16926-fix-custom_sql_for_model.patch

File 16926-fix-custom_sql_for_model.patch, 1.2 KB (added by adsworth, 4 years ago)
  • django/core/management/sql.py

    diff --git a/django/core/management/sql.py b/django/core/management/sql.py
    index 8bfdadb..8512cca 100644
    a b def custom_sql_for_model(model, style, connection): 
    168168                 os.path.join(app_dir, "%s.sql" % opts.object_name.lower())]
    169169    for sql_file in sql_files:
    170170        if os.path.exists(sql_file):
    171             fp = open(sql_file, 'rbU')
    172             for statement in statements.split(fp.read().decode(settings.FILE_CHARSET)):
     171            # First try opening the file Python3 style.
     172            # The encoding parameter is only available in Python3.
     173            # If that fails open it in the traditional 2.x style and decode the
     174            # result.
     175            try:
     176                fp = open(sql_file, 'r', encoding=settings.FILE_CHARSET)
     177                sql_data = fp.read()
     178            except TypeError:
     179                fp = open(sql_file, 'rU')
     180                sql_data = fp.read().decode(settings.FILE_CHARSET)
     181
     182            for statement in statements.split(sql_data):
    173183                # Remove any comments from the file
    174184                statement = re.sub(ur"--.*([\n\Z]|$)", "", statement)
    175185                if statement.strip():
Back to Top