Description
I've located a couple things with cmd2 that differ with the standard library cmd. I set stdin and stdout in the init to a file object that is attached to a socket like:
class FirstApp(cmd2.Cmd):
def __init__(self,fd):
super().__init__(stdin=fd, stdout=fd)
self.use_rawinput = False
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(('localhost',9996))
srv.listen(0)
sock_resp, addr_resp = srv.accept()
file = sock_resp.makefile('rw',None)
c = FirstApp(file)
c.cmdloop()
comparing the behavior between cmd2 and cmd by just changing class FirstApp(cmd2.Cmd):
to class FirstApp(cmd.Cmd):
I see the following changes:
-
cmd2 doesn't flush its output while cmd does resulting in no output being sent to the socket unless I call file.flush(). cmd does flush it's output while writing to stdout.
-
cmd2 doesn't write the prompt to it's stdout (the fd), cmd does send the prompt.
For both of the above I think the behavior of cmd is the expected behavior while cmd2 is not. That could be up to opinion though.
As a workaround for number 1 I redefined file.write() to a new function that calls file.write() then file.flush() which seems to work.