def patch_webbrowser():
    """
    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'))
    webbrowser.register('qutebrowser', None, webbrowser.BackgroundBrowser('qutebrowser'))

    # Fix the opera browser, see https://github.com/michael-lazar/rtv/issues/476.
    # By default, opera will open a new tab in the current window, which is
    # what we want to do anyway.
    webbrowser.register('opera', None, webbrowser.BackgroundBrowser('opera'))

    # 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)
def patch_webbrowser ( ) : def register_patch ( name , klass , instance = None , update_tryorder = None , preferred = False ) : 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 webbrowser . register ( 'surf' , None , webbrowser . BackgroundBrowser ( 'surf' ) ) webbrowser . register ( 'vimb' , None , webbrowser . BackgroundBrowser ( 'vimb' ) ) webbrowser . register ( 'qutebrowser' , None , webbrowser . BackgroundBrowser ( 'qutebrowser' ) ) webbrowser . register ( 'opera' , None , webbrowser . BackgroundBrowser ( 'opera' ) ) 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 )
Some custom patches on top of the python webbrowser module to fix
    user reported bugs and limitations of the module.