本文整理匯總了Python中six.get_function_code方法的典型用法代碼示例。如果您正苦於以下問題:Python six.get_function_code方法的具體用法?Python six.get_function_code怎麽用?Python six.get_function_code使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類six
的用法示例。
在下文中一共展示了six.get_function_code方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _build_from_function
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def _build_from_function(node, name, member, module):
# verify this is not an imported function
try:
code = six.get_function_code(member)
except AttributeError:
# Some implementations don't provide the code object,
# such as Jython.
code = None
filename = getattr(code, 'co_filename', None)
if filename is None:
assert isinstance(member, object)
object_build_methoddescriptor(node, member, name)
elif filename != getattr(module, '__file__', None):
attach_dummy_node(node, name, member)
else:
object_build_function(node, member, name)
示例2: test_frame_stack
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def test_frame_stack():
def to_code_names(frames):
code_names = deque()
for frame in reversed(frames):
code_name = frame.f_code.co_name
if code_name not in mock_code_names:
break
code_names.appendleft(code_name)
return list(code_names)
baz_frame = foo()
foo_frame = baz_frame.f_back.f_back
frames = frame_stack(baz_frame)
assert to_code_names(frames) == ['foo', 'bar', 'baz']
# base frame.
frames = frame_stack(baz_frame, base_frame=foo_frame)
assert to_code_names(frames) == ['bar', 'baz']
# ignored codes.
frames = frame_stack(baz_frame, ignored_codes=[
six.get_function_code(foo),
six.get_function_code(baz),
])
assert to_code_names(frames) == ['bar']
示例3: deprecated
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def deprecated(version, version_removed):
'''This is a decorator which can be used to mark functions
as deprecated.
It will result in a warning being emitted when the function is used.'''
def __wrapper(func, *args, **kwargs):
'''Warn the user, and then proceed.'''
code = six.get_function_code(func)
warnings.warn_explicit(
"{:s}.{:s}\n\tDeprecated as of JAMS version {:s}."
"\n\tIt will be removed in JAMS version {:s}."
.format(func.__module__, func.__name__,
version, version_removed),
category=DeprecationWarning,
filename=code.co_filename,
lineno=code.co_firstlineno + 1
)
return func(*args, **kwargs)
return decorator(__wrapper)
示例4: make_decorator
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def make_decorator(func):
"""
Wraps a test decorator so as to properly replicate metadata
of the decorated function, including nose's additional stuff
(namely, setup and teardown).
"""
def decorate(newfunc):
if hasattr(func, 'compat_func_name'):
name = func.compat_func_name
else:
name = func.__name__
newfunc.__dict__ = func.__dict__
newfunc.__doc__ = func.__doc__
newfunc.__module__ = func.__module__
if not hasattr(newfunc, 'compat_co_firstlineno'):
newfunc.compat_co_firstlineno = six.get_function_code(func).co_firstlineno
try:
newfunc.__name__ = name
except TypeError:
# can't set func name in 2.3
newfunc.compat_func_name = name
return newfunc
return decorate
示例5: from_function
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def from_function(cls, f, *args, **kwargs):
"""
Create a new instance from a function. Gets the code object from
the function and passes it and any other specified parameters to
:meth:`from_code`.
Arguments:
f(function): The function to get the code object from.
Returns:
CodeObject: A new :class:`CodeObject` instance.
"""
return cls.from_code(six.get_function_code(f), *args, **kwargs)
示例6: primal_name
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def primal_name(func, wrt):
"""Name for the primal of a function."""
if not isinstance(func, types.FunctionType):
raise TypeError(func)
varnames = six.get_function_code(func).co_varnames
return PRIMAL_NAME.format(func.__name__, ''.join(varnames[i] for i in wrt))
示例7: _adjoint_name
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def _adjoint_name(func, wrt, template):
if not isinstance(func, types.FunctionType):
raise TypeError
varnames = six.get_function_code(func).co_varnames
return template.format(func.__name__, ''.join(varnames[i] for i in wrt))
示例8: fix_js_args
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def fix_js_args(func):
'''Use this function when unsure whether func takes this and arguments as its last 2 args.
It will append 2 args if it does not.'''
fcode = six.get_function_code(func)
fargs = fcode.co_varnames[fcode.co_argcount - 2:fcode.co_argcount]
if fargs == ('this', 'arguments') or fargs == ('arguments', 'var'):
return func
code = append_arguments(six.get_function_code(func), ('this', 'arguments'))
return types.FunctionType(
code,
six.get_function_globals(func),
func.__name__,
closure=six.get_function_closure(func))
示例9: fix_js_args
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def fix_js_args(func):
'''Use this function when unsure whether func takes this and arguments as its last 2 args.
It will append 2 args if it does not.'''
fcode = six.get_function_code(func)
fargs = fcode.co_varnames[fcode.co_argcount-2:fcode.co_argcount]
if fargs==('this', 'arguments') or fargs==('arguments', 'var'):
return func
code = append_arguments(six.get_function_code(func), ('this','arguments'))
return types.FunctionType(code, six.get_function_globals(func), func.__name__, closure=six.get_function_closure(func))
示例10: __init__
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def __init__(self, func, prototype=None, extensible=True, source=None):
cand = fix_js_args(func)
has_scope = cand is func
func = cand
self.argcount = six.get_function_code(func).co_argcount - 2 - has_scope
self.code = func
self.source = source if source else '{ [python code] }'
self.func_name = func.__name__ if not func.__name__.startswith('PyJs_anonymous') else ''
self.extensible = extensible
self.prototype = prototype
self.own = {}
#set own property length to the number of arguments
self.define_own_property('length', {'value': Js(self.argcount), 'writable': False,
'enumerable': False, 'configurable': False})
if self.func_name:
self.define_own_property('name', {'value': Js(self.func_name), 'writable': False,
'enumerable': False, 'configurable': True})
# set own prototype
proto = Js({})
# constructor points to this function
proto.define_own_property('constructor',{'value': self, 'writable': True,
'enumerable': False, 'configurable': True})
self.define_own_property('prototype', {'value': proto, 'writable': True,
'enumerable': False, 'configurable': False})
示例11: construct_new_test_function
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def construct_new_test_function(original_func, name, build_params):
"""Builds a new test function based on parameterized data.
:param original_func: The original test function that is used as a template
:param name: The fullname of the new test function
:param build_params: A dictionary or list containing args or kwargs
for the new test
:return: A new function object
"""
new_func = types.FunctionType(
six.get_function_code(original_func),
six.get_function_globals(original_func),
name=name,
argdefs=six.get_function_defaults(original_func)
)
for key, val in original_func.__dict__.items():
if key != 'build_data':
new_func.__dict__[key] = val
# Support either an arg list or kwarg dict for our data
build_args = build_params if isinstance(build_params, list) else []
build_kwargs = build_params if isinstance(build_params, dict) else {}
# Build a test wrapper to execute with our kwargs
def test_wrapper(func, test_args, test_kwargs):
@functools.wraps(func)
def wrapper(self):
return func(self, *test_args, **test_kwargs)
return wrapper
return test_wrapper(new_func, build_args, build_kwargs)
示例12: test_get_function_code
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def test_get_function_code():
def f():
pass
assert isinstance(six.get_function_code(f), types.CodeType)
if not hasattr(sys, "pypy_version_info"):
pytest.raises(AttributeError, six.get_function_code, hasattr)
示例13: _is_in_app_main
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def _is_in_app_main():
# type: () -> bool
"""Returns True iff app.run is active."""
f = sys._getframe().f_back # pylint: disable=protected-access
while f:
if f.f_code == six.get_function_code(app.run): # pytype: disable=wrong-arg-types
return True
f = f.f_back
return False
示例14: construct_new_test_function
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def construct_new_test_function(original_func, name, build_params):
"""Builds a new test function based on parameterized data.
:param original_func: The original test function that is used as a template
:param name: The fullname of the new test function
:param build_params: A dictionary or list containing args or kwargs
for the new test
:return: A new function object
"""
new_func = types.FunctionType(
six.get_function_code(original_func),
six.get_function_globals(original_func),
name=name,
argdefs=six.get_function_defaults(original_func)
)
# Support either an arg list or kwarg dict for our data
build_args = build_params if isinstance(build_params, list) else []
build_kwargs = build_params if isinstance(build_params, dict) else {}
# Build a test wrapper to execute with our kwargs
def test_wrapper(func, test_args, test_kwargs):
@functools.wraps(func)
def wrapper(self):
return func(self, *test_args, **test_kwargs)
return wrapper
return test_wrapper(new_func, build_args, build_kwargs)
示例15: construct_new_test_function
# 需要導入模塊: import six [as 別名]
# 或者: from six import get_function_code [as 別名]
def construct_new_test_function(original_func, name, build_params):
"""Builds a new test function based on parameterized data.
:param original_func: The original test function that is used as a template
:param name: The fullname of the new test function
:param build_params: A dictionary or list containing args or kwargs
for the new test
:return: A new function object
"""
new_func = types.FunctionType(
six.get_function_code(original_func),
six.get_function_globals(original_func),
name=name,
argdefs=six.get_function_defaults(original_func),
closure=six.get_function_closure(original_func)
)
for key, val in original_func.__dict__.items():
if key != 'build_data':
new_func.__dict__[key] = val
# Support either an arg list or kwarg dict for our data
build_args = build_params if isinstance(build_params, list) else []
build_kwargs = build_params if isinstance(build_params, dict) else {}
# Build a test wrapper to execute with our kwargs
def test_wrapper(func, test_args, test_kwargs):
@functools.wraps(func)
def wrapper(self):
return func(self, *test_args, **test_kwargs)
return wrapper
return test_wrapper(new_func, build_args, build_kwargs)