Commit d7cc1bd8 authored by Michael W. Hudson's avatar Michael W. Hudson

Fix

[ 924301 ] A leak case with cmd.py & readline & exception

by ensuring that the readline completion function is always reset
even in the case of an exception being raised.  As a bonus, this
makes the documentation for pre & postloop accurate again.
parent 1f34eb17
...@@ -107,32 +107,49 @@ class Cmd: ...@@ -107,32 +107,49 @@ class Cmd:
""" """
self.preloop() self.preloop()
if intro is not None: if self.use_rawinput and self.completekey:
self.intro = intro try:
if self.intro: import readline
self.stdout.write(str(self.intro)+"\n") self.old_completer = readline.get_completer()
stop = None readline.set_completer(self.complete)
while not stop: readline.parse_and_bind(self.completekey+": complete")
if self.cmdqueue: except ImportError:
line = self.cmdqueue.pop(0) pass
else: try:
if self.use_rawinput: if intro is not None:
try: self.intro = intro
line = raw_input(self.prompt) if self.intro:
except EOFError: self.stdout.write(str(self.intro)+"\n")
line = 'EOF' stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else: else:
self.stdout.write(self.prompt) if self.use_rawinput:
self.stdout.flush() try:
line = self.stdin.readline() line = raw_input(self.prompt)
if not len(line): except EOFError:
line = 'EOF' line = 'EOF'
else: else:
line = line[:-1] # chop \n self.stdout.write(self.prompt)
line = self.precmd(line) self.stdout.flush()
stop = self.onecmd(line) line = self.stdin.readline()
stop = self.postcmd(stop, line) if not len(line):
self.postloop() line = 'EOF'
else:
line = line[:-1] # chop \n
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def precmd(self, line): def precmd(self, line):
"""Hook method executed just before the command line is """Hook method executed just before the command line is
...@@ -147,27 +164,15 @@ class Cmd: ...@@ -147,27 +164,15 @@ class Cmd:
def preloop(self): def preloop(self):
"""Hook method executed once when the cmdloop() method is called.""" """Hook method executed once when the cmdloop() method is called."""
if self.completekey: pass
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
def postloop(self): def postloop(self):
"""Hook method executed once when the cmdloop() method is about to """Hook method executed once when the cmdloop() method is about to
return. return.
""" """
if self.completekey: pass
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def parseline(self, line): def parseline(self, line):
"""Parse the line into a command name and a string containing """Parse the line into a command name and a string containing
the arguments. Returns a tuple containing (command, args, line). the arguments. Returns a tuple containing (command, args, line).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment