diff mercurial/cext/util.h @ 40598:fa33196088c4

revlog: replace PyInt_AS_LONG with a more portable helper function PyInt_AS_LONG disappears on Python, and our previous #define was producing some problems on Python 3. Let's give up and make an inline helper function that makes this more sane. Differential Revision: https://phab.mercurial-scm.org/D5235
author Augie Fackler <augie@google.com>
date Tue, 06 Nov 2018 11:12:56 -0500
parents 9a639a33ad1f
children 84391ddf4c78
line wrap: on
line diff
--- a/mercurial/cext/util.h	Mon Nov 12 22:51:36 2018 +0900
+++ b/mercurial/cext/util.h	Tue Nov 06 11:12:56 2018 -0500
@@ -58,4 +58,17 @@
 	return _PyDict_NewPresized(((1 + expected_size) / 2) * 3);
 }
 
+/* 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)
+{
+	*out = PyLong_AsLong(pylong);
+	/* Fast path to avoid hitting PyErr_Occurred if the value was obviously
+	 * not an error. */
+	if (*out != -1) {
+		return true;
+	}
+	return PyErr_Occurred() == NULL;
+}
+
 #endif /* _HG_UTIL_H_ */