Skip to content
Myr

Merge pull request #4134 from mlcrazy/master

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

Raise exceptions in utils.set_environ

authored by Cory Benfield · committed by GitHub · parents b3d138e, 613f61d · Browse files · 4 files+37 added−10 removed
This commit

State

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

Diff stats

4 files+37 added−10 removed

  • tests:+28 added−2 removed
  • other:+9 added−8 removed

4 files+37 added−10 removed

modified:requests/utils.py+7 added-7 removed
610 unmodified lines
611 Save previous value, yield, and then restore the previous value stored in612 the environment variable 'env_name'.613614 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_value627628629def should_bypass_proxies(url, no_proxy):630 """
274 unmodified lines
modified:tests/test_utils.py+28 added-2 removed
1# -*- coding: utf-8 -*-23import os4import copy5from io import BytesIO67import pytest8from requests import compat
8 unmodified lines
17 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)22from requests._internal_utils import unicode_is_ascii2324from .compat import StringIO, cStringIO25
625 unmodified lines
651 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) == expected655656657@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 ))664def 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) == value669670 assert os.environ == environ_copy671672673def 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')679680 assert 'Expected exception' in str(exception.value)
modified:AUTHORS.rst+1 added
182 unmodified lines
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- Ed Morley (`@edmorley <https://github.com/edmorley>`_)187- Matt Liu <liumatt@gmail.com> (`@mlcrazy <https://github.com/mlcrazy>`_)
modified:HISTORY.rst+1 added-1 removed
13 unmodified lines
14**Bugfixes**1516- Resolve installation failure if multiprocessing is not available17- Resolve tests crash if multiprocessing is not able to determine the number of CPU cores18- Resolve error swallowing in utils set_environ generator1920212.17.3 (2017-05-29)22+++++++++++++++++++
1458 unmodified lines
1481++++++++++++++++++14821483* Frustration1484* Conception1484