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.
Details
- Reviewers
- None
- Group Reviewers
hg-reviewers - Commits
- rHGfa33196088c4: revlog: replace PyInt_AS_LONG with a more portable helper function
Diff Detail
- Repository
- rHG Mercurial
- Lint
Automatic diff as part of commit; lint not applicable. - Unit
Automatic diff as part of commit; unit tests not applicable.
Event Timeline
+/* 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.
I got lazy, but we can change it if we like.
+ *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.
I don't either, but it was easy to add the if statement.
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.
Good catch, fixed.
@@ -464,7 +470,9 @@
if (iter == NULL) return -2; while ((iter_item = PyIter_Next(iter))) {
- iter_item_long = PyInt_AS_LONG(iter_item);
+ if (!pylong_to_long(iter_item, &iter_item_long)) {
+ return -1;
Py_DECREF(iter_item) and return -2.
I didn't notice it last time, sorry.
+ }
Py_DECREF(iter_item); if (iter_item_long < min_idx) min_idx = iter_item_long;