[Python-checkins] cpython: Issue #16996: webbrowser module now uses shutil.which() to find a

serhiy.storchaka python-checkins at python.org
Wed Feb 13 11:20:35 CET 2013


http://hg.python.org/cpython/rev/050c94f5f72c
changeset:   82193:050c94f5f72c
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Feb 13 12:19:40 2013 +0200
summary:
  Issue #16996: webbrowser module now uses shutil.which() to find a
web-browser on the executable search path.

files:
  Lib/webbrowser.py |  71 +++++++++-------------------------
  Misc/NEWS         |   3 +
  2 files changed, 23 insertions(+), 51 deletions(-)


diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py
--- a/Lib/webbrowser.py
+++ b/Lib/webbrowser.py
@@ -5,6 +5,7 @@
 import io
 import os
 import shlex
+import shutil
 import sys
 import stat
 import subprocess
@@ -83,7 +84,7 @@
 
     """
     cmd = browser.split()[0]
-    if not _iscommand(cmd):
+    if not shutil.which(cmd):
         return [None, None]
     name = os.path.basename(cmd)
     try:
@@ -102,38 +103,6 @@
     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
 
 class BaseBrowser(object):
@@ -453,58 +422,58 @@
 def register_X_browsers():
 
     # use xdg-open if around
-    if _iscommand("xdg-open"):
+    if shutil.which("xdg-open"):
         register("xdg-open", None, BackgroundBrowser("xdg-open"))
 
     # 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"))
 
     # 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"))
 
     # 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"))
 
     # The Mozilla/Netscape browsers
     for browser in ("mozilla-firefox", "firefox",
                     "mozilla-firebird", "firebird",
                     "seamonkey", "mozilla", "netscape"):
-        if _iscommand(browser):
+        if shutil.which(browser):
             register(browser, None, Mozilla(browser))
 
     # Konqueror/kfm, the KDE browser.
-    if _iscommand("kfm"):
+    if shutil.which("kfm"):
         register("kfm", Konqueror, Konqueror("kfm"))
-    elif _iscommand("konqueror"):
+    elif shutil.which("konqueror"):
         register("konqueror", Konqueror, Konqueror("konqueror"))
 
     # Gnome's Galeon and Epiphany
     for browser in ("galeon", "epiphany"):
-        if _iscommand(browser):
+        if shutil.which(browser):
             register(browser, None, Galeon(browser))
 
     # Skipstone, another Gtk/Mozilla based browser
-    if _iscommand("skipstone"):
+    if shutil.which("skipstone"):
         register("skipstone", None, BackgroundBrowser("skipstone"))
 
     # Google Chrome/Chromium browsers
     for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):
-        if _iscommand(browser):
+        if shutil.which(browser):
             register(browser, None, Chrome(browser))
 
     # Opera, quite popular
-    if _iscommand("opera"):
+    if shutil.which("opera"):
         register("opera", None, Opera("opera"))
 
     # Next, Mosaic -- old but still in use.
-    if _iscommand("mosaic"):
+    if shutil.which("mosaic"):
         register("mosaic", None, BackgroundBrowser("mosaic"))
 
     # Grail, the Python browser. Does anybody still use it?
-    if _iscommand("grail"):
+    if shutil.which("grail"):
         register("grail", Grail, None)
 
 # Prefer X browsers if present
@@ -514,15 +483,15 @@
 # Also try console browsers
 if os.environ.get("TERM"):
     # The Links/elinks browsers <http://artax.karlin.mff.cuni.cz/~mikulas/links/>
-    if _iscommand("links"):
+    if shutil.which("links"):
         register("links", None, GenericBrowser("links"))
-    if _iscommand("elinks"):
+    if shutil.which("elinks"):
         register("elinks", None, Elinks("elinks"))
     # The Lynx browser <http://lynx.isc.org/>, <http://lynx.browser.org/>
-    if _iscommand("lynx"):
+    if shutil.which("lynx"):
         register("lynx", None, GenericBrowser("lynx"))
     # The w3m browser <http://w3m.sourceforge.net/>
-    if _iscommand("w3m"):
+    if shutil.which("w3m"):
         register("w3m", None, GenericBrowser("w3m"))
 
 #
@@ -552,7 +521,7 @@
                             "Internet Explorer\\IEXPLORE.EXE")
     for browser in ("firefox", "firebird", "seamonkey", "mozilla",
                     "netscape", "opera", iexplore):
-        if _iscommand(browser):
+        if shutil.which(browser):
             register(browser, None, BackgroundBrowser(browser))
 
 #
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -253,6 +253,9 @@
 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
   the disk is full.  Original patch by Amir Szekely.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list