This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: webbrowser.open opens twice on Windows if BROWSER is set
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: andig2
Priority: normal Keywords:

Created on 2015-04-14 09:51 by andig2, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg240863 - (view) Author: (andig2) Date: 2015-04-14 09:51
Setup: IE default browser, FF37 preferred browser and configured in BROWSER env variable

Test:

import webbrowser
url = "http://localhost"
webbrowser.open(url + '?XDEBUG_SESSION_START=sublime.xdebug')

Behaviour:

1st run: FF opens -> ok
2nd run: FF opens new tab -> ok, in addition, IE opens -> WRONG
msg240865 - (view) Author: (andig2) Date: 2015-04-14 10:37
Here is a matrix of the test results depending on which browser is open upon starting the test script:

No browser open: FF opens tab (ok)
IE open: new tab in FF (ok), nothing in IE (ok)
FF open: new tab in FF (ok), new tab in in IE (NOT ok)
Both open: new tab in FF (ok), new tab in in IE (NOT ok)
msg240867 - (view) Author: (andig2) Date: 2015-04-14 10:50
Looking further in webbrowser.py, FF uses GenericBrowser:

class GenericBrowser(BaseBrowser):
    """Class for all browsers started with a command
       and without remote functionality."""

   ...

    def open(self, url, new=0, autoraise=True):
        cmdline = [self.name] + [arg.replace("%s", url)
                                 for arg in self.args]
        try:
            if sys.platform[:3] == 'win':
                p = subprocess.Popen(cmdline)
            else:
                p = subprocess.Popen(cmdline, close_fds=True)
            return not p.wait()

If FF is already open, Popen.wait() returns TRUE for FF but FALSE for IE. This leads to fallthrough for FF and then calling open for IE.
msg240871 - (view) Author: (andig2) Date: 2015-04-14 11:13
It boils down to Popen.wait() returning 1 if FF is already open resulting in GenericBrowser.open() returning False.

That part was last touched here https://github.com/python/cpython/commit/a456db5e058f955f235fe7a51e8c111d0a8ecf4e
msg240873 - (view) Author: (andig2) Date: 2015-04-14 11:41
And finally: FF returns exit code 1 when its started and already running. That situation is not handled by the GenericBrowser Popen logic.
msg240897 - (view) Author: (andig2) Date: 2015-04-14 14:47
Last but not least: this is due to using BROWSER for defining the executable path. 

Workaround: set BROWSER=firefox and add firefox to path.
History
Date User Action Args
2022-04-11 14:58:15adminsetgithub: 68133
2015-04-14 14:48:00andig2setstatus: open -> closed
resolution: not a bug
messages: + msg240897
2015-04-14 11:41:30andig2setmessages: + msg240873
2015-04-14 11:13:24andig2setmessages: + msg240871
2015-04-14 10:50:36andig2setmessages: + msg240867
2015-04-14 10:37:21andig2setmessages: + msg240865
2015-04-14 09:51:09andig2create