Improve error messages for invalid `shelve` datum
Chưa có ai nhận issue này.
- 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:
In https://github.com/python/cpython/issues/137829, I got confused because of the following:
import shelve
import dbm.gnu
db = dbm.gnu.open("myshelf", "c")
db.close()
def serializer(obj, protocol=None):
pass
def deserializer(data):
return data.decode("utf-8")
with shelve.open("myshelf", serializer=serializer, deserializer=deserializer) as s:
s["foo"] = "bar"
The traceback is
Traceback (most recent call last):
File "/home/arf/Desktop/source-codes/cpython/repro.py", line 17, in <module>
s["foo"] = "bar"
~^^^^^^^
File "/home/arf/Desktop/source-codes/cpython/Lib/shelve.py", line 138, in __setitem__
self.dict[key.encode(self.keyencoding)] = serialized_value
~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: gdbm mappings have bytes or string indices only
I got confused because I thought that the issue stemmed from the key (because of the word "indices") but the error is raised due to the serialized_value type as serializer returns None. IOW, s["foo"] = "bar" above is equivalent to s["foo"] = None. I suggest:
- changing the error message when the value type is incorrect (namely, don't talk about "indices"). Ideally, we should use "key" and "value" to distinguish the messages.
- add some "must be [...], not [...]" with the input type.
cc @furkanonder
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-138768
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với Lib/shelve.py ở dòng 138 và tái hiện ví dụ shelve/dbm.gnu được cung cấp để truy tìm lý do một giá trị đã tuần tự hóa không hợp lệ lại tạo ra lỗi gây hiểu lầm. Cập nhật nội dung được báo cáo để phân biệt khóa với giá trị và bao gồm kiểu đã nhận; hoàn thành khi ví dụ xác định rõ giá trị không hợp lệ.
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ó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100