Commit f5d5a5cf authored by Tim Peters's avatar Tim Peters

Merge rev 29370 from 3.3 branch.

positive_id():  Use a trick from Armin Rigo to deduce the
native platorm address size.
parent e6f505df
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
import sys import sys
import time import time
import struct
from struct import pack, unpack from struct import pack, unpack
from binascii import hexlify from binascii import hexlify
import cPickle as pickle import cPickle as pickle
...@@ -140,22 +141,18 @@ def readable_tid_repr(tid): ...@@ -140,22 +141,18 @@ def readable_tid_repr(tid):
# unsigned, but produces a FutureWarning, because Python 2.4 will display # unsigned, but produces a FutureWarning, because Python 2.4 will display
# it as signed. So when you want to prodce an address, use positive_id() to # it as signed. So when you want to prodce an address, use positive_id() to
# obtain it. # obtain it.
# _ADDRESS_MASK is 2**(number_of_bits_in_a_native_pointer). Adding this to
# a negative address gives a positive int with the same hex representation as
# the significant bits in the original.
_ADDRESS_MASK = 256 ** struct.calcsize('P')
def positive_id(obj): def positive_id(obj):
"""Return id(obj) as a non-negative integer.""" """Return id(obj) as a non-negative integer."""
result = id(obj) result = id(obj)
if result < 0: if result < 0:
# This is a puzzle: there's no way to know the natural width of result += _ADDRESS_MASK
# addresses on this box (in particular, there's no necessary assert result > 0
# relation to sys.maxint). Try 32 bits first (and on a 32-bit
# box, adding 2**32 gives a positive number with the same hex
# representation as the original result).
result += 1L << 32
if result < 0:
# Undo that, and try 64 bits.
result -= 1L << 32
result += 1L << 64
assert result >= 0 # else addresses are fatter than 64 bits
return result return result
# So full of undocumented magic it's hard to fathom. # So full of undocumented magic it's hard to fathom.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment