Skip to content

Commit aeb28f5

Browse files
eendebakptblurb-it[bot]carljm
authored
gh-91539: improve performance of get_proxies_environment (#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 <carl@oddbird.net> * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <carl@oddbird.net> * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <carl@oddbird.net> * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <carl@oddbird.net> * whitespace * Update Misc/NEWS.d/next/Library/2022-04-15-11-29-38.gh-issue-91539.7WgVuA.rst Co-authored-by: Carl Meyer <carl@oddbird.net> * Update Lib/urllib/request.py Co-authored-by: Carl Meyer <carl@oddbird.net> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Carl Meyer <carl@oddbird.net>
1 parent 9442105 commit aeb28f5

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

β€ŽLib/urllib/request.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -2508,28 +2508,34 @@ def getproxies_environment():
25082508
this seems to be the standard convention. If you need a
25092509
different way, you can pass a proxies dictionary to the
25102510
[Fancy]URLopener constructor.
2511-
25122511
"""
2513-
proxies = {}
25142512
# in order to prefer lowercase variables, process environment in
25152513
# two passes: first matches any, second pass matches lowercase only
2516-
for name, value in os.environ.items():
2517-
name = name.lower()
2518-
if value and name[-6:] == '_proxy':
2519-
proxies[name[:-6]] = value
2514+
2515+
# select only environment variables which end in (after making lowercase) _proxy
2516+
proxies = {}
2517+
environment = []
2518+
for name in os.environ.keys():
2519+
# fast screen underscore position before more expensive case-folding
2520+
if len(name) > 5 and name[-6] == "_" and name[-5:].lower() == "proxy":
2521+
value = os.environ[name]
2522+
proxy_name = name[:-6].lower()
2523+
environment.append((name, value, proxy_name))
2524+
if value:
2525+
proxies[proxy_name] = value
25202526
# CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
25212527
# (non-all-lowercase) as it may be set from the web server by a "Proxy:"
25222528
# header from the client
25232529
# If "proxy" is lowercase, it will still be used thanks to the next block
25242530
if 'REQUEST_METHOD' in os.environ:
25252531
proxies.pop('http', None)
2526-
for name, value in os.environ.items():
2532+
for name, value, proxy_name in environment:
2533+
# not case-folded, checking here for lower-case env vars only
25272534
if name[-6:] == '_proxy':
2528-
name = name.lower()
25292535
if value:
2530-
proxies[name[:-6]] = value
2536+
proxies[proxy_name] = value
25312537
else:
2532-
proxies.pop(name[:-6], None)
2538+
proxies.pop(proxy_name, None)
25332539
return proxies
25342540

25352541
def proxy_bypass_environment(host, proxies=None):
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)