codetree 2009.12.24.py3k.cpp
codetree - directly hack compiled python code object
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', '')
('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 ''
co_firstlineno 1
co_lnotab b'\t\x06'
co_freevars []
co_cellvars [])
>>> ## disassemble codetree
>>> print( tree.dis() )
1 0 LOAD_CONST 0 ()
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 ()
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
| File | Type | Py Version | Uploaded on | Size | # downloads |
|---|---|---|---|---|---|
| codetree-2009.12.24.py3k.cpp.tar.gz (md5) | Source | 2009-12-29 | 78KB | 555 | |
- Author: kai zhu
- Home Page: http://pypi.python.org/pypi/codetree
- License: gpl
-
Categories
- Development Status :: 3 - Alpha
- Intended Audience :: Developers
- Intended Audience :: End Users/Desktop
- Intended Audience :: Science/Research
- License :: OSI Approved :: GNU General Public License (GPL)
- Natural Language :: English
- Operating System :: POSIX
- Operating System :: POSIX :: Linux
- Programming Language :: C
- Programming Language :: Python
- Programming Language :: Python :: 3
- Programming Language :: Python :: 3.1
- Topic :: Multimedia
- Topic :: Multimedia :: Graphics
- Topic :: Scientific/Engineering
- Topic :: Scientific/Engineering :: Mathematics
- Topic :: Scientific/Engineering :: Visualization
- Topic :: Software Development
- Topic :: Software Development :: Code Generators
- Topic :: Software Development :: Libraries
- Topic :: Software Development :: Libraries :: Python Modules
- Package Index Owner: davidbranniganz
- DOAP record: codetree-2009.12.24.py3k.cpp.xml
