python / python/cpython

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

Aperta
#137,881 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.15 stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando i punti di ingresso dbm.open() e shelve.open() e il comportamento esistente di selezione del backend descritto nell’issue. Esamina le PR collegati gh-137882 e gh-138007 prima di iniziare, poiché indicano che il lavoro è già stato spostato altrove. Il lavoro sarà considerato completato quando saranno disponibili un’API concordata per la selezione del backend e una copertura per le scelte esplicite del backend in tutte le implementazioni supportate.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
databases
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.