Backported version of astropy-helpers#91

@embray - does this look right? Or should I just cherry-pick the merge commit for #91 and then adjust it as needed?