Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e225280

Browse files
committed
Enable -Wconversion
Closes diofant#202
1 parent ed0ac43 commit e225280

File tree

8 files changed

+108
-63
lines changed

8 files changed

+108
-63
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ${{ matrix.os }}
1919
env:
2020
PYTEST_ADDOPTS: --verbose
21-
CFLAGS: -Wall -Wpedantic -Werror -std=c17 -Wconversion -Wno-sign-conversion
21+
CFLAGS: -Wall -Wpedantic -Werror -std=c17 -Wconversion
2222
steps:
2323
- uses: actions/checkout@v4
2424
with:

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ${{ matrix.os }}
77
env:
88
PYTEST_ADDOPTS: --verbose
9-
CFLAGS: -Wall -Wpedantic -Werror -std=c17 -Wconversion -Wno-sign-conversion
9+
CFLAGS: -Wall -Wpedantic -Werror -std=c17 -Wconversion
1010
strategy:
1111
fail-fast: true
1212
matrix:

.github/workflows/os.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
os: [ubuntu-24.04, ubuntu-24.04-arm, macos-14]
1111
env:
1212
PYTEST_ADDOPTS: --verbose
13-
CFLAGS: -Wall -Wpedantic -Werror -std=c17 -Wconversion -Wno-sign-conversion
13+
CFLAGS: -Wall -Wpedantic -Werror -std=c17 -Wconversion
1414
steps:
1515
- uses: actions/checkout@v4
1616
with:

fmt.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
# pragma GCC diagnostic push
33
# pragma GCC diagnostic ignored "-Wnewline-eof"
44
#endif
5+
#if defined(__GNUC__) || defined(__clang__)
6+
# pragma GCC diagnostic push
7+
# pragma GCC diagnostic ignored "-Wsign-conversion"
8+
#endif
59

610
#include "pythoncapi_compat.h"
711

812
#define PY_SSIZE_T_CLEAN
913
#include <Python.h>
1014

15+
#if defined(__GNUC__) || defined(__clang__)
16+
# pragma GCC diagnostic pop
17+
#endif
1118
#if defined(__clang__)
1219
# pragma GCC diagnostic pop
1320
#endif
@@ -162,8 +169,8 @@ parse_internal_render_format_spec(PyObject *obj,
162169
PyObject *format_spec,
163170
Py_ssize_t start, Py_ssize_t end,
164171
InternalFormatSpec *format,
165-
char default_type,
166-
char default_align)
172+
Py_UCS4 default_type,
173+
Py_UCS4 default_align)
167174
{
168175
Py_ssize_t pos = start;
169176
int kind = PyUnicode_KIND(format_spec);
@@ -353,15 +360,16 @@ parse_internal_render_format_spec(PyObject *obj,
353360
break;
354361
}
355362
default:
356-
invalid_thousands_separator_type(format->thousands_separators, format->type);
363+
invalid_thousands_separator_type((int)format->thousands_separators,
364+
format->type);
357365
return 0;
358366
}
359367
}
360368

