New submission from Denver Coneybeare <denver.coneybe...@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 <rep...@bugs.python.org> <http://bugs.python.org/issue11491> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com