本文整理汇总了Python中new.code函数的典型用法代码示例。如果您正苦于以下问题:Python code函数的具体用法?Python code怎么用?Python code使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了code函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
开发者ID:Nuevosmedios,项目名称:quicklearn-env,代码行数:7,代码来源:assembler.py
示例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)
开发者ID:Nuevosmedios,项目名称:quicklearn-env,代码行数:28,代码来源:assembler.py
示例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)
开发者ID:Nuevosmedios,项目名称:quicklearn-env,代码行数:9,代码来源:assembler.py
示例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)
开发者ID:Nuevosmedios,项目名称:quicklearn-env,代码行数:9,代码来源:assembler.py
示例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)
开发者ID:Nuevosmedios,项目名称:quicklearn-env,代码行数:13,代码来源:assembler.py
示例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
开发者ID:VDOMBoxGroup,项目名称:runtime2.0,代码行数:32,代码来源:decorators.py
示例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
开发者ID:bniemczyk,项目名称:bnirc,代码行数:7,代码来源:persistant.py
示例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
开发者ID:SmartTeleMax,项目名称:ppa,代码行数:28,代码来源:_TALCommon.py
示例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),
)
开发者ID:pombredanne,项目名称:pypy,代码行数:26,代码来源:pycode.py
示例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,
)
开发者ID:pombredanne,项目名称:bbfreeze,代码行数:28,代码来源:freezer.py
示例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)
开发者ID:3rdandUrban-dev,项目名称:Nuxleus,代码行数:26,代码来源:modulefinder.py
示例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,
"",
(),
(),
)
开发者ID:abed-hawa,项目名称:amara,代码行数:25,代码来源:assembler.py
示例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
开发者ID:Nuevosmedios,项目名称:quicklearn-env,代码行数:7,代码来源:assembler.py
示例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
开发者ID:tzickel,项目名称:bytehook,代码行数:32,代码来源:bytehook.py
示例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
开发者ID:453483289,项目名称:flare-bytecode_graph,代码行数:30,代码来源:bytecode_graph.py
示例16: replace_functions
def replace_functions(co, repl):
"""replace the functions in the code object co with those from repl.
repl can either be a code object or a source code string.
returns a new code object.
"""
import new
if isinstance(repl, basestring):
repl = compile(repl, co.co_name, "exec")
name2repl = {}
for c in repl.co_consts:
if isinstance(c, type(repl)):
name2repl[c.co_name] = c
consts = list(co.co_consts)
for i in range(len(consts)):
c = consts[i]
if isinstance(c, type(repl)):
if c.co_name in name2repl:
consts[i] = name2repl[c.co_name]
print "codehack: replaced %s in %s" % (c.co_name, co.co_filename)
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, co.co_filename, co.co_name,
co.co_firstlineno, co.co_lnotab,
co.co_freevars, co.co_cellvars)
开发者ID:RyanHope,项目名称:ccfreeze,代码行数:27,代码来源:codehack.py
示例17: __new__
def __new__(cls, f, *args, **kwargs):
fc = f.func_code
try:
i = get_index(fc.co_names, "user")
except ValueError: # functions does not uses user, so no need to modify
return f
user_context[f.__name__] = True
new_names = tuple([x for x in fc.co_names if f != "user"])
new_varnames = tuple([x for x in fc.co_varnames] + ["user"])
new_code = fc.co_code
# subtract 1 from higher LOAD_GLOBAL
for x in range(i + 1, len(fc.co_names)):
new_code = new_code.replace(chr(opmap['LOAD_GLOBAL']) + chr(x), chr(opmap['LOAD_GLOBAL']) + chr(x - 1))
# load argument instead of global
new_code = new_code.replace(chr(opmap['LOAD_GLOBAL']) + chr(i), chr(opmap['LOAD_FAST']) + chr(fc.co_argcount))
new_fc = code(fc.co_argcount + 1, fc.co_nlocals + 1, fc.co_stacksize, fc.co_flags, new_code, fc.co_consts,
new_names, new_varnames, fc.co_filename, fc.co_name, fc.co_firstlineno, fc.co_lnotab, fc.co_freevars,
fc.co_cellvars)
f.func_code = new_fc
# None as default argument for user
if f.func_defaults:
f.func_defaults = tuple([x for x in f.func_defaults] + [None])
else:
f.func_defaults = (None,)
return f
开发者ID:keat01,项目名称:pyLoad,代码行数:33,代码来源:Api.py
示例18: _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
assert (
self.co_kwonlyargcount == 0
), "kwonlyargcount is py3k only, cannot turn this code object into a Python2 one"
return new.code(
self.co_argcount,
# self.co_kwonlyargcount, # this does not exists in python2
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),
)
开发者ID:Qointum,项目名称:pypy,代码行数:30,代码来源:pycode.py
示例19: ascode
def ascode(self):
if self.nested:
L = list(self.args[5])
for i, elt in zip(range(len(L)), L):
if isinstance(elt, CodeWrapper):
L[i] = elt.ascode()
self.args[5] = tuple(L)
return new.code(*self.args)
开发者ID:grodniewicz,项目名称:oship,代码行数:8,代码来源:function.py
示例20: _make_wrapper_func
def _make_wrapper_func (wrapper, func):
"""Return wrapper function with changed name
"""
name = func.__name__ + "_wrapper"
c = wrapper.func_code
newcode = new.code( c.co_argcount, c.co_nlocals, c.co_stacksize,
c.co_flags, c.co_code, c.co_consts, c.co_names,
c.co_varnames, "indigo core", name, 1, c.co_lnotab, c.co_freevars, c.co_cellvars )
new_wrapper = new.function(newcode, globals(), name=name, closure=wrapper.func_closure, argdefs=wrapper.func_defaults)
return new_wrapper
开发者ID:mojca,项目名称:indigo,代码行数:11,代码来源:indigo.py
注:本文中的new.code函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论