Issue #16996: webbrowser module now uses shutil.which() to find a

web-browser on the executable search path.
This commit is contained in:
Serhiy Storchaka 2013-02-13 12:19:40 +02:00
parent 0c15b5d897
commit 540dcba28b
2 changed files with 23 additions and 51 deletions

View file

@ -5,6 +5,7 @@
import io import io
import os import os
import shlex import shlex
import shutil
import sys import sys
import stat import stat
import subprocess import subprocess
@ -83,7 +84,7 @@ def _synthesize(browser, update_tryorder=1):
""" """
cmd = browser.split()[0] cmd = browser.split()[0]
if not _iscommand(cmd): if not shutil.which(cmd):
return [None, None] return [None, None]
name = os.path.basename(cmd) name = os.path.basename(cmd)
try: try:
@ -102,38 +103,6 @@ def _synthesize(browser, update_tryorder=1):
return [None, None] return [None, None]
if sys.platform[:3] == "win":
def _isexecutable(cmd):
cmd = cmd.lower()
if os.path.isfile(cmd) and cmd.endswith((".exe", ".bat")):
return True
for ext in ".exe", ".bat":
if os.path.isfile(cmd + ext):
return True
return False
else:
def _isexecutable(cmd):
if os.path.isfile(cmd):
mode = os.stat(cmd)[stat.ST_MODE]
if mode & stat.S_IXUSR or mode & stat.S_IXGRP or mode & stat.S_IXOTH:
return True
return False
def _iscommand(cmd):
"""Return True if cmd is executable or can be found on the executable
search path."""
if _isexecutable(cmd):
return True
path = os.environ.get("PATH")
if not path:
return False
for d in path.split(os.pathsep):
exe = os.path.join(d, cmd)
if _isexecutable(exe):
return True
return False
# General parent classes # General parent classes
class BaseBrowser(object): class BaseBrowser(object):
@ -453,58 +422,58 @@ class Grail(BaseBrowser):
def register_X_browsers(): def register_X_browsers():
# use xdg-open if around # use xdg-open if around
if _iscommand("xdg-open"): if shutil.which("xdg-open"):
register("xdg-open", None, BackgroundBrowser("xdg-open")) register("xdg-open", None, BackgroundBrowser("xdg-open"))
# The default GNOME3 browser # The default GNOME3 browser
if "GNOME_DESKTOP_SESSION_ID" in os.environ and _iscommand("gvfs-open"): if "GNOME_DESKTOP_SESSION_ID" in os.environ and shutil.which("gvfs-open"):
register("gvfs-open", None, BackgroundBrowser("gvfs-open")) register("gvfs-open", None, BackgroundBrowser("gvfs-open"))
# The default GNOME browser # The default GNOME browser
if "GNOME_DESKTOP_SESSION_ID" in os.environ and _iscommand("gnome-open"): if "GNOME_DESKTOP_SESSION_ID" in os.environ and shutil.which("gnome-open"):
register("gnome-open", None, BackgroundBrowser("gnome-open")) register("gnome-open", None, BackgroundBrowser("gnome-open"))
# The default KDE browser # The default KDE browser
if "KDE_FULL_SESSION" in os.environ and _iscommand("kfmclient"): if "KDE_FULL_SESSION" in os.environ and shutil.which("kfmclient"):
register("kfmclient", Konqueror, Konqueror("kfmclient")) register("kfmclient", Konqueror, Konqueror("kfmclient"))
# The Mozilla/Netscape browsers # The Mozilla/Netscape browsers
for browser in ("mozilla-firefox", "firefox", for browser in ("mozilla-firefox", "firefox",
"mozilla-firebird", "firebird", "mozilla-firebird", "firebird",
"seamonkey", "mozilla", "netscape"): "seamonkey", "mozilla", "netscape"):
if _iscommand(browser): if shutil.which(browser):
register(browser, None, Mozilla(browser)) register(browser, None, Mozilla(browser))
# Konqueror/kfm, the KDE browser. # Konqueror/kfm, the KDE browser.
if _iscommand("kfm"): if shutil.which("kfm"):
register("kfm", Konqueror, Konqueror("kfm")) register("kfm", Konqueror, Konqueror("kfm"))
elif _iscommand("konqueror"): elif shutil.which("konqueror"):
register("konqueror", Konqueror, Konqueror("konqueror")) register("konqueror", Konqueror, Konqueror("konqueror"))
# Gnome's Galeon and Epiphany # Gnome's Galeon and Epiphany
for browser in ("galeon", "epiphany"): for browser in ("galeon", "epiphany"):
if _iscommand(browser): if shutil.which(browser):
register(browser, None, Galeon(browser)) register(browser, None, Galeon(browser))
# Skipstone, another Gtk/Mozilla based browser # Skipstone, another Gtk/Mozilla based browser
if _iscommand("skipstone"): if shutil.which("skipstone"):
register("skipstone", None, BackgroundBrowser("skipstone")) register("skipstone", None, BackgroundBrowser("skipstone"))
# Google Chrome/Chromium browsers # Google Chrome/Chromium browsers
for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"): for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):
if _iscommand(browser): if shutil.which(browser):
register(browser, None, Chrome(browser)) register(browser, None, Chrome(browser))
# Opera, quite popular # Opera, quite popular
if _iscommand("opera"): if shutil.which("opera"):
register("opera", None, Opera("opera")) register("opera", None, Opera("opera"))
# Next, Mosaic -- old but still in use. # Next, Mosaic -- old but still in use.
if _iscommand("mosaic"): if shutil.which("mosaic"):
register("mosaic", None, BackgroundBrowser("mosaic")) register("mosaic", None, BackgroundBrowser("mosaic"))
# Grail, the Python browser. Does anybody still use it? # Grail, the Python browser. Does anybody still use it?
if _iscommand("grail"): if shutil.which("grail"):
register("grail", Grail, None) register("grail", Grail, None)
# Prefer X browsers if present # Prefer X browsers if present
@ -514,15 +483,15 @@ if os.environ.get("DISPLAY"):
# Also try console browsers # Also try console browsers
if os.environ.get("TERM"): if os.environ.get("TERM"):
# The Links/elinks browsers <http://artax.karlin.mff.cuni.cz/~mikulas/links/> # The Links/elinks browsers <http://artax.karlin.mff.cuni.cz/~mikulas/links/>
if _iscommand("links"): if shutil.which("links"):
register("links", None, GenericBrowser("links")) register("links", None, GenericBrowser("links"))
if _iscommand("elinks"): if shutil.which("elinks"):
register("elinks", None, Elinks("elinks")) register("elinks", None, Elinks("elinks"))
# The Lynx browser <http://lynx.isc.org/>, <http://lynx.browser.org/> # The Lynx browser <http://lynx.isc.org/>, <http://lynx.browser.org/>
if _iscommand("lynx"): if shutil.which("lynx"):
register("lynx", None, GenericBrowser("lynx")) register("lynx", None, GenericBrowser("lynx"))
# The w3m browser <http://w3m.sourceforge.net/> # The w3m browser <http://w3m.sourceforge.net/>
if _iscommand("w3m"): if shutil.which("w3m"):
register("w3m", None, GenericBrowser("w3m")) register("w3m", None, GenericBrowser("w3m"))
# #
@ -552,7 +521,7 @@ if sys.platform[:3] == "win":
"Internet Explorer\\IEXPLORE.EXE") "Internet Explorer\\IEXPLORE.EXE")
for browser in ("firefox", "firebird", "seamonkey", "mozilla", for browser in ("firefox", "firebird", "seamonkey", "mozilla",
"netscape", "opera", iexplore): "netscape", "opera", iexplore):
if _iscommand(browser): if shutil.which(browser):
register(browser, None, BackgroundBrowser(browser)) register(browser, None, BackgroundBrowser(browser))
# #

View file

@ -253,6 +253,9 @@ Core and Builtins
Library Library
------- -------
- Issue #16996: webbrowser module now uses shutil.which() to find a
web-browser on the executable search path.
- Issue #16800: tempfile.gettempdir() no longer left temporary files when - Issue #16800: tempfile.gettempdir() no longer left temporary files when
the disk is full. Original patch by Amir Szekely. the disk is full. Original patch by Amir Szekely.