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

Fix a signedness compiler warning in vector.to_py #5438

Merged
merged 1 commit into from
May 15, 2023

Conversation

imphil
Copy link
Contributor

@imphil imphil commented May 15, 2023

When compiling cythonized code which uses std::vector we get the following compiler warning on GCC 8 and Python 3.9 (which is turned into an error in our case):

my_file.cpp: In function ‘PyObject* __pyx_convert_vector_to_py_int(const std::vector<int>&)’:
my_file.cpp:4716:33: warning: comparison of integer expressions of different signedness: ‘Py_ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Wsign-compare]
   for (__pyx_t_5 = 0; __pyx_t_5 < __pyx_t_4; __pyx_t_5+=1) {
                       ~~~~~~~~~~^~~~~~~~~~~

The generated code in question is as follows:

/* "vector.to_py":75
 *     cdef object item
 *
 *     for i in range(v.size()):             # <<<<<<<<<<<<<<
 *         item = v[i]
 *         Py_INCREF(item)
 */
  __pyx_t_3 = __pyx_v_v.size();
  __pyx_t_4 = __pyx_t_3;
  for (__pyx_t_5 = 0; __pyx_t_5 < __pyx_t_4; __pyx_t_5+=1) {
    __pyx_v_i = __pyx_t_5;

__pyx_t_5 is of type ‘Py_ssize_t’ (signed), and __pyx_t_4 aka __pyx_t_3 is size_t (unsigned), causing GCC to rightfully complain.

Fix the generated code by explicitly using the signed variant of the vector's size in the loop.

This bug has been introduced in
#4081, which also contains some discussion on the use of signed vs unsigned types. This patch chooses to keep the status quo and only fixes the compiler warning.

When compiling cythonized code which uses `std::vector` we get the
following compiler warning on GCC 8 and Python 3.9 (which is turned into
an error in our case):

```
my_file.cpp: In function ‘PyObject* __pyx_convert_vector_to_py_int(const std::vector<int>&)’:
my_file.cpp:4716:33: warning: comparison of integer expressions of different signedness: ‘Py_ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Wsign-compare]
   for (__pyx_t_5 = 0; __pyx_t_5 < __pyx_t_4; __pyx_t_5+=1) {
                       ~~~~~~~~~~^~~~~~~~~~~
```

The generated code in question is as follows:

```
/* "vector.to_py":75
 *     cdef object item
 *
 *     for i in range(v.size()):             # <<<<<<<<<<<<<<
 *         item = v[i]
 *         Py_INCREF(item)
 */
  __pyx_t_3 = __pyx_v_v.size();
  __pyx_t_4 = __pyx_t_3;
  for (__pyx_t_5 = 0; __pyx_t_5 < __pyx_t_4; __pyx_t_5+=1) {
    __pyx_v_i = __pyx_t_5;
```

`__pyx_t_5` is of type `‘Py_ssize_t’` (signed), and `__pyx_t_4` aka
`__pyx_t_3` is `size_t` (unsigned), causing GCC to rightfully complain.

Fix the generated code by explicitly using the signed variant of the
vector's size in the loop.

This bug has been introduced in
cython#4081, which also contains some
discussion on the use of signed vs unsigned types. This patch chooses to
keep the status quo and only fixes the compiler warning.
@scoder scoder added this to the 3.0 milestone May 15, 2023
@scoder
Copy link
Contributor

scoder commented May 15, 2023

Thanks, that looks god.

@scoder scoder merged commit f797c86 into cython:master May 15, 2023
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