Cross platform difference: Object hex address case
Tim Peters
tim.one at comcast.net
Tue Feb 25 18:07:11 EST 2003
[Follower]
> For at least release 2.2.1 there seems to be a difference in operation
> between Windows & Linux Python versions when an object's hexadecimal
> address is displayed.
>
> Assume you have an object that has an id that in hexadecimal
> representation will contain one or more of the letters A-F.
>
> If you call (explicitly or implicitly) __repr__() or __str__() the
> case of the letters used in the hexadecimal representation will differ
> between platforms.
>
> e.g.
>
> Linux:
>
> >>> oo.__repr__()
> '<object object at 0x80a1118>'
>
> Windows:
>
> >>> oo.__repr__()
> '<object object at 0x80A1118>'
>
> Note that the 'a' is lowercase under Linux and upper case under
> Windows.
>
> Is this behaviour intentional? Desired? A bug?
It's whatever the platform sprintf does with a %p format code. That is
indeed platform-dependent, and Python is happy to produce the style of
address most natural for the platform.
> If there is no justification for the difference I would like to
> recommend that the difference be removed. As the hex() function
> returns lowercase values under both Linux & Windows I'd suggest the
> common form should use lower case hex values.
Whether hex() produces lower or upper case actually depends on its argument:
>>> hex(65535)
'0xffff'
>>> hex(65535**2)
'0xFFFE0001L'
>>>
and there's no guarantee that a platform address fits in a Python short int.
> The reason this is a problem for me is that for a unit test I need to
> be able to compare the string representation of an object to its
> expected value. I am using code similar to:
>
> obj = object()
> assert("<object object at 0x%x>" % id(obj) == str(obj))
You can write a cleverer test than that <wink>.
> Obviously I could use "%X" as the formatter if I was always under
> Windows, but considering hex() always returns the equivalent of "%x"
> it would seem the Windows version's behaviour is non-standard.
Thw Windows %p behavior is surprising to Linux users, and vice versa.
Python doesn't have a preference.
More information about the Python-list
mailing list