wim*_*wim 6 python debugging cpython
我想知道在cpython中是否可以进行以下内省:
>>> def potato(x=69):
... if x == 69 and ???:
... print '69 was taken from argument defaults'
... if x == 69 and ???:
... print '69 was passed as positional arg'
... if x == 69 and ???:
... print '69 was passed as kwarg'
...
>>> potato()
69 was taken from argument defaults
>>> potato(69)
69 was passed as positional arg
>>> potato(x=69)
69 was passed as kwarg
Run Code Online (Sandbox Code Playgroud)
我对python2和python3的答案感兴趣,如果它们不同的话.
任何种类的Blackmagic涉及inspect,traceback,pdb,sys._getframe等是permissable这里.当然,不允许修改函数的argspec.
尽管框架有一个名为code_context 的字符串,它为您提供了调用该函数的源代码行,但看起来检查不能直接提供此信息。问题在于,人们必须重写一个小型解析器才能理解它。
这是一个更简单的解决方案,基于您要检查的函数的包装器。它不会更改 arg 规范,并且 arg 验证也不会更改:
import inspect
def track_args(func):
def tracker(*args, **kwargs):
r = func(*args, **kwargs)
for arg_num,arg_name in enumerate(inspect.getargspec(func)[0]):
if arg_name in kwargs:
print "%s was provided as keyword arg" % arg_name
elif arg_num < len(args):
print "%s was provided as positional arg" % arg_name
else:
print "%s was provided by default value" % arg_name
return r
return tracker
@track_args
def f(a, b, c=30):
print "I'm f()"
f(20, b=10)
f(20)
Run Code Online (Sandbox Code Playgroud)
具有有效参数的结果:
I'm f()
a was provided as positional arg
b was provided as keyword arg
c was provided by default value
Run Code Online (Sandbox Code Playgroud)
参数无效的结果:
Traceback (most recent call last):
File "test.py", line 21, in <module>
f(20)
File "test.py", line 5, in tracker
r = func(*args, **kwargs)
TypeError: f() takes at least 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)