Improve readability of the major parts of the KGB code.

Review Request #10985 — Created April 2, 2020 and submitted


This adds another round of improvements to the KGB code, with a goal of
better documenting the operations we perform and improving readability
all throughout.

FunctionSpy.__init__ has been broken up into a few functions. Two of
the major parts, the logic around patching the owner to prevent spy
conflicts and the creation of the proxy function, have been split into
new, documented methods. This makes these parts easier to reason about.

The rest of the function has been slightly reorganized to do as little
as possible before we perform validation of arguments and signatures,
and to group together as much of the state setting as possible.

Comments have been added throughout, to better help understand what's
going on and to encourage the organization of new code.

A lot of variable accesses have been reduced. There are many functions
where we'd repeatedly access the same attributes from the spy over and
over, and this wasn't efficient and added to the visual noise of the
function. We now pull out variables we use over and over.

Unit tests pass on all supported versions of Python.

Improve readability of the major parts of the KGB code.
  1. Ship It!
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to master (12f0ddd)