From fc877b2687c5d2ceda2530780643da8ec3c735c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20A=C5=9F=C4=B1c=C4=B1?= Date: Mon, 21 Dec 2009 09:07:32 +0000 Subject: [PATCH] Fix exceptions raise after quitting history view. --- ChangeLog | 3 +++ pisi/cli/history.py | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 84bda808..b16340f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2009-12-21 Fatih Aşıcı + * pisi/cli/history.py: Fix exceptions raise after quitting history view. + 2009-12-16 Serdar Dalgıç * pisi/cli/blame.py: Several enhancements in blame: - pisi bl works for LC_ALL=C diff --git a/pisi/cli/history.py b/pisi/cli/history.py index 78bebba7..23742833 100644 --- a/pisi/cli/history.py +++ b/pisi/cli/history.py @@ -78,10 +78,41 @@ Lists previous operations.""") print " *", pkg print - def redirect_output(self): + def redirect_output(self, func): if os.isatty(sys.stdout.fileno()): - output = os.popen("less","w") - sys.stdout = sys.stderr = output + class LessException(Exception): + pass + + class LessPipe(): + def __init__(self): + import subprocess + self.less = subprocess.Popen(["less", "-"], + stdin=subprocess.PIPE) + + def __del__(self): + self.less.stdin.close() + self.less.wait() + + def flush(self): + self.less.stdin.flush() + + def write(self, s): + try: + self.less.stdin.write(s) + except IOError: + raise LessException + + stdout, stderr = sys.stdout, sys.stderr + sys.stdout = sys.stderr = LessPipe() + try: + func() + except LessException: + pass + finally: + sys.stdout, sys.stderr = stdout, stderr + + else: + func() def run(self): self.init(database = False, write = False) @@ -95,5 +126,4 @@ Lists previous operations.""") self.takeback(opno) return - self.redirect_output() - self.print_history() + self.redirect_output(self.print_history)