Creating functions programmatically

Michael Hudson mwh at python.net
Wed Dec 11 07:14:58 EST 2002


"Anders J. Munch" <andersjm at dancontrol.dk> writes:

> One thing that I don't see how to achieve with exec is control over
> location in diagnostic messages.  Regular Python diagnostics are fine,
> but file and line should not point to the exec statement in the .py
> file, but to the appropriate place in the file I'm parsing.  I could
> use something akin to C's #line here, is there any Python equivalent?

One option is to use exec to build the function object, then pick it
apart and rebuild it using the new module with co_firstline and
co_filename tweaked to be the correct values.

Cheers,
M.

-- 
  Considering that this thread is completely on-topic in the way only
  c.l.py threads can be, I think I can say that you should replace
  "Oblivion" with "Gravity", and increase your Radiohead quotient.
                                      -- Ben Wolfson, comp.lang.python



More information about the Python-list mailing list