Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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

Metadata

Release files for codetree 2009.12.24.py3k.cpp

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for codetree 2009.12.24.py3k.cpp
File Size Uploaded Provenance
codetree-2009.12.24.py3k.cpp.tar.gz 80.2 kB Without provenance Details

Release files / codetree-2009.12.24.py3k.cpp.tar.gz

Download URL codetree-2009.12.24.py3k.cpp.tar.gz
Size 80.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a09876da783fa56f4705830672700fb4439d778add84171dcf3693f6861a94fc
BLAKE2b-256 checksum
How to use checksums
e032c66578759a018edde64e5f67e813daad117cc3128369fa73a78004361226
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for codetree 2009.12.24.py3k.cpp

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page