python / python/cpython

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

Đang mở
#137,881 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.15 stdlib type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách kiểm tra các điểm vào dbm.open() và shelve.open() cùng với hành vi lựa chọn backend hiện có được mô tả trong issue. Hãy xem các PR được liên kết gh-137882 và gh-138007 trước khi bắt đầu, vì chúng cho thấy công việc đã được chuyển sang nơi khác. Công việc được xem là hoàn tất khi có một API lựa chọn backend đã được thống nhất và có coverage cho các lựa chọn backend tường minh trên tất cả các triển khai được hỗ trợ.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
databases
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.