backport #1320 and Pylons@09beb29 from master to 1.5