Getting a code obj's function?

Michael Hudson mwh21 at cam.ac.uk
Fri Dec 15 06:06:46 EST 2000


Bjoern Giesler <un4e at rzstud1.rz.uni-karlsruhe.de> writes:

> Hi,
> 
> another inane question...
> 
> ...is there a standard way to get the function object that a code object is
> associated with? 

Well, by mucking with the new module, you can get two functions with
the same code object.

Also it's possible to execute a code object without an associated
function - like the top-level of a module for example.

> If there is no standard way, could somebody please comment on the
> following: I've modified my Python interpreter so that a code object
> has one more member, co_func.  This gets set to None initially, and
> PyFunc_New() sets it to the new function object that is created.
> What are the potential problems with this approach, if any?  I see
> all kinds of memory leaks / double frees coming, but haven't really
> enough understanding of Python to know what might happen.  My
> assumption was that a code object is assigned to a maximum of
> exactly one function. Is this correct?

I think you can probably come up with something that will work most of
the time, but not all the time.  Whether that's good enough for you is
up to you...

Cheers,
M.

-- 
7. It is easier to write an incorrect program than understand a
   correct one.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html



More information about the Python-list mailing list