Skip to content

Commit 24d2650

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 aec1333 commit 24d2650

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
@@ -2497,28 +2497,34 @@ def getproxies_environment():
24972497
this seems to be the standard convention. If you need a
24982498
different way, you can pass a proxies dictionary to the
24992499
[Fancy]URLopener constructor.
2500-
25012500
"""
2502-
proxies = {}
25032501
# in order to prefer lowercase variables, process environment in
25042502
# two passes: first matches any, second pass matches lowercase only
2505-
for name, value in os.environ.items():
2506-
name = name.lower()
2507-
if value and name[-6:] == '_proxy':
2508-
proxies[name[:-6]] = value
2503+
2504+
# select only environment variables which end in (after making lowercase) _proxy
2505+
proxies = {}
2506+
environment = []
2507+
for name in os.environ.keys():
2508+
# fast screen underscore position before more expensive case-folding
2509+
if len(name) > 5 and name[-6] == "_" and name[-5:].lower() == "proxy":
2510+
value = os.environ[name]
2511+
proxy_name = name[:-6].lower()
2512+
environment.append((name, value, proxy_name))
2513+
if value:
2514+
proxies[proxy_name] = value
25092515
# CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
25102516
# (non-all-lowercase) as it may be set from the web server by a "Proxy:"
25112517
# header from the client
25122518
# If "proxy" is lowercase, it will still be used thanks to the next block
25132519
if 'REQUEST_METHOD' in os.environ:
25142520
proxies.pop('http', None)
2515-
for name, value in os.environ.items():
2521+
for name, value, proxy_name in environment:
2522+
# not case-folded, checking here for lower-case env vars only
25162523
if name[-6:] == '_proxy':
2517-
name = name.lower()
25182524
if value:
2519-
proxies[name[:-6]] = value
2525+
proxies[proxy_name] = value
25202526
else:
2521-
proxies.pop(name[:-6], None)
2527+
proxies.pop(proxy_name, None)
25222528
return proxies
25232529

25242530
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)