Skip to content

gh-149707: Fix compiler warning in _ctypes_test on strchr()#149791

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:ctypes_test
May 15, 2026
Merged

gh-149707: Fix compiler warning in _ctypes_test on strchr()#149791
vstinner merged 1 commit into
python:mainfrom
vstinner:ctypes_test

Conversation

@vstinner
Copy link
Copy Markdown
Member

@vstinner vstinner commented May 13, 2026

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr': Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text type is "const char*".

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr':
Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards
'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text
type is "const char*".
@vstinner
Copy link
Copy Markdown
Member Author

See for example the glibc commit which introduced C23 support in string/string.h.

The code is now:

extern char *strchr (const char *__s, int __c)
     __THROW __attribute_pure__ __nonnull ((1));
# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
#  define strchr(S, C)                                         \
  __glibc_const_generic (S, const char *, strchr (S, C))
# endif

@vstinner vstinner added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels May 13, 2026
@StanFromIreland StanFromIreland changed the title Fix compiler warning in _ctypes_test on strchr() gh-149707: Fix compiler warning in _ctypes_test on strchr() May 13, 2026
@StanFromIreland
Copy link
Copy Markdown
Member

I edited the title to add the issue, someone reported this recently: #149707

}

EXPORT(char *)my_strchr(const char *s, int c)
EXPORT(const char *) my_strchr(const char *s, int c)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EXPORT(const char *) my_strchr(const char *s, int c)
EXPORT(const char *)my_strchr(const char *s, int c)

For consistency with the others.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The majority of exported functions have a space or write EXPORT() on a separated line. I would prefer to fix all functions to add a space, but I don't think that it's worth it :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I had a small sample size, I only checked a few.

@vstinner vstinner merged commit 5465b69 into python:main May 15, 2026
70 checks passed
@vstinner vstinner deleted the ctypes_test branch May 15, 2026 11:04
@miss-islington-app
Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 15, 2026

GH-149862 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 15, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 15, 2026

GH-149863 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label May 15, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 15, 2026

GH-149864 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label May 15, 2026
@vstinner vstinner added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 15, 2026
@miss-islington-app
Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 15, 2026
…thonGH-149791)

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr':
Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards
'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text
type is "const char*".
(cherry picked from commit 5465b69)

Co-authored-by: Victor Stinner <vstinner@python.org>
@miss-islington-app
Copy link
Copy Markdown

Sorry @vstinner, I had trouble completing the backport.
Please retry by removing and re-adding the "needs backport to 3.15" label.
Please backport backport using cherry_picker on the command line.

cherry_picker 5465b69255890650df99debb8256e0a7bc68138b 3.15

@vstinner vstinner added needs backport to 3.15 pre-release feature fixes, bugs and security fixes and removed needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels May 15, 2026
@miss-islington-app
Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 15, 2026
…thonGH-149791)

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr':
Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards
'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text
type is "const char*".
(cherry picked from commit 5465b69)

Co-authored-by: Victor Stinner <vstinner@python.org>
@miss-islington-app
Copy link
Copy Markdown

Sorry @vstinner, I had trouble completing the backport.
Please retry by removing and re-adding the "needs backport to 3.15" label.
Please backport backport using cherry_picker on the command line.

cherry_picker 5465b69255890650df99debb8256e0a7bc68138b 3.15

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 15, 2026

GH-149865 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 15, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 15, 2026

GH-149866 is a backport of this pull request to the 3.14 branch.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 15, 2026

GH-149867 is a backport of this pull request to the 3.13 branch.

@vstinner
Copy link
Copy Markdown
Member Author

Hum. I don't understand why the bot failed to backport the change to 3.15. I backported the change manually. I backported it to 3.15, 3.14 and 3.13 branches, I didn't notice that the bot already created backports to 3.13 and 3.14 in the meanwhile...

vstinner added a commit that referenced this pull request May 15, 2026
…149791) (#149867)

gh-149707: Fix compiler warning in _ctypes_test on strchr() (#149791)

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr':
Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards
'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text
type is "const char*".

(cherry picked from commit 5465b69)
vstinner added a commit that referenced this pull request May 15, 2026
…149791) (#149866)

gh-149707: Fix compiler warning in _ctypes_test on strchr() (#149791)

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr':
Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards
'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text
type is "const char*".

(cherry picked from commit 5465b69)
vstinner added a commit that referenced this pull request May 15, 2026
…149791) (#149865)

gh-149707: Fix compiler warning in _ctypes_test on strchr() (#149791)

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr':
Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards
'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text
type is "const char*".

(cherry picked from commit 5465b69)
@StanFromIreland
Copy link
Copy Markdown
Member

The bot is playing trick on you ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants