[PATCH RFC] parser: use PyInt_FromSsize_t in index_stats

Adrian Buehlmann adrian at cadifra.com
Wed May 9 05:20:30 CDT 2012


On 2012-05-09 11:06, Adrian Buehlmann wrote:
> # HG changeset patch
> # User Adrian Buehlmann <adrian at cadifra.com>
> # Date 1336550330 -7200
> # Node ID 109b67cf1a9fd715109f4ebe1018f28213ac2bff
> # Parent  ddd4996740c785cc187766249977ea3ece8c17ab
> parser: use PyInt_FromSsize_t in index_stats
> 
> Eliminates
> 
>   mercurial/parsers.c(515) : warning C4244: 'function' : conversion from
>   'Py_ssize_t' to 'long', possible loss of data
>   mercurial/parsers.c(520) : warning C4244: 'function' : conversion from
>   'Py_ssize_t' to 'long', possible loss of data
>   mercurial/parsers.c(521) : warning C4244: 'function' : conversion from
>   'Py_ssize_t' to 'long', possible loss of data
> 
> when compiling for Windows x64 target using the Microsoft compiler.
> 
> PyInt_FromSsize_t does not exist for Python 2.4 and earlier, so we define a
> fallback in util.h to use PyInt_FromLong when compiling for Python 2.4.
> 
> diff --git a/mercurial/parsers.c b/mercurial/parsers.c
> --- a/mercurial/parsers.c
> +++ b/mercurial/parsers.c
> @@ -506,13 +506,13 @@
>  		return NULL;
>  
>  #define istat(__n, __d) \
> -	if (PyDict_SetItemString(obj, __d, PyInt_FromLong(self->__n)) == -1) \
> +	if (PyDict_SetItemString(obj, __d, PyInt_FromSsize_t(self->__n)) == -1) \
>  		goto bail;
>  
>  	if (self->added) {
>  		Py_ssize_t len = PyList_GET_SIZE(self->added);
>  		if (PyDict_SetItemString(obj, "index entries added",
> -					 PyInt_FromLong(len)) == -1)
> +					 PyInt_FromSsize_t(len)) == -1)
>  			goto bail;
>  	}
>  
> diff --git a/mercurial/util.h b/mercurial/util.h
> --- a/mercurial/util.h
> +++ b/mercurial/util.h
> @@ -109,6 +109,7 @@
>  typedef int Py_ssize_t;
>  typedef Py_ssize_t (*lenfunc)(PyObject *);
>  typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
> +#define PyInt_FromSsize_t PyInt_FromLong
>  
>  #if !defined(PY_SSIZE_T_MIN)
>  #define PY_SSIZE_T_MAX INT_MAX

Windows uses the LLP64 data model, where "long" is 32 bits.

Py_ssize_t has the size of a pointer, so it's 64 bits on x64.

http://www.viva64.com/en/a/0012/
http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx
http://blogs.msdn.com/b/oldnewthing/archive/2005/01/31/363790.aspx


More information about the Mercurial-devel mailing list