Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a44d353

Browse files
committed
Trent Mick <[email protected]>:
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.
1 parent d49e5b4 commit a44d353

22 files changed

Lines changed: 153 additions & 153 deletions

Modules/_tkinter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ Tktt_Repr(self)
17051705
TkttObject *v = (TkttObject *)self;
17061706
char buf[100];
17071707

1708-
sprintf(buf, "<tktimertoken at 0x%lx%s>", (long)v,
1708+
sprintf(buf, "<tktimertoken at %p%s>", v,
17091709
v->func == NULL ? ", handler deleted" : "");
17101710
return PyString_FromString(buf);
17111711
}

Modules/flmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ generic_repr(g)
435435
genericobject *g;
436436
{
437437
char buf[100];
438-
sprintf(buf, "<FORMS_object at %lx, objclass=%d>",
439-
(long)g, g->ob_generic->objclass);
438+
sprintf(buf, "<FORMS_object at %p, objclass=%d>",
439+
g, g->ob_generic->objclass);
440440
return PyString_FromString(buf);
441441
}
442442

@@ -1906,8 +1906,8 @@ form_repr(f)
19061906
formobject *f;
19071907
{
19081908
char buf[100];
1909-
sprintf(buf, "<FORMS_form at %lx, window=%ld>",
1910-
(long)f, f->ob_form->window);
1909+
sprintf(buf, "<FORMS_form at %p, window=%ld>",
1910+
f, f->ob_form->window);
19111911
return PyString_FromString(buf);
19121912
}
19131913

Modules/mpzmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ mpz_format(objp, base, withname)
262262

263263
#ifdef MPZ_DEBUG
264264
fprintf(stderr,
265-
"mpz_format: cp (str end) 0x%x, begin 0x%x, diff %d, i %d\n",
265+
"mpz_format: cp (str end) %p, begin %p, diff %d, i %d\n",
266266
cp, PyString_AS_STRING(strobjp),
267267
cp - PyString_AS_STRING(strobjp), i);
268268
#endif /* def MPZ_DEBUG */
@@ -1765,7 +1765,7 @@ void *mp_allocate( alloc_size )
17651765
Py_FatalError("mp_allocate failure");
17661766

17671767
#ifdef MPZ_DEBUG
1768-
fprintf(stderr, "mp_allocate : address 0x%08x\n", res);
1768+
fprintf(stderr, "mp_allocate : address %08p\n", res);
17691769
#endif /* def MPZ_DEBUG */
17701770

17711771
MP_SET_TEST(res,alloc_size);
@@ -1782,7 +1782,7 @@ void *mp_reallocate( ptr, old_size, new_size )
17821782
void *res;
17831783

