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

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Wed Nov 7 13:44:53 EST 2018


durin42 added a comment.


  In https://phab.mercurial-scm.org/D5235#78172, @yuja wrote:
  
  > > +/* 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.
  
  
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D5235

To: durin42, #hg-reviewers
Cc: yuja, mercurial-devel


More information about the Mercurial-devel mailing list