[Python-Dev] FW: 64-bit port of Python

Tim Peters tim_one@email.msn.com
Tue, 8 Feb 2000 20:29:24 -0500


Raises some interesting issues wrt 64-bit Windows.

-----Original Message-----
From: Trent Mick [mailto:trentm@ActiveState.com]
Sent: Tuesday, February 08, 2000 9:35 AM
To: Tim Peters; python-list@python.org
Subject: RE: 64-bit port of Python

[Trent and Tim talk about 64-bit porting issues in the Python code. Trent
also sneakily changes email addresses.]

[Tim Peters]:
> It's not what you suspect <wink>.  Almost everything boiled down
> to mistaken
> and unintended assumptions that sizeof(int) == sizeof(long), in and around
> the implementations of (unbounded) long arithmetic, and
> overflow-checking of
> int arithmetic.  All that stuff was fixed then.  AFAIK, core Python code
> *never* casts a pointer to any sort of int, or vice versa, either
> explicitly or implicitly.

A couple of example where I think the Python core does just that:

"Modules/arraymodule.c::728":

  static PyObject *
  array_buffer_info(self, args)
  	arrayobject *self;
  	PyObject *args;
  {
  	return Py_BuildValue("ll",
  			     (long)(self->ob_item), (long)(self->ob_size));
  }

where 'ob_item' is a pointer. "Python/bltinmodule.c::899":

  static PyObject *
  builtin_id(self, args)
	PyObject *self;
	PyObject *args;
  {
	PyObject *v;

	if (!PyArg_ParseTuple(args, "O:id", &v))
		return NULL;
	return PyInt_FromLong((long)v);
  }

Python sort of relies on C's 'long' to be the largest native integer. This
is evidenced by all the use of PyInt_FromLong() above. There are no format
specifiers in the PyArg_Parse*() and Py_BuildValue() functions for
converting a pointer. This was fine when 'long' would do. On Win64
sizeof(long)==4 and size(void*)==8.

I think this also brings up some wider issues in the Python source. For
instance, the python integer type uses the C 'long' type. Was it the
implicit intention that this be the system's largest native integral type,
i.e. 32-bits on a 32 sys and 64-bits on a 64-bit system? If so, then the
representation of the Python integer type will have to change (i.e. the use
of 'long' cannot be relied upon). One should then carry through and change
(or obselete) the *_AsLong(), *_FromLong() Python/C API functions to become
something like *_AsLargestNativeInt(), *_FromLargestNativeInt()  (or some
less bulky name).

Alternatively, if the python integer type continues to use the C 'long' type
for 64-bit systems then the following ugly thing happens:
 - A Python integer on a 64-bit Intel chip compiled with MSVC is 32-bits
wide.
 - A Python integer on a 64-bit Intel chip compiled with gcc is 64-bits
wide.
That cannot be good.