diff --git a/rtv/objects.py b/rtv/objects.py index cea2c89..8c568a8 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -27,12 +27,31 @@ _logger = logging.getLogger(__name__) def patch_webbrowser(): """ - Patch webbrowser on macOS to support setting BROWSER=firefox, - BROWSER=chrome, etc.. - - https://bugs.python.org/issue31348 + Some custom patches on top of the python webbrowser module to fix + user reported bugs and limitations of the module. """ + # 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 webbrowser.register('surf', None, webbrowser.BackgroundBrowser('surf')) webbrowser.register('vimb', None, webbrowser.BackgroundBrowser('vimb')) @@ -43,21 +62,14 @@ def patch_webbrowser(): # what we want to do anyway. webbrowser.register('opera', None, webbrowser.BackgroundBrowser('opera')) - if sys.platform != 'darwin' or 'BROWSER' not in os.environ: - return - - # This is a copy of what's at the end of webbrowser.py, except that - # it adds MacOSXOSAScript entries instead of GenericBrowser entries. - _userchoices = os.environ["BROWSER"].split(os.pathsep) - for cmdline in reversed(_userchoices): - if cmdline in ('safari', 'firefox', 'chrome', 'default'): - browser = webbrowser.MacOSXOSAScript(cmdline) - try: + # https://bugs.python.org/issue31348 + # Use MacOS actionscript when opening the program defined in by $BROWSER + if sys.platform == 'darwin' and 'BROWSER' in os.environ: + _userchoices = os.environ["BROWSER"].split(os.pathsep) + for cmdline in reversed(_userchoices): + if cmdline in ('safari', 'firefox', 'chrome', 'default'): + browser = webbrowser.MacOSXOSAScript(cmdline) 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