https://github.com/python/cpython/commit/2a37f013ec81099a6156160ce66803b2609... commit: 2a37f013ec81099a6156160ce66803b2609bb7f4 branch: 3.7 author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> committer: GitHub <noreply@github.com> date: 2018-11-26T13:49:28-08:00 summary: bpo-35308: Fix regression where BROWSER env var is not respected. (GH-10693) Regression introduced in e3ce695 and 25b804a, where the old parameter update_tryorder to _synthesize was first ignored, then given the opposite value in the attempt to fix bpo-31014. (cherry picked from commit 8c281ed403fd915284d5bba2405d7c47f8195066) Co-authored-by: Zhiming Wang <i@zhimingwang.org> files: A Misc/NEWS.d/next/Library/2018-11-24-10-33-42.bpo-35308.9--2iy.rst M Lib/test/test_webbrowser.py M Lib/webbrowser.py diff --git a/Lib/test/test_webbrowser.py b/Lib/test/test_webbrowser.py index 71f2e27467ee..519a9432abe0 100644 --- a/Lib/test/test_webbrowser.py +++ b/Lib/test/test_webbrowser.py @@ -309,6 +309,24 @@ def test_environment(self): webbrowser = support.import_fresh_module('webbrowser') webbrowser.get() + def test_environment_preferred(self): + webbrowser = support.import_fresh_module('webbrowser') + try: + webbrowser.get() + least_preferred_browser = webbrowser.get(webbrowser._tryorder[-1]).name + except (webbrowser.Error, AttributeError, IndexError) as err: + self.skipTest(str(err)) + + with support.EnvironmentVarGuard() as env: + env["BROWSER"] = least_preferred_browser + webbrowser = support.import_fresh_module('webbrowser') + self.assertEqual(webbrowser.get().name, least_preferred_browser) + + with support.EnvironmentVarGuard() as env: + env["BROWSER"] = sys.executable + webbrowser = support.import_fresh_module('webbrowser') + self.assertEqual(webbrowser.get().name, sys.executable) + if __name__=='__main__': unittest.main() diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py index 1e27c83fd947..82bff835fdd0 100755 --- a/Lib/webbrowser.py +++ b/Lib/webbrowser.py @@ -86,7 +86,7 @@ def open_new_tab(url): return open(url, 2) -def _synthesize(browser, *, preferred=True): +def _synthesize(browser, *, preferred=False): """Attempt to synthesize a controller base on existing controllers. This is useful to create a controller when a user specifies a path to @@ -563,7 +563,7 @@ def register_standard_browsers(): # and prepend to _tryorder for cmdline in userchoices: if cmdline != '': - cmd = _synthesize(cmdline, preferred=False) + cmd = _synthesize(cmdline, preferred=True) if cmd[1] is None: register(cmdline, None, GenericBrowser(cmdline), preferred=True) diff --git a/Misc/NEWS.d/next/Library/2018-11-24-10-33-42.bpo-35308.9--2iy.rst b/Misc/NEWS.d/next/Library/2018-11-24-10-33-42.bpo-35308.9--2iy.rst new file mode 100644 index 000000000000..a33fe2e4812b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-11-24-10-33-42.bpo-35308.9--2iy.rst @@ -0,0 +1,2 @@ +Fix regression in ``webbrowser`` where default browsers may be preferred +over browsers in the ``BROWSER`` environment variable.