Backport 1.8 buffer fixes

Backports b7aec60, eo67e6b, 6c4e648 in that order. Cleans up pep3118 code and fixes two segfaults.