diff options
Diffstat (limited to 'numpy/lib/utils.py')
-rw-r--r-- | numpy/lib/utils.py | 133 |
1 files changed, 48 insertions, 85 deletions
diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 2e70bfd15..f54946722 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1,4 +1,4 @@ -from __future__ import division +from __future__ import division, absolute_import, print_function import os import sys @@ -11,7 +11,7 @@ from numpy.core import product, ndarray, ufunc __all__ = ['issubclass_', 'issubsctype', 'issubdtype', 'deprecate', 'deprecate_with_doc', 'get_numarray_include', 'get_include', 'info', 'source', 'who', 'lookfor', 'byte_bounds', - 'may_share_memory', 'safe_eval'] + 'safe_eval'] def get_include(): """ @@ -84,17 +84,10 @@ def get_numarray_include(type=None): return include_dirs + [get_include()] -if sys.version_info < (2, 4): - # Can't set __name__ in 2.3 - import new - def _set_function_name(func, name): - func = new.function(func.__code__, func.__globals__, - name, func.__defaults__, func.__closure__) - return func -else: - def _set_function_name(func, name): - func.__name__ = name - return func +def _set_function_name(func, name): + func.__name__ = name + return func + class _Deprecate(object): """ @@ -257,12 +250,11 @@ def byte_bounds(a): a_data = ai['data'][0] astrides = ai['strides'] ashape = ai['shape'] - nd_a = len(ashape) bytes_a = int(ai['typestr'][2:]) a_low = a_high = a_data if astrides is None: # contiguous case - a_high += product(ashape, dtype=int)*bytes_a + a_high += a.size * bytes_a else: for shape, stride in zip(ashape, astrides): if stride < 0: @@ -273,36 +265,6 @@ def byte_bounds(a): return a_low, a_high -def may_share_memory(a, b): - """ - Determine if two arrays can share memory - - The memory-bounds of a and b are computed. If they overlap then - this function returns True. Otherwise, it returns False. - - A return of True does not necessarily mean that the two arrays - share any element. It just means that they *might*. - - Parameters - ---------- - a, b : ndarray - - Returns - ------- - out : bool - - Examples - -------- - >>> np.may_share_memory(np.array([1,2]), np.array([5,8,9])) - False - - """ - a_low, a_high = byte_bounds(a) - b_low, b_high = byte_bounds(b) - if b_low >= a_high or a_low >= b_high: - return False - return True - #----------------------------------------------------------------------------- # Function for output and information on the variables used. #----------------------------------------------------------------------------- @@ -393,15 +355,15 @@ def who(vardict=None): sp2 = max(10,maxshape) sp3 = max(10,maxbyte) prval = "Name %s Shape %s Bytes %s Type" % (sp1*' ', sp2*' ', sp3*' ') - print prval + "\n" + "="*(len(prval)+5) + "\n" + print(prval + "\n" + "="*(len(prval)+5) + "\n") for k in range(len(sta)): val = sta[k] - print "%s %s %s %s %s %s %s" % (val[0], ' '*(sp1-len(val[0])+4), + print("%s %s %s %s %s %s %s" % (val[0], ' '*(sp1-len(val[0])+4), val[1], ' '*(sp2-len(val[1])+5), val[2], ' '*(sp3-len(val[2])+5), - val[3]) - print "\nUpper bound on total bytes = %d" % totalbytes + val[3])) + print("\nUpper bound on total bytes = %d" % totalbytes) return #----------------------------------------------------------------------------- @@ -442,7 +404,7 @@ def _makenamedict(module='numpy'): thedict = {module.__name__:module.__dict__} dictlist = [module.__name__] totraverse = [module.__dict__] - while 1: + while True: if len(totraverse) == 0: break thisdict = totraverse.pop(0) @@ -528,19 +490,19 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'): try: obj = _namedict[namestr][object] if id(obj) in objlist: - print >> output, "\n *** Repeat reference found in %s *** " % namestr + print("\n *** Repeat reference found in %s *** " % namestr, file=output) else: objlist.append(id(obj)) - print >> output, " *** Found in %s ***" % namestr + print(" *** Found in %s ***" % namestr, file=output) info(obj) - print >> output, "-"*maxwidth + print("-"*maxwidth, file=output) numfound += 1 except KeyError: pass if numfound == 0: - print >> output, "Help for %s not found." % object + print("Help for %s not found." % object, file=output) else: - print >> output, "\n *** Total of %d references found. ***" % numfound + print("\n *** Total of %d references found. ***" % numfound, file=output) elif inspect.isfunction(object): name = object.__name__ @@ -551,8 +513,8 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'): else: argstr = name + arguments - print >> output, " " + argstr + "\n" - print >> output, inspect.getdoc(object) + print(" " + argstr + "\n", file=output) + print(inspect.getdoc(object), file=output) elif inspect.isclass(object): name = object.__name__ @@ -572,28 +534,28 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'): else: argstr = name + arguments - print >> output, " " + argstr + "\n" + print(" " + argstr + "\n", file=output) doc1 = inspect.getdoc(object) if doc1 is None: if hasattr(object,'__init__'): - print >> output, inspect.getdoc(object.__init__) + print(inspect.getdoc(object.__init__), file=output) else: - print >> output, inspect.getdoc(object) + print(inspect.getdoc(object), file=output) methods = pydoc.allmethods(object) if methods != []: - print >> output, "\n\nMethods:\n" + print("\n\nMethods:\n", file=output) for meth in methods: if meth[0] == '_': continue thisobj = getattr(object, meth, None) if thisobj is not None: methstr, other = pydoc.splitdoc(inspect.getdoc(thisobj) or "None") - print >> output, " %s -- %s" % (meth, methstr) + print(" %s -- %s" % (meth, methstr), file=output) - elif type(object) is types.InstanceType: ## check for __call__ method - print >> output, "Instance of class: ", object.__class__.__name__ - print >> output + elif isinstance(object, types.InstanceType): ## check for __call__ method + print("Instance of class: ", object.__class__.__name__, file=output) + print(file=output) if hasattr(object, '__call__'): arguments = inspect.formatargspec(*inspect.getargspec(object.__call__.__func__)) arglist = arguments.split(', ') @@ -612,14 +574,14 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'): else: argstr = name + arguments - print >> output, " " + argstr + "\n" + print(" " + argstr + "\n", file=output) doc = inspect.getdoc(object.__call__) if doc is not None: - print >> output, inspect.getdoc(object.__call__) - print >> output, inspect.getdoc(object) + print(inspect.getdoc(object.__call__), file=output) + print(inspect.getdoc(object), file=output) else: - print >> output, inspect.getdoc(object) + print(inspect.getdoc(object), file=output) elif inspect.ismethod(object): name = object.__name__ @@ -636,11 +598,11 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'): else: argstr = name + arguments - print >> output, " " + argstr + "\n" - print >> output, inspect.getdoc(object) + print(" " + argstr + "\n", file=output) + print(inspect.getdoc(object), file=output) elif hasattr(object, '__doc__'): - print >> output, inspect.getdoc(object) + print(inspect.getdoc(object), file=output) def source(object, output=sys.stdout): @@ -684,10 +646,10 @@ def source(object, output=sys.stdout): # Local import to speed up numpy's import time. import inspect try: - print >> output, "In file: %s\n" % inspect.getsourcefile(object) - print >> output, inspect.getsource(object) + print("In file: %s\n" % inspect.getsourcefile(object), file=output) + print(inspect.getsource(object), file=output) except: - print >> output, "Not available for this object." + print("Not available for this object.", file=output) # Cache for lookfor: {id(module): {name: (docstring, kind, index), ...}...} @@ -755,7 +717,7 @@ def lookfor(what, module=None, import_modules=True, regenerate=False, whats = str(what).lower().split() if not whats: return - for name, (docstring, kind, index) in cache.iteritems(): + for name, (docstring, kind, index) in cache.items(): if kind in ('module', 'object'): # don't show modules or objects continue @@ -823,7 +785,7 @@ def lookfor(what, module=None, import_modules=True, regenerate=False, pager = pydoc.getpager() pager("\n".join(help_text)) else: - print "\n".join(help_text) + print("\n".join(help_text)) def _lookfor_generate_cache(module, import_modules, regenerate): """ @@ -835,7 +797,7 @@ def _lookfor_generate_cache(module, import_modules, regenerate): Module for which to generate docstring cache import_modules : bool Whether to import sub-modules in packages. - regenerate: bool + regenerate : bool Re-generate the docstring cache Returns @@ -848,7 +810,12 @@ def _lookfor_generate_cache(module, import_modules, regenerate): global _lookfor_caches # Local import to speed up numpy's import time. import inspect - from cStringIO import StringIO + + if sys.version_info[0] >= 3: + # In Python3 stderr, stdout are text files. + from io import StringIO + else: + from StringIO import StringIO if module is None: module = "numpy" @@ -1140,18 +1107,14 @@ def safe_eval(source): """ # Local imports to speed up numpy's import time. import warnings - from numpy.testing.utils import WarningManager - warn_ctx = WarningManager() - warn_ctx.__enter__() - try: + + with warnings.catch_warnings(): # compiler package is deprecated for 3.x, which is already solved here warnings.simplefilter('ignore', DeprecationWarning) try: import compiler except ImportError: import ast as compiler - finally: - warn_ctx.__exit__() walker = SafeEval() try: |