Namegen: fix python 2 compatibility and add an upper limit