Skip to main content

codetree - directly hack compiled python code object

Project description

codetree - directly hack compiled python code object

REQUIRES PYTHON3.1

QUICK TEST: $ python3.1 setup.py build dev --quicktest

DESCRIPTION: codetree - directly hack compiled python code object

SUMMARY:
codetree converts compiled python code objects into trees, which can be edited and recompiled

RECENT CHANGELOG:
20091224 - modularized package - fix install issues - added sdist check
20091205 - moved source code to c++
20091116 - package integrated

DEMO USAGE:

>>> from codetree import *
>>> ## source code
>>> src = 'def foo(aa):\n def closure():\n nonlocal aa\n aa += "!"\n print(aa)\n return closure()\nfoo("hello")'; print( src )
def foo(aa):
def closure():
nonlocal aa
aa += "!"
print(aa)
return closure()
foo("hello")

>>> ## compile source code
>>> codeobj = compile(src, '', 'exec')
>>> exec( codeobj )
hello!

>>> ## convert code object into editable codetree
>>> tree = codetree(codeobj)
>>> for item in vars(tree).items(): print( item )
('co_firstlineno', 1)
('co_code', b'd\x00\x00\x84\x00\x00Z\x00\x00e\x00\x00d\x01\x00\x83\x01\x00\x01d\x02\x00S')
('co_freevars', [])
('co_name', '<module>')
('co_filename', '')
('co_lnotab', b'\t\x06')
('co_flags', 64)
('co_nlocals', 0)
('co_stacksize', 2)
('co_argcount', 0)
('co_cellvars', [])
('co_kwonlyargcount', 0)
('co_varnames', [])
('co_names', ['foo'])

>>> ## edit / compile / exec codetree
>>> element, depth, index, subtree = tree.find('hello')
>>> subtree[index] = 'goodbye'
>>> exec( tree.compile() )
goodbye!

>>> ## codetree structure
>>> print( tree )
codetree(
co_argcount 0
co_kwonlyargcount 0
co_nlocals 0
co_stacksize 2
co_flags 64
co_code b'd\x00\x00\x84\x00\x00Z\x00\x00e\x00\x00d\x01\x00\x83\x01\x00\x01d\x02\x00S'
co_consts
codetree(
co_argcount 1
co_kwonlyargcount 0
co_nlocals 2
co_stacksize 2
co_flags 3
co_code b'\x87\x00\x00f\x01\x00d\x01\x00\x86\x00\x00}\x01\x00|\x01\x00\x83\x00\x00S'
co_consts
None
codetree(
co_argcount 0
co_kwonlyargcount 0
co_nlocals 0
co_stacksize 2
co_flags 19
co_code b'\x88\x00\x00d\x01\x007\x89\x00\x00t\x00\x00\x88\x00\x00\x83\x01\x00\x01d\x00\x00S'
co_consts
None
!
co_names ['print']
co_varnames []
co_filename ''
co_name 'closure'
co_firstlineno 2
co_lnotab b'\x00\x02\n\x01'
co_freevars ['aa']
co_cellvars [])
co_names []
co_varnames ['aa', 'closure']
co_filename ''
co_name 'foo'
co_firstlineno 1
co_lnotab b'\x00\x01\x0f\x04'
co_freevars []
co_cellvars ['aa'])
goodbye
None
co_names ['foo']
co_varnames []
co_filename ''
co_name '<module>'
co_firstlineno 1
co_lnotab b'\t\x06'
co_freevars []
co_cellvars [])

>>> ## disassemble codetree
>>> print( tree.dis() )
1 0 LOAD_CONST 0 (<code object foo at 0x922c848, file "", line 1>)
3 MAKE_FUNCTION 0
6 STORE_NAME 0 (foo)

7 9 LOAD_NAME 0 (foo)
12 LOAD_CONST 1 ('goodbye')
15 CALL_FUNCTION 1
18 POP_TOP
19 LOAD_CONST 2 (None)
22 RETURN_VALUE

2 0 LOAD_CLOSURE 0 (aa)
3 BUILD_TUPLE 1
6 LOAD_CONST 1 (<code object closure at 0x922c890, file "", line 2>)
9 MAKE_CLOSURE 0
12 STORE_FAST 1 (closure)

6 15 LOAD_FAST 1 (closure)
18 CALL_FUNCTION 0
21 RETURN_VALUE

4 0 LOAD_DEREF 0 (aa)
3 LOAD_CONST 1 ('!')
6 INPLACE_ADD
7 STORE_DEREF 0 (aa)

5 10 LOAD_GLOBAL 0 (print)
13 LOAD_DEREF 0 (aa)
16 CALL_FUNCTION 1
19 POP_TOP
20 LOAD_CONST 0 (None)
23 RETURN_VALUE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codetree-2009.12.24.py3k.cpp.tar.gz (80.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page