title: Embedding CPython: how do you constuct Python callables to wrap C callback pointers?
body: <p>Suppose I am embedding the CPython interpreter into a larger program, written in C.  The C component of the program occasionally needs to call functions written in Python, supplying callback functions to them as arguments.</p>
  
  <p>Using the CPython <a href="https://docs.python.org/3/extending/" rel="noreferrer">extending and embedding</a> APIs, how do I construct a Python "callable" object that wraps a C pointer-to-function, so that I can pass that object to Python code and have the Python code successfully call back into the C code?</p>
  
  <blockquote>
    <p><em>Note: this is a revised version of a question <a href="https://stackoverflow.com/questions/50381150/extending-python-with-c-passing-callback-function-of-c-to-python-and-invoking-c/50382792">originally posted</a> by user <a href="https://stackoverflow.com/users/1489866/dhanasubbu">dhanasubbu</a>, which I answered, but which was then deleted.  I think it was actually a good question, so I have converted what I wrote into a self-answer to my own statement of the question.  Alternative answers are welcome.</em></p>
  </blockquote>