本文整理汇总了Python中new.code函数的典型用法代码示例。如果您正苦于以下问题:Python code函数的具体用法?Python code怎么用?Python code使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了code函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gen_map
def gen_map(code, ob):
code.BUILD_MAP(0)
for k, v in ob.items():
code.DUP_TOP()
code(k, v)
code.ROT_THREE()
code.STORE_SUBSCR()
示例2: Call
def Call(func, args=(), kwargs=(), star=None, dstar=None, fold=True, code=None):
if code is None:
data = (func, tuple(args), tuple(kwargs), star or (), dstar or (), fold)
if fold and (args or kwargs or star or dstar):
return fold_args(Call, *data)
else:
return data
code(func, *args)
for k, v in kwargs:
code(k, v)
argc = len(args)
kwargc = len(kwargs)
if star:
if dstar:
code(star, dstar)
return code.CALL_FUNCTION_VAR_KW(argc, kwargc)
else:
code(star)
return code.CALL_FUNCTION_VAR(argc, kwargc)
else:
if dstar:
code(dstar)
return code.CALL_FUNCTION_KW(argc, kwargc)
else:
return code.CALL_FUNCTION(argc, kwargc)
示例3: Getattr
def Getattr(ob, name, code=None):
try:
name = const_value(name)
except NotAConstant:
return Call(Const(getattr), [ob, name])
if code is None:
return fold_args(Getattr, ob, name)
code(ob)
code.LOAD_ATTR(name)
示例4: If
def If(cond, then, else_=Pass, code=None):
if code is None:
return cond, then, else_
else_clause = Label()
end_if = Label()
code(cond, else_clause.JUMP_IF_FALSE_OR_POP, then)
if code.stack_size is not None:
end_if.JUMP_FORWARD(code)
code(else_clause, Code.POP_TOP, else_, end_if)
示例5: Or
def Or(values, code=None):
if code is None:
return fold_args(Or, tuple(values))
end = Label()
for value in values[:-1]:
try:
if not const_value(value):
continue # false constants can be skipped
except NotAConstant: # but non-constants require code
code(value, end.JUMP_IF_TRUE_OR_POP)
else: # and true constants end the chain right away
return code(value, end)
code(values[-1], end)
示例6: uncover
def uncover(function):
name = function.func_name
code = function.func_code
name = name[1:] if name[0] == "_" else name
arguments = tuple((name[1:] if name[0] == "_" else name)
for name in code.co_varnames[:code.co_argcount])
new_code = new.code(
code.co_argcount,
code.co_nlocals,
code.co_stacksize,
code.co_flags,
code.co_code,
code.co_consts,
code.co_names,
arguments + code.co_names,
code.co_filename,
code.co_name,
code.co_firstlineno,
code.co_lnotab)
print function.func_closure
new_function = new.function(
new_code,
function.func_globals,
name,
function.func_defaults,
function.func_closure)
return new_function
示例7: function
def function(self):
def fn():
pass
t = self.fn_tuple
fn.func_code = new.code(t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8],t[9],t[10],t[11],t[12],t[13])
fn.__doc__ = self.doc_string
return fn
示例8: compile
def compile(self, expr):
if type(expr) is unicode:
code = compile(expr.encode('utf-8'), '', 'eval')
# XXX This stupid compiler encoded all strings to utf-8, so we
# need to convert them to unicode.
consts = []
for const in code.co_consts:
if type(const) is str:
# We have to leave ascii strings just str not unicode
# because they can be python function keywords or
# something else
try:
const.decode('ascii')
except UnicodeError: # UnicodeDecodeError
consts.append(const.decode('utf-8'))
else:
consts.append(const)
else:
consts.append(const)
import new
code = new.code(code.co_argcount, code.co_nlocals,
code.co_stacksize, code.co_flags, code.co_code,
tuple(consts), code.co_names, code.co_varnames,
code.co_filename, code.co_name,
code.co_firstlineno, code.co_lnotab)
else:
code = compile(expr, '', 'eval')
return code
示例9: _to_code
def _to_code(self):
"""For debugging only."""
consts = [None] * len(self.co_consts_w)
num = 0
for w in self.co_consts_w:
if isinstance(w, PyCode):
consts[num] = w._to_code()
else:
consts[num] = self.space.unwrap(w)
num += 1
return new.code(
self.co_argcount,
self.co_nlocals,
self.co_stacksize,
self.co_flags,
self.co_code,
tuple(consts),
tuple(self.co_names),
tuple(self.co_varnames),
self.co_filename,
self.co_name,
self.co_firstlineno,
self.co_lnotab,
tuple(self.co_freevars),
tuple(self.co_cellvars),
)
示例10: replace_paths_in_code
def replace_paths_in_code(co, newname):
import new
if newname.endswith(".pyc"):
newname = newname[:-1]
consts = list(co.co_consts)
for i in range(len(consts)):
if isinstance(consts[i], type(co)):
consts[i] = replace_paths_in_code(consts[i], newname)
return new.code(
co.co_argcount,
co.co_nlocals,
co.co_stacksize,
co.co_flags,
co.co_code,
tuple(consts),
co.co_names,
co.co_varnames,
newname,
co.co_name,
co.co_firstlineno,
co.co_lnotab,
co.co_freevars,
co.co_cellvars,
)
示例11: replace_paths_in_code
def replace_paths_in_code(self, co):
new_filename = original_filename = os.path.normpath(co.co_filename)
for f, r in self.replace_paths:
if original_filename.startswith(f):
new_filename = r + original_filename[len(f):]
break
if self.debug and original_filename not in self.processed_paths:
if new_filename != original_filename:
self.msgout(2, "co_filename %r changed to %r" \
% (original_filename,new_filename,))
else:
self.msgout(2, "co_filename %r remains unchanged" \
% (original_filename,))
self.processed_paths.append(original_filename)
consts = list(co.co_consts)
for i in range(len(consts)):
if isinstance(consts[i], type(co)):
consts[i] = self.replace_paths_in_code(consts[i])
return new.code(co.co_argcount, co.co_nlocals, co.co_stacksize,
co.co_flags, co.co_code, tuple(consts), co.co_names,
co.co_varnames, new_filename, co.co_name,
co.co_firstlineno, co.co_lnotab,
co.co_freevars, co.co_cellvars)
示例12: assemble
def assemble(self, name, args, docstring, filename, firstlineno):
"""Get a Python code object"""
self.next_block()
self.emit("RETURN_VALUE")
stacksize = self._compute_stack_size()
blocks = self._get_blocks_in_order()
consts, names, varnames = self._compute_lookups(blocks, args, docstring)
bytecode = self._compute_jump_offsets(blocks)
codestring = bytecode.tostring()
return new.code(
len(args),
len(varnames),
stacksize,
CO_OPTIMIZED | CO_NEWLOCALS,
codestring,
consts,
names,
varnames,
filename,
name,
firstlineno,
"",
(),
(),
)
示例13: YieldStmt
def YieldStmt(value=None, code=None):
if code is None:
return (value,)
r = code(value, Code.YIELD_VALUE)
if stack_effects[YIELD_VALUE][1]:
code.POP_TOP()
return r
示例14: hook
def hook(func, lineno=None, insert_func=runpdb, with_state=False):
global hookpointcounter
hookpoints[hookpointcounter] = insert_func
code = func.func_code
newconsts, noneindex, minusoneindex, hookpointindex = getoraddtotuple(code.co_consts, None, -1, hookpointcounter)
newnames, replaceindex, runhookpointindex = getoraddtotuple(code.co_names, __name__, 'run_hookpoint')
if with_state:
newnames, localsindex, globalsindex = getoraddtotuple(newnames, 'locals', 'globals')
pdbtracecode = createbytecode('LOAD_CONST', minusoneindex, 'LOAD_CONST', noneindex, 'IMPORT_NAME', replaceindex, 'LOAD_ATTR', runhookpointindex, 'LOAD_CONST', hookpointindex, 'LOAD_GLOBAL', localsindex, 'CALL_FUNCTION', 0, 'LOAD_GLOBAL', globalsindex, 'CALL_FUNCTION', 0, 'CALL_FUNCTION', 3, 'POP_TOP')
else:
pdbtracecode = createbytecode('LOAD_CONST', minusoneindex, 'LOAD_CONST', noneindex, 'IMPORT_NAME', replaceindex, 'LOAD_ATTR', runhookpointindex, 'LOAD_CONST', hookpointindex, 'CALL_FUNCTION', 1, 'POP_TOP')
if lineno is None:
newcode = insertbytecode(code.co_code, 0, pdbtracecode)
newlnotab = fixlines(code.co_lnotab, 0, len(pdbtracecode))
else:
addr = line2addr(func, lineno)
if addr is None:
raise Exception('Line not found')
newcode = insertbytecode(code.co_code, addr, pdbtracecode)
newlnotab = fixlines(code.co_lnotab, addr, len(pdbtracecode))
# TODO is this correct ?
newstacksize = code.co_stacksize + 4 if with_state else 2
newfunc = new.code(code.co_argcount, code.co_nlocals, newstacksize, code.co_flags, newcode, newconsts, newnames, code.co_varnames, code.co_filename, code.co_name, code.co_firstlineno, newlnotab, code.co_freevars, code.co_cellvars)
# TODO make this thread safe (index returning number)
hookpointcounter += 1
if func.func_code in mapping:
mapping[newfunc] = mapping[func.func_code]
else:
mapping[newfunc] = func.func_code
origin[hookpointcounter - 1] = mapping[newfunc]
func.func_code = newfunc
return hookpointcounter - 1
示例15: get_code
def get_code(self, start=None):
'''
Produce a new code object based on the graph
'''
self.refactor()
# generate a new co_lineno
new_co_lineno = self.calc_lnotab()
# generate new bytecode stream
new_co_code = ""
for x in self.nodes(start):
new_co_code += x.bin()
# create a new code object with modified bytecode and updated line numbers
# a new code object is necessary because co_code is readonly
rvalue = new.code(self.code.co_argcount,
self.code.co_nlocals,
self.code.co_stacksize,
self.code.co_flags,
new_co_code,
self.code.co_consts,
self.code.co_names,
self.code.co_varnames,
self.code.co_filename,
self.code.co_name,
self.code.co_firstlineno,
new_co_lineno)
return rvalue