python / python/cpython

Add backend parameter to dbm.open() and shelve.open() for explicit DBM backend selection

未關閉
#137,881 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

3.15 stdlib type-feature
主要語言
Python
星號
77.2k
分支
36k
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:

  1. Custom serializers work with some backends but not others (e.g., gdbm type restrictions)
  2. Cross-platform consistency is needed across different environments
  3. Predictable behavior is required regardless of which backends are installed
  4. 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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先檢查 dbm.open() 和 shelve.open() 入口點,以及 issue 中描述的現有後端選擇行為。開始之前先查看連結的 PR gh-137882 和 gh-138007,因為它們表明工作已經轉移到其他地方。完成標準包括:達成共識的後端選擇 API,以及涵蓋所有受支援實作中的明確後端選擇。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
databases
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。