Skip to content

Commit

Permalink
Fix some compiler warnings (#1774)
Browse files Browse the repository at this point in the history
This PR fixes some compiler warnings that I noticed in the failed
Windows build in PR #1773. There are two classes of fixes:

- Fix occurrences of `int` used to represent the size of a tuple / list
/ dict. Those should be `Py_ssize_t`
- Fix two functions that treat a mask as having type `int`, when it
should be `unsigned int`. All the actual masks used have type `unsigned
int`.

The warnings themselves are relatively harmless, but silencing them
gives us a better chance of noticing warnings that we _should_ be taking
notice of.

The PR also adds a `-v` flag to the `pip install` commands that install
Traits, so that compiler warnings will always be visible in the build
log. Without the `-v`, we only get to see the warnings for failed
builds.
  • Loading branch information
mdickinson authored Mar 26, 2024
1 parent 1b9bcc8 commit 53654f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-core-traits-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
# setuptools may not exist in a newly-created venv
# https://github.com/python/cpython/issues/95299
python -m pip uninstall -y setuptools
python -m pip install .
python -m pip install -v .
python -m pip list
- name: Test Traits package
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-traits-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- name: Install dependencies and local packages
- name: Install test dependencies and local package
run: |
python -m pip install .[test]
python -m pip install -v .[test]
- name: Create clean test directory
run: |
mkdir testdir
Expand Down
49 changes: 24 additions & 25 deletions traits/ctraits.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ set_value(PyObject **field, PyObject *value)
+----------------------------------------------------------------------------*/

static PyObject *
get_trait_flag(trait_object *trait, int mask)
get_trait_flag(trait_object *trait, unsigned int mask)
{
if (trait->flags & mask) {
Py_RETURN_TRUE;
Expand All @@ -557,7 +557,7 @@ get_trait_flag(trait_object *trait, int mask)
+----------------------------------------------------------------------------*/

static int
set_trait_flag(trait_object *trait, int mask, PyObject *value)
set_trait_flag(trait_object *trait, unsigned int mask, PyObject *value)
{
int flag = PyObject_IsTrue(value);

Expand Down Expand Up @@ -890,7 +890,6 @@ has_traits_getattro(has_traits_object *obj, PyObject *name)
static PyObject *
get_trait(has_traits_object *obj, PyObject *name, int instance)
{
int i, n;
PyDictObject *itrait_dict;
trait_object *trait;
trait_object *itrait;
Expand Down Expand Up @@ -955,13 +954,13 @@ get_trait(has_traits_object *obj, PyObject *name, int instance)

/* Copy the class trait's notifier list into the instance trait: */
if ((notifiers = trait->notifiers) != NULL) {
n = PyList_GET_SIZE(notifiers);
Py_ssize_t n = PyList_GET_SIZE(notifiers);
itrait->notifiers = inotifiers = (PyListObject *)PyList_New(n);
if (inotifiers == NULL) {
return NULL;
}

for (i = 0; i < n; i++) {
for (Py_ssize_t i = 0; i < n; i++) {
item = PyList_GET_ITEM(notifiers, i);
PyList_SET_ITEM(inotifiers, i, item);
Py_INCREF(item);
Expand Down Expand Up @@ -3261,7 +3260,7 @@ validate_trait_type(
PyObject *value)
{
PyObject *type_info = trait->py_validate;
int kind = PyTuple_GET_SIZE(type_info);
Py_ssize_t kind = PyTuple_GET_SIZE(type_info);

if (((kind == 3) && (value == Py_None))
|| PyObject_TypeCheck(
Expand All @@ -3283,7 +3282,7 @@ validate_trait_instance(
PyObject *value)
{
PyObject *type_info = trait->py_validate;
int kind = PyTuple_GET_SIZE(type_info);
Py_ssize_t kind = PyTuple_GET_SIZE(type_info);

if (((kind == 3) && (value == Py_None))
|| (PyObject_IsInstance(value, PyTuple_GET_ITEM(type_info, kind - 1))
Expand Down Expand Up @@ -3668,13 +3667,12 @@ validate_trait_tuple_check(
{
trait_object *itrait;
PyObject *bitem, *aitem, *tuple;
int i, j, n;

if (PyTuple_Check(value)) {
n = PyTuple_GET_SIZE(traits);
Py_ssize_t n = PyTuple_GET_SIZE(traits);
if (n == PyTuple_GET_SIZE(value)) {
tuple = NULL;
for (i = 0; i < n; i++) {
for (Py_ssize_t i = 0; i < n; i++) {
bitem = PyTuple_GET_ITEM(value, i);
itrait = (trait_object *)PyTuple_GET_ITEM(traits, i);
if (itrait->validate == NULL) {
Expand All @@ -3701,7 +3699,7 @@ validate_trait_tuple_check(
if (tuple == NULL) {
return NULL;
}
for (j = 0; j < i; j++) {
for (Py_ssize_t j = 0; j < i; j++) {
bitem = PyTuple_GET_ITEM(value, j);
Py_INCREF(bitem);
PyTuple_SET_ITEM(tuple, j, bitem);
Expand Down Expand Up @@ -3747,7 +3745,7 @@ validate_trait_coerce_type(
trait_object *trait, has_traits_object *obj, PyObject *name,
PyObject *value)
{
int i, n;
Py_ssize_t i, n;
PyObject *type2;

PyObject *type_info = trait->py_validate;
Expand Down Expand Up @@ -3991,36 +3989,36 @@ validate_trait_complex(
trait_object *trait, has_traits_object *obj, PyObject *name,
PyObject *value)
{
int i, j, k, kind, in_range;
int in_range;
long mode, rc;
PyObject *result, *type_info, *type, *type2, *args;

PyObject *list_type_info = PyTuple_GET_ITEM(trait->py_validate, 1);
int n = PyTuple_GET_SIZE(list_type_info);
for (i = 0; i < n; i++) {
Py_ssize_t n = PyTuple_GET_SIZE(list_type_info);
for (Py_ssize_t i = 0; i < n; i++) {
type_info = PyTuple_GET_ITEM(list_type_info, i);

switch (PyLong_AsLong(PyTuple_GET_ITEM(type_info, 0))) {
case 0: /* Type check: */
kind = PyTuple_GET_SIZE(type_info);
case 0: { /* Type check: */
Py_ssize_t kind = PyTuple_GET_SIZE(type_info);
if (((kind == 3) && (value == Py_None))
|| PyObject_TypeCheck(
value, (PyTypeObject *)PyTuple_GET_ITEM(
type_info, kind - 1))) {
goto done;
}
break;

case 1: /* Instance check: */
kind = PyTuple_GET_SIZE(type_info);
}
case 1: { /* Instance check: */
Py_ssize_t kind = PyTuple_GET_SIZE(type_info);
if (((kind == 3) && (value == Py_None))
|| (PyObject_IsInstance(
value, PyTuple_GET_ITEM(type_info, kind - 1))
> 0)) {
goto done;
}
break;

}
case 2: /* Self type check: */
if (((PyTuple_GET_SIZE(type_info) == 2) && (value == Py_None))
|| PyObject_TypeCheck(value, Py_TYPE(obj))) {
Expand Down Expand Up @@ -4101,8 +4099,9 @@ validate_trait_complex(
goto done;
}

k = PyTuple_GET_SIZE(type_info);
for (j = 2; j < k; j++) {
Py_ssize_t k = PyTuple_GET_SIZE(type_info);
Py_ssize_t j = 2;
for (; j < k; j++) {
type2 = PyTuple_GET_ITEM(type_info, j);
if (type2 == Py_None) {
break;
Expand Down Expand Up @@ -4313,7 +4312,7 @@ _trait_set_validate(trait_object *trait, PyObject *args)
{
PyObject *validate;
PyObject *v1, *v2, *v3;
int n, kind;
Py_ssize_t kind;

if (!PyArg_ParseTuple(args, "O", &validate)) {
return NULL;
Expand All @@ -4325,7 +4324,7 @@ _trait_set_validate(trait_object *trait, PyObject *args)
}

if (PyTuple_CheckExact(validate)) {
n = PyTuple_GET_SIZE(validate);
Py_ssize_t n = PyTuple_GET_SIZE(validate);
if (n > 0) {
kind = PyLong_AsLong(PyTuple_GET_ITEM(validate, 0));

Expand Down

0 comments on commit 53654f9

Please sign in to comment.