Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

use sys.getfilesystemencoding() to join paths if needed #463

Closed
wants to merge 1 commit into from

4 participants

@dgerber

This allows to serve an application from a path containing non-ascii characters (see #149 ).

@mitsuhiko
Owner

Generally: yes. However there are some more things that need to be considered. I guess I will add some helpers for that to Werkzeug and integrate it into Flask then.

@kennethreitz
Collaborator

Closing for now.

@untitaker
Collaborator

@mitsuhiko Can you elaborate on the "things that need to be considered"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2012
  1. @dgerber
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 4 deletions.
  1. +14 −4 flask/helpers.py
View
18 flask/helpers.py
@@ -466,6 +466,16 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
return rv
+def join_paths(*paths):
+ try:
+ return os.path.join(*paths)
+ except UnicodeDecodeError:
+ encoding = sys.getfilesystemencoding()
+ paths = (x if isinstance(x, unicode) else unicode(x, encoding)
+ for x in paths)
+ return os.path.join(*paths)
+
+
def safe_join(directory, filename):
"""Safely join `directory` and `filename`.
@@ -488,7 +498,7 @@ def wiki_page(filename):
raise NotFound()
if os.path.isabs(filename) or filename.startswith('../'):
raise NotFound()
- return os.path.join(directory, filename)
+ return join_paths(directory, filename)
def send_from_directory(directory, filename, **options):
@@ -642,7 +652,7 @@ def __init__(self, import_name, template_folder=None):
def _get_static_folder(self):
if self._static_folder is not None:
- return os.path.join(self.root_path, self._static_folder)
+ return join_paths(self.root_path, self._static_folder)
def _set_static_folder(self, value):
self._static_folder = value
static_folder = property(_get_static_folder, _set_static_folder)
@@ -675,7 +685,7 @@ def jinja_loader(self):
.. versionadded:: 0.5
"""
if self.template_folder is not None:
- return FileSystemLoader(os.path.join(self.root_path,
+ return FileSystemLoader(join_paths(self.root_path,
self.template_folder))
def get_send_file_options(self, filename):
@@ -732,4 +742,4 @@ def open_resource(self, resource, mode='rb'):
"""
if mode not in ('r', 'rb'):
raise ValueError('Resources can only be opened for reading')
- return open(os.path.join(self.root_path, resource), mode)
+ return open(join_paths(self.root_path, resource), mode)
Something went wrong with that request. Please try again.