Trent Mick <trentm@activestate.com>:

The common technique for printing out a pointer has been to cast to a long
and use the "%lx" printf modifier. This is incorrect on Win64 where casting
to a long truncates the pointer. The "%p" formatter should be used instead.

The problem as stated by Tim:
> Unfortunately, the C committee refused to define what %p conversion "looks
> like" -- they explicitly allowed it to be implementation-defined. Older
> versions of Microsoft C even stuck a colon in the middle of the address (in
> the days of segment+offset addressing)!

The result is that the hex value of a pointer will maybe/maybe not have a 0x
prepended to it.


Notes on the patch:

There are two main classes of changes:
- in the various repr() functions that print out pointers
- debugging printf's in the various thread_*.h files (these are why the
patch is large)


Closes SourceForge patch #100505.
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
index 72d4242..c8ebd24 100644
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -241,20 +241,20 @@
 
 	if ( self->b_base == NULL )
 	{
-		sprintf(buf, "<%s buffer ptr %lx, size %d at %lx>",
+		sprintf(buf, "<%s buffer ptr %p, size %d at %p>",
 			status,
-			(long)self->b_ptr,
+			self->b_ptr,
 			self->b_size,
-			(long)self);
+			self);
 	}
 	else
 	{
-		sprintf(buf, "<%s buffer for %lx, ptr %lx, size %d at %lx>",
+		sprintf(buf, "<%s buffer for %p, ptr %p, size %d at %p>",
 			status,
-			(long)self->b_base,
-			(long)self->b_ptr,
+			self->b_base,
+			self->b_ptr,
 			self->b_size,
-			(long)self);
+			self);
 	}
 
 	return PyString_FromString(buf);
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 4653d27..17b7bf4 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -353,11 +353,11 @@
 	else
 		name = PyString_AsString(op->cl_name);
 	if (mod == NULL || !PyString_Check(mod))
-		sprintf(buf, "<class ?.%.100s at %lx>", name, (long)op);
+		sprintf(buf, "<class ?.%.100s at %p>", name, op);
 	else
-		sprintf(buf, "<class %.50s.%.50s at %lx>",
+		sprintf(buf, "<class %.50s.%.50s at %p>",
 			PyString_AsString(mod),
-			name, (long)op);
+			name, op);
 	return PyString_FromString(buf);
 }
 
@@ -805,12 +805,12 @@
 			cname = "?";
 		PyErr_Clear();
 		if (mod == NULL || !PyString_Check(mod))
-			sprintf(buf, "<?.%.100s instance at %lx>",
-				cname, (long)inst);
+			sprintf(buf, "<?.%.100s instance at %p>",
+				cname, inst);
 		else
-			sprintf(buf, "<%.50s.%.50s instance at %lx>",
+			sprintf(buf, "<%.50s.%.50s instance at %p>",
 				PyString_AsString(mod),
-				cname, (long)inst);
+				cname, inst);
 		return PyString_FromString(buf);
 	}
 	res = PyEval_CallObject(func, (PyObject *)NULL);
@@ -1704,8 +1704,8 @@
 			icname = PyString_AsString(iclassname);
 		else
 			icname = "?";
-		sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>",
-			fcname, fname, icname, (long)self);
+		sprintf(buf, "<method %.60s.%.60s of %.60s instance at %p>",
+			fcname, fname, icname, self);
 	}
 	Py_XDECREF(funcname);
 	return PyString_FromString(buf);
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index cbc1aff..99c0dd5 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -240,11 +240,11 @@
 	PyFileObject *f;
 {
 	char buf[300];
-	sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>",
+	sprintf(buf, "<%s file '%.256s', mode '%.10s' at %p>",
 		f->f_fp == NULL ? "closed" : "open",
 		PyString_AsString(f->f_name),
 		PyString_AsString(f->f_mode),
-		(long)f);
+		f);
 	return PyString_FromString(buf);
 }
 
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 29ade28..f455ef5 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -806,8 +806,8 @@
 					char buf[100];
 					PyFloat_AsString(buf, p);
 					fprintf(stderr,
-			     "#   <float at %lx, refcnt=%d, val=%s>\n",
-						(long)p, p->ob_refcnt, buf);
+			     "#   <float at %p, refcnt=%d, val=%s>\n",
+						p, p->ob_refcnt, buf);
 				}
 			}
 			list = list->next;
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 2736d65..f91cc6d 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -202,11 +202,11 @@
 {
 	char buf[140];
 	if (op->func_name == Py_None)
-		sprintf(buf, "<anonymous function at %lx>", (long)op);
+		sprintf(buf, "<anonymous function at %p>", op);
 	else
-		sprintf(buf, "<function %.100s at %lx>",
+		sprintf(buf, "<function %.100s at %p>",
 			PyString_AsString(op->func_name),
-			(long)op);
+			op);
 	return PyString_FromString(buf);
 }
 
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 2e8939e..d182b30 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -957,8 +957,8 @@
 			     i++, p++) {
 				if (PyInt_Check(p) && p->ob_refcnt != 0)
 					fprintf(stderr,
-				"#   <int at %lx, refcnt=%d, val=%ld>\n",
-					  (long)p, p->ob_refcnt, p->ob_ival);
+				"#   <int at %p, refcnt=%d, val=%ld>\n",
+						p, p->ob_refcnt, p->ob_ival);
 			}
 			list = list->next;
 		}
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
index 580bb2f..ec98de4 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -148,9 +148,9 @@
 		sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name);
 	else
 		sprintf(buf,
-			"<built-in method %.80s of %.80s object at %lx>",
+			"<built-in method %.80s of %.80s object at %p>",
 			m->m_ml->ml_name, m->m_self->ob_type->tp_name,
-			(long)m->m_self);
+			m->m_self);
 	return PyString_FromString(buf);
 }
 
diff --git a/Objects/object.c b/Objects/object.c
index 6eaff67..694aade 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -229,12 +229,12 @@
 	}
 	else {
 		if (op->ob_refcnt <= 0)
-			fprintf(fp, "<refcnt %u at %lx>",
-				op->ob_refcnt, (long)op);
+			fprintf(fp, "<refcnt %u at %p>",
+				op->ob_refcnt, op);
 		else if (op->ob_type->tp_print == NULL) {
 			if (op->ob_type->tp_repr == NULL) {
-				fprintf(fp, "<%s object at %lx>",
-					op->ob_type->tp_name, (long)op);
+				fprintf(fp, "<%s object at %p>",
+					op->ob_type->tp_name, op);
 			}
 			else {
 				PyObject *s;
@@ -280,8 +280,8 @@
 		return PyString_FromString("<NULL>");
 	else if (v->ob_type->tp_repr == NULL) {
 		char buf[120];
-		sprintf(buf, "<%.80s object at %lx>",
-			v->ob_type->tp_name, (long)v);
+		sprintf(buf, "<%.80s object at %p>",
+			v->ob_type->tp_name, v);
 		return PyString_FromString(buf);
 	}
 	else {