In the previous commit you remarked it added too many lines.  This is the commit which takes advantage of that change to reduce lines.  A function call to PyErr_Occurred may be cheep, but checking the return value of GetBufferInfo is cheeper.

There are a few other function that return void, which could be modified to return a bool the same way, which can help isolate errors.  Throwing an exception may be even better, but I'm not going to write that code...

As it is, PyErr_Occurred is called way too much in your code.  It should probably only be called after Py* functions because who else, but you, would know that your functions are expecting the callers to check PyErr_Occurred?  For example, what is it about the function GetBufferInfo that tells me I need to check PyErr_Occurred, but that I do not have to after a call to sort_hist_by_count?  Is the *only* way to know checking the implementation details?