From baf550f79d69a98c17c1f555b0ad23dc94cba53a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Sun, 18 Dec 2016 16:31:33 -0100 Subject: [PATCH] Warning message added when non-ascci filename used in send_file() --- flask/helpers.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/flask/helpers.py b/flask/helpers.py index c6c2cddc..3da83854 100644 --- a/flask/helpers.py +++ b/flask/helpers.py @@ -504,6 +504,11 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False, a :class:`~datetime.datetime` or timestamp. If a file was passed, this overrides its mtime. """ + import warnings + if type(filename_or_fp) is str: + if not is_ascii(filename_or_fp): + warnings.warn("Non-ASCII filename is being used in send_file", UnicodeWarning) + mtime = None fsize = None if isinstance(filename_or_fp, string_types): @@ -958,3 +963,16 @@ def total_seconds(td): :rtype: int """ return td.days * 60 * 60 * 24 + td.seconds + +def is_ascii(string_to_test): + """Returns if a string object is fully ASCII encoded. + + :param string string_to_test: the string to be evaluated + + :returns: True if string_to_test is fully ASCII encoded, False if not + :rtype: boolean + """ + if all(ord(char) < 128 for char in string_to_test): + return True + else: + return False