本文整理汇总了Python中inspect.CO_VARARGS属性的典型用法代码示例。如果您正苦于以下问题:Python inspect.CO_VARARGS属性的具体用法?Python inspect.CO_VARARGS怎么用?Python inspect.CO_VARARGS使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类inspect
的用法示例。
在下文中一共展示了inspect.CO_VARARGS属性的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: inspect_func_args
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_VARARGS [as 别名]
def inspect_func_args(fn):
if py3k:
co = fn.__code__
else:
co = fn.func_code
nargs = co.co_argcount
names = co.co_varnames
args = list(names[:nargs])
varargs = None
if co.co_flags & CO_VARARGS:
varargs = co.co_varnames[nargs]
nargs = nargs + 1
varkw = None
if co.co_flags & CO_VARKEYWORDS:
varkw = co.co_varnames[nargs]
if py3k:
return args, varargs, varkw, fn.__defaults__
else:
return args, varargs, varkw, fn.func_defaults
示例2: get_args_index
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_VARARGS [as 别名]
def get_args_index(target) -> int:
"""
Returns the index of the "*args" parameter if such a parameter exists in
the function arguments or -1 otherwise.
:param target:
The target function for which the args index should be determined
:return:
The arguments index if it exists or -1 if not
"""
code = target.__code__
if not bool(code.co_flags & inspect.CO_VARARGS):
return -1
return code.co_argcount + code.co_kwonlyargcount
示例3: get_kwargs_index
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_VARARGS [as 别名]
def get_kwargs_index(target) -> int:
"""
Returns the index of the "**kwargs" parameter if such a parameter exists in
the function arguments or -1 otherwise.
:param target:
The target function for which the kwargs index should be determined
:return:
The keyword arguments index if it exists or -1 if not
"""
code = target.__code__
if not bool(code.co_flags & inspect.CO_VARKEYWORDS):
return -1
return (
code.co_argcount +
code.co_kwonlyargcount +
(1 if code.co_flags & inspect.CO_VARARGS else 0)
)
示例4: CaptureFrameLocals
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_VARARGS [as 别名]
def CaptureFrameLocals(self, frame):
"""Captures local variables and arguments of the specified frame.
Args:
frame: frame to capture locals and arguments.
Returns:
(arguments, locals) tuple.
"""
# Capture all local variables (including method arguments).
variables = {n: self.CaptureNamedVariable(n, v, 1,
self.default_capture_limits)
for n, v in six.viewitems(frame.f_locals)}
# Split between locals and arguments (keeping arguments in the right order).
nargs = frame.f_code.co_argcount
if frame.f_code.co_flags & inspect.CO_VARARGS: nargs += 1
if frame.f_code.co_flags & inspect.CO_VARKEYWORDS: nargs += 1
frame_arguments = []
for argname in frame.f_code.co_varnames[:nargs]:
if argname in variables: frame_arguments.append(variables.pop(argname))
return (frame_arguments, list(six.viewvalues(variables)))
示例5: inspect_getargspec
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_VARARGS [as 别名]
def inspect_getargspec(func):
"""getargspec based on fully vendored getfullargspec from Python 3.3."""
if inspect.ismethod(func):
func = func.__func__
if not inspect.isfunction(func):
raise TypeError("{!r} is not a Python function".format(func))
co = func.__code__
if not inspect.iscode(co):
raise TypeError("{!r} is not a code object".format(co))
nargs = co.co_argcount
names = co.co_varnames
nkwargs = co.co_kwonlyargcount if py3k else 0
args = list(names[:nargs])
nargs += nkwargs
varargs = None
if co.co_flags & inspect.CO_VARARGS:
varargs = co.co_varnames[nargs]
nargs = nargs + 1
varkw = None
if co.co_flags & inspect.CO_VARKEYWORDS:
varkw = co.co_varnames[nargs]
return ArgSpec(args, varargs, varkw, func.__defaults__)
示例6: inspect_getfullargspec
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_VARARGS [as 别名]
def inspect_getfullargspec(func):
"""Fully vendored version of getfullargspec from Python 3.3."""
if inspect.ismethod(func):
func = func.__func__
if not inspect.isfunction(func):
raise TypeError("{!r} is not a Python function".format(func))
co = func.__code__
if not inspect.iscode(co):
raise TypeError("{!r} is not a code object".format(co))
nargs = co.co_argcount
names = co.co_varnames
nkwargs = co.co_kwonlyargcount if py3k else 0
args = list(names[:nargs])
kwonlyargs = list(names[nargs : nargs + nkwargs])
nargs += nkwargs
varargs = None
if co.co_flags & inspect.CO_VARARGS:
varargs = co.co_varnames[nargs]
nargs = nargs + 1
varkw = None
if co.co_flags & inspect.CO_VARKEYWORDS:
varkw = co.co_varnames[nargs]
return FullArgSpec(
args,
varargs,
varkw,
func.__defaults__,
kwonlyargs,
func.__kwdefaults__ if py3k else None,
func.__annotations__ if py3k else {},
)
示例7: get_signature
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_VARARGS [as 别名]
def get_signature(func):
"""
Gathers information about the call signature of `func`.
"""
code = func.__code__
# Names of regular parameters
parameters = tuple(code.co_varnames[:code.co_argcount])
# Flags
has_varargs = bool(code.co_flags & inspect.CO_VARARGS)
has_varkw = bool(code.co_flags & inspect.CO_VARKEYWORDS)
has_kwonly = bool(code.co_kwonlyargcount)
# A mapping of parameter names to default values
default_values = func.__defaults__ or ()
defaults = dict(zip(parameters[-len(default_values):], default_values))
# Type annotations for all parameters
type_hints = typing.get_type_hints(func) if typing else func.__annotations__
types = tuple(normalize_type(type_hints.get(param, AnyType)) for param in parameters)
# Type annotations for required parameters
required = types[:-len(defaults)] if defaults else types
# Complexity
complexity = tuple(map(type_complexity, types)) if typing else None
return Signature(parameters, types, complexity, defaults, required,
has_varargs, has_varkw, has_kwonly)