17841784
#ifdef MPZ_DEBUG
1785-
fprintf(stderr, "mp_reallocate: old address 0x%08x, old size %ld\n",
1785+
fprintf(stderr, "mp_reallocate: old address %08p, old size %ld\n",
17861786
ptr, old_size);
17871787
#endif /* def MPZ_DEBUG */
17881788

@@ -1792,7 +1792,7 @@ void *mp_reallocate( ptr, old_size, new_size )
17921792
Py_FatalError("mp_reallocate failure");
17931793

17941794
#ifdef MPZ_DEBUG
1795-
fprintf(stderr, "mp_reallocate: new address 0x%08x, new size %ld\n",
1795+
fprintf(stderr, "mp_reallocate: new address %08p, new size %ld\n",
17961796
res, new_size);
17971797
#endif /* def MPZ_DEBUG */
17981798

@@ -1808,7 +1808,7 @@ void mp_free( ptr, size )
18081808
{
18091809

18101810
#ifdef MPZ_DEBUG
1811-
fprintf(stderr, "mp_free : old address 0x%08x, old size %ld\n",
1811+
fprintf(stderr, "mp_free : old address %08p, old size %ld\n",
18121812
ptr, size);
18131813
#endif /* def MPZ_DEBUG */
18141814

Objects/bufferobject.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,20 +241,20 @@ buffer_repr(self)
241241

242242
if ( self->b_base == NULL )
243243
{
244-
sprintf(buf, "<%s buffer ptr %lx, size %d at %lx>",
244+
sprintf(buf, "<%s buffer ptr %p, size %d at %p>",
245245
status,
246-
(long)self->b_ptr,
246+
self->b_ptr,
247247
self->b_size,
248-
(long)self);
248+
self);
249249
}
250250
else
251251
{
252-
sprintf(buf, "<%s buffer for %lx, ptr %lx, size %d at %lx>",
252+
sprintf(buf, "<%s buffer for %p, ptr %p, size %d at %p>",
253253
status,
254-
(long)self->b_base,
255-
(long)self->b_ptr,
254+
self->b_base,
255+
self->b_ptr,
256256
self->b_size,
257-
(long)self);
257+
self);
258258
}
259259

260260
return PyString_FromString(buf);

Objects/classobject.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,11 @@ class_repr(op)
353353
else
354354
name = PyString_AsString(op->cl_name);
355355
if (mod == NULL || !PyString_Check(mod))
356-
sprintf(buf, "<class ?.%.100s at %lx>", name, (long)op);
356+
sprintf(buf, "<class ?.%.100s at %p>", name, op);
357357
else
358-
sprintf(buf, "<class %.50s.%.50s at %lx>",
358+
sprintf(buf, "<class %.50s.%.50s at %p>",
359359
PyString_AsString(mod),
360-
name, (long)op);
360+
name, op);
361361
return PyString_FromString(buf);
362362
}
363363

@@ -805,12 +805,12 @@ instance_repr(inst)
805805
cname = "?";
806806
PyErr_Clear();
807807
if (mod == NULL || !PyString_Check(mod))
808-
sprintf(buf, "<?.%.100s instance at %lx>",
809-
cname, (long)inst);
808+
sprintf(buf, "<?.%.100s instance at %p>",
809+
cname, inst);
810810
else
811-
sprintf(buf, "<%.50s.%.50s instance at %lx>",
811+
sprintf(buf, "<%.50s.%.50s instance at %p>",
812812
PyString_AsString(mod),
813-
cname, (long)inst);
813+
cname, inst);
814814
return PyString_FromString(buf);
815815
}
816816
res = PyEval_CallObject(func, (PyObject *)NULL);
@@ -1704,8 +1704,8 @@ instancemethod_repr(a)
17041704
icname = PyString_AsString(iclassname);
17051705
else
17061706
icname = "?";
1707-
sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>",
1708-
fcname, fname, icname, (long)self);
1707+
sprintf(buf, "<method %.60s.%.60s of %.60s instance at %p>",
1708+
fcname, fname, icname, self);
17091709
}
17101710
Py_XDECREF(funcname);
17111711
return PyString_FromString(buf);

Objects/fileobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ file_repr(f)
240240
PyFileObject *f;
241241
{
242242
char buf[300];
243-
sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>",
243+
sprintf(buf, "<%s file '%.256s', mode '%.10s' at %p>",
244244
f->f_fp == NULL ? "closed" : "open",
245245
PyString_AsString(f->f_name),
246246
PyString_AsString(f->f_mode),
247-
(long)f);
247+
f);
248248
return PyString_FromString(buf);
249249
}
250250

Objects/floatobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,8 @@ PyFloat_Fini()
806806
char buf[100];
807807
PyFloat_AsString(buf, p);
808808
fprintf(stderr,
809-
"# <float at %lx, refcnt=%d, val=%s>\n",
810-
(long)p, p->ob_refcnt, buf);
809+
"# <float at %p, refcnt=%d, val=%s>\n",
810+
p, p->ob_refcnt, buf);
811811
}
812812
}
813813
list = list->next;

Objects/funcobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ func_repr(op)
202202
{
203203
char buf[140];
204204
if (op->func_name == Py_None)
205-
sprintf(buf, "<anonymous function at %lx>", (long)op);
205+
sprintf(buf, "<anonymous function at %p>", op);
206206
else
207-
sprintf(buf, "<function %.100s at %lx>",
207+
sprintf(buf, "<function %.100s at %p>",
208208
PyString_AsString(op->func_name),
209-
(long)op);
209+
op);
210210
return PyString_FromString(buf);
211211
}
212212

Objects/intobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,8 @@ PyInt_Fini()
957957
i++, p++) {
958958
if (PyInt_Check(p) && p->ob_refcnt != 0)
959959
fprintf(stderr,
960-
"# <int at %lx, refcnt=%d, val=%ld>\n",
961-
(long)p, p->ob_refcnt, p->ob_ival);
960+
"# <int at %p, refcnt=%d, val=%ld>\n",
961+
p, p->ob_refcnt, p->ob_ival);
962962
}
963963
list = list->next;
964964
}

Objects/methodobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ meth_repr(m)
148148
sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name);
149149
else
150150
sprintf(buf,
151-
"<built-in method %.80s of %.80s object at %lx>",
151+
"<built-in method %.80s of %.80s object at %p>",
152152
m->m_ml->ml_name, m->m_self->ob_type->tp_name,
153-
(long)m->m_self);
153+
m->m_self);
154154
return PyString_FromString(buf);
155155
}
156156

0 commit comments

Comments
 (0)