Docs: "Extending Python with C or C++" explanation of PY_SSIZE_T_CLEAN explains nothing
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 65/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Lĩnh vực
- documentation
Hướng nghiên cứu
Bắt đầu với chương “Extending Python with C or C++”, đặc biệt là “A Simple Example” và “Extracting Parameters in Extension Functions”, sau đó so sánh chúng với phần được liên kết trong tài liệu tham khảo Python/C API về chuỗi và bộ đệm. Cải thiện phần giải thích và các liên kết về PY_SSIZE_T_CLEAN để người mới hiểu được tác động của nó và khi nào cần định nghĩa nó trước Python.h.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Documentation
The manual chapter "Extending Python with C or C++" opens with "A Simple Example", first presenting these two lines of source:
#define PY_SSIZE_T_CLEAN
#include <Python.h>
This is shortly followed by a note:
Note Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include
Python.hbefore any standard headers are included.It is recommended to always define
PY_SSIZE_T_CLEANbefore includingPython.h. See Extracting Parameters in Extension Functions for a description of this macro.
The problem
Is, in a nutshell, with the latter half of that note.
The section referenced, "Extracting Parameters in Extension Functions" contains no real explanation of PY_SSIZE_T_CLEAN at all; the only difference is that the #define in its example code is now followed by a comment:
#define PY_SSIZE_T_CLEAN /* Make "s#" use Py_ssize_t rather than int. */
#include <Python.h>
I would submit that this "explanation" explains nothing, unless the user already knows about the PyArg_Parse*() family of functions and understands their byzantine format syntax. To anyone else, "s#" (a PyArg_Parse() format string token — concepts which haven't even been introduced by that point) has no obvious meaning, to say nothing of the type it "uses". Even an experienced C/C++ programmer, unfamiliar with Python's C API, can't really be expected to get anything useful out of that comment.
The "Extracting Parameters..." section does also direct the reader to the Python/C API reference. (Just generally, for information about argument parsing. Not in any way that explicitly or obviously relates to PY_SSIZE_T_CLEAN.)
If the user chooses to explore, there they will find an actual explanation of PY_SSIZE_T_CLEAN (though it still requires reading a fair amount of the surrounding context to understand):
(from https://docs.python.org/3/c-api/arg.html#strings-and-buffers):
Note For all
#variants of formats (s#,y#, etc.), the macroPY_SSIZE_T_CLEANmust be defined before includingPython.h. On Python 3.9 and older, the type of the length argument isPy_ssize_tif thePY_SSIZE_T_CLEANmacro is defined, orintotherwise.
Proposed fix
It might be better to just point them at the relevant section of the reference manual right from the start, rather than giving them the runaround through "Extracting Parameters..." and hoping they're curious enough to follow a second link.
Or, if dropping them in the deep end of PyArg_Parse() right away is too daunting for new API users (which it absolutely, totally is), then perhaps "Extracting Parameters..." should include some sort of real (if handwavey) explanation of PY_SSIZE_T_CLEAN, along with a link to the reference manual. Something like:
Note The macro
PY_SSIZE_T_CLEANmust be defined before includingPython.h, to ensure backwards compatibility in Python's argument parsing functions.In Python 3.9 and older, when a variable-size buffer argument is parsed, its size will be returned as a type
Py_ssize_tvalue ifPY_SSIZE_T_CLEANis set, or as a typeintif it is unset. Python 3.10 and later will always usePy_ssize_tfor buffer size values.See Parsing arguments and building values in the Python/C API Reference Manual for more information about the
PyArg_Parse()functions and howPY_SSIZE_T_CLEANinteracts with format strings containing the sized tokenss#,y#, etc.In new code, store and pass argument buffer sizes exclusively as type
Py_ssize_t, and always#define PY_SSIZE_T_CLEANbefore includingPython.hto ensure compatibility with older Python releases.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 558
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.
Issue khác của python/cpython
-
docs pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
build type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
stdlib topic-email type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Tất cả issue của python/cpython
Issue tương tự
-
link-check link-check:sphinx-theme
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
OpenHands/extensions#626 · 1 bình luận ·
-
Change observation tooltip text Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
CSCfi/sd-search-api#39 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100