python / python/cpython

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

オープン
#137,881 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

3.15 stdlib type-feature
主要言語
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:

  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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、dbm.open() と shelve.open() のエントリポイント、および issue に記載されている既存のバックエンド選択動作を調査します。開始前にリンクされている PR gh-137882 と gh-138007 を確認してください。これらは、作業がすでに別の場所に移されていることを示しています。完了条件には、合意されたバックエンド選択 API と、サポートされているすべての実装における明示的なバックエンド選択のカバレッジが含まれます。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
databases
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。