D5235: revlog: replace PyInt_AS_LONG with a more portable helper function

Yuya Nishihara yuya at tcha.org
Wed Nov 7 07:31:01 EST 2018


> +/* Convert a PyInt or PyLong to a long. Returns false if there is an
> +   error, in which case an exception will already have been set. */
> +static inline bool pylong_to_long(PyObject *pylong, long *out)

Nit: I prefer 0/-1 return value instead of bool since that's the convention
of CPython API.

> +	*out = PyLong_AsLong(pylong);
> +	return PyErr_Occurred() == NULL;

Perhaps, checking `*out != -1` can be a fast path, though I don't know how
much we care about the performance here.

>  static int rustla_contains(rustlazyancestorsObject *self, PyObject *rev)
>  {
> -	if (!(PyInt_Check(rev))) {
> +	long lrev;
> +	if (!pylong_to_long(rev, &lrev)) {
>  		return 0;

Needs `PyErr_Clear() since `non_int in self` isn't an error.


More information about the Mercurial-devel mailing list