Skip to content

Commit cbc69af

Browse files
eendebakptmiss-islington
authored andcommitted
pythongh-91539: improve performance of get_proxies_environment (pythonGH-91566)
* improve performance of get_proxies_environment when there are many environment variables * πŸ“œπŸ€– Added by blurb_it. * fix case of short env name * fix formatting * fix whitespace * whitespace * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <[email protected]> * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <[email protected]> * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <[email protected]> * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <[email protected]> * whitespace * Update Misc/NEWS.d/next/Library/2022-04-15-11-29-38.gh-issue-91539.7WgVuA.rst Co-authored-by: Carl Meyer <[email protected]> * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Carl Meyer <[email protected]> (cherry picked from commit aeb28f5) Co-authored-by: Pieter Eendebak <[email protected]>
1 parent de0a656 commit cbc69af

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

β€ŽLib/urllib/request.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,28 +2492,34 @@ def getproxies_environment():
24922492
this seems to be the standard convention. If you need a
24932493
different way, you can pass a proxies dictionary to the
24942494
[Fancy]URLopener constructor.
2495-
24962495
"""
2497-
proxies = {}
24982496
# in order to prefer lowercase variables, process environment in
24992497
# two passes: first matches any, second pass matches lowercase only
2500-
for name, value in os.environ.items():
2501-
name = name.lower()
2502-
if value and name[-6:] == '_proxy':
2503-
proxies[name[:-6]] = value
2498+
2499+
# select only environment variables which end in (after making lowercase) _proxy
2500+
proxies = {}
2501+
environment = []
2502+
for name in os.environ.keys():
2503+
# fast screen underscore position before more expensive case-folding
2504+
if len(name) > 5 and name[-6] == "_" and name[-5:].lower() == "proxy":
2505+
value = os.environ[name]
2506+
proxy_name = name[:-6].lower()
2507+
environment.append((name, value, proxy_name))
2508+
if value:
2509+
proxies[proxy_name] = value
25042510
# CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
25052511
# (non-all-lowercase) as it may be set from the web server by a "Proxy:"
25062512
# header from the client
25072513
# If "proxy" is lowercase, it will still be used thanks to the next block
25082514
if 'REQUEST_METHOD' in os.environ:
25092515
proxies.pop('http', None)
2510-
for name, value in os.environ.items():
2516+
for name, value, proxy_name in environment:
2517+
# not case-folded, checking here for lower-case env vars only
25112518
if name[-6:] == '_proxy':
2512-
name = name.lower()
25132519
if value:
2514-
proxies[name[:-6]] = value
2520+
proxies[proxy_name] = value
25152521
else:
2516-
proxies.pop(name[:-6], None)
2522+
proxies.pop(proxy_name, None)
25172523
return proxies
25182524

25192525
def proxy_bypass_environment(host, proxies=None):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performance of ``urllib.request.getproxies_environment`` when there are many environment variables

0 commit comments

Comments
Β (0)