backport. closes #460, #552, which were also fixed going forward by https://github.com/Pylons/pyramid/commit/95d5b72927484fa3882c5c47bf79d591a5af50a5