From 775cde0914265d7dab2bc2501ed7abe6b85c4bae Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Mon, 20 Nov 2017 09:16:35 +0000 Subject: [PATCH 1/6] Clarify that Response.ok will *only* return True/False --- requests/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index 4041cac3..ce4e284e 100644 --- a/requests/models.py +++ b/requests/models.py @@ -686,11 +686,11 @@ class Response(object): @property def ok(self): - """Returns True if :attr:`status_code` is less than 400. + """Returns True if :attr:`status_code` is less than 400, False if not. This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If - the status code, is between 200 and 400, this will return True. This + the status code is between 200 and 400, this will return True. This is **not** a check to see if the response code is ``200 OK``. """ try: From acd2645444d280ac0c5d2d227fdd222cb1ac609c Mon Sep 17 00:00:00 2001 From: Mingyuan Xia Date: Tue, 21 Nov 2017 04:01:04 +0800 Subject: [PATCH 2/6] #4373, fix possible winreg value type difference (#4377) * #4373, fix possible winreg value type difference * add a test for ProxyOverride and ProxyEnable on win32 * add tests for winreg key ProxyEnable with two possible types * fixing AppVeyor failures --- requests/utils.py | 6 ++++-- tests/test_utils.py | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/requests/utils.py b/requests/utils.py index 1cba5a93..42daa2d7 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -52,8 +52,10 @@ if sys.platform == 'win32': try: internetSettings = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings') - proxyEnable = winreg.QueryValueEx(internetSettings, - 'ProxyEnable')[0] + # ProxyEnable could be REG_SZ or REG_DWORD, normalizing it + proxyEnable = int(winreg.QueryValueEx(internetSettings, + 'ProxyEnable')[0]) + # ProxyOverride is almost always a string proxyOverride = winreg.QueryValueEx(internetSettings, 'ProxyOverride')[0] except OSError: diff --git a/tests/test_utils.py b/tests/test_utils.py index 2292a8f0..2dd16923 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -5,6 +5,7 @@ import copy import filecmp from io import BytesIO import zipfile +from collections import deque import pytest from requests import compat @@ -666,6 +667,7 @@ def test_should_bypass_proxies_win_registry(url, expected, override, pass ie_settings = RegHandle() + proxyEnableValues = deque([1, "1"]) def OpenKey(key, subkey): return ie_settings @@ -673,7 +675,9 @@ def test_should_bypass_proxies_win_registry(url, expected, override, def QueryValueEx(key, value_name): if key is ie_settings: if value_name == 'ProxyEnable': - return [1] + # this could be a string (REG_SZ) or a 32-bit number (REG_DWORD) + proxyEnableValues.rotate() + return [proxyEnableValues[0]] elif value_name == 'ProxyOverride': return [override] @@ -684,6 +688,7 @@ def test_should_bypass_proxies_win_registry(url, expected, override, monkeypatch.setenv('NO_PROXY', '') monkeypatch.setattr(winreg, 'OpenKey', OpenKey) monkeypatch.setattr(winreg, 'QueryValueEx', QueryValueEx) + assert should_bypass_proxies(url, None) == expected @pytest.mark.parametrize( From 39446def395672b96642ad9b317fab09ee771be1 Mon Sep 17 00:00:00 2001 From: Daniel Roseman Date: Fri, 3 Nov 2017 13:53:08 +0000 Subject: [PATCH 3/6] Clarify behaviour of `json` parameter. `json` is ignored if `data` or `files` is not empty. --- docs/user/quickstart.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 6829d592..1a2c6fbf 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -281,6 +281,7 @@ the ``json`` parameter (added in version 2.4.2) and it will be encoded automatic >>> r = requests.post(url, json=payload) +Note, the ``json`` parameter is ignored if either ``data`` or ``files`` is passed. POST a Multipart-Encoded File ----------------------------- From 19919b44c4af95f125704c902acecdf83d70a3e4 Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Tue, 21 Nov 2017 12:39:22 -0500 Subject: [PATCH 4/6] Add documentation for available status codes There was no way to determine what actual names were available outside of looking at the source code. They were not listed in the documentation or accessible through the interactive help. In addition, doing `pydoc requests.status_codes` displayed some pretty unhelpful information - the utf-8 encoding string was included in the module name, there was no description, and internal variables used for initialisation leaked into the module scope: DATA code = 511 codes = title = 'network_authentication' titles = ('network_authentication_required', 'network_auth', ... This change prevents the internal variables from leaking, adds a docstring (which has the side-effect of correcting the module name), and appends information on the allowed status code names to the docstring when the module is initialised. The improved module documentation is then used in the API documentation to provide another easy reference to the complete list of status codes. --- docs/api.rst | 12 +----------- requests/status_codes.py | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index ed61bb38..c3e00e54 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -109,17 +109,7 @@ Status Code Lookup .. autoclass:: requests.codes -:: - - >>> requests.codes['temporary_redirect'] - 307 - - >>> requests.codes.teapot - 418 - - >>> requests.codes['\o/'] - 200 - +.. automodule:: requests.status_codes Migrating to 1.x diff --git a/requests/status_codes.py b/requests/status_codes.py index dee89190..96b86ddb 100644 --- a/requests/status_codes.py +++ b/requests/status_codes.py @@ -1,5 +1,22 @@ # -*- coding: utf-8 -*- +""" +The ``codes`` object defines a mapping from common names for HTTP statuses +to their numerical codes, accessible either as attributes or as dictionary +items. + +>>> requests.codes['temporary_redirect'] +307 +>>> requests.codes.teapot +418 +>>> requests.codes['\o/'] +200 + +Some codes have multiple names, and both upper- and lower-case versions of +the names are allowed. For example, ``codes.ok``, ``codes.OK``, and +``codes.okay`` all correspond to the HTTP status code 200. +""" + from .structures import LookupDict _codes = { @@ -84,8 +101,19 @@ _codes = { codes = LookupDict(name='status_codes') -for code, titles in _codes.items(): - for title in titles: - setattr(codes, title, code) - if not title.startswith(('\\', '/')): - setattr(codes, title.upper(), code) +def _init(): + for code, titles in _codes.items(): + for title in titles: + setattr(codes, title, code) + if not title.startswith(('\\', '/')): + setattr(codes, title.upper(), code) + + def doc(code): + names = ', '.join('``%s``' % n for n in _codes[code]) + return '* %d: %s' % (code, names) + + global __doc__ + __doc__ = (__doc__ + '\n' + + '\n'.join(doc(code) for code in sorted(_codes))) + +_init() From 714c9dc96759942226a0dc16fa1e3d6d51f56291 Mon Sep 17 00:00:00 2001 From: Anton Fedchin Date: Fri, 10 Nov 2017 10:32:41 +0300 Subject: [PATCH 5/6] utils: winreg module may not exist like on windows universal platform. --- requests/utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/requests/utils.py b/requests/utils.py index 42daa2d7..f9565287 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -45,10 +45,14 @@ if sys.platform == 'win32': # provide a proxy_bypass version on Windows without DNS lookups def proxy_bypass_registry(host): - if is_py3: - import winreg - else: - import _winreg as winreg + try: + if is_py3: + import winreg + else: + import _winreg as winreg + except ImportError: + return False + try: internetSettings = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings') From 351ec982bb38a069a6b236b8d77dc09f26c2f444 Mon Sep 17 00:00:00 2001 From: Anton Fedchin Date: Sat, 25 Nov 2017 12:07:41 +0300 Subject: [PATCH 6/6] update changelog --- HISTORY.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.rst b/HISTORY.rst index 79202df6..b099ecdb 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -16,6 +16,7 @@ dev - Parsing empty ``Link`` headers with ``parse_header_links()`` no longer return one bogus entry - Fixed issue where loading the default certificate bundle from a zip archive would raise an ``IOError`` +- Fixed issue with unexpected ``ImportError`` on windows system which do not support ``winreg`` module 2.18.4 (2017-08-15)