how to extract source code from code objects ?

John Roth johnroth at ameritech.net
Sat Jan 4 02:55:02 CET 2003


"Michele Simionato" <mis6 at pitt.edu> wrote in message
news:2259b0e2.0301020814.2cbeb25c at posting.google.com...
> I can compile a string in a code object:
>
> >>> code_object=compile('x=1','<string>','exec')
>
> now the code_object.co_code attribute contains the bytecode
corresponding
> to the statement x=1:
>
> >>> code_object.co_code
> '\x7f\x00\x00\x7f\x01\x00d\x00\x00Z\x00\x00d\x01\x00S'
>
> Is there a way to have back the source code, i.e. the string 'x=1'
> instead of the bytecode ?
> TIA,

Look at the "inspect" module - it's 3.11 in the 2.2 Python Library
Reference.
I think this may do exactly what you want. It does, of course, require
that you have the source modules availible.

John Roth
>
>
> --
> Michele Simionato - Dept. of Physics and Astronomy
> 210 Allen Hall Pittsburgh PA 15260 U.S.A.
> Phone: 001-412-624-9041 Fax: 001-412-624-9163
> Home-page: http://www.phyast.pitt.edu/~micheles/






More information about the Python-list mailing list