Get information about the arguments accepted by a code object. Three things are returned: (args, varargs, varkw), where 'args' is a list of argument names (possibly containing nested lists), and 'varargs' and 'varkw' are the names of the * and ** arguments or None.
(co)
| 63 | CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 1, 2, 4, 8 |
| 64 | |
| 65 | def getargs(co): |
| 66 | """Get information about the arguments accepted by a code object. |
| 67 | |
| 68 | Three things are returned: (args, varargs, varkw), where 'args' is |
| 69 | a list of argument names (possibly containing nested lists), and |
| 70 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
| 71 | |
| 72 | """ |
| 73 | |
| 74 | if not iscode(co): |
| 75 | raise TypeError('arg is not a code object') |
| 76 | |
| 77 | nargs = co.co_argcount |
| 78 | names = co.co_varnames |
| 79 | args = list(names[:nargs]) |
| 80 | |
| 81 | # The following acrobatics are for anonymous (tuple) arguments. |
| 82 | # Which we do not need to support, so remove to avoid importing |
| 83 | # the dis module. |
| 84 | for i in range(nargs): |
| 85 | if args[i][:1] in ['', '.']: |
| 86 | raise TypeError("tuple function arguments are not supported") |
| 87 | varargs = None |
| 88 | if co.co_flags & CO_VARARGS: |
| 89 | varargs = co.co_varnames[nargs] |
| 90 | nargs = nargs + 1 |
| 91 | varkw = None |
| 92 | if co.co_flags & CO_VARKEYWORDS: |
| 93 | varkw = co.co_varnames[nargs] |
| 94 | return args, varargs, varkw |
| 95 | |
| 96 | def getargspec(func): |
| 97 | """Get the names and default values of a function's arguments. |
no test coverage detected