Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70562 > unrolled thread

retrieve source code from code object as returned by compile()

Started byJustin Ezequiel <justin.ezequiel@gmail.com>
First post2014-04-23 17:58 -0700
Last post2014-04-26 12:03 +1200
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  retrieve source code from code object as returned by compile() Justin Ezequiel <justin.ezequiel@gmail.com> - 2014-04-23 17:58 -0700
    Re: retrieve source code from code object as returned by compile() Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-24 17:53 +1200
      Re: retrieve source code from code object as returned by compile() Justin Ezequiel <justin.ezequiel@gmail.com> - 2014-04-24 19:50 -0700
        Re: retrieve source code from code object as returned by compile() Amirouche Boubekki <amirouche.boubekki@gmail.com> - 2014-04-25 18:34 +0200
          Re: retrieve source code from code object as returned by compile() Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-26 12:03 +1200

#70562 — retrieve source code from code object as returned by compile()

FromJustin Ezequiel <justin.ezequiel@gmail.com>
Date2014-04-23 17:58 -0700
Subjectretrieve source code from code object as returned by compile()
Message-ID<4dfca89b-82c6-42f4-aedb-2fe82a7f7dc1@googlegroups.com>
Is there a way to get the original source?
I am trying to retrieve the main script from a py2exe'd old program.
The programmer neglected to commit to SVN before leaving.

Using "Easy Python Decompiler" I am able to get the source for the imported modules.
Using "Resources Viewer" from PlexData and some code I am able to retrieve the code object. I am however stumped as to how to retrieve the source from this code object. 

PythonWin 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> s = 'import time\nprint time.time()\n'
>>> c = compile(s, 'foo.py', 'exec')
>>> exec(c)
1398299623.77
>>> c
<code object <module> at 01E5C5C0, file "foo.py", line 1>
>>> for n in dir(c):
... 	if n.startswith('_'): continue
... 	print n
... 	a = getattr(c, n)
... 	print type(a)
... 	print `a`
... 	print
... 
co_argcount
<type 'int'>
0

co_cellvars
<type 'tuple'>
()

co_code
<type 'str'>
'd\x00\x00d\x01\x00k\x00\x00Z\x00\x00e\x00\x00i\x00\x00\x83\x00\x00GHd\x01\x00S'

co_consts
<type 'tuple'>
(-1, None)

co_filename
<type 'str'>
'foo.py'

co_firstlineno
<type 'int'>
1

co_flags
<type 'int'>
64

co_freevars
<type 'tuple'>
()

co_lnotab
<type 'str'>
'\x0c\x01'

co_name
<type 'str'>
'<module>'

co_names
<type 'tuple'>
('time',)

co_nlocals
<type 'int'>
0

co_stacksize
<type 'int'>
2

co_varnames
<type 'tuple'>
()
>>> len(s)
30
>>> len(c.co_code)
27
>>> 

[toc] | [next] | [standalone]


#70566

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-04-24 17:53 +1200
Message-ID<brrn75Fh59nU1@mid.individual.net>
In reply to#70562
Justin Ezequiel wrote:
> Using "Easy Python Decompiler" I am able to get the source for the imported
> modules. Using "Resources Viewer" from PlexData and some code I am able to
> retrieve the code object. I am however stumped as to how to retrieve the
> source from this code object.

Easy Python Decompiler should be able to do that, but you
may need to delve into its innards a bit to find an entry
point where you can feed in a code object.

Alternatively you could create a .pyc file out of the code
object and then use Easy Python Decompiler on that. The
following snippet of code should do that:

import marshal
import py_compile
import time

with open('output.pyc', 'wb') as fc:
     fc.write('\0\0\0\0')
     py_compile.wr_long(fc, long(time.time()))
     marshal.dump(codeobject, fc)
     fc.flush()
     fc.seek(0, 0)
     fc.write(py_compile.MAGIC)

(Taken from: 
http://stackoverflow.com/questions/8627835/generate-pyc-from-python-ast)

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#70582

FromJustin Ezequiel <justin.ezequiel@gmail.com>
Date2014-04-24 19:50 -0700
Message-ID<3abe4779-a237-4113-aaf0-24990895151f@googlegroups.com>
In reply to#70566
On Thursday, April 24, 2014 1:53:38 PM UTC+8, Gregory Ewing wrote:
> Alternatively you could create a .pyc file out of the code
> object and then use Easy Python Decompiler on that. The
> following snippet of code should do that:
> 
> (Taken from: 
> 
> http://stackoverflow.com/questions/8627835/generate-pyc-from-python-ast)

Woohoo! Success! Thank you Greg!

[toc] | [prev] | [next] | [standalone]


#70605

FromAmirouche Boubekki <amirouche.boubekki@gmail.com>
Date2014-04-25 18:34 +0200
Message-ID<mailman.9501.1398447453.18130.python-list@python.org>
In reply to#70582

[Multipart message — attachments visible in raw view] — view raw

in python3, I do inspect.getsource(object)
[doc<https://docs.python.org/3/library/inspect.html#inspect.getsource>],
I don't know the limitations.

On Python 2, there is meta <https://pypi.python.org/pypi/meta>.

My interest is different, I use to retrieve the definition of function to
submit it to a database, instead of stored procedures, but I have the
source of the code. It can also be used to retrieve the ast.


2014-04-25 4:50 GMT+02:00 Justin Ezequiel <justin.ezequiel@gmail.com>:

> On Thursday, April 24, 2014 1:53:38 PM UTC+8, Gregory Ewing wrote:
> > Alternatively you could create a .pyc file out of the code
> > object and then use Easy Python Decompiler on that. The
> > following snippet of code should do that:
> >
> > (Taken from:
> >
> > http://stackoverflow.com/questions/8627835/generate-pyc-from-python-ast)
>
> Woohoo! Success! Thank you Greg!
> --
> https://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#70619

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-04-26 12:03 +1200
Message-ID<bs0bduFgof6U2@mid.individual.net>
In reply to#70605
Amirouche Boubekki wrote:
> in python3, I do inspect.getsource(object) [doc 
> <https://docs.python.org/3/library/inspect.html#inspect.getsource>], I 
> don't know the limitations.

The limitation relevant here is that it requires the
original source file to be present. :-)

-- 
Greg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web