diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2014-08-01 09:59:00 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2014-08-01 10:22:53 -0600 |
commit | 3eb219fd97200282b6aed9fe760e843d4916bc06 (patch) | |
tree | f5f31599dd47d3c1c9d50261dfc38821078b2651 /numpy/compat/_inspect.py | |
parent | ae7c942ced535fb39384aefeb8d32df92fb15988 (diff) | |
download | numpy-3eb219fd97200282b6aed9fe760e843d4916bc06.tar.gz |
BUG: Fix bugs in unused code paths.
The `getargspec` needed to import the disassembler to support parsing
tuple arguments. Since numpy never uses those, the corresponding code
is deleted and an TypeError raised if such arguments are encountered.
Also fix the unused formatargs function so it does not require the
string module.
Diffstat (limited to 'numpy/compat/_inspect.py')
-rw-r--r-- | numpy/compat/_inspect.py | 85 |
1 files changed, 29 insertions, 56 deletions
diff --git a/numpy/compat/_inspect.py b/numpy/compat/_inspect.py index 6a499e727..c1aa22ec4 100644 --- a/numpy/compat/_inspect.py +++ b/numpy/compat/_inspect.py @@ -20,7 +20,9 @@ def ismethod(object): __name__ name with which this method was defined im_class class object in which this method belongs im_func function object containing implementation of method - im_self instance to which this method is bound, or None""" + im_self instance to which this method is bound, or None + + """ return isinstance(object, types.MethodType) def isfunction(object): @@ -33,7 +35,9 @@ def isfunction(object): func_defaults tuple of any default values for arguments func_doc (same as __doc__) func_globals global namespace in which this function was defined - func_name (same as __name__)""" + func_name (same as __name__) + + """ return isinstance(object, types.FunctionType) def iscode(object): @@ -51,7 +55,9 @@ def iscode(object): 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""" + co_varnames tuple of names of arguments and local variables + + """ return isinstance(object, types.CodeType) # ------------------------------------------------ argument list extraction @@ -63,51 +69,23 @@ def getargs(co): 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.""" + 'varargs' and 'varkw' are the names of the * and ** arguments or None. + + """ if not iscode(co): raise TypeError('arg is not a code object') - code = co.co_code nargs = co.co_argcount names = co.co_varnames args = list(names[:nargs]) - step = 0 # The following acrobatics are for anonymous (tuple) arguments. + # Which we do not need to support, so remove to avoid importing + # the dis module. for i in range(nargs): if args[i][:1] in ['', '.']: - stack, remain, count = [], [], [] - while step < len(code): - op = ord(code[step]) - step = step + 1 - if op >= dis.HAVE_ARGUMENT: - opname = dis.opname[op] - value = ord(code[step]) + ord(code[step+1])*256 - step = step + 2 - if opname in ['UNPACK_TUPLE', 'UNPACK_SEQUENCE']: - remain.append(value) - count.append(value) - elif opname == 'STORE_FAST': - stack.append(names[value]) - - # Special case for sublists of length 1: def foo((bar)) - # doesn't generate the UNPACK_TUPLE bytecode, so if - # `remain` is empty here, we have such a sublist. - if not remain: - stack[0] = [stack[0]] - break - else: - remain[-1] = remain[-1] - 1 - while remain[-1] == 0: - remain.pop() - size = count.pop() - stack[-size:] = [stack[-size:]] - if not remain: break - remain[-1] = remain[-1] - 1 - if not remain: break - args[i] = stack[0] - + raise TypeError("tuple function arguments are not supported") varargs = None if co.co_flags & CO_VARARGS: varargs = co.co_varnames[nargs] @@ -124,6 +102,7 @@ def getargspec(func): 'args' is a list of the argument names (it may contain nested lists). 'varargs' and 'varkw' are the names of the * and ** arguments or None. 'defaults' is an n-tuple of the default values of the last n arguments. + """ if ismethod(func): @@ -139,7 +118,9 @@ def getargvalues(frame): A tuple of four things is returned: (args, varargs, varkw, locals). 'args' is a list of the argument names (it may contain nested lists). 'varargs' and 'varkw' are the names of the * and ** arguments or None. - 'locals' is the locals dictionary of the given frame.""" + 'locals' is the locals dictionary of the given frame. + + """ args, varargs, varkw = getargs(frame.f_code) return args, varargs, varkw, frame.f_locals @@ -150,7 +131,9 @@ def joinseq(seq): return '(' + ', '.join(seq) + ')' def strseq(object, convert, join=joinseq): - """Recursively walk a sequence, stringifying each element.""" + """Recursively walk a sequence, stringifying each element. + + """ if type(object) in [list, tuple]: return join([strseq(_o, convert, join) for _o in object]) else: @@ -167,7 +150,9 @@ def formatargspec(args, varargs=None, varkw=None, defaults=None, The first four arguments are (args, varargs, varkw, defaults). The other four arguments are the corresponding optional formatting functions that are called to turn names and values into strings. The ninth - argument is an optional function to format the sequence of arguments.""" + argument is an optional function to format the sequence of arguments. + + """ specs = [] if defaults: firstdefault = len(args) - len(defaults) @@ -193,7 +178,9 @@ def formatargvalues(args, varargs, varkw, locals, The first four arguments are (args, varargs, varkw, locals). The next four arguments are the corresponding optional formatting functions that are called to turn names and values into strings. The ninth - argument is an optional function to format the sequence of arguments.""" + argument is an optional function to format the sequence of arguments. + + """ def convert(name, locals=locals, formatarg=formatarg, formatvalue=formatvalue): return formatarg(name) + formatvalue(locals[name]) @@ -204,18 +191,4 @@ def formatargvalues(args, varargs, varkw, locals, specs.append(formatvarargs(varargs) + formatvalue(locals[varargs])) if varkw: specs.append(formatvarkw(varkw) + formatvalue(locals[varkw])) - return '(' + string.join(specs, ', ') + ')' - -if __name__ == '__main__': - import inspect - def foo(x, y, z=None): - return None - - print(inspect.getargs(foo.__code__)) - print(getargs(foo.__code__)) - - print(inspect.getargspec(foo)) - print(getargspec(foo)) - - print(inspect.formatargspec(*inspect.getargspec(foo))) - print(formatargspec(*getargspec(foo))) + return '(' + ', '.join(specs) + ')' |