Skip to content

Commit 740f5bc

Browse files
committed
Fix _dump_command to use repr() and avoid data loss
1 parent a547468 commit 740f5bc

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

Lib/dbm/__main__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ def _whichdb_command(filenames):
2222
def _dump_command(filename):
2323
try:
2424
with dbm_open(filename, "r") as db:
25-
for key in db.keys():
26-
key_str = key.decode("utf-8", errors="replace")
27-
value_str = db[key].decode("utf-8", errors="replace")
28-
print(f"{key_str}: {value_str}")
25+
for key in db:
26+
print(f"{key!r}: {db[key]!r}")
2927
return 0
3028
except error:
3129
print(f"Error: Database '{filename}' not found", file=sys.stderr)

Lib/test/test_dbm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ def test_whichdb_output_format(self):
364364

365365
def test_dump_command(self):
366366
output = self.run_cmd_ok('--dump', self.test_db)
367-
self.assertIn(b'key1: value1', output)
368-
self.assertIn(b'key2: value2', output)
367+
self.assertIn(b"b'key1': b'value1'", output)
368+
self.assertIn(b"b'key2': b'value2'", output)
369369

370370
def test_dump_empty_database(self):
371371
output = self.run_cmd_ok('--dump', self.empty_db)

0 commit comments

Comments
 (0)