Merge branch 'develop' into key_val_lists
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Conflicts: requests/models.py
authored by Ian Cordasco · committed by Ian Cordasco · parents 1722f28, a94fcb8 · Browse files · 8 files·+74 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Conflicts: requests/models.py
Diff stats
8 files+74 added−18 removed
- tests:+30 added−4 removed
- other:+44 added−14 removed
Provenance
- Commit
ceec572- Tree
1100fa0- Author
Ian Cordasco <graffatcolmingov@gmail.com>- Committer
Ian Cordasco <graffatcolmingov@gmail.com>- Parent 1
1722f28- Parent 2
a94fcb8
8 files changed+74 added−18 removed
modified:requests/__init__.py+2 added-2 removed
13 unmodified lines14 15 """16 17 __title__ = 'requests'18-__version__ = '0.13.5'19-__build__ = 0x00130518+__version__ = '0.13.6'19+__build__ = 0x00130620 __author__ = 'Kenneth Reitz'21 __license__ = 'ISC'22 __copyright__ = 'Copyright 2012 Kenneth Reitz'23 10 unmodified linesmodified:requests/api.py+12 added-3 removed
36 unmodified lines37 :param prefetch: (optional) if ``True``, the response content will be immediately downloaded.38 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.39 """40 41- s = kwargs.pop('session') if 'session' in kwargs else sessions.session()42- return s.request(method=method, url=url, **kwargs)41+ # if this session was passed in, leave it open (and retain pooled connections);42+ # if we're making it just for this call, then close it when we're done.43+ adhoc_session = False44+ session = kwargs.pop('session', None)45+ if session is None:46+ session = sessions.session()47+ adhoc_session = True48 49+ try:50+ return session.request(method=method, url=url, **kwargs)51+ finally:52+ if adhoc_session:53+ session.close()54 45-55 def get(url, **kwargs):56 """Sends a GET request. Returns :class:`Response` object.57 58 :param url: URL for the new :class:`Request` object.70 unmodified linesmodified:requests/compat.py+2 added
93 unmodified lines94 builtin_str = str95 bytes = str96 str = unicode97 basestring = basestring98+ numeric_types = (int, long, float)99 100 101 102 elif is_py3:7 unmodified lines110 builtin_str = str111 str = str112 bytes = bytes113 basestring = (str,bytes)114+ numeric_types = (int, float)115 modified:requests/models.py+5 added-4 removed
30 unmodified lines31 stream_decode_response_unicode, get_netrc_auth, get_environ_proxies,32 to_key_val_list, DEFAULT_CA_BUNDLE_PATH)33 from .compat import (34 cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes,35- StringIO, is_py2, chardet, json, builtin_str)35+ StringIO, is_py2, chardet, json, builtin_str, numeric_types)36 37 REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)38 CONTENT_CHUNK_SIZE = 10 * 102439 17 unmodified lines57 allow_redirects=False,58 proxies=None,59 hooks=None,60 config=None,61- prefetch=False,61+ prefetch=True,62 _poolmanager=None,63 verify=None,64 session=None,65 cert=None):289 unmodified lines355 new_fields.append((field, newvalue))356 else:357 new_fields.append((field, val))358 fields = new_fields359-359 (body, content_type) = encode_multipart_formdata(fields)360 361 return (body, content_type)362 88 unmodified lines451 return True452 except ValueError:453 return False454 456- def send(self, anyway=False, prefetch=False):455+ def send(self, anyway=False, prefetch=True):456 """Sends the request. Returns True if successful, False if not.457 If there was an HTTPError during transmission,458 self.response.status_code will contain the HTTPError code.459 307 unmodified lines767 except AttributeError:768 self._content = None769 770 self._content_consumed = True771+ # don't need to release the connection; that's been handled by urllib3772+ # since we exhausted the data.773 return self._content774 775 @property776 def text(self):63 unmodified linesmodified:requests/sessions.py+13 added-5 removed
66 unmodified lines67 proxies=None,68 hooks=None,69 params=None,70 config=None,71- prefetch=False,71+ prefetch=True,72 verify=True,73 cert=None):74 75 self.headers = to_key_val_list(headers or [])30 unmodified lines106 def __enter__(self):107 return self108 109 def __exit__(self, *args):110- pass110+ self.close()111 112+ def close(self):113+ """Dispose of any internal state.114+115+ Currently, this just closes the PoolManager, which closes pooled116+ connections.117+ """118+ self.poolmanager.clear()119+120 def request(self, method, url,121 params=None,122 data=None,123 headers=None,5 unmodified lines129 proxies=None,130 hooks=None,131 return_response=True,132 config=None,125- prefetch=False,133+ prefetch=None,134 verify=None,135 cert=None):136 137 """Constructs and sends a :class:`Request <Request>`.11 unmodified lines149 :param allow_redirects: (optional) Boolean. Set to True by default.150 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.151 :param return_response: (optional) If False, an un-sent Request object will returned.152 :param config: (optional) A configuration dictionary. See ``request.defaults`` for allowed keys and their default values.145- :param prefetch: (optional) if ``True``, the response content will be immediately downloaded.153+ :param prefetch: (optional) whether to immediately download the response content. Defaults to ``True``.154 :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.155 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.156 """157 4 unmodified lines162 files = [] if files is None else files163 headers = [] if headers is None else headers164 params = [] if params is None else params165 hooks = {} if hooks is None else hooks158- prefetch = self.prefetch or prefetch166+ prefetch = prefetch if prefetch is not None else self.prefetch167 168 # use session's hooks as defaults169 for key, cb in list(self.hooks.items()):170 hooks.setdefault(key, cb)166 unmodified linesadded:tests/informal/test_leaked_connections.py+26 added
1+"""2+This is an informal test originally written by Bluehorn;3+it verifies that Requests does not leak connections when4+the body of the request is not read.5+"""6+7+import gc, os, subprocess, requests, sys8+9+def main():10+ gc.disable()11+12+ for x in range(20):13+ requests.head("http://www.google.com/")14+15+ print("Open sockets after 20 head requests:")16+ pid = os.getpid()17+ subprocess.call("lsof -p%d -a -iTCP" % (pid,), shell=True)18+19+ gcresult = gc.collect()20+ print("Garbage collection result: %s" % (gcresult,))21+22+ print("Open sockets after garbage collection:")23+ subprocess.call("lsof -p%d -a -iTCP" % (pid,), shell=True)24+25+if __name__ == '__main__':26+ sys.exit(main())modified:tests/test_requests.py+4 added-4 removed
850 unmodified lines851 c = json.loads(r.text).get('cookies')852 assert 'k' in c853 854 ds1 = pickle.loads(pickle.dumps(requests.session()))855- ds2 = pickle.loads(pickle.dumps(requests.session(prefetch=True)))856- assert not ds1.prefetch857- assert ds2.prefetch855+ ds2 = pickle.loads(pickle.dumps(requests.session(prefetch=False)))856+ assert ds1.prefetch857+ assert not ds2.prefetch858 859 # def test_invalid_content(self):860 # # WARNING: if you're using a terrible DNS provider (comcast),861 # # this will fail.40 unmodified lines902 '''\tUntent his person and share the air with us?'''903 )904 905 # Make a request and monkey-patch its contents906- r = get(httpbin('get'))906+ r = get(httpbin('get'), prefetch=False)907 r.raw = StringIO(quote)908 909 lines = list(r.iter_lines())910 len_lines = len(lines)130 unmodified linesmodified:HISTORY.rst+10 added
1 unmodified line2 3 History4 -------5 6+0.13.6 (2012-08-06)7++++++++++++++++++++8+9+- Long awaited fix for hanging connections!10+11+0.13.5 (2012-07-27)12++++++++++++++++++++13+14+- Packaging fix15+16 0.13.4 (2012-07-27)17 +++++++++++++++++++18 19 - GSSAPI/Kerberos authentication!502 unmodified lines