Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Behaviour of "default" method of cmd.Cmd when no input is sent #143804

Closed as not planned
Labels
docsDocumentation in the Doc dirpendingThe issue will be closed if no feedback is provided
@pythonhacker

Description

@pythonhacker

Bug report

Bug description:

importcmdclassSampleShell(cmd.Cmd):""" A sample shell class """defdefault(self,line):print("Got - ",line)defdo_EOF(self,line):returnTrueif__name__=="__main__":SampleShell().cmdloop()

If you execute this simple command shell instance - you find that even when not entering a valid input - the previous result is repeated.

(Cmd)HelloGot-Hello(Cmd)Got-Hello(Cmd)Got-Hello(Cmd)HowareyouGot-Howareyou(Cmd)Got-Howareyou

This seems to be the current default behavior ofdefault in thecmd.Cmd class.

However I don't see why this should be default. For example if one is constructing command interpreters which are say chat applications - that usually don't do any specific commands like "do_command" but override thedefault method - then this has implications as the empty input translates to chat processing of the previous line - which is not the behavior you want.

So right now one has to codeagainst it by caching the last line and checking new input against it. In this case,

classSampleShell(cmd.Cmd):""" A sample shell class """last=Nonedefdefault(self,line):ifline==self.last:returnprint("Got - ",line)self.last=linedefdo_EOF(self,line):returnTrue

Now the behavior is fine.

(Cmd)HelloGot-Hello(Cmd) (Cmd)HiGot-Hi(Cmd) (Cmd)

This is definitely a bug - OR (highly unlikely) a behavior not documented in the module documentation ofcmd or
the documentation is not clear (explicit)

This exists on all Pythion 3.x versions I checked - 3.10, 3.11, 3.12, 3.13, 3.14..

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirpendingThe issue will be closed if no feedback is provided

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp