Skip to content
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

test: Explicitly mark char as signed in dtype tests #5545

Merged
merged 1 commit into from
Mar 6, 2025

Conversation

pganssle-google
Copy link
Contributor

@pganssle-google pganssle-google commented Feb 27, 2025

Description

When building in an environment where char is unsigned by default (e.g. due to -funsigned-char).

This explicitly uses signed char in two tests where the sign of the char is important (though only test_dtype_normalized_num fails without this change).

Example failure:

$ g++ --version
g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
g++ -o pybind11/tests/test_numpy_dtypes.os -c -std=c++20 -fPIC -fvisibility=hidden -O0 -g -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -Wunused-result -Werror -funsigned-char -Wpedantic -isystem /usr/include/python3.12 -isystem /usr/include/eigen3 -DPYBIND11_INTERNALS_VERSION=10000000 -DPYBIND11_SMART_HOLDER_PADDING_ON -DPYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX -DPYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD_IF_AVAILABLE -DPYBIND11_TEST_BOOST -Ipybind11/include -Ipybind11/include -Ipybind11/include pybind11/tests/test_numpy_dtypes.cpp
        for a, b in m.test_dtype_normalized_num():
>           assert a == b
E           assert 1 == 2

a          = 1
b          = 2
byteorder  = 'little'
d1         = dtype({'names': ['a', 'b'], 'formats': ['<i4', '<f8'], 'offsets': [1, 10], 'itemsize': 20})
d2         = dtype([('a', '<i4'), ('b', '<f4')])
e          = '<'
expected_chars = ['b', 'h', 'i', 'l', 'q', 'B', ...]
simple_dtype = dtype({'names': ['bool_', 'uint_', 'float_', 'ldbl_'], 'formats': ['?', '<u4', '<f4', '<f16'], 'offsets': [0, 4, 8, 16], 'itemsize': 32})

test_numpy_dtypes.py:195: AssertionError
===================================================== short test summary info =====================================================
SKIPPED [1] test_stl.py:157: no <experimental/optional>
FAILED test_numpy_dtypes.py::test_dtype - assert 1 == 2
============================================ 1 failed, 1162 passed, 1 skipped in 6.04s ============================================

Suggested changelog entry:

Explicitly used ``signed char`` for two numpy dtype tests. As seen when
compiling using ``clang`` on Linux with the ``-funsigned-char`` flag.

When run on a platform where `char` is unsigned by default — which is
implementation-defined.

This explicitly uses `signed char` in two tests where the sign of the
char is important (though only `test_dtype_normalized_num` fails without
this change).
@rwgk
Copy link
Collaborator

rwgk commented Mar 4, 2025

Looks good to me.

@seberg Is there a chance that you could take a quick look at this 2-lines-test-only change? Is that a good way of dealing with the situation?

@pganssle-google It would be great if you could record in the PR description what the platform is that made this change necessary. Simply: OS, compiler version, example compilation command (with all options; doesn't have to be pretty).

@seberg
Copy link
Contributor

seberg commented Mar 4, 2025

Looks right to me, surprising nobody noticed. NumPy has no "char", just signed/unsigned byte a dtype.

@pganssle-google
Copy link
Contributor Author

@pganssle-google It would be great if you could record in the PR description what the platform is that made this change necessary. Simply: OS, compiler version, example compilation command (with all options; doesn't have to be pretty).

You might know better than me since you used to own pybind11 in Google, but I believe we're using clang, and looking at the build command, I think building with -funsigned-char should trigger this issue.

I will edit the original PR description, let me know (or edit directly if you can?) if you want any other changes.

@rwgk
Copy link
Collaborator

rwgk commented Mar 5, 2025

You might know better than me since you used to own pybind11 in Google, but I believe we're using clang, and looking at the build command, I think building with -funsigned-char should trigger this issue.

Confirmed locally, thanks! Same with gcc.

We should probably have some builds in our CI using -funsigned-char. I'll try to do that soon.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

I'll merge this now, so that it doesn't become a stumbling block for @nevedaren.

We can add testing with -funsigned-char later.

@rwgk rwgk merged commit 79be5c8 into pybind:master Mar 6, 2025
79 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs changelog Possibly needs a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants