Skip to content

Commit

Permalink
unicode: Added FILE_CHARSET setting and use it to decode files read f…
Browse files Browse the repository at this point in the history
…rom disk.

Based on a patch from Ivan Sagalaev. Fixed #4021.


git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5058 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Apr 22, 2007
1 parent 9470a6c commit 03b46fc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions django/conf/global_settings.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
DEFAULT_CONTENT_TYPE = 'text/html' DEFAULT_CONTENT_TYPE = 'text/html'
DEFAULT_CHARSET = 'utf-8' DEFAULT_CHARSET = 'utf-8'


# Encoding of files read from disk (template and initial SQL files).
FILE_CHARSET = 'utf-8'

# E-mail address that error messages come from. # E-mail address that error messages come from.
SERVER_EMAIL = 'root@localhost' SERVER_EMAIL = 'root@localhost'


Expand Down
6 changes: 3 additions & 3 deletions django/core/management.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -379,11 +379,11 @@ def get_custom_sql_for_model(model):
for sql_file in sql_files: for sql_file in sql_files:
if os.path.exists(sql_file): if os.path.exists(sql_file):
fp = open(sql_file, 'U') fp = open(sql_file, 'U')
for statement in statements.split(fp.read()): for statement in statements.split(fp.read().decode(settings.FILE_CHARSET)):
# Remove any comments from the file # Remove any comments from the file
statement = re.sub(r"--.*[\n\Z]", "", statement) statement = re.sub(ur"--.*[\n\Z]", "", statement)
if statement.strip(): if statement.strip():
output.append(statement + ";") output.append(statement + u";")
fp.close() fp.close()


return output return output
Expand Down
2 changes: 1 addition & 1 deletion django/template/loaders/app_directories.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_template_sources(template_name, template_dirs=None):
def load_template_source(template_name, template_dirs=None): def load_template_source(template_name, template_dirs=None):
for filepath in get_template_sources(template_name, template_dirs): for filepath in get_template_sources(template_name, template_dirs):
try: try:
return (open(filepath).read(), filepath) return (open(filepath).read().decode(settings.FILE_CHARSET), filepath)
except IOError: except IOError:
pass pass
raise TemplateDoesNotExist, template_name raise TemplateDoesNotExist, template_name
Expand Down
2 changes: 1 addition & 1 deletion django/template/loaders/eggs.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def load_template_source(template_name, template_dirs=None):
pkg_name = 'templates/' + template_name pkg_name = 'templates/' + template_name
for app in settings.INSTALLED_APPS: for app in settings.INSTALLED_APPS:
try: try:
return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name)) return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name)).decode(settings.FILE_CHARSET)
except: except:
pass pass
raise TemplateDoesNotExist, template_name raise TemplateDoesNotExist, template_name
Expand Down
2 changes: 1 addition & 1 deletion django/template/loaders/filesystem.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def load_template_source(template_name, template_dirs=None):
tried = [] tried = []
for filepath in get_template_sources(template_name, template_dirs): for filepath in get_template_sources(template_name, template_dirs):
try: try:
return (open(filepath).read(), filepath) return (open(filepath).read().decode(settings.FILE_CHARSET), filepath)
except IOError: except IOError:
tried.append(filepath) tried.append(filepath)
if tried: if tried:
Expand Down
8 changes: 8 additions & 0 deletions docs/settings.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@ Subject-line prefix for e-mail messages sent with ``django.core.mail.mail_admins
or ``django.core.mail.mail_managers``. You'll probably want to include the or ``django.core.mail.mail_managers``. You'll probably want to include the
trailing space. trailing space.


FILE_CHARSET
------------

Default: ``'utf-8'``

The character encoding used to decode any files read from disk. This includes
template files and initial SQL data files.

FIXTURE_DIRS FIXTURE_DIRS
------------- -------------


Expand Down

0 comments on commit 03b46fc

Please sign in to comment.