Add backend parameter to dbm.open() and shelve.open() for explicit DBM backend selection
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
Proposal:
Summary
Add an optional backend parameter to dbm.open() and shelve.open() functions to allow explicit selection of DBM backend implementations, improving compatibility and predictability across different systems.
Motivation
Currently, Python's DBM module automatically selects an available backend implementation in priority order (dbm.sqlite3, dbm.gnu, dbm.ndbm, dbm.dumb). This can cause compatibility issues when:
- Custom serializers work with some backends but not others (e.g., gdbm type restrictions)
- Cross-platform consistency is needed across different environments
- Predictable behavior is required regardless of which backends are installed
- Testing needs to validate behavior with specific backends
Real-world example
# This works with dbm.sqlite3 but fails with dbm.gnu
def custom_serializer(obj, protocol):
if protocol == 5 and isinstance(obj, bytearray):
return obj # Causes "gdbm mappings have bytes or string indices only"
return pickle.dumps(obj, protocol)
# Currently no way to ensure consistent backend
with shelve.open('data.db', serializer=custom_serializer) as shelf:
shelf['key'] = bytearray(b'data') # May fail depending on system
See: https://github.com/python/cpython/issues/137829
Proposed Solution
Add an optional backend parameter to both functions:
# dbm.open()
dbm.open(file, flag='r', mode=0o666, backend=None)
# shelve.open()
shelve.open(filename, flag='c', protocol=None, writeback=False,
backend=None, *, serializer=None, deserializer=None)
Usage Examples:
import dbm
import shelve
# Explicit backend selection
with dbm.open('data.db', 'c', backend='dbm.dumb') as db:
db[b'key'] = b'value'
# Cross-platform consistency
with shelve.open('data.shelf', backend='dbm.sqlite3') as shelf:
shelf['key'] = complex_object
# Testing with specific backends
for backend in ['dbm.gnu', 'dbm.ndbm', 'dbm.dumb']:
with shelve.open(f'test_{backend}.db', backend=backend) as shelf:
test_serialization_compatibility(shelf)
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
- gh-137882
- gh-138007
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先检查 dbm.open() 和 shelve.open() 入口点,以及 issue 中描述的现有后端选择行为。开始之前先查看链接的 PR gh-137882 和 gh-138007,因为它们表明工作已经转移到了其他地方。完成标准包括:达成一致的后端选择 API,以及覆盖所有受支持实现中的显式后端选择。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- databases
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100