Fixed #12849 -- Corrected the way strings are encoded for display by the colorizer so that they work with unicode. Thanks to jype for the report, and frasern for his work on the issue.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12803 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-03-18 13:24:11 +00:00
parent 7471dab660
commit be8a1f612d
2 changed files with 23 additions and 23 deletions

View File

@ -11,6 +11,7 @@ from optparse import make_option, OptionParser
import django import django
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.management.color import color_style from django.core.management.color import color_style
from django.utils.encoding import smart_str
try: try:
set set
@ -28,7 +29,7 @@ class CommandError(Exception):
result, raising this exception (with a sensible description of the result, raising this exception (with a sensible description of the
error) is the preferred way to indicate that something has gone error) is the preferred way to indicate that something has gone
wrong in the execution of a command. wrong in the execution of a command.
""" """
pass pass
@ -37,7 +38,7 @@ def handle_default_options(options):
Include any default options that all commands should accept here Include any default options that all commands should accept here
so that ManagementUtility can handle them before searching for so that ManagementUtility can handle them before searching for
user commands. user commands.
""" """
if options.settings: if options.settings:
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
@ -83,7 +84,7 @@ class BaseCommand(object):
specialized methods as needed. specialized methods as needed.
Several attributes affect behavior at various steps along the way: Several attributes affect behavior at various steps along the way:
``args`` ``args``
A string listing the arguments accepted by the command, A string listing the arguments accepted by the command,
suitable for use in help messages; e.g., a command which takes suitable for use in help messages; e.g., a command which takes
@ -117,7 +118,7 @@ class BaseCommand(object):
rather than all applications' models, call rather than all applications' models, call
``self.validate(app)`` from ``handle()``, where ``app`` is the ``self.validate(app)`` from ``handle()``, where ``app`` is the
application's Python module. application's Python module.
""" """
# Metadata about this command. # Metadata about this command.
option_list = ( option_list = (
@ -147,7 +148,7 @@ class BaseCommand(object):
Return the Django version, which should be correct for all Return the Django version, which should be correct for all
built-in Django commands. User-supplied commands should built-in Django commands. User-supplied commands should
override this method. override this method.
""" """
return django.get_version() return django.get_version()
@ -155,7 +156,7 @@ class BaseCommand(object):
""" """
Return a brief description of how to use this command, by Return a brief description of how to use this command, by
default from the attribute ``self.help``. default from the attribute ``self.help``.
""" """
usage = '%%prog %s [options] %s' % (subcommand, self.args) usage = '%%prog %s [options] %s' % (subcommand, self.args)
if self.help: if self.help:
@ -167,7 +168,7 @@ class BaseCommand(object):
""" """
Create and return the ``OptionParser`` which will be used to Create and return the ``OptionParser`` which will be used to
parse the arguments to this command. parse the arguments to this command.
""" """
return OptionParser(prog=prog_name, return OptionParser(prog=prog_name,
usage=self.usage(subcommand), usage=self.usage(subcommand),
@ -178,7 +179,7 @@ class BaseCommand(object):
""" """
Print the help message for this command, derived from Print the help message for this command, derived from
``self.usage()``. ``self.usage()``.
""" """
parser = self.create_parser(prog_name, subcommand) parser = self.create_parser(prog_name, subcommand)
parser.print_help() parser.print_help()
@ -187,7 +188,7 @@ class BaseCommand(object):
""" """
Set up any environment changes requested (e.g., Python path Set up any environment changes requested (e.g., Python path
and Django settings), then run this command. and Django settings), then run this command.
""" """
parser = self.create_parser(argv[0], argv[1]) parser = self.create_parser(argv[0], argv[1])
options, args = parser.parse_args(argv[2:]) options, args = parser.parse_args(argv[2:])
@ -201,7 +202,7 @@ class BaseCommand(object):
``self.requires_model_validation``). If the command raises a ``self.requires_model_validation``). If the command raises a
``CommandError``, intercept it and print it sensibly to ``CommandError``, intercept it and print it sensibly to
stderr. stderr.
""" """
# Switch to English, because django-admin.py creates database content # Switch to English, because django-admin.py creates database content
# like permissions, and those shouldn't contain any translations. # like permissions, and those shouldn't contain any translations.
@ -214,7 +215,7 @@ class BaseCommand(object):
except ImportError, e: except ImportError, e:
# If settings should be available, but aren't, # If settings should be available, but aren't,
# raise the error and quit. # raise the error and quit.
sys.stderr.write(self.style.ERROR(str('Error: %s\n' % e))) sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
sys.exit(1) sys.exit(1)
try: try:
if self.requires_model_validation: if self.requires_model_validation:
@ -230,15 +231,15 @@ class BaseCommand(object):
if self.output_transaction: if self.output_transaction:
print self.style.SQL_KEYWORD("COMMIT;") print self.style.SQL_KEYWORD("COMMIT;")
except CommandError, e: except CommandError, e:
sys.stderr.write(self.style.ERROR(str('Error: %s\n' % e))) sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
sys.exit(1) sys.exit(1)
def validate(self, app=None, display_num_errors=False): def validate(self, app=None, display_num_errors=False):
""" """
Validates the given app, raising CommandError for any errors. Validates the given app, raising CommandError for any errors.
If app is None, then this will validate all installed apps. If app is None, then this will validate all installed apps.
""" """
from django.core.management.validation import get_validation_errors from django.core.management.validation import get_validation_errors
try: try:
@ -258,7 +259,7 @@ class BaseCommand(object):
""" """
The actual logic of the command. Subclasses must implement The actual logic of the command. Subclasses must implement
this method. this method.
""" """
raise NotImplementedError() raise NotImplementedError()
@ -269,7 +270,7 @@ class AppCommand(BaseCommand):
Rather than implementing ``handle()``, subclasses must implement Rather than implementing ``handle()``, subclasses must implement
``handle_app()``, which will be called once for each application. ``handle_app()``, which will be called once for each application.
""" """
args = '<appname appname ...>' args = '<appname appname ...>'
@ -293,7 +294,7 @@ class AppCommand(BaseCommand):
Perform the command's actions for ``app``, which will be the Perform the command's actions for ``app``, which will be the
Python module corresponding to an application name given on Python module corresponding to an application name given on
the command line. the command line.
""" """
raise NotImplementedError() raise NotImplementedError()
@ -308,7 +309,7 @@ class LabelCommand(BaseCommand):
If the arguments should be names of installed applications, use If the arguments should be names of installed applications, use
``AppCommand`` instead. ``AppCommand`` instead.
""" """
args = '<label label ...>' args = '<label label ...>'
label = 'label' label = 'label'
@ -328,7 +329,7 @@ class LabelCommand(BaseCommand):
""" """
Perform the command's actions for ``label``, which will be the Perform the command's actions for ``label``, which will be the
string as given on the command line. string as given on the command line.
""" """
raise NotImplementedError() raise NotImplementedError()
@ -341,7 +342,7 @@ class NoArgsCommand(BaseCommand):
no arguments are passed to the command. no arguments are passed to the command.
Attempting to pass arguments will raise ``CommandError``. Attempting to pass arguments will raise ``CommandError``.
""" """
args = '' args = ''
@ -353,7 +354,7 @@ class NoArgsCommand(BaseCommand):
def handle_noargs(self, **options): def handle_noargs(self, **options):
""" """
Perform this command's actions. Perform this command's actions.
""" """
raise NotImplementedError() raise NotImplementedError()
@ -419,7 +420,7 @@ def _make_writeable(filename):
""" """
Make sure that the file is writeable. Useful if our source is Make sure that the file is writeable. Useful if our source is
read-only. read-only.
""" """
import stat import stat
if sys.platform.startswith('java'): if sys.platform.startswith('java'):

View File

@ -38,7 +38,6 @@ def colorize(text='', opts=(), **kwargs):
print colorize('and so should this') print colorize('and so should this')
print 'this should not be red' print 'this should not be red'
""" """
text = str(text)
code_list = [] code_list = []
if text == '' and len(opts) == 1 and opts[0] == 'reset': if text == '' and len(opts) == 1 and opts[0] == 'reset':
return '\x1b[%sm' % RESET return '\x1b[%sm' % RESET