python functiontype_Python types.CodeType方法代码示例

本文整理汇总了Python中types.CodeType方法的典型用法代码示例。如果您正苦于以下问题:Python types.CodeType方法的具体用法?Python types.CodeType怎么用?Python types.CodeType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块types的用法示例。

在下文中一共展示了types.CodeType方法的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: _extract_code_globals

​点赞 6

# 需要导入模块: 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

开发者ID:pywren,项目名称:pywren-ibm-cloud,代码行数:25,

示例2: iscode

​点赞 6

# 需要导入模块: 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)

开发者ID:war-and-code,项目名称:jawfish,代码行数:19,

示例3: to_code

​点赞 6

# 需要导入模块: 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

)

开发者ID:edibledinos,项目名称:pwnypack,代码行数:27,

示例4: iscode

​点赞 6

# 需要导入模块: 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.

开发者ID:Frank-qlu,项目名称:recruit,代码行数:24,

示例5: recursiveDisassembly

​点赞 6

# 需要导入模块: 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

开发者ID:SergeySatskiy,项目名称:codimension,代码行数:18,

示例6: copycode

​点赞 6

# 需要导入模块: 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)

开发者ID:glyph,项目名称:automat,代码行数:18,

示例7: run

​点赞 6

# 需要导入模块: 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)

开发者ID:glmcdona,项目名称:meddle,代码行数:19,

示例8: runeval

​点赞 6

# 需要导入模块: 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)

开发者ID:glmcdona,项目名称:meddle,代码行数:19,

示例9: __init__

​点赞 6

# 需要导入模块: 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()

开发者ID:neuroo,项目名称:equip,代码行数:23,

示例10: iscode

​点赞 6

# 需要导入模块: 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.

开发者ID:ktraunmueller,项目名称:Computable,代码行数:22,

示例11: _get_code_lines

​点赞 6

# 需要导入模块: 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()

开发者ID:fabioz,项目名称:PyDev.Debugger,代码行数:25,

示例12: save_codeobject

​点赞 5

# 需要导入模块: 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)

开发者ID:pywren,项目名称:pywren-ibm-cloud,代码行数:31,

示例13: is_internal_attribute

​点赞 5

# 需要导入模块: 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('__')

开发者ID:remg427,项目名称:misp42splunk,代码行数:36,

示例14: test_getcode

​点赞 5

# 需要导入模块: 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

开发者ID:pytest-dev,项目名称:py,代码行数:6,

示例15: py_mk_func

​点赞 5

# 需要导入模块: 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

开发者ID:thautwarm,项目名称:restrain-jit,代码行数:13,

示例16: create_module

​点赞 5

# 需要导入模块: import types [as 别名]

# 或者: from types import CodeType [as 别名]

def create_module(self, spec):

bc: types.CodeType = spec.__bytecode__

doc = None

if len(bc.co_consts) and isinstance(bc.co_consts[0], str):

doc = bc.co_consts[0]

mod = types.ModuleType(self.mod_name, doc)

mod.__bytecode__ = bc

return mod

开发者ID:Xython,项目名称:YAPyPy,代码行数:10,

注:本文中的types.CodeType方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值