This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Meta: Clean up various issues in C internals
Type: Stage: patch review
Components: Interpreter Core Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, benjamin.peterson, methane, miss-islington, petdance, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2020-03-12 03:23 by petdance, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 18950 closed petdance, 2020-03-12 03:50
PR 18973 merged petdance, 2020-03-13 03:34
PR 19152 merged petdance, 2020-03-25 03:43
PR 19170 merged petdance, 2020-03-26 03:43
PR 19185 open petdance, 2020-03-27 00:46
PR 19186 closed petdance, 2020-03-27 00:51
PR 19209 merged petdance, 2020-03-28 17:44
PR 19210 closed petdance, 2020-03-28 17:50
PR 19236 merged serhiy.storchaka, 2020-03-30 22:03
PR 19327 closed petdance, 2020-04-03 03:22
PR 19345 merged serhiy.storchaka, 2020-04-03 18:22
PR 19405 merged petdance, 2020-04-07 03:38
PR 19445 open petdance, 2020-04-09 03:56
PR 19472 merged serhiy.storchaka, 2020-04-11 10:12
PR 20508 merged ammar2, 2020-05-29 09:22
PR 24856 merged miss-islington, 2021-03-14 11:54
Messages (21)
msg363993 - (view) Author: Andy Lester (petdance) * Date: 2020-03-12 03:23
This is a meta-ticket for a number of small PRs that clean up some internals.

Issues will include:

* Removing unnecessary casts
* consting pointers that can be made const
* Removing unused function arguments
* etc
msg364443 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-03-17 16:38
New changeset 982307b9cceef36e30ac43b13032d68c3b921adc by Andy Lester in branch 'master':
bpo-39943: Remove unused self from find_nfc_index() (GH-18973)
https://github.com/python/cpython/commit/982307b9cceef36e30ac43b13032d68c3b921adc
msg365049 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-03-26 04:13
New changeset 62d21c9d900664b2ca30c2d7edd80b6628abdf62 by Andy Lester in branch 'master':
bpo-39943: Properly const the pointers in dictkeys_get_index (GH-19170)
https://github.com/python/cpython/commit/62d21c9d900664b2ca30c2d7edd80b6628abdf62
msg365126 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-03-27 01:18
What is the rationale for adding const? For example, does the PR 19185 fix any compiler warning or any bug? While PR 19185 is correct, I am not sure that we should modify the 607K lines of C code of CPython to add const everywhere. I'm fine with using const for new code, but I'm not sure that it's worth it to modify exiting code.
msg365127 - (view) Author: Andy Lester (petdance) * Date: 2020-03-27 01:41
It doesn't quiet any compiler warnings given the default compiler warnings that ./configure sets.  

However, it does quiet the -Wcast-qual compiler warning that could be a helpful addition some time in the future. I think it would be great, for example, if it were made impossible to send a pointer that points at a string literal into a function that would modify its contents.

Consting pointers also helps static analyzers like splint by letting it know the intent of the API.  It makes it possible for the analyzer to detect uninitialized buffers, for example.

All of the 20 or so patches that I've submitted, like BPO 39770 that you merged, have come about from my cranking up the warning options on both GCC and clang and sifting through the warnings.  My hope is that by making more things const, we can detect existing bugs and prevent new ones.

You say "I'm not sure that it's worth it to modify exiting code."  Is your concern the amount of work it would take to find the problems?  If so, it's not that much work when you let the compiler find the problems, which I've done.  I've already spent the time digging and validating.
msg365151 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-03-27 14:08
> However, it does quiet the -Wcast-qual compiler warning that could be a helpful addition some time in the future.

Aha, that's intesting. It helps me if I can see that your change fix a compiler warning that I can reproduce. If I build Objects/obmalloc.c with -Wcast-qual using GCC 9.2.1 (on Fedora 31), I get:

Objects/obmalloc.c:2455:66: warning: cast discards 'const' qualifier from pointer target type [-Wcast-qual]
 2455 |     fprintf(stderr, "    The %d pad bytes at tail=%p are ", SST, (void *)tail);
      |                                                                  ^


But I get no warning for pymemallocator_eq().


About the (void *) cast, it was added by Zackery Spytz in bpo-36594:

commit 1a2252ed39bc1b71cdaa935d7726d82909af93ab
Author: Zackery Spytz <zspytz@gmail.com>
Date:   Mon May 6 10:56:51 2019 -0600

    bpo-36594: Fix incorrect use of %p in format strings (GH-12769)
    
    In addition, fix some other minor violations of C99.

(...)
-    fprintf(stderr, "    The %d pad bytes at tail=%p are ", SST, tail);
+    fprintf(stderr, "    The %d pad bytes at tail=%p are ", SST, (void *)tail);
(...)

Extract of the issue:

"""
gcc warns with -pedantic:

ptr.c: In function ‘main’:
ptr.c:5:13: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int *’ [-Wformat=]
     printf ("%p", &i);
"""

