本文整理匯總了Python中types.CodeType方法的典型用法代碼示例。如果您正苦於以下問題:Python types.CodeType方法的具體用法?Python types.CodeType怎麽用?Python types.CodeType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類types
的用法示例。
在下文中一共展示了types.CodeType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _extract_code_globals
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def _extract_code_globals(co):
"""
Find all globals names read or written to by codeblock co
"""
out_names = _extract_code_globals_cache.get(co)
if out_names is None:
names = co.co_names
out_names = {names[oparg] for _, oparg in _walk_global_ops(co)}
# Declaring a function inside another one using the "def ..."
# syntax generates a constant code object corresonding to the one
# of the nested function's As the nested function may itself need
# global variables, we need to introspect its code, extract its
# globals, (look for code object in it's co_consts attribute..) and
# add the result to code_globals
if co.co_consts:
for const in co.co_consts:
if isinstance(const, types.CodeType):
out_names |= _extract_code_globals(const)
_extract_code_globals_cache[co] = out_names
return out_names
示例2: iscode
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def iscode(object):
"""Return true if the object is a code object.
Code objects provide these attributes:
co_argcount number of arguments (not including * or ** args)
co_code string of raw compiled bytecode
co_consts tuple of constants used in the bytecode
co_filename name of file in which this code object was created
co_firstlineno number of first line in Python source code
co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
co_lnotab encoded mapping of line numbers to bytecode indices
co_name name with which this code object was defined
co_names tuple of names of local variables
co_nlocals number of local variables
co_stacksize virtual machine stack space required
co_varnames tuple of names of arguments and local variables"""
return isinstance(object, types.CodeType)
示例3: to_code
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def to_code(self):
"""
Convert this instance back into a native python code object. This
only works if the internals of the code object are compatible with
those of the running python version.
Returns:
types.CodeType: The native python code object.
"""
if self.internals is not get_py_internals():
raise ValueError('CodeObject is not compatible with the running python internals.')
if six.PY2:
return types.CodeType(
self.co_argcount, self.co_nlocals, self.co_stacksize, self.co_flags, self.co_code, self.co_consts,
self.co_names, self.co_varnames, self.co_filename, self.co_name, self.co_firstlineno, self.co_lnotab,
self.co_freevars, self.co_cellvars
)
else:
return types.CodeType(
self.co_argcount, self.co_kwonlyargcount, self.co_nlocals, self.co_stacksize, self.co_flags,
self.co_code, self.co_consts, self.co_names, self.co_varnames, self.co_filename, self.co_name,
self.co_firstlineno, self.co_lnotab, self.co_freevars, self.co_cellvars
)
示例4: iscode
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def iscode(object):
"""Return true if the object is a code object.
Code objects provide these attributes:
co_argcount number of arguments (not including * or ** args)
co_code string of raw compiled bytecode
co_consts tuple of constants used in the bytecode
co_filename name of file in which this code object was created
co_firstlineno number of first line in Python source code
co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
co_lnotab encoded mapping of line numbers to bytecode indices
co_name name with which this code object was defined
co_names tuple of names of local variables
co_nlocals number of local variables
co_stacksize virtual machine stack space required
co_varnames tuple of names of arguments and local variables
"""
return isinstance(object, types.CodeType)
# ------------------------------------------------ argument list extraction
# These constants are from Python's compile.h.
示例5: recursiveDisassembly
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def recursiveDisassembly(codeObject, name=None):
"""Disassemble recursively"""
what = 'module' if name is None else name
res = '\n\nDisassembly of ' + what + ':\n' + getCodeDisassembly(codeObject)
for item in codeObject.co_consts:
if type(item) == CodeType:
itemName = item.co_name
if name:
itemName = name + '.' + itemName
res += recursiveDisassembly(item, itemName)
return res
# The idea is taken from here:
# https://stackoverflow.com/questions/11141387/given-a-python-pyc-file-is-there-a-tool-that-let-me-view-the-bytecode
# https://stackoverflow.com/questions/32562163/how-can-i-understand-a-pyc-file-content
示例6: copycode
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def copycode(template, changes):
names = [
"argcount", "nlocals", "stacksize", "flags", "code", "consts",
"names", "varnames", "filename", "name", "firstlineno", "lnotab",
"freevars", "cellvars"
]
if hasattr(code, "co_kwonlyargcount"):
names.insert(1, "kwonlyargcount")
if hasattr(code, "co_posonlyargcount"):
# PEP 570 added "positional only arguments"
names.insert(1, "posonlyargcount")
values = [
changes.get(name, getattr(template, "co_" + name))
for name in names
]
return code(*values)
示例7: run
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def run(self, cmd, globals=None, locals=None):
if globals is None:
import __main__
globals = __main__.__dict__
if locals is None:
locals = globals
self.reset()
sys.settrace(self.trace_dispatch)
if not isinstance(cmd, types.CodeType):
cmd = cmd+'\n'
try:
exec cmd in globals, locals
except BdbQuit:
pass
finally:
self.quitting = 1
sys.settrace(None)
示例8: runeval
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def runeval(self, expr, globals=None, locals=None):
if globals is None:
import __main__
globals = __main__.__dict__
if locals is None:
locals = globals
self.reset()
sys.settrace(self.trace_dispatch)
if not isinstance(expr, types.CodeType):
expr = expr+'\n'
try:
return eval(expr, globals, locals)
except BdbQuit:
pass
finally:
self.quitting = 1
sys.settrace(None)
示例9: __init__
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def __init__(self, co_origin):
if not isinstance(co_origin, types.CodeType):
raise Exception('The creation of the `CodeObject` should get the original code_object')
self.co_origin = co_origin
self.fields = dict(zip(CO_FIELDS, [getattr(self.co_origin, f) for f in CO_FIELDS]))
self.code = array('B')
self.linestarts = dict(findlinestarts(co_origin))
self.lnotab = array('B')
self.append_code = self.code.append
self.insert_code = self.code.insert
self.prev_lineno = -1
# Used for conversion from a LOAD_NAME in the probe code to a LOAD_FAST
# in the final bytecode if the names are variable names (in co_varnames)
self.name_to_fast = set()
# Used for conversion from a LOAD_NAME in the probe code to a LOAD_GLOBAL
# when the name is from an injected import
self.name_to_global = set()
示例10: iscode
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def iscode(object):
"""Return true if the object is a code object.
Code objects provide these attributes:
co_argcount number of arguments (not including * or ** args)
co_code string of raw compiled bytecode
co_consts tuple of constants used in the bytecode
co_filename name of file in which this code object was created
co_firstlineno number of first line in Python source code
co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
co_lnotab encoded mapping of line numbers to bytecode indices
co_name name with which this code object was defined
co_names tuple of names of local variables
co_nlocals number of local variables
co_stacksize virtual machine stack space required
co_varnames tuple of names of arguments and local variables"""
return isinstance(object, types.CodeType)
# ------------------------------------------------ argument list extraction
# These constants are from Python's compile.h.
示例11: _get_code_lines
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def _get_code_lines(code):
if not isinstance(code, types.CodeType):
path = code
with open(path) as f:
src = f.read()
code = compile(src, path, 'exec', 0, dont_inherit=True)
return _get_code_lines(code)
def iterate():
# First, get all line starts for this code object. This does not include
# bodies of nested class and function definitions, as they have their
# own objects.
for _, lineno in dis.findlinestarts(code):
yield lineno
# For nested class and function definitions, their respective code objects
# are constants referenced by this object.
for const in code.co_consts:
if isinstance(const, types.CodeType) and const.co_filename == code.co_filename:
for lineno in _get_code_lines(const):
yield lineno
return iterate()
示例12: save_codeobject
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def save_codeobject(self, obj):
"""
Save a code object
"""
if PY3: # pragma: no branch
if hasattr(obj, "co_posonlyargcount"): # pragma: no branch
args = (
obj.co_argcount, obj.co_posonlyargcount,
obj.co_kwonlyargcount, obj.co_nlocals, obj.co_stacksize,
obj.co_flags, obj.co_code, obj.co_consts, obj.co_names,
obj.co_varnames, obj.co_filename, obj.co_name,
obj.co_firstlineno, obj.co_lnotab, obj.co_freevars,
obj.co_cellvars
)
else:
args = (
obj.co_argcount, obj.co_kwonlyargcount, obj.co_nlocals,
obj.co_stacksize, obj.co_flags, obj.co_code, obj.co_consts,
obj.co_names, obj.co_varnames, obj.co_filename,
obj.co_name, obj.co_firstlineno, obj.co_lnotab,
obj.co_freevars, obj.co_cellvars
)
else:
args = (
obj.co_argcount, obj.co_nlocals, obj.co_stacksize, obj.co_flags, obj.co_code,
obj.co_consts, obj.co_names, obj.co_varnames, obj.co_filename, obj.co_name,
obj.co_firstlineno, obj.co_lnotab, obj.co_freevars, obj.co_cellvars
)
self.save_reduce(types.CodeType, args, obj=obj)
示例13: is_internal_attribute
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def is_internal_attribute(obj, attr):
"""Test if the attribute given is an internal python attribute. For
example this function returns `True` for the `func_code` attribute of
python objects. This is useful if the environment method
:meth:`~SandboxedEnvironment.is_safe_attribute` is overridden.
>>> from jinja2.sandbox import is_internal_attribute
>>> is_internal_attribute(str, "mro")
True
>>> is_internal_attribute(str, "upper")
False
"""
if isinstance(obj, types.FunctionType):
if attr in UNSAFE_FUNCTION_ATTRIBUTES:
return True
elif isinstance(obj, types.MethodType):
if attr in UNSAFE_FUNCTION_ATTRIBUTES or \
attr in UNSAFE_METHOD_ATTRIBUTES:
return True
elif isinstance(obj, type):
if attr == 'mro':
return True
elif isinstance(obj, (types.CodeType, types.TracebackType, types.FrameType)):
return True
elif isinstance(obj, types.GeneratorType):
if attr in UNSAFE_GENERATOR_ATTRIBUTES:
return True
elif hasattr(types, 'CoroutineType') and isinstance(obj, types.CoroutineType):
if attr in UNSAFE_COROUTINE_ATTRIBUTES:
return True
elif hasattr(types, 'AsyncGeneratorType') and isinstance(obj, types.AsyncGeneratorType):
if attr in UNSAFE_ASYNC_GENERATOR_ATTRIBUTES:
return True
return attr.startswith('__')
示例14: test_getcode
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def test_getcode():
code = py.builtin._getcode(test_getcode)
assert isinstance(code, types.CodeType)
assert py.builtin._getcode(4) is None
示例15: py_mk_func
# 需要導入模塊: import types [as 別名]
# 或者: from types import CodeType [as 別名]
def py_mk_func(name: str, code: types.CodeType):
code = bc.Bytecode.from_code(code)
a = yield code_info(code)
for each in a.glob_deps:
yield require_global(each)
a = yield const(a)
f = yield from_lower(NS.RestrainJIT, py_mk_func.__name__)
n = yield const(name)
a = yield app(f, [n, a])
return a