Skip to content

Commit

Permalink
more consistent function behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
ilanschnell committed Dec 17, 2023
1 parent e66f1db commit b88ce4e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions bitarray/_bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,9 @@ find_bit(bitarrayobject *self, int vi, Py_ssize_t a, Py_ssize_t b, int right)
return -1;
}

/* Returns:
-1: on error and sets exception
0, 1: value of integer sub or first item of sub if bitarray of length 1
/* Given sub_bitarray, return:
-1: on error (after setting exception)
0, 1: value of integer sub or single item of sub if bitarray of length 1
2: when sub is bitarray of length 0, 2, 3, ...
*/
static int
Expand Down Expand Up @@ -2593,7 +2593,7 @@ bitwise_check(PyObject *a, PyObject *b, const char *ostr)
return -1;
}

if (unequal_size_or_endian((bitarrayobject *) a, (bitarrayobject *) b))
if (ensure_eq_size_endian((bitarrayobject *) a, (bitarrayobject *) b) < 0)
return -1;

return 0;
Expand Down
4 changes: 2 additions & 2 deletions bitarray/_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ binary_function(PyObject *args, const char *format, const char oper)
bitarray_type_obj, (PyObject *) &a,
bitarray_type_obj, (PyObject *) &b))
return NULL;
if (unequal_size_or_endian(a, b))
if (ensure_eq_size_endian(a, b) < 0)
return NULL;

wbuff_a = WBUFF(a);
Expand Down Expand Up @@ -352,7 +352,7 @@ correspond_all(PyObject *module, PyObject *args)
bitarray_type_obj, (PyObject *) &a,
bitarray_type_obj, (PyObject *) &b))
return NULL;
if (unequal_size_or_endian(a, b))
if (ensure_eq_size_endian(a, b) < 0)
return NULL;

cwords = a->nbits / 64; /* complete 64-bit words */
Expand Down
10 changes: 5 additions & 5 deletions bitarray/bitarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,20 @@ conv_pybit(PyObject *value, int *vi)
return 1;
}

/* return 1 when a and b have unequal length or bit-endianness and set
exception, otherwise (when length and endianness are equal) return 0 */
/* Return 0 if bitarrays have equal length and bit-endianness.
Otherwise, set exception and return -1. */
static inline int
unequal_size_or_endian(bitarrayobject *a, bitarrayobject *b)
ensure_eq_size_endian(bitarrayobject *a, bitarrayobject *b)
{
if (a->nbits != b->nbits) {
PyErr_SetString(PyExc_ValueError,
"bitarrays of equal length expected");
return 1;
return -1;
}
if (a->endian != b->endian) {
PyErr_SetString(PyExc_ValueError,
"bitarrays of equal bit-endianness expected");
return 1;
return -1;
}
return 0;
}

0 comments on commit b88ce4e

Please sign in to comment.