We should check that GCC doesn't emit warning with -pedantic nor -Wcast-qual, and when both flags are combined :-)
msg365155 - (view) Author: Andy Lester (petdance) * Date: 2020-03-27 15:14
Casting tail to (void *)tail was the correct thing to do.  The problem is that casting to void* casts away the constness of tail.  The even more correct thing to do is what my patch does, which is cast it to (const void *)tail.

There is no functional difference between sending a const void * and a void * to fprintf.  However, it's one more bit of noise for -Wcast-qual to gripe about.  My hope is to clear up the noise to find the real problems.

For example, if you had this very bad code:

    const char *msg = "literal";
    strcpy(msg, "another string");

then the compiler would complain you're passing a non-const char * to the first arg of strcpy that wants a const char *.  That's a good thing.

However, you could naively change that to:

    strcpy((char *)msg, "another string");

and that would compile just fine, but the code would still be a big problem. It would require -Wcast-qual to warn you that you're casting away the constness of a pointer.

...

For pymemallocator_eq, changing the arguments to const doesn't quiet any warnings in this case with this one function.  However, it's good to make them const because the arguments are not getting modified.  They're getting passed to memcmp(), which properly takes const void *.
msg365346 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-03-30 20:19
New changeset fc2d8d62af25be90f5fd490df141a775d9619b23 by Andy Lester in branch 'master':
bpo-39943: Remove unnecessary casts in import.c that remove constness (GH-19209)
https://github.com/python/cpython/commit/fc2d8d62af25be90f5fd490df141a775d9619b23
msg365418 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-03-31 20:23
New changeset 2c003eff8fef430f1876adf88e466bcfcbd0cc9e by Serhiy Storchaka in branch 'master':
bpo-39943: Clean up marshal.c. (GH-19236)
https://github.com/python/cpython/commit/2c003eff8fef430f1876adf88e466bcfcbd0cc9e
msg366098 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2020-04-10 01:05
New changeset 38ada3bac8205a7690d573d715b0e84e60297c4c by Andy Lester in branch 'master':
bpo-39943: Keep constness of pointer objects. (19405)
https://github.com/python/cpython/commit/38ada3bac8205a7690d573d715b0e84e60297c4c
msg366194 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-11 07:48
New changeset cd8295ff758891f21084a6a5ad3403d35dda38f7 by Serhiy Storchaka in branch 'master':
bpo-39943: Add the const qualifier to pointers on non-mutable PyUnicode data. (GH-19345)
https://github.com/python/cpython/commit/cd8295ff758891f21084a6a5ad3403d35dda38f7
msg366242 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-12 11:58
New changeset 8f87eefe7f0576c05c488874eb9601a7a87c7312 by Serhiy Storchaka in branch 'master':
bpo-39943: Add the const qualifier to pointers on non-mutable PyBytes data. (GH-19472)
https://github.com/python/cpython/commit/8f87eefe7f0576c05c488874eb9601a7a87c7312
msg366355 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-04-14 00:51
Not sure if it's related to changes made recently on this issue, but I started to notice these compiler warnings on Windows:



