python / python/cpython

the dbm docs are vague about what encoding is used when a str is stored

未關閉
#122,996 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

docs extension-modules stdlib triaged type-feature
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Documentation

The documentation for the dbm module, eg at https://docs.python.org/3.13/library/dbm.html include an example storing keys and values which are of type str. The documentation says about strings:

Key and values are always stored as [bytes](https://docs.python.org/3.13/library/stdtypes.html#bytes).
This means that when strings are used they are implicitly converted
to the default encoding before being stored.

It is not at all clear to me what "the default encoding" means. For example, one might assume it was the encoding from locale.getenconding() but I think not. Looking at the dbm.sqlite code one sees CAST(? AS BLOB) as the insert parameter placeholder. That says to me that the encoding is whatever the database is using, and that is not apparent to me from looking at the code. And I imagine that the other dbm backends may use other, different, choices for the default encoding.

Ideally I'd like:

  • an explicit statement about how the encoding is chosen if that is possible, or a statement that this is backend and possibly current-locale dependent otherwise
  • a statement that users should probably always do their own conversion to bytes before storing values if they want control
  • possibly an optional encoding parameter for the dbm.open calls to provide an encoding, with the current (vague but historicly compatible) behaviour if unset, but if provided then an explicit catch of str values in __setitem__ if set, and conversion according to what was provided

I can probably make a PR for the second and third items, and the "backend dependent" flavour of the first one.

Linked PRs
  • gh-138030

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 docs.python.org/3.13/library/dbm.html 的 dbm 文件開始,檢查 dbm.sqlite 程式碼對 CAST(? AS BLOB) 的處理。比較 dbm 後端如何處理 str 值,然後更新文件以說明編碼行為,並建議在呼叫端需要控制時將值轉換為 bytes。開始之前檢查連結的 PR gh-138030。

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

評估

技術堆疊
python
領域
documentation
Issue 類型
文件
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
30/100

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

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