trouble with cmd.Cmd and prompting

Peter Otten __peter__ at web.de
Tue Jan 3 10:57:26 EST 2017


Cameron Simpson wrote:

> On 03Jan2017 00:14, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>>On Tue, 3 Jan 2017 11:33:15 +1100, Cameron Simpson <cs at zip.com.au>
>>declaimed the following:
>>>I'm using cmd.Cmd to write a little FTP-like command line to interface to
>>>a storage system of mine and encountering weird behaviour. When I enter a
>>>command the next prompt appears _before_ the associated operation runs,
>>>or so it appears.
>><SNIP>
>>>Has anyone seen this kind of thing before?
>>
>>Haven't used the module but there is something I find intriguing in the
>>help system
>>-=-=-=-=-
>> Cmd.precmd(line)
>>    Hook method executed just before the command line is interpreted, but
>>after the input prompt is generated and issued.
>>-=-=-=-=-
>>"... AFTER the input prompt is ... issued"
>>
>>I don't know, but that sure sounds to me like the cmd object tends to
>>process one line behind... Though that behavior is not shown in the turtle
>>example in the help system.
> 
> Hmm. Interesting. I had read that text to imply (based on what I imagined
> _should_ happen) that the flow of control went:
> 
>   cmdloop:
>     issue prompt>
>     line=input()  # or readline
>     line=precmd(line)
>     stop=onecmd(line) # which calls do_blah...
>     stop=postcmd(stop,line)
>     if stop:
>       break
> 
> but your reading of it suggests that this is possible:
> 
>   issue prompt>
>   cmdloop:
>     line=input()  # or readline
>     issue prompt>
>     line=precmd(line)
>     stop=onecmd(line) # which calls do_blah...
>     stop=postcmd(stop,line)
>     if stop:
>       break
> 
> The text for Cmd.cmdloop starts with this:
> 
>   Repeatedly issue a prompt, accept input, parse an initial prefix off the
>   received input, and dispatch to action methods, passing them the
>   remainder of the line as argument.
> 
> which argues for the former, and was what I naively expected.
> 
> I guess I'd better dig out the source; I dislike going that far, not
> merely out of laziness, but also because the source is not the spec.
> 
> Thanks for the suggestion,
> Cameron Simpson <cs at zip.com.au>

I don't believe Dennis' reading of the docs is correct, and the relevant 
part of the source (Python 3.4) does not show anything to support it:

            while not stop:
                if self.cmdqueue:
                    line = self.cmdqueue.pop(0)
                else:
                    if self.use_rawinput:
                        try:
                            line = input(self.prompt)
                        except EOFError:
                            line = 'EOF'
                    else:
                        self.stdout.write(self.prompt)
                        self.stdout.flush()
                        line = self.stdin.readline()
                        if not len(line):
                            line = 'EOF'
                        else:
                            line = line.rstrip('\r\n')
                line = self.precmd(line)
                stop = self.onecmd(line)
                stop = self.postcmd(stop, line)
            self.postloop()

Is there something asynchronous in your command? Perhaps you can post a 
minimal runnable example that shows the odd behaviour.




More information about the Python-list mailing list