carsonyl / pypac

Find and use proxy auto-config (PAC) files with Python and Requests.
https://pypac.readthedocs.io
Apache License 2.0
71 stars 18 forks source link

"TypeError: putenv() argument 2 must be string, not None" Error when using pac_context_for_url #27

Closed zephyrj closed 5 years ago

zephyrj commented 5 years ago

pypac version: 0.9.0 (retrieved from pip) Python version: Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32

When the configured pac file returns "DIRECT" for a URL then the function will fail with the following error:

  File "file.py", line 27, in get_job_info
    with pac_context_for_url('http://' + self.host):
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\pypac\api.py", line 305, in pac_context_for_url
    os.environ['HTTP_PROXY'] = proxies.get('http')
  File "C:\Python27\lib\os.py", line 422, in __setitem__
    putenv(key, item)
TypeError: putenv() argument 2 must be string, not None

In this case the pac file will return "DIRECT" for the url - the code gets to line 303 proxies = resolver.get_proxy_for_requests(url) which will result in the code going to pypac/resolver.py:133 and the function proxy_parameter_for_requests("DIRECT") will be called. This will then set proxy_url_or_direct to None which will be used for the returns values in the dictionary for 'http' and 'https'. These value of None are used in

os.environ['HTTP_PROXY'] = proxies.get('http')
os.environ['HTTPS_PROXY'] = proxies.get('https')

Resulting in the error putenv() argument 2 must be string, not None

zephyrj commented 5 years ago

I'm no python expert but I have got around the issue for now by doing:

if not (proxies.get('http') == None):
    os.environ['HTTP_PROXY'] = proxies.get('http')
if not (proxies.get('https') == None):
    os.environ['HTTPS_PROXY'] = proxies.get('https')
carsonyl commented 5 years ago

Thanks for reporting this issue!