[Python-checkins] bpo-31014: Fix the webbrowser module. (GH-7267)

Miss Islington (bot) webhook-mailer at python.org
Sun Jul 8 04:09:30 EDT 2018


https://github.com/python/cpython/commit/a410f9f614b62cd7df220186d081ffd73786be91
commit: a410f9f614b62cd7df220186d081ffd73786be91
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-07-08T01:09:21-07:00
summary:

bpo-31014: Fix the webbrowser module. (GH-7267)


webbrowser._synthesize() called webbrowser.register() with
outdated signature.

Co-Authored-By: John Still <john at jmsdvl.com>
(cherry picked from commit 25b804a9c21c735ce322877f105ebab2539ccfc1)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
A Misc/NEWS.d/next/Library/2018-05-31-06-48-55.bpo-31014.SNY681.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 7a396bdc4114..71f2e27467ee 100644
--- a/Lib/test/test_webbrowser.py
+++ b/Lib/test/test_webbrowser.py
@@ -1,5 +1,7 @@
 import webbrowser
 import unittest
+import os
+import sys
 import subprocess
 from unittest import mock
 from test import support
@@ -290,6 +292,23 @@ def test_get(self):
             webbrowser.get('fakebrowser')
         self.assertIsNotNone(webbrowser._tryorder)
 
+    def test_synthesize(self):
+        webbrowser = support.import_fresh_module('webbrowser')
+        name = os.path.basename(sys.executable).lower()
+        webbrowser.register(name, None, webbrowser.GenericBrowser(name))
+        webbrowser.get(sys.executable)
+
+    def test_environment(self):
+        webbrowser = support.import_fresh_module('webbrowser')
+        try:
+            browser = webbrowser.get().name
+        except (webbrowser.Error, AttributeError) as err:
+            self.skipTest(str(err))
+        with support.EnvironmentVarGuard() as env:
+            env["BROWSER"] = browser
+            webbrowser = support.import_fresh_module('webbrowser')
+            webbrowser.get()
+
 
 if __name__=='__main__':
     unittest.main()
diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py
index d717193d05c2..1e27c83fd947 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, update_tryorder=1):
+def _synthesize(browser, *, preferred=True):
     """Attempt to synthesize a controller base on existing controllers.
 
     This is useful to create a controller when a user specifies a path to
@@ -113,7 +113,7 @@ def _synthesize(browser, update_tryorder=1):
         controller = copy.copy(controller)
         controller.name = browser
         controller.basename = os.path.basename(browser)
-        register(browser, None, controller, update_tryorder)
+        register(browser, None, instance=controller, preferred=preferred)
         return [None, controller]
     return [None, None]
 
@@ -563,7 +563,7 @@ def register_standard_browsers():
         # and prepend to _tryorder
         for cmdline in userchoices:
             if cmdline != '':
-                cmd = _synthesize(cmdline, -1)
+                cmd = _synthesize(cmdline, preferred=False)
                 if cmd[1] is None:
                     register(cmdline, None, GenericBrowser(cmdline), preferred=True)
 
diff --git a/Misc/NEWS.d/next/Library/2018-05-31-06-48-55.bpo-31014.SNY681.rst b/Misc/NEWS.d/next/Library/2018-05-31-06-48-55.bpo-31014.SNY681.rst
new file mode 100644
index 000000000000..bd3c8bbd0f2b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-05-31-06-48-55.bpo-31014.SNY681.rst
@@ -0,0 +1,3 @@
+Fixed creating a controller for :mod:`webbrowser` when a user specifies a
+path to an entry in the BROWSER environment variable.  Based on patch by
+John Still.



More information about the Python-checkins mailing list