diff options
Diffstat (limited to 'numpy/compat')
-rw-r--r-- | numpy/compat/_inspect.py | 85 | ||||
-rw-r--r-- | numpy/compat/py3k.py | 3 | ||||
-rw-r--r-- | numpy/compat/setup.py | 2 | ||||
-rw-r--r-- | numpy/compat/tests/test_compat.py | 23 |
4 files changed, 54 insertions, 59 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) + ')' diff --git a/numpy/compat/py3k.py b/numpy/compat/py3k.py index f5ac3f9f8..d95a362ca 100644 --- a/numpy/compat/py3k.py +++ b/numpy/compat/py3k.py @@ -36,7 +36,7 @@ if sys.version_info[0] >= 3: return str(s) def isfileobj(f): - return isinstance(f, (io.FileIO, io.BufferedReader)) + return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter)) def open_latin1(filename, mode='r'): return open(filename, mode=mode, encoding='iso-8859-1') @@ -57,7 +57,6 @@ else: asstr = str strchar = 'S' - def isfileobj(f): return isinstance(f, file) diff --git a/numpy/compat/setup.py b/numpy/compat/setup.py index c163bcaf9..26161f330 100644 --- a/numpy/compat/setup.py +++ b/numpy/compat/setup.py @@ -8,5 +8,5 @@ def configuration(parent_package='',top_path=None): return config if __name__ == '__main__': - from numpy.distutils.core import setup + from numpy.distutils.core import setup setup(configuration=configuration) diff --git a/numpy/compat/tests/test_compat.py b/numpy/compat/tests/test_compat.py new file mode 100644 index 000000000..9822ab374 --- /dev/null +++ b/numpy/compat/tests/test_compat.py @@ -0,0 +1,23 @@ +from os.path import join + +from numpy.compat import isfileobj +from numpy.testing import assert_, run_module_suite +from numpy.testing.utils import tempdir + + +def test_isfileobj(): + with tempdir(prefix="numpy_test_compat_") as folder: + filename = join(folder, 'a.bin') + + with open(filename, 'wb') as f: + assert_(isfileobj(f)) + + with open(filename, 'ab') as f: + assert_(isfileobj(f)) + + with open(filename, 'rb') as f: + assert_(isfileobj(f)) + + +if __name__ == "__main__": + run_module_suite() |