Refs #46 - Fixed Python 2 and Python 3 compatibility in doctests related to string/unicode.

This commit is contained in:
Benoît Bryon 2014-02-25 00:47:20 +01:00
parent 546bdc2461
commit 70c6b00442
3 changed files with 23 additions and 31 deletions

View file

@ -13,28 +13,22 @@ from django.utils.encoding import force_str
def encode_basename_ascii(value):
"""Return US-ASCII encoded ``value`` for use in Content-Disposition header.
u"""Return US-ASCII encoded ``value`` for Content-Disposition header.
>>> encode_basename_ascii('éà') # doctest: +IGNORE_UNICODE
u'ea'
>>> print(encode_basename_ascii(u'éà'))
ea
Spaces are converted to underscores.
>>> encode_basename_ascii(' ') # doctest: +IGNORE_UNICODE
u'_'
Text with non US-ASCII characters is expected to be unicode.
>>> from six import b
>>> encode_basename_ascii(b('éà')) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
UnicodeDecodeError: ...
>>> print(encode_basename_ascii(' '))
_
Of course, ASCII values are not modified.
>>> encode_basename_ascii('ea') # doctest: +IGNORE_UNICODE
u'ea'
>>> print(encode_basename_ascii('ea'))
ea
>>> print(encode_basename_ascii(b'ea'))
ea
"""
if isinstance(value, six.binary_type):
@ -48,34 +42,34 @@ def encode_basename_ascii(value):
def encode_basename_utf8(value):
"""Return UTF-8 encoded ``value`` for use in Content-Disposition header.
u"""Return UTF-8 encoded ``value`` for use in Content-Disposition header.
>>> encode_basename_utf8(u' .txt')
'%20.txt'
>>> print(encode_basename_utf8(u' .txt'))
%20.txt
>>> encode_basename_utf8(u'éà')
'%C3%A9%C3%A0'
>>> print(encode_basename_utf8(u'éà'))
%C3%A9%C3%A0
"""
return urllib.parse.quote(force_str(value))
def content_disposition(filename):
"""Return value of ``Content-Disposition`` header with 'attachment'.
u"""Return value of ``Content-Disposition`` header with 'attachment'.
>>> content_disposition('demo.txt')
'attachment; filename=demo.txt'
>>> print(content_disposition('demo.txt'))
attachment; filename=demo.txt
If filename is empty, only "attachment" is returned.
>>> content_disposition('')
'attachment'
>>> print(content_disposition(''))
attachment
If filename contains non US-ASCII characters, the returned value contains
UTF-8 encoded filename and US-ASCII fallback.
>>> content_disposition(u'é.txt')
"attachment; filename=e.txt; filename*=UTF-8''%C3%A9.txt"
>>> print(content_disposition(u'é.txt'))
attachment; filename=e.txt; filename*=UTF-8''%C3%A9.txt
"""
if not filename:

View file

@ -26,8 +26,8 @@ def url_basename(url, content_type):
If URL contains extension, it is kept as-is.
>>> url_basename(u'/path/to/somefile.rst', 'text/plain') # doctest: +IGNORE_UNICODE
u'somefile.rst'
>>> print(url_basename(u'/path/to/somefile.rst', 'text/plain'))
somefile.rst
"""
return url.split('/')[-1]

View file

@ -6,7 +6,6 @@ deps =
nose
rednose
coverage
doctest-ignore-unicode
commands =
pip install -e ./
pip install -e demo/
@ -29,7 +28,6 @@ deps =
nose
rednose
Sphinx
doctest-ignore-unicode
commands =
make --directory=docs clean html doctest
whitelist_externals =