Skip to content
Myr

Check that a filename is a basestring instance

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Instead of only checking one or another type of string-like object that we accept, let's be able to check both. Previously, we only checked if the filename was an instance of the native str type which on Python 2 excluded unicode filenames and bytes-like filenames on Python 3. Fixes #2411

authored by Ian Cordasco · committed by Ian Cordasco · parent d2d576b · 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

3 files+30 added−3 removed

modified:requests/compat.py-1 removed
97 unmodified lines
98 str = unicode99 basestring = basestring100 numeric_types = (int, long, float)101102102elif 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 cookielib
9 unmodified lines
modified:requests/utils.py+4 added-2 removed
23 unmodified lines
24from . import __version__25from . import certs26from .compat import parse_http_list as _parse_list_header27from .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)30from .cookies import RequestsCookieJar, cookiejar_from_dict31from .structures import CaseInsensitiveDict32from .exceptions import InvalidURL33
81 unmodified lines
115116def 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)122123124def from_key_val_list(value):
575 unmodified lines
modified:test_requests.py+26 added
1263 unmodified lines
1264 assert get_environ_proxies(1265 'http://localhost.localdomain:5000/v1.0/') == {}1266 assert get_environ_proxies('http://www.requests.com/') != {}12671268 def test_guess_filename_when_int(self):1269 from requests.utils import guess_filename1270 assert None is guess_filename(1)12711272 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)12761277 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)12841285 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)12931294 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