Skip to content

gh-114576: Add command-line interface for dbm module #137893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

furkanonder
Copy link
Contributor

@furkanonder furkanonder commented Aug 18, 2025

Copy link
Contributor

@donBarbos donBarbos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Could you add dbm to main CLI list Doc/library/cmdline.rst?
And I think this feature deserves to be in the "What's New" entry.



.. _dbm-commandline:
.. program:: dbm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. program:: directive should be placed closer to options

Suggested change
.. program:: dbm

Comment on lines +524 to +525

.. option:: --whichdb file [file ...]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

Suggested change
.. option:: --whichdb file [file ...]
.. program:: dbm
.. option:: --whichdb file [file ...]

Comment on lines +567 to +570

.. option:: -h, --help

Show the help message.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should add help flag as option because it's not a common practice.

Suggested change
.. option:: -h, --help
Show the help message.

for filename in filenames:
if os.path.exists(filename):
db_type = whichdb(filename)
print(f"{db_type or 'UNKNOWN'} - {filename}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A hyphen is not needed.

def _dump_command(filename):
try:
with dbm_open(filename, "r") as db:
for key in db.keys():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use keys(), it reads all keys in memory. Use iteration.

Comment on lines 26 to 27
key_str = key.decode("utf-8", errors="replace")
value_str = db[key].decode("utf-8", errors="replace")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keys and values are bytes. Such decoding may result in loss of information.

How to output bytes without loss? One way to use repr() (not very readable for non-ASCII text). Other way to write to sys.stdout.buffer (this will not work on Windows with not redirected stdout). There are other ways, this should be considered together with the problem of separators (see below).

Comment on lines +60 to +62
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--whichdb",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of options, why not use subparsers for subcommands?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose not to include it to maintain clarity and simplicity, but I’m happy to add it if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants