diff options
Diffstat (limited to 'Lib/webbrowser.py')
-rw-r--r-- | Lib/webbrowser.py | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py index f3c53d423cf..202f34a1a2e 100644 --- a/Lib/webbrowser.py +++ b/Lib/webbrowser.py @@ -1,7 +1,8 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 """Interfaces for launching and remotely controlling Web browsers.""" # Maintained by Georg Brandl. +import io import os import shlex import sys @@ -159,7 +160,7 @@ class GenericBrowser(BaseBrowser): and without remote functionality.""" def __init__(self, name): - if isinstance(name, basestring): + if isinstance(name, str): self.name = name self.args = ["%s"] else: @@ -205,12 +206,18 @@ class UnixBrowser(BaseBrowser): """Parent class for all Unix browsers with remote functionality.""" raise_opts = None + background = False + redirect_stdout = True + # In remote_args, %s will be replaced with the requested URL. %action will + # be replaced depending on the value of 'new' passed to open. + # remote_action is used for new=0 (open). If newwin is not None, it is + # used for new=1 (open_new). If newtab is not None, it is used for + # new=3 (open_new_tab). After both substitutions are made, any empty + # strings in the transformed remote_args list will be removed. remote_args = ['%action', '%s'] remote_action = None remote_action_newwin = None remote_action_newtab = None - background = False - redirect_stdout = True def _invoke(self, args, remote, autoraise): raise_opt = [] @@ -223,19 +230,13 @@ class UnixBrowser(BaseBrowser): cmdline = [self.name] + raise_opt + args if remote or self.background: - inout = file(os.devnull, "r+") + inout = io.open(os.devnull, "r+") else: # for TTY browsers, we need stdin/out inout = None - # if possible, put browser in separate process group, so - # keyboard interrupts don't affect browser as well as Python - setsid = getattr(os, 'setsid', None) - if not setsid: - setsid = getattr(os, 'setpgrp', None) - p = subprocess.Popen(cmdline, close_fds=True, stdin=inout, stdout=(self.redirect_stdout and inout or None), - stderr=inout, preexec_fn=setsid) + stderr=inout, start_new_session=True) if remote: # wait five seconds. If the subprocess is not finished, the # remote invocation has (hopefully) started a new instance. @@ -272,6 +273,7 @@ class UnixBrowser(BaseBrowser): args = [arg.replace("%s", url).replace("%action", action) for arg in self.remote_args] + args = [arg for arg in args if arg] success = self._invoke(args, True, autoraise) if not success: # remote invocation failed, try straight way @@ -343,7 +345,7 @@ class Konqueror(BaseBrowser): else: action = "openURL" - devnull = file(os.devnull, "r+") + devnull = io.open(os.devnull, "r+") # if possible, put browser in separate process group, so # keyboard interrupts don't affect browser as well as Python setsid = getattr(os, 'setsid', None) @@ -652,22 +654,22 @@ def main(): -t: open new tab""" % sys.argv[0] try: opts, args = getopt.getopt(sys.argv[1:], 'ntd') - except getopt.error, msg: - print >>sys.stderr, msg - print >>sys.stderr, usage + except getopt.error as msg: + print(msg, file=sys.stderr) + print(usage, file=sys.stderr) sys.exit(1) new_win = 0 for o, a in opts: if o == '-n': new_win = 1 elif o == '-t': new_win = 2 if len(args) != 1: - print >>sys.stderr, usage + print(usage, file=sys.stderr) sys.exit(1) url = args[0] open(url, new_win) - print "\a" + print("\a") if __name__ == "__main__": main() |