diff options
author | Philipp Wagner <mail@philipp-wagner.com> | 2023-05-15 20:51:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-15 20:51:07 +0200 |
commit | f797c86c31f4bceabb386278f0d1686cb218da9b (patch) | |
tree | 1504bb2ed9d9a2ae81007d1939129915e406a34e /docs/examples/userguide/numpy_tutorial/compute_py.py | |
parent | 663c924462adec202c9f469fa346bda1b2264dfa (diff) | |
download | cython-f797c86c31f4bceabb386278f0d1686cb218da9b.tar.gz |
Fix a signedness compiler warning in vector.to_py (GH-5438)
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
https://github.com/cython/cython/pull/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.
Diffstat (limited to 'docs/examples/userguide/numpy_tutorial/compute_py.py')
0 files changed, 0 insertions, 0 deletions