Re: [Python-Dev] [Python-checkins] r43028 - python/trunk/Modules/_ctypes/cfield.c
Author: thomas.heller Date: Tue Mar 14 21:39:27 2006 New Revision: 43028
Modified: python/trunk/Modules/_ctypes/cfield.c Log: Cast an Py_ssize_t to int, to avoid a compiler warning.
Modified: python/trunk/Modules/_ctypes/cfield.c ============================================================================== --- python/trunk/Modules/_ctypes/cfield.c (original) +++ python/trunk/Modules/_ctypes/cfield.c Tue Mar 14 21:39:27 2006 @@ -251,10 +251,10 @@
if (bits) result = PyString_FromFormat("<Field type=%s, ofs=%d:%d, bits=%d>", - name, self->offset, size, bits); + name, (int)self->offset, size, bits); else result = PyString_FromFormat("<Field type=%s, ofs=%d, size=%d>", - name, self->offset, size); + name, (int)self->offset, size); return result; }
[Neal Norwitz]
This isn't exactly correct. On a 64-bit system, the value will be cast into a 32-bit integer. This is true for both Win64 and Unix. If you change the cast to a long and use %ld (lowercase ell), that will work correctly on Unix, but not Win64. To always display the correct value on all platforms, you need an #ifdef MS_WIN64. For Windows, you use %Id (that's a capital letter eye) and reference the value without a cast. For Unix, you use %ld (lowercase ell), and cast the value to a (long) to avoid a warning.
I'm copying this to python-dev because it's important people understand this :-): all of the above is kinda true but irrelevant. Martin already checked in a patch so that PyString_FromFormat() understands the C99 "z" qualifier on _all_ platforms. So the correct way to repair this one wasn't to add a cast, and much less to add an #ifdef, it was to change ofs=%d to ofs=%zd in the format. I'm going to check that change in now, but don't make me do it again :-)
On 3/15/06, Tim Peters
[Neal Norwitz]
This isn't exactly correct. On a 64-bit system, the value will be cast into a 32-bit integer. This is true for both Win64 and Unix. If you change the cast to a long and use %ld (lowercase ell), that will work correctly on Unix, but not Win64. To always display the correct value on all platforms, you need an #ifdef MS_WIN64. For Windows, you use %Id (that's a capital letter eye) and reference the value without a cast. For Unix, you use %ld (lowercase ell), and cast the value to a (long) to avoid a warning.
I'm copying this to python-dev because it's important people understand this :-): all of the above is kinda true but irrelevant.
Whoops, my comment only applies to *printf(), not PyString's as you correctly point out. For an example, see the definition for PRINT_TOTAL_REFS() in http://svn.python.org/view/python/trunk/Python/pythonrun.c?rev=42876&view=markup n --
Martin already checked in a patch so that PyString_FromFormat() understands the C99 "z" qualifier on _all_ platforms. So the correct way to repair this one wasn't to add a cast, and much less to add an #ifdef, it was to change
ofs=%d
to
ofs=%zd
in the format. I'm going to check that change in now, but don't make me do it again :-) _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/nnorwitz%40gmail.com
participants (2)
-
Neal Norwitz
-
Tim Peters