Fixes error swallowing in set_environ
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by mlcrazy · committed by mlcrazy · parent b6ee4b2 · Browse files · 3 files·+36 added−9 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+36 added−9 removed
- tests:+28 added−2 removed
- other:+8 added−7 removed
Provenance
- Commit
1d7fd6c- Tree
5e01726- Author
mlcrazy <liumatt@gmail.com>- Committer
mlcrazy <liumatt@gmail.com>- Parent
b6ee4b2
3 files changed+36 added−9 removed
modified:requests/utils.py+7 added-7 removed
610 unmodified lines611 Save previous value, yield, and then restore the previous value stored in612 the environment variable 'env_name'.613 614 If 'value' is None, do nothing"""615- if value is not None:615+ value_changed = value is not None616+ if value_changed:617 old_value = os.environ.get(env_name)618 os.environ[env_name] = value619 try:620 yield621 finally:621- if value is None:622- return623- if old_value is None:624- del os.environ[env_name]625- else:626- os.environ[env_name] = old_value622+ if value_changed:623+ if old_value is None:624+ del os.environ[env_name]625+ else:626+ os.environ[env_name] = old_value627 628 629 def should_bypass_proxies(url, no_proxy):630 """274 unmodified linesmodified:tests/test_utils.py+28 added-2 removed
1 # -*- coding: utf-8 -*-2 3 import os4+import copy5 from io import BytesIO6 7 import pytest8 from requests import compat8 unmodified lines17 parse_header_links, prepend_scheme_if_needed,18 requote_uri, select_proxy, should_bypass_proxies, super_len,19 to_key_val_list, to_native_string,20 unquote_header_value, unquote_unreserved,20- urldefragauth, add_dict_to_cookiejar)21+ urldefragauth, add_dict_to_cookiejar, set_environ)22 from requests._internal_utils import unicode_is_ascii23 24 from .compat import StringIO, cStringIO25 625 unmodified lines651 monkeypatch.setenv('no_proxy', '')652 monkeypatch.setenv('NO_PROXY', '')653 monkeypatch.setattr(winreg, 'OpenKey', OpenKey)654 monkeypatch.setattr(winreg, 'QueryValueEx', QueryValueEx)654- assert should_bypass_proxies(url, no_proxy=None) == expected655+656+657+@pytest.mark.parametrize(658+ 'env_name, value', (659+ ('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain'),660+ ('no_proxy', None),661+ ('a_new_key', '192.168.0.0/24,127.0.0.1,localhost.localdomain'),662+ ('a_new_key', None),663+ ))664+def test_set_environ(env_name, value):665+ """Tests set_environ will set environ values and will restore the environ."""666+ environ_copy = copy.deepcopy(os.environ)667+ with set_environ(env_name, value):668+ assert os.environ.get(env_name) == value669+670+ assert os.environ == environ_copy671+672+673+def test_set_environ_raises_exception():674+ """Tests set_environ will raise exceptions in context when the675+ value parameter is None."""676+ with pytest.raises(Exception) as exception:677+ with set_environ('test1', None):678+ raise Exception('Expected exception')679+680+ assert 'Expected exception' in str(exception.value)modified:AUTHORS.rst+1 added
181 unmodified lines182 - David Fontenot (`@davidfontenot <https://github.com/davidfontenot>`_)183 - Shmuel Amar (`@shmuelamar <https://github.com/shmuelamar>`_)184 - Gary Wu (`@garywu <https://github.com/garywu>`_)185 - Ryan Pineo (`@ryanpineo <https://github.com/ryanpineo>`_)186+- Matt Liu <liumatt@gmail.com> (`@mlcrazy <https://github.com/mlcrazy>`_)