361369
if (format->type == 'n'
362370
&& format->frac_thousands_separator != LT_NO_LOCALE)
363371
{
364-
invalid_thousands_separator_type(format->frac_thousands_separator,
372+
invalid_thousands_separator_type((int)format->frac_thousands_separator,
365373
format->type);
366374
return 0;
367375
}
@@ -733,7 +741,7 @@ fill_number(PyUnicodeWriter *writer, const NumberFieldWidths *spec,
733741
}
734742
}
735743
if (spec->n_sign == 1) {
736-
PyUnicodeWriter_WriteChar(writer, spec->sign);
744+
PyUnicodeWriter_WriteChar(writer, (Py_UCS4)spec->sign);
737745
}
738746
if (spec->n_prefix) {
739747
PyUnicodeWriter_WriteSubstring(writer, prefix, p_start, spec->n_prefix + p_start);

main.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
# pragma GCC diagnostic push
33
# pragma GCC diagnostic ignored "-Wnewline-eof"
44
#endif
5+
#if defined(__GNUC__) || defined(__clang__)
6+
# pragma GCC diagnostic push
7+
# pragma GCC diagnostic ignored "-Wsign-conversion"
8+
#endif
59

610
#include "pythoncapi_compat.h"
711

812
#define PY_SSIZE_T_CLEAN
913
#include <Python.h>
1014

15+
#if defined(__GNUC__) || defined(__clang__)
16+
# pragma GCC diagnostic pop
17+
#endif
1118
#if defined(__clang__)
1219
# pragma GCC diagnostic pop
1320
#endif
@@ -39,7 +46,7 @@ _Thread_local gmp_global global = {
3946
};
4047

4148
static MPZ_Object *
42-
MPZ_new(zz_size_t size)
49+
MPZ_new(int64_t size)
4350
{
4451
MPZ_Object *res;
4552

@@ -233,7 +240,7 @@ MPZ_from_str(PyObject *obj, int base)
233240
len--;
234241
}
235242

236-
zz_err ret = zz_from_str(str, len, (int8_t)base, &res->z);
243+
zz_err ret = zz_from_str(str, (size_t)len, (int8_t)base, &res->z);
237244

238245
if (ret == ZZ_MEM) {
239246
/* LCOV_EXCL_START */
@@ -275,7 +282,7 @@ MPZ_from_int(PyObject *obj)
275282
}
276283
if (long_export.digits) {
277284
res = MPZ_new(0);
278-
if (!res || zz_import(long_export.ndigits,
285+
if (!res || zz_import((size_t)long_export.ndigits,
279286
long_export.digits, *int_layout, &res->z))
280287
{
281288
return (MPZ_Object *)PyErr_NoMemory(); /* LCOV_EXCL_LINE */
@@ -332,7 +339,8 @@ MPZ_to_int(MPZ_Object *u)
332339
size_t size = (zz_bitlen(&u->z) + int_layout->bits_per_digit
333340
- 1)/int_layout->bits_per_digit;
334341
void *digits;
335-
PyLongWriter *writer = PyLongWriter_Create(zz_isneg(&u->z), size, &digits);
342+
PyLongWriter *writer = PyLongWriter_Create(zz_isneg(&u->z),
343+
(Py_ssize_t)size, &digits);
336344

337345
if (!writer) {
338346
return NULL; /* LCOV_EXCL_LINE */
@@ -354,7 +362,7 @@ MPZ_to_int(MPZ_Object *u)
354362
}
355363

356364
static void
357-
revstr(unsigned char *s, size_t l, size_t r)
365+
revstr(unsigned char *s, Py_ssize_t l, Py_ssize_t r)
358366
{
359367
while (l < r) {
360368
unsigned char tmp = s[l];
@@ -376,7 +384,7 @@ MPZ_to_bytes(MPZ_Object *u, Py_ssize_t length, int is_little, int is_signed)
376384
}
377385

378386
uint8_t *buffer = (uint8_t *)PyBytes_AS_STRING(bytes);
379-
zz_err ret = zz_to_bytes(&u->z, length, is_signed, &buffer);
387+
zz_err ret = zz_to_bytes(&u->z, (size_t)length, is_signed, &buffer);
380388

381389
if (ret == ZZ_OK) {
382390
if (is_little && length) {
@@ -415,22 +423,22 @@ MPZ_from_bytes(PyObject *obj, int is_little, int is_signed)
415423
is_little = 0;
416424
}
417425
if (is_little) {
418-
uint8_t *tmp = malloc(length);
426+
uint8_t *tmp = malloc((size_t)length);
419427

420428
if (!tmp) {
421429
/* LCOV_EXCL_START */
422430
Py_DECREF(bytes);
423431
return (MPZ_Object *)PyErr_NoMemory();
424432
/* LCOV_EXCL_STOP */
425433
}
426-
memcpy(tmp, buffer, length);
434+
memcpy(tmp, buffer, (size_t)length);
427435
revstr(tmp, 0, length - 1);
428436
buffer = tmp;
429437
}
430438

431439
MPZ_Object *res = MPZ_new(0);
432440

433-
if (!res || zz_from_bytes(buffer, length, is_signed, &res->z)) {
441+
if (!res || zz_from_bytes(buffer, (size_t)length, is_signed, &res->z)) {
434442
/* LCOV_EXCL_START */
435443
Py_DECREF(bytes);
436444
if (is_little) {
@@ -464,7 +472,14 @@ PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
464472
}
465473

466474
Py_UCS1 *out = PyUnicode_1BYTE_DATA(result);
475+
#if defined(__GNUC__) || defined(__clang__)
476+
# pragma GCC diagnostic push
477+
# pragma GCC diagnostic ignored "-Wsign-conversion"
478+
#endif
467479
int kind = PyUnicode_KIND(unicode);
480+
#if defined(__GNUC__) || defined(__clang__)
481+
# pragma GCC diagnostic pop
482+
#endif
468483
const void *data = PyUnicode_DATA(unicode);
469484

470485
for (Py_ssize_t i = 0; i < len; ++i) {
@@ -864,7 +879,7 @@ hash(PyObject *self)
864879
Py_hash_t r;
865880

866881
assert(-(uint64_t)INT64_MIN > PyHASH_MODULUS);
867-
(void)zz_rem_u64(&u->z, PyHASH_MODULUS, (uint64_t *)&r);
882+
(void)zz_rem_u64(&u->z, (uint64_t)PyHASH_MODULUS, (uint64_t *)&r);
868883
if (negative) {
869884
(void)zz_neg(&u->z, &u->z);
870885
r = -r;
@@ -1223,7 +1238,9 @@ power(PyObject *self, PyObject *other, PyObject *module)
12231238

12241239
int64_t exp;
12251240

1226-
if (!res || zz_to_i64(&v->z, &exp) || zz_pow(&u->z, exp, &res->z)) {
1241+
if (!res || zz_to_i64(&v->z, &exp)
1242+
|| zz_pow(&u->z, (uint64_t)exp, &res->z))
1243+
{
12271244
/* LCOV_EXCL_START */
12281245
Py_CLEAR(res);
12291246
PyErr_SetNone(PyExc_MemoryError);
@@ -1624,11 +1641,11 @@ static PyObject *
16241641
__reduce_ex__(PyObject *self, PyObject *Py_UNUSED(args))
16251642
{
16261643
MPZ_Object *u = (MPZ_Object *)self;
1627-
Py_ssize_t len = zz_bitlen(&u->z);
1644+
zz_bitcnt_t len = zz_bitlen(&u->z);
16281645

16291646
return Py_BuildValue("N(N)",
16301647
PyObject_GetAttrString(self, "_from_bytes"),
1631-
MPZ_to_bytes(u, (len + 7)/8 + 1, 0, 1));
1648+
MPZ_to_bytes(u, (Py_ssize_t)(len + 7)/8 + 1, 0, 1));
16321649
}
16331650

16341651
static PyObject *
@@ -1637,7 +1654,7 @@ __sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
16371654
MPZ_Object *u = (MPZ_Object *)self;
16381655

16391656
return PyLong_FromSize_t(sizeof(MPZ_Object)
1640-
+ (u->z).alloc*sizeof(zz_limb_t));
1657+
+ (unsigned int)(u->z).alloc*sizeof(zz_limb_t));
16411658
}
16421659

16431660
static PyObject *
@@ -2046,7 +2063,7 @@ gmp_isqrt_rem(PyObject *Py_UNUSED(module), PyObject *arg)
20462063
goto err; \
20472064
} \
20482065
Py_XDECREF(x); \
2049-
if (zz_##name(n, &res->z)) { \
2066+
if (zz_##name((uint64_t)n, &res->z)) { \
20502067
/* LCOV_EXCL_START */ \
20512068
PyErr_NoMemory(); \
20522069
goto err; \
@@ -2068,7 +2085,7 @@ get_round_mode(PyObject *rndstr)
20682085
if (!PyUnicode_Check(rndstr)) {
20692086
invalid:
20702087
PyErr_SetString(PyExc_ValueError, "invalid rounding mode specified");
2071-
return -1;
2088+
return (zz_rnd)-1;
20722089
}
20732090

20742091
Py_UCS4 rndchr = PyUnicode_READ_CHAR(rndstr, 0);

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ test-command = "pytest {package}"
7878
LD_LIBRARY_PATH = "$(pwd)/.local/lib:$LD_LIBRARY_PATH"
7979
PKG_CONFIG_PATH = "$(pwd)/.local/lib/pkgconfig"
8080
PYTEST_ADDOPTS = "--verbose"
81-
CFLAGS = "-Wall -Wpedantic -Werror -std=c17 -Wconversion -Wno-sign-conversion"
81+
CFLAGS = "-Wall -Wpedantic -Werror -std=c17 -Wconversion"
8282

8383
[tool.cibuildwheel.windows]
8484
before-all = "msys2 -c scripts/cibw_before_all.sh"

0 commit comments

Comments
 (0)