pypy / spli /

""" Usage: python_file func_name output_file

import py
import sys
import types
from spli.objects import Int, Str, spli_None
from spli.pycode import Code
from rpython.rlib.rstruct.runpack import runpack
import struct

FMT = 'iiii'
int_lgt = len(struct.pack('i', 0))
header_lgt = int_lgt * len(FMT)

class NotSupportedFormat(Exception):

def serialize_str(value):
    return struct.pack('i', len(value)) + value

def unserialize_str(data, start):
    end_lgt = start + int_lgt
    lgt = runpack('i', data[start:end_lgt])
    assert lgt >= 0
    end_str = end_lgt + lgt
    return data[end_lgt:end_str], end_str

def serialize_const(const):
    if isinstance(const, int):
        return 'd' + struct.pack('i', const)
    elif isinstance(const, str):
        return 's' + serialize_str(const)
    elif const is None:
        return 'n'
    elif isinstance(const, types.CodeType):
        return 'c' + serialize(const)
        raise NotSupportedFormat(str(const))

def unserialize_const(c, start):
    assert start >= 0
    if c[start] == 'd':
        end = start + int_lgt + 1
        intval = runpack('i', c[start + 1:end])
        return Int(intval), end
    elif c[start] == 's':
        value, end = unserialize_str(c, start + 1)
        return Str(value), end
    elif c[start] == 'n':
        return spli_None, start + 1
    elif c[start] == 'c':
        return unserialize_code(c, start + 1)
        raise NotSupportedFormat(c[start])

def unserialize_consts(constrepr):
    pos = int_lgt
    consts_w = []
    num = runpack('i', constrepr[:int_lgt])
    for i in range(num):
        next_const, pos = unserialize_const(constrepr, pos)
    return consts_w, pos

def unserialize_names(namesrepr, num):
    pos = 0
    names = []
    for i in range(num):
        name, pos = unserialize_str(namesrepr, pos)
    return names, pos

def unserialize_code(coderepr, start=0):
    coderepr = coderepr[start:]
    header = coderepr[:header_lgt]
    argcount, nlocals, stacksize, code_len = runpack(FMT, header)
    assert code_len >= 0
    names_pos = code_len + header_lgt
    code = coderepr[header_lgt:names_pos]
    num = runpack('i', coderepr[names_pos:names_pos + int_lgt])
    names, end_names = unserialize_names(coderepr[names_pos + int_lgt:], num)
    const_start = names_pos + int_lgt + end_names
    consts, pos = unserialize_consts(coderepr[const_start:])
    pos = start + const_start + pos
    return Code(argcount, nlocals, stacksize, code, consts, names), pos

# ------------------- PUBLIC API ----------------------

def serialize(code):
    header = struct.pack(FMT, code.co_argcount, code.co_nlocals,
                         code.co_stacksize, len(code.co_code))
    namesrepr = (struct.pack('i', len(code.co_names)) +
                 "".join(serialize_str(name) for name in code.co_names))
    constsrepr = (struct.pack('i', len(code.co_consts)) +
                  "".join([serialize_const(const) for const in code.co_consts]))
    return header + code.co_code + namesrepr + constsrepr

def deserialize(data, start=0):
    return unserialize_code(data)[0]

def main(argv):
    if len(argv) != 3:
        print __doc__
    code_file = argv[1]
    mod = py.path.local(code_file).read()
    r = serialize(compile(mod, code_file, "exec"))
    outfile = py.path.local(argv[2])

if __name__ == '__main__':
    import sys
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.