-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
ctypes string_at/wstring_at: incorrect argument name used in docs and docstring #87969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
string_at and wstring_at first argument in docs in address but actually in code is ptr. See cpython/Lib/ctypes/__init__.py Line 513 in 750f484
cpython/Lib/ctypes/__init__.py Line 525 in 750f484
Noticed this when going over stubtest errors in typeshed. |
It's a bit worse: the actual name is "ptr", the function docstrings say "addr", and the documentation (https://docs.python.org/3.9/library/ctypes.html#ctypes.string_at) has "address". I'd favor updating them all to say "ptr", because changing the name of the runtime parameter would risk breaking users' code. |
I also certainly agree with that. Moreover, the documentation and docstring uses the name "address" and "addr" respectively which are misleading because it is asking for ctypes.c_char_p or ctypes.c_wchar_p which are C pointer types, it is not asking for the address of instances of ctypes.c_char_p or ctypes.c_wchar_p. Therefore a change in the documentation and docstring is required. Note - If this issue is confirmed then I'll submit a PR to apply the change as described in this issue. |
string_at() and wstring_at() take a c_void_p value. This can be initialized by an integer (i.e. an address), bytes, str, or any ctypes pointer, array, or byref() reference. Additionally it works with an object that has a compatible _as_parameter_ attribute, e.g.: >>> class A:
... _as_parameter_ = b'spam'
...
>>> ctypes.string_at(A)
b'spam' If the docstring is change to use "ptr", for string_at() it can succinctly say the following: "Return the byte string at void *ptr." For wstring_at(), replace "byte string" with "wide-character string" or "wchar_t string". Specifying the type matters because it depends on the platform. Windows uses a 16-bit wchar_t, and the memory will be interpreted as UTF-16 (e.g. surrogate pairs), whereas other platforms use a 32-bit wchar_t, and the memory will be interpreted as UTF-32. For example: Windows: >>> ascii(ctypes.wstring_at(b'\x00\xd8\x00\xdc'))
"'\\U00010000'" Linux: >>> try: ctypes.wstring_at(b'\x00\xd8\x00\xdc')
... except Exception as e: print(e)
...
character U+dc00d800 is not in range [U+0000; U+10ffff] |
Yeah, you're right and also it is true that changing the docstring to "ptr" can succinctly say "Return the byte string at void *ptr." But it's a kind of problem if different argument names are used for the same argument in docstring, argument list and documentation. If that's the problem then why not change all of them to use the name "address" or "addr" to avoid confusion and make it clear simple. What is your view? |
Change them all to refer to >>> ctypes.string_at(ptr=b'spam')
b'spam' |
Ok...let me review again. Having different names for the same argument can cause confusion, right? So are you telling that we should change the name to "ptr" to avoid a breaking change? If this issue is confirmed, I'm ready to submit a PR to make this change. |
The function parameter name is "ptr". Changing that name could break existing code. I don't think it's likely, but it's not worth breaking even one script just for this. So change the docstring and documentation to refer to "ptr". |
Ok..then submitting the PR in 10 minutes. |
PR opened for this issue. |
Ping |
…w]string_at() (#25384) The implementation uses 'ptr' for the name of the first parameter of ctypes.string_at() and ctypes.wstring_at(). Align docs and docstrings with the naming used in the implementation. Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
…pes' [w]string_at() (pythonGH-25384) The implementation uses 'ptr' for the name of the first parameter of ctypes.string_at() and ctypes.wstring_at(). Align docs and docstrings with the naming used in the implementation. (cherry picked from commit 81a926b) Co-authored-by: Shreyan Avigyan <shreyan.avigyan@gmail.com> Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
…pes' [w]string_at() (pythonGH-25384) The implementation uses 'ptr' for the name of the first parameter of ctypes.string_at() and ctypes.wstring_at(). Align docs and docstrings with the naming used in the implementation. (cherry picked from commit 81a926b) Co-authored-by: Shreyan Avigyan <shreyan.avigyan@gmail.com> Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
…ypes' [w]string_at() (GH-25384) (GH-118046) gh-87969: Align docs and docstrings with implementation for ctypes' [w]string_at() (GH-25384) The implementation uses 'ptr' for the name of the first parameter of ctypes.string_at() and ctypes.wstring_at(). Align docs and docstrings with the naming used in the implementation. (cherry picked from commit 81a926b) Co-authored-by: Shreyan Avigyan <shreyan.avigyan@gmail.com> Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Uh oh!
There was an error while loading. Please reload this page.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: