|
@@ -8,9 +8,12 @@ import sys |
|
|
import types |
|
|
import types |
|
|
|
|
|
|
|
|
OPMAP = opcode.opmap |
|
|
OPMAP = opcode.opmap |
|
|
|
|
|
OP_HASBUILD = [OPMAP[n] for n in ("BUILD_TUPLE", "BUILD_LIST", "BUILD_MAP", |
|
|
|
|
|
"BUILD_SET")] |
|
|
OP_HASCALL = [OPMAP[n] for n in ("CALL_FUNCTION", "CALL_FUNCTION_VAR", |
|
|
OP_HASCALL = [OPMAP[n] for n in ("CALL_FUNCTION", "CALL_FUNCTION_VAR", |
|
|
"CALL_FUNCTION_KW", "CALL_FUNCTION_VAR_KW")] |
|
|
"CALL_FUNCTION_KW", "CALL_FUNCTION_VAR_KW")] |
|
|
OP_HASMAKE = [OPMAP[n] for n in ("MAKE_FUNCTION", "MAKE_CLOSURE")] |
|
|
OP_HASMAKE = [OPMAP[n] for n in ("MAKE_FUNCTION", "MAKE_CLOSURE")] |
|
|
|
|
|
OP_LITERALARG = opcode.hasjabs + opcode.hasjrel + OP_HASBUILD + OP_HASMAKE |
|
|
|
|
|
|
|
|
MARKOV_START = -1 |
|
|
MARKOV_START = -1 |
|
|
MARKOV_END = -2 |
|
|
MARKOV_END = -2 |
|
@@ -73,6 +76,8 @@ def print_function(func): |
|
|
print " (to {0})".format(arg) |
|
|
print " (to {0})".format(arg) |
|
|
elif code in opcode.hasjrel: |
|
|
elif code in opcode.hasjrel: |
|
|
print " (+{0})".format(arg) |
|
|
print " (+{0})".format(arg) |
|
|
|
|
|
elif code in OP_HASBUILD: |
|
|
|
|
|
print " ({0} items)".format(arg) |
|
|
elif code in OP_HASCALL: |
|
|
elif code in OP_HASCALL: |
|
|
print " ({0} args, {1} kwargs)".format(*arg) |
|
|
print " ({0} args, {1} kwargs)".format(*arg) |
|
|
elif code in OP_HASMAKE: |
|
|
elif code in OP_HASMAKE: |
|
@@ -98,6 +103,7 @@ def run(): |
|
|
|
|
|
|
|
|
def _parse_func(func, chain): |
|
|
def _parse_func(func, chain): |
|
|
codeobj = func.__code__ |
|
|
codeobj = func.__code__ |
|
|
|
|
|
print codeobj.co_argcount |
|
|
codestring = codeobj.co_code |
|
|
codestring = codeobj.co_code |
|
|
length = len(codestring) |
|
|
length = len(codestring) |
|
|
i = 0 |
|
|
i = 0 |
|
@@ -124,13 +130,9 @@ def _get_argument(codeobj, codestring, i, code): |
|
|
return codeobj.co_varnames[arg] |
|
|
return codeobj.co_varnames[arg] |
|
|
elif code in opcode.hascompare: |
|
|
elif code in opcode.hascompare: |
|
|
return opcode.cmp_op[arg] |
|
|
return opcode.cmp_op[arg] |
|
|
elif code in opcode.hasjabs: |
|
|
|
|
|
return arg |
|
|
|
|
|
elif code in opcode.hasjrel: |
|
|
|
|
|
return arg - i - 2 |
|
|
|
|
|
elif code in OP_HASCALL: |
|
|
elif code in OP_HASCALL: |
|
|
return (ord(codestring[i]), ord(codestring[i + 1])) |
|
|
return (ord(codestring[i]), ord(codestring[i + 1])) |
|
|
elif code in OP_HASMAKE: |
|
|
|
|
|
|
|
|
elif code in OP_LITERALARG: |
|
|
return arg |
|
|
return arg |
|
|
raise NotImplementedError(code, opcode.opname[code]) |
|
|
raise NotImplementedError(code, opcode.opname[code]) |
|
|
|
|
|
|
|
@@ -144,15 +146,12 @@ def _make_codes(chain): |
|
|
codes = [] |
|
|
codes = [] |
|
|
instruction = random.choice(chain[MARKOV_START]) |
|
|
instruction = random.choice(chain[MARKOV_START]) |
|
|
constants, names, varnames = [], [], [] |
|
|
constants, names, varnames = [], [], [] |
|
|
i = 0 |
|
|
|
|
|
while 1: |
|
|
while 1: |
|
|
i += 1 |
|
|
|
|
|
code, arg = instruction |
|
|
code, arg = instruction |
|
|
if code == MARKOV_END: |
|
|
if code == MARKOV_END: |
|
|
break |
|
|
break |
|
|
codes.append(code) |
|
|
codes.append(code) |
|
|
if code >= opcode.HAVE_ARGUMENT: |
|
|
if code >= opcode.HAVE_ARGUMENT: |
|
|
i += 2 |
|
|
|
|
|
if code in opcode.hasconst: |
|
|
if code in opcode.hasconst: |
|
|
if arg not in constants: |
|
|
if arg not in constants: |
|
|
constants.append(arg) |
|
|
constants.append(arg) |
|
@@ -167,14 +166,10 @@ def _make_codes(chain): |
|
|
_coerce_arg_into_codes(codes, varnames.index(arg)) |
|
|
_coerce_arg_into_codes(codes, varnames.index(arg)) |
|
|
elif code in opcode.hascompare: |
|
|
elif code in opcode.hascompare: |
|
|
_coerce_arg_into_codes(codes, opcode.cmp_op.index(arg)) |
|
|
_coerce_arg_into_codes(codes, opcode.cmp_op.index(arg)) |
|
|
elif code in opcode.hasjabs: |
|
|
|
|
|
_coerce_arg_into_codes(codes, arg) |
|
|
|
|
|
elif code in opcode.hasjrel: |
|
|
|
|
|
_coerce_arg_into_codes(codes, arg + i) |
|
|
|
|
|
elif code in OP_HASCALL: |
|
|
elif code in OP_HASCALL: |
|
|
codes.append(arg[0]) |
|
|
codes.append(arg[0]) |
|
|
codes.append(arg[1]) |
|
|
codes.append(arg[1]) |
|
|
elif code in OP_HASMAKE: |
|
|
|
|
|
|
|
|
elif code in OP_LITERALARG: |
|
|
_coerce_arg_into_codes(codes, arg) |
|
|
_coerce_arg_into_codes(codes, arg) |
|
|
else: |
|
|
else: |
|
|
raise NotImplementedError(code, opcode.opname[code]) |
|
|
raise NotImplementedError(code, opcode.opname[code]) |
|
|