factor out dump() function, handle reference loops
Merge pull request #1172 from hwhw/master

factor out dump() function, handle reference loops