[New-bugs-announce] [issue11491] dbm.open(..., flag="n") raises dbm.error if file exists and is rejected by whichdb

Denver Coneybeare report at bugs.python.org
Mon Mar 14 07:42:42 CET 2011

New submission from Denver Coneybeare <denver.coneybeare at gmail.com>:

dbm.open() with flag="n" raises dbm.error if the given file exists but whichdb doesn't recognize it.  In the documentation for dbm.open() the "n" flag is documented to "Always create a new, empty database, open for reading and writing".  To me, this implies that if the file exists it will unconditionally be overwritten with a newly-created database, irrespective of its contents.

The code below illustrates a scenario (and indeed the scenario that I ran into) where dbm.open(..., flag="n") will throw dbm.error when it should just blow away the existing file and create a new, empty database:

import dbm
open("test.db", "w").close() # create empty file
dbm.open("test.db", flag="n")

The cause of the exception is that within dbm.open() there is a call to whichdb to determine the file type.  The fix would be to skip this whichdb check if the "n" flag is specified.

I don't think that this change will cause backward compatibility issues, since I find it hard to believe that existing applications would rely on this exception being raised in this scenario.  However, to *guarantee* no compatibility break an alternate fix could leave the current behavior of the "n" flag and introduce a new flag, perhaps "o" for "overwrite", with this "unconditional overwrite" behavior.

A proposed patch is attached: dbm_open_n_flag_error_invalid_file_fix_v1.patch

components: Library (Lib)
files: dbm_open_n_flag_error_invalid_file_fix_v1.patch
keywords: patch
messages: 130791
nosy: denversc
priority: normal
severity: normal
status: open
title: dbm.open(..., flag="n") raises dbm.error if file exists and is rejected by whichdb
type: behavior
versions: Python 3.3
Added file: http://bugs.python.org/file21108/dbm_open_n_flag_error_invalid_file_fix_v1.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list