D:\a\cpython\cpython\Modules\sre_lib.h(822,21): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1058,17): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1064,17): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1134,13): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(822,21): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1058,17): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1064,17): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1134,13): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(822,21): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1058,17): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1064,17): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\sre_lib.h(1134,13): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\_sre.c(457,26): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
D:\a\cpython\cpython\Modules\_sre.c(471,26): warning C4090: 'function': different 'const' qualifiers [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
msg366373 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-14 08:03
Yes, it is a consequence of PR 19345. But it looks like a compiler bug. It complains about implicit conversion from `const void **` to `void *` in memcpy() and PyMem_Del().
msg366391 - (view) Author: Andy Lester (petdance) * Date: 2020-04-14 14:58
I remember coming across a similar error from GCC about casting from a const double pointer to a single pointer void and it said (I believe) something about having to have each cast having to be valid.  I think it was implying something like that if you have 

const void **p

you have to cast that as

(void *)(const void *)p

I will see if I can find that message and/or I can find out more about this cast problem in the Windows compiler.
msg366642 - (view) Author: Andy Lester (petdance) * Date: 2020-04-17 03:49
I'm assuming that you're getting this sre_lib.h error when compiling Modules/_sre.c.
msg366942 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-04-21 21:23
> Yes, it is a consequence of PR 19345. But it looks like a compiler bug. It complains about implicit conversion from `const void **` to `void *` in memcpy() and PyMem_Del().

Is it possible to add an explicit cast to make the compiler warnings quiet?
msg370280 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2020-05-29 08:16
For sre.c, this is definitely a bug in MSVC, see:

* https://stackoverflow.com/questions/10403713/why-does-visual-c-warn-on-implicit-cast-from-const-void-to-void-in-c-but
* https://godbolt.org/z/BpMqA_


I'm trying to get rid of MSVC warnings to unblock https://github.com/python/cpython/pull/18532 so I'll make a pull request that adds explicit casts for this.
msg370564 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-01 17:21
New changeset 06e3a27a3c863495390a07c695171a8e62a6e0d2 by Ammar Askar in branch 'master':
bpo-39943: Fix MSVC warnings in sre extension (GH-20508)
https://github.com/python/cpython/commit/06e3a27a3c863495390a07c695171a8e62a6e0d2
msg370565 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-01 17:24
If someone is interested, there is a one remaining compiler warning in frameobject.c which is likely easy to fix:

D:\a\cpython\cpython\Objects\frameobject.c(400,1): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

See https://bugs.python.org/issue40228#msg368383

---

Moreover, the following change introduced a warning in pythonrun.c:

D:\a\cpython\cpython\Python\pythonrun.c(579,21): warning C4244: '=': conversion from 'Py_ssize_t' to 'int', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

commit 15bc9ab301d73f20bff47a12ef05326feb40f797
Author: Guido van Rossum <guido@python.org>
Date:   Thu May 14 19:22:48 2020 -0700

    bpo-40612: Fix SyntaxError edge cases in traceback formatting (GH-20072)
msg388667 - (view) Author: miss-islington (miss-islington) Date: 2021-03-14 12:17
New changeset cf8d6ef9629dc1bffadfcec251a2ffe30d5addaa by Miss Islington (bot) in branch '3.9':
bpo-39943: Fix MSVC warnings in sre extension (GH-20508)
https://github.com/python/cpython/commit/cf8d6ef9629dc1bffadfcec251a2ffe30d5addaa
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84124
2021-03-14 12:17:39miss-islingtonsetmessages: + msg388667
2021-03-14 11:54:06miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request23616
2020-06-01 17:24:49vstinnersetmessages: + msg370565
2020-06-01 17:21:48vstinnersetmessages: + msg370564
2020-05-29 09:22:00ammar2setpull_requests: + pull_request19754
2020-05-29 08:16:12ammar2setnosy: + ammar2
messages: + msg370280
2020-04-21 21:23:14vstinnersetmessages: + msg366942
2020-04-17 03:49:54petdancesetmessages: + msg366642
2020-04-14 14:58:40petdancesetmessages: + msg366391
2020-04-14 08:03:56serhiy.storchakasetmessages: + msg366373
2020-04-14 00:51:26vstinnersetnosy: + vstinner
messages: + msg366355
2020-04-12 11:58:30serhiy.storchakasetmessages: + msg366242
2020-04-11 10:12:34serhiy.storchakasetpull_requests: + pull_request18827
2020-04-11 07:48:46serhiy.storchakasetmessages: + msg366194
2020-04-10 13:35:40vstinnersetnosy: - vstinner
2020-04-10 01:05:45benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg366098
2020-04-09 16:33:51brett.cannonsetnosy: - brett.cannon
2020-04-09 03:56:29petdancesetpull_requests: + pull_request18800
2020-04-07 03:38:40petdancesetpull_requests: + pull_request18766
2020-04-03 18:22:21serhiy.storchakasetpull_requests: + pull_request18709
2020-04-03 03:22:08petdancesetpull_requests: + pull_request18692
2020-03-31 20:23:27serhiy.storchakasetmessages: + msg365418
2020-03-30 22:03:46serhiy.storchakasetpull_requests: + pull_request18593
2020-03-30 20:19:18brett.cannonsetnosy: + brett.cannon
messages: + msg365346
2020-03-28 17:50:49petdancesetpull_requests: + pull_request18573
2020-03-28 17:44:18petdancesetpull_requests: + pull_request18572
2020-03-27 15:14:42petdancesetmessages: + msg365155
2020-03-27 14:08:55vstinnersetmessages: + msg365151
2020-03-27 01:41:11petdancesetmessages: + msg365127
2020-03-27 01:18:41vstinnersetmessages: + msg365126
2020-03-27 00:51:47petdancesetpull_requests: + pull_request18545
2020-03-27 00:46:32petdancesetpull_requests: + pull_request18544
2020-03-26 04:13:09methanesetnosy: + methane
messages: + msg365049
2020-03-26 03:43:50petdancesetpull_requests: + pull_request18530
2020-03-25 03:43:45petdancesetpull_requests: + pull_request18513
2020-03-17 16:38:16vstinnersetnosy: + vstinner
messages: + msg364443
2020-03-13 03:34:47petdancesetpull_requests: + pull_request18322
2020-03-12 05:30:10serhiy.storchakasetnosy: + serhiy.storchaka
2020-03-12 03:50:06petdancesetkeywords: + patch
stage: patch review
pull_requests: + pull_request18301
2020-03-12 03:31:27petdancesettitle: Meta: Clean up various issues -> Meta: Clean up various issues in C internals
2020-03-12 03:23:07petdancecreate