From 11bd4a13f6677d9b4951d50a252e222fa8b6fca1 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 1 Apr 2015 21:28:56 +0200 Subject: [PATCH] Serialize arguments via json on restart. We want to ignore some positional arguments without ignoring flags/values - and since there's no easy way to "unparse" an argparse namespace, we instead pass it as json. Also note we can't pass it as a file easily, as args have to be available very early. Passing it as an argument shouldn't be an issue though. --- qutebrowser/app.py | 22 ++++++++++++++++------ qutebrowser/qutebrowser.py | 9 +++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index cf6ba5a72..862214450 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -31,6 +31,7 @@ import functools import traceback import faulthandler import datetime +import json from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox from PyQt5.QtGui import QDesktopServices, QPixmap, QIcon @@ -683,19 +684,28 @@ class Application(QApplication): # cwd=None and see if that works out. # See https://github.com/The-Compiler/qutebrowser/issues/323 cwd = None - for arg in sys.argv[1:]: - if arg.startswith('-'): - # We only want to preserve options on a restart. - args.append(arg) + # Add all open pages so they get reopened. page_args = [] for win in pages: page_args.extend(win) page_args.append('') - if page_args: - args.extend(page_args[:-1]) + + # Serialize the argparse namespace into json and pass that to the new + # process via --json-args. + # We do this as there's no way to "unparse" the namespace while + # ignoring some arguments. + argdict = vars(self._args) + argdict['session'] = None + argdict['url'] = [] + argdict['command'] = page_args[:-1] + argdict['json_args'] = None + data = json.dumps(argdict) + args += ['--json-args', data] + log.destroy.debug("args: {}".format(args)) log.destroy.debug("cwd: {}".format(cwd)) + return args, cwd @cmdutils.register(instance='app', ignore_args=True) diff --git a/qutebrowser/qutebrowser.py b/qutebrowser/qutebrowser.py index 7961bdbbb..ee6663f0f 100644 --- a/qutebrowser/qutebrowser.py +++ b/qutebrowser/qutebrowser.py @@ -20,6 +20,7 @@ """Early initialization and main entry point.""" import sys +import json import qutebrowser try: @@ -58,6 +59,7 @@ def get_argparser(): parser.add_argument('-R', '--override-restore', help="Don't restore a " "session even if one would be restored.", action='store_true') + parser.add_argument('--json-args', help=argparse.SUPPRESS) debug = parser.add_argument_group('debug arguments') debug.add_argument('-l', '--loglevel', dest='loglevel', @@ -118,6 +120,13 @@ def main(): """Main entry point for qutebrowser.""" parser = get_argparser() args = parser.parse_args() + if args.json_args is not None: + # Restoring after a restart. + # When restarting, we serialize the argparse namespace into json, and + # construct a "fake" argparse.Namespace here based on the data loaded + # from json. + data = json.loads(args.json_args) + args = argparse.Namespace(**data) earlyinit.earlyinit(args) # We do this imports late as earlyinit needs to be run first (because of # the harfbuzz fix and version checking).