Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webbrowser default browser detection and/or public API for _trylist. #68429

Closed
davesteele mannequin opened this issue May 19, 2015 · 14 comments
Closed

webbrowser default browser detection and/or public API for _trylist. #68429

davesteele mannequin opened this issue May 19, 2015 · 14 comments
Assignees
Labels
type-feature A feature request or enhancement

Comments

@davesteele
Copy link
Mannequin

davesteele mannequin commented May 19, 2015

BPO 24241
Nosy @ncoghlan, @vstinner, @bitdancer, @berkerpeksag, @davesteele, @ultimatecoder
PRs
  • bpo-24241: Preferred X browser #85
  • bpo-24241: Add dedicated webbrowser.register test case #288
  • bpo-24241: Add versionchanged directive to the documentation #342
  • Files
  • preferredbrowser.diff: Prioritize the default desktop browser
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ncoghlan'
    closed_at = <Date 2017-02-25.08:15:11.057>
    created_at = <Date 2015-05-19.13:43:02.029>
    labels = ['type-feature']
    title = 'webbrowser default browser detection and/or public API for _trylist.'
    updated_at = <Date 2018-05-30.12:37:14.406>
    user = 'https://github.com/davesteele'

    bugs.python.org fields:

    activity = <Date 2018-05-30.12:37:14.406>
    actor = 'berker.peksag'
    assignee = 'ncoghlan'
    closed = True
    closed_date = <Date 2017-02-25.08:15:11.057>
    closer = 'ncoghlan'
    components = []
    creation = <Date 2015-05-19.13:43:02.029>
    creator = 'daves'
    dependencies = []
    files = ['39940']
    hgrepos = []
    issue_num = 24241
    keywords = ['patch']
    message_count = 14.0
    messages = ['243590', '243593', '243598', '246874', '287731', '288077', '288078', '288539', '288541', '288550', '290366', '290401', '290404', '318167']
    nosy_count = 6.0
    nosy_names = ['ncoghlan', 'vstinner', 'r.david.murray', 'berker.peksag', 'daves', 'jaysinh.shukla']
    pr_nums = ['85', '288', '342']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue24241'
    versions = ['Python 3.6']

    @davesteele
    Copy link
    Mannequin Author

    davesteele mannequin commented May 19, 2015

    When calling webbrowser.open*(), the module goes through a list of installed browsers, and uses the first one that succeeds, to process the request.

    The first 'browsers' in the 'X' list are 'xdg-open' and others of that ilk. The problem is that they only have one 'open' behavior - the 'new' parameter is ignored ('same window', 'new window', 'new tab').

    I can get the fully supported default browser in GNOME with e.g.

    def browser():
        app = Gio.app_info_get_default_for_type('x-scheme-handler/https', True)
        bpath = app.get_filename()
    
        for candidate in webbrowser._tryorder:
            if candidate in bpath:
                return webbrowser.get(using=candidate)
    
        return webbrowser.get()

    ... but this code accesses a private list.

    The problems are:

    1. webbrowser does not determine and return the current default browser.

    2. There is no public interface for determining the current/installed browser type list.

    The problem can be mitigated by resolving (1) and/or (2).

    Also, the browser type list in the documentation is missing many entries.

    Checked on 2.7 and 3.4. I believe this is valid across all supported versions.

    @davesteele davesteele mannequin added the type-bug An unexpected behavior, bug, or error label May 19, 2015
    @bitdancer
    Copy link
    Member

    Any change is going to be an enhancement, so it will only go in 3.6 (at this point...beta/feature freeze for 3.5 is this weekend).

    Would you like to propose a patch?

    @bitdancer bitdancer changed the title webbrowser (very often) doesn't support the 'new' parameter webbrowser default browser detection and/or public API for _trylist. May 19, 2015
    @bitdancer bitdancer added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels May 19, 2015
    @davesteele
    Copy link
    Mannequin Author

    davesteele mannequin commented May 19, 2015

    On Tue, May 19, 2015 at 10:19 AM, R. David Murray
    <report@bugs.python.org> wrote:
    ...

    Would you like to propose a patch?

    My preferred solution would be a bit intrusive - check for and use
    xdg-settings or gi.repository.Gio to identify the default browser, and
    match the result against the type list. I'm not sure that would get
    approval, nor am I sure of the best standard-library-compliant way to
    go about it.

    @davesteele
    Copy link
    Mannequin Author

    davesteele mannequin commented Jul 18, 2015

    Patch attached, to sort the desktop default browser to the top of _tryorder.

    @davesteele
    Copy link
    Mannequin Author

    davesteele mannequin commented Feb 14, 2017

    See #85

    @ultimatecoder
    Copy link
    Mannequin

    ultimatecoder mannequin commented Feb 18, 2017

    Hello David,
    I hope you are still working on #85 I tried to add few suggestions. Please write back on any confusions. Thanks!

    @davesteele
    Copy link
    Mannequin Author

    davesteele mannequin commented Feb 18, 2017

    Jaysinh,

    Thanks for the feedback. I'm adding the documentation now.

    As I noted in github, I'm not sure what the preferred exception handling would be for xdg-settings.

    @ncoghlan
    Copy link
    Contributor

    To summarise the changes that were made during the PR review, here's the eventual commit message for the merged PR:

    ============
    bpo-24241: Improve preferred webbrowser handling (#85)

    • Add 'preferred' argument to webbrowser.register
    • Use xdg-settings to specify preferred X browser

    The first change replaces the existing undocumented tri-state
    'try_order' parameter with the documented boolean keyword-only
    'preferred' parameter. Setting it to True places the browser at the
    front of the list, preferring it as the return to a subsequent get() call.

    The second change adds a private _os_preferred_browser setting
    and then uses that to make the default browser reported by
    xdg-settings first in the try list when running under X (or
    another environment that sets the DISPLAY variable).
    This avoids the problem where the first entry in the tryorder
    queue otherwise defaults to xdg-open, which doesn't support
    the "new window" option.
    ============

    @ncoghlan
    Copy link
    Contributor

    After merging this, I belatedly realised that codecov's complaint about the lack of code coverage for the diff was actually valid: now that "preferred" is a public API, it should have a cross-platform test case.

    That should be a fairly straightforward test to write, as it can just use an arbitrary string, and then poke around in _tryorder directly to make sure it is updated as expected - it doesn't need to use a valid browser reference.

    @ncoghlan ncoghlan reopened this Feb 25, 2017
    @ncoghlan ncoghlan self-assigned this Feb 25, 2017
    @ncoghlan
    Copy link
    Contributor

    Second PR merged with the missing test case.

    @berkerpeksag
    Copy link
    Member

    New changeset 370f7a9 by Berker Peksag in branch 'master':
    bpo-24241: Add versionchanged directive to the documentation (#342)
    370f7a9

    @ncoghlan
    Copy link
    Contributor

    New changeset 56a8ecc by Nick Coghlan in branch 'master':
    bpo-24241: Add dedicated webbrowser.register test case (#288)
    56a8ecc

    @ncoghlan
    Copy link
    Contributor

    New changeset e3ce695 by Nick Coghlan (David Steele) in branch 'master':
    bpo-24241: Improve preferred webbrowser handling (#85)
    e3ce695

    @vstinner
    Copy link
    Member

    It seems like this issue introduced a regression: bpo-33693.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants