407
407
"""Return size of given open file."""
408
408
return os.fstat(f.fileno())[ST_SIZE]
411
if hasattr(os, 'urandom'): # python 2.4 and later
410
# Define rand_bytes based on platform.
412
# Python 2.4 and later have os.urandom,
413
# but it doesn't work on some arches
412
415
rand_bytes = os.urandom
413
elif sys.platform == 'linux2':
414
rand_bytes = file('/dev/urandom', 'rb').read
416
# not well seeded, but better than nothing
421
s += chr(random.randint(0, 255))
416
except (NotImplementedError, AttributeError):
417
# If python doesn't have os.urandom, or it doesn't work,
418
# then try to first pull random data from /dev/urandom
419
if os.path.exists("/dev/urandom"):
420
rand_bytes = file('/dev/urandom', 'rb').read
421
# Otherwise, use this hack as a last resort
423
# not well seeded, but better than nothing
428
s += chr(random.randint(0, 255))
426
432
## TODO: We could later have path objects that remember their list
427
433
## decomposition (might be too tricksy though.)