Merge pull request #589 from michael-lazar/fix_webbrowser_register_py37
Applied a patch to fix the webbrowser module for python 3.7
This commit is contained in:
@@ -27,12 +27,31 @@ _logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
def patch_webbrowser():
|
def patch_webbrowser():
|
||||||
"""
|
"""
|
||||||
Patch webbrowser on macOS to support setting BROWSER=firefox,
|
Some custom patches on top of the python webbrowser module to fix
|
||||||
BROWSER=chrome, etc..
|
user reported bugs and limitations of the module.
|
||||||
|
|
||||||
https://bugs.python.org/issue31348
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# https://bugs.python.org/issue31014
|
||||||
|
# https://github.com/michael-lazar/rtv/issues/588
|
||||||
|
def register_patch(name, klass, instance=None, update_tryorder=None, preferred=False):
|
||||||
|
"""
|
||||||
|
Wrapper around webbrowser.register() that detects if the function was
|
||||||
|
invoked with the legacy function signature. If so, the signature is
|
||||||
|
fixed before passing it along to the underlying function.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
register(name, klass, instance, -1)
|
||||||
|
register(name, klass, instance, update_tryorder=-1)
|
||||||
|
register(name, klass, instance, preferred=True)
|
||||||
|
"""
|
||||||
|
if update_tryorder is not None:
|
||||||
|
preferred = (update_tryorder == -1)
|
||||||
|
return webbrowser._register(name, klass, instance, preferred=preferred)
|
||||||
|
|
||||||
|
if sys.version_info[:2] >= (3, 7):
|
||||||
|
webbrowser._register = webbrowser.register
|
||||||
|
webbrowser.register = register_patch
|
||||||
|
|
||||||
# Add support for browsers that aren't defined in the python standard library
|
# Add support for browsers that aren't defined in the python standard library
|
||||||
webbrowser.register('surf', None, webbrowser.BackgroundBrowser('surf'))
|
webbrowser.register('surf', None, webbrowser.BackgroundBrowser('surf'))
|
||||||
webbrowser.register('vimb', None, webbrowser.BackgroundBrowser('vimb'))
|
webbrowser.register('vimb', None, webbrowser.BackgroundBrowser('vimb'))
|
||||||
@@ -43,21 +62,14 @@ def patch_webbrowser():
|
|||||||
# what we want to do anyway.
|
# what we want to do anyway.
|
||||||
webbrowser.register('opera', None, webbrowser.BackgroundBrowser('opera'))
|
webbrowser.register('opera', None, webbrowser.BackgroundBrowser('opera'))
|
||||||
|
|
||||||
if sys.platform != 'darwin' or 'BROWSER' not in os.environ:
|
# https://bugs.python.org/issue31348
|
||||||
return
|
# Use MacOS actionscript when opening the program defined in by $BROWSER
|
||||||
|
if sys.platform == 'darwin' and 'BROWSER' in os.environ:
|
||||||
# This is a copy of what's at the end of webbrowser.py, except that
|
_userchoices = os.environ["BROWSER"].split(os.pathsep)
|
||||||
# it adds MacOSXOSAScript entries instead of GenericBrowser entries.
|
for cmdline in reversed(_userchoices):
|
||||||
_userchoices = os.environ["BROWSER"].split(os.pathsep)
|
if cmdline in ('safari', 'firefox', 'chrome', 'default'):
|
||||||
for cmdline in reversed(_userchoices):
|
browser = webbrowser.MacOSXOSAScript(cmdline)
|
||||||
if cmdline in ('safari', 'firefox', 'chrome', 'default'):
|
|
||||||
browser = webbrowser.MacOSXOSAScript(cmdline)
|
|
||||||
try:
|
|
||||||
webbrowser.register(cmdline, None, browser, update_tryorder=-1)
|
webbrowser.register(cmdline, None, browser, update_tryorder=-1)
|
||||||
except TypeError:
|
|
||||||
# 3.7 nightly build changed the method signature
|
|
||||||
# pylint: disable=unexpected-keyword-arg
|
|
||||||
webbrowser.register(cmdline, None, browser, preferred=True)
|
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
|||||||
Reference in New Issue
Block a user