Merge pull request #2413 from sigmavirus24/bug/2411
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Check that filenames are unicode or bytes
authored by Kenneth Reitz · committed by Kenneth Reitz · parents cf3a7f4, eec44db · Browse files · 3 files·+30 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−3 removed
- other:+30 added−3 removed
Provenance
- Commit
3314eb9- Tree
724bf83- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
cf3a7f4- Parent 2
eec44db
3 files changed+30 added−3 removed
modified:requests/compat.py-1 removed
97 unmodified lines98 str = unicode99 basestring = basestring100 numeric_types = (int, long, float)101 102-102 elif is_py3:103 from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag104 from urllib.request import parse_http_list, getproxies, proxy_bypass105 from http import cookiejar as cookielib9 unmodified linesmodified:requests/utils.py+4 added-2 removed
23 unmodified lines24 from . import __version__25 from . import certs26 from .compat import parse_http_list as _parse_list_header27 from .compat import (quote, urlparse, bytes, str, OrderedDict, unquote, is_py2,28- builtin_str, getproxies, proxy_bypass, urlunparse)28+ builtin_str, getproxies, proxy_bypass, urlunparse,29+ basestring)30 from .cookies import RequestsCookieJar, cookiejar_from_dict31 from .structures import CaseInsensitiveDict32 from .exceptions import InvalidURL33 81 unmodified lines115 116 def guess_filename(obj):117 """Tries to guess the filename of the given object."""118 name = getattr(obj, 'name', None)118- if name and isinstance(name, builtin_str) and name[0] != '<' and name[-1] != '>':119+ if (name and isinstance(name, basestring) and name[0] != '<' and120+ name[-1] != '>'):121 return os.path.basename(name)122 123 124 def from_key_val_list(value):575 unmodified linesmodified:test_requests.py+26 added
1263 unmodified lines1264 assert get_environ_proxies(1265 'http://localhost.localdomain:5000/v1.0/') == {}1266 assert get_environ_proxies('http://www.requests.com/') != {}1267 1268+ def test_guess_filename_when_int(self):1269+ from requests.utils import guess_filename1270+ assert None is guess_filename(1)1271+1272+ def test_guess_filename_when_filename_is_an_int(self):1273+ from requests.utils import guess_filename1274+ fake = type('Fake', (object,), {'name': 1})()1275+ assert None is guess_filename(fake)1276+1277+ def test_guess_filename_with_file_like_obj(self):1278+ from requests.utils import guess_filename1279+ from requests import compat1280+ fake = type('Fake', (object,), {'name': b'value'})()1281+ guessed_name = guess_filename(fake)1282+ assert b'value' == guessed_name1283+ assert isinstance(guessed_name, compat.bytes)1284+1285+ def test_guess_filename_with_unicode_name(self):1286+ from requests.utils import guess_filename1287+ from requests import compat1288+ filename = b'value'.decode('utf-8')1289+ fake = type('Fake', (object,), {'name': filename})()1290+ guessed_name = guess_filename(fake)1291+ assert filename == guessed_name1292+ assert isinstance(guessed_name, compat.str)1293+1294 def test_is_ipv4_address(self):1295 from requests.utils import is_ipv4_address1296 assert is_ipv4_address('8.8.8.8')1297 assert not is_ipv4_address('8.8.8.8.8')287 unmodified lines