copied from pyutils