diff options
47 files changed, 1009 insertions, 1434 deletions
diff --git a/INSTALL.txt b/INSTALL.txt index d593dc8ee..278aab9ef 100644 --- a/INSTALL.txt +++ b/INSTALL.txt @@ -15,7 +15,8 @@ PREREQUISITES Building NumPy requires the following software installed: -1) Python__ 2.4.x or newer +1) For Python 2, Python__ 2.6.x or newer. + For Python 3, Python__ 3.2.x or newer. On Debian and derivative (Ubuntu): python python-dev @@ -39,10 +40,10 @@ Fortran ABI mismatch The two most popular open source fortran compilers are g77 and gfortran. Unfortunately, they are not ABI compatible, which means that concretely you -should avoid mixing libraries built with one with another. In particular, if -your blas/lapack/atlas is built with g77, you *must* use g77 when building -numpy and scipy; on the contrary, if your atlas is built with gfortran, you -*must* build numpy/scipy with gfortran. +should avoid mixing libraries built with one with another. In particular, +if your blas/lapack/atlas is built with g77, you *must* use g77 when +building numpy and scipy; on the contrary, if your atlas is built with +gfortran, you *must* build numpy/scipy with gfortran. Choosing the fortran compiler ----------------------------- @@ -58,11 +59,11 @@ To build with gfortran: How to check the ABI of blas/lapack/atlas ----------------------------------------- -One relatively simple and reliable way to check for the compiler used to build -a library is to use ldd on the library. If libg2c.so is a dependency, this -means that g77 has been used. If libgfortran.so is a dependency, gfortran has -been used. If both are dependencies, this means both have been used, which is -almost always a very bad idea. +One relatively simple and reliable way to check for the compiler used to +build a library is to use ldd on the library. If libg2c.so is a dependency, +this means that g77 has been used. If libgfortran.so is a dependency, +gfortran has been used. If both are dependencies, this means both have been +used, which is almost always a very bad idea. Building with ATLAS support =========================== @@ -70,70 +71,68 @@ Building with ATLAS support Ubuntu 8.10 (Intrepid) ---------------------- -You can install the necessary packages for optimized ATLAS with this command: +You can install the necessary packages for optimized ATLAS with this +command: sudo apt-get install libatlas-base-dev If you have a recent CPU with SIMD support (SSE, SSE2, etc...), you should -also install the corresponding package for optimal performance. For example, -for SSE2: +also install the corresponding package for optimal performance. For +example, for SSE2: sudo apt-get install libatlas3gf-sse2 *NOTE*: if you build your own atlas, Intrepid changed its default fortran -compiler to gfortran. So you should rebuild everything from scratch, including -lapack, to use it on Intrepid. +compiler to gfortran. So you should rebuild everything from scratch, +including lapack, to use it on Intrepid. Ubuntu 8.04 and lower --------------------- -You can install the necessary packages for optimized ATLAS with this command: +You can install the necessary packages for optimized ATLAS with this +command: sudo apt-get install atlas3-base-dev If you have a recent CPU with SIMD support (SSE, SSE2, etc...), you should -also install the corresponding package for optimal performance. For example, -for SSE2: +also install the corresponding package for optimal performance. For +example, for SSE2: sudo apt-get install atlas3-sse2 Windows 64 bits notes ===================== -Note: only AMD64 is supported (IA64 is not) - AMD64 is the version most people -want. +Note: only AMD64 is supported (IA64 is not) - AMD64 is the version most +people want. Free compilers (mingw-w64) -------------------------- http://mingw-w64.sourceforge.net/ -To use the free compilers (mingw-w64), you need to build your own toolchain, as -the mingw project only distribute cross-compilers (cross-compilation is not -supported by numpy). Since this toolchain is still being worked on, serious -compiler bugs can be expected. binutil 2.19 + gcc 4.3.3 + mingw-w64 runtime -gives you a working C compiler (but the C++ is broken). gcc 4.4 will hopefully -be able to run natively. +To use the free compilers (mingw-w64), you need to build your own +toolchain, as the mingw project only distribute cross-compilers +(cross-compilation is not supported by numpy). Since this toolchain is +still being worked on, serious compiler bugs can be expected. binutil 2.19 ++ gcc 4.3.3 + mingw-w64 runtime gives you a working C compiler (but the C++ +is broken). gcc 4.4 will hopefully be able to run natively. -This is the only tested way to get a numpy with a FULL blas/lapack (scipy does -not work because of C++). +This is the only tested way to get a numpy with a FULL blas/lapack (scipy +does not work because of C++). MS compilers ------------ -If you are familiar with MS tools, that's obviously the easiest path, and the -compilers are hopefully more mature (although in my experience, they are quite -fragile, and often segfault on invalid C code). The main drawback is that no -fortran compiler + MS compiler combination has been tested - mingw-w64 gfortran -+ MS compiler does not work at all (it is unclear whether it ever will). - -For python 2.5, you need VS 2005 (MS compiler version 14) targeting -AMD64 bits, or the Platform SDK v6.0 or below (which gives command -line versions of 64 bits target compilers). The PSDK is free. +If you are familiar with MS tools, that's obviously the easiest path, and +the compilers are hopefully more mature (although in my experience, they +are quite fragile, and often segfault on invalid C code). The main drawback +is that no fortran compiler + MS compiler combination has been tested - +mingw-w64 gfortran + MS compiler does not work at all (it is unclear +whether it ever will). For python 2.6, you need VS 2008. The freely available version does not contains 64 bits compilers (you also need the PSDK, v6.1). -It is *crucial* to use the right version: python 2.5 -> version 14, python 2.6, -version 15. You can check the compiler version with cl.exe /?. Note also that -for python 2.5, 64 bits and 32 bits versions use a different compiler version. +It is crucial to use the right MS compiler version. For python 2.6, you +must use version 15. You can check the compiler version with cl.exe /?. diff --git a/doc/release/1.8.0-notes.rst b/doc/release/1.8.0-notes.rst index c2fc57e6c..95bad26cc 100644 --- a/doc/release/1.8.0-notes.rst +++ b/doc/release/1.8.0-notes.rst @@ -7,6 +7,11 @@ This release supports Python 2.6 -2.7 and 3.2 - 3.3. Highlights ========== +Python 2 and Python 3 are supported by the same code base. The +2to3 fixer is no longer run. + + + Dropped Support =============== diff --git a/doc/sphinxext/numpydoc/docscrape_sphinx.py b/doc/sphinxext/numpydoc/docscrape_sphinx.py index d202bad34..2061f3f10 100644 --- a/doc/sphinxext/numpydoc/docscrape_sphinx.py +++ b/doc/sphinxext/numpydoc/docscrape_sphinx.py @@ -5,6 +5,12 @@ import sphinx import collections from .docscrape import NumpyDocString, FunctionDoc, ClassDoc +if sys.version_info[0] >= 3: + sixu = lambda s: s +else: + sixu = lambda s: unicode(s, 'unicode_escape') + + class SphinxDocString(NumpyDocString): def __init__(self, docstring, config={}): self.use_plots = config.get('use_plots', False) @@ -95,11 +101,11 @@ class SphinxDocString(NumpyDocString): if others: maxlen_0 = max(3, max([len(x[0]) for x in others])) - hdr = u"="*maxlen_0 + u" " + u"="*10 - fmt = u'%%%ds %%s ' % (maxlen_0,) + hdr = sixu("=")*maxlen_0 + sixu(" ") + sixu("=")*10 + fmt = sixu('%%%ds %%s ') % (maxlen_0,) out += ['', hdr] for param, param_type, desc in others: - desc = u" ".join(x.strip() for x in desc).strip() + desc = sixu(" ").join(x.strip() for x in desc).strip() if param_type: desc = "(%s) %s" % (param_type, desc) out += [fmt % (param.strip(), desc)] diff --git a/doc/sphinxext/numpydoc/numpydoc.py b/doc/sphinxext/numpydoc/numpydoc.py index 773a87d37..b8a5e959c 100644 --- a/doc/sphinxext/numpydoc/numpydoc.py +++ b/doc/sphinxext/numpydoc/numpydoc.py @@ -17,16 +17,22 @@ It will: """ from __future__ import division, absolute_import, print_function +import os, sys, re, pydoc import sphinx +import inspect import collections if sphinx.__version__ < '1.0.1': raise RuntimeError("Sphinx 1.0.1 or newer is required") -import os, sys, re, pydoc from .docscrape_sphinx import get_doc_object, SphinxDocString from sphinx.util.compat import Directive -import inspect + +if sys.version_info[0] >= 3: + sixu = lambda s: s +else: + sixu = lambda s: unicode(s, 'unicode_escape') + def mangle_docstrings(app, what, name, obj, options, lines, reference_offset=[0]): @@ -36,32 +42,32 @@ def mangle_docstrings(app, what, name, obj, options, lines, if what == 'module': # Strip top title - title_re = re.compile(u'^\\s*[#*=]{4,}\\n[a-z0-9 -]+\\n[#*=]{4,}\\s*', + title_re = re.compile(sixu('^\\s*[#*=]{4,}\\n[a-z0-9 -]+\\n[#*=]{4,}\\s*'), re.I|re.S) - lines[:] = title_re.sub(u'', u"\n".join(lines)).split(u"\n") + lines[:] = title_re.sub(sixu(''), sixu("\n").join(lines)).split(sixu("\n")) else: - doc = get_doc_object(obj, what, u"\n".join(lines), config=cfg) + doc = get_doc_object(obj, what, sixu("\n").join(lines), config=cfg) if sys.version_info[0] >= 3: doc = str(doc) else: doc = str(doc).decode('utf-8') - lines[:] = doc.split(u"\n") + lines[:] = doc.split(sixu("\n")) if app.config.numpydoc_edit_link and hasattr(obj, '__name__') and \ obj.__name__: if hasattr(obj, '__module__'): - v = dict(full_name=u"%s.%s" % (obj.__module__, obj.__name__)) + v = dict(full_name=sixu("%s.%s") % (obj.__module__, obj.__name__)) else: v = dict(full_name=obj.__name__) - lines += [u'', u'.. htmlonly::', u''] - lines += [u' %s' % x for x in + lines += [sixu(''), sixu('.. htmlonly::'), sixu('')] + lines += [sixu(' %s') % x for x in (app.config.numpydoc_edit_link % v).split("\n")] # replace reference numbers so that there are no duplicates references = [] for line in lines: line = line.strip() - m = re.match(u'^.. \\[([a-z0-9_.-])\\]', line, re.I) + m = re.match(sixu('^.. \\[([a-z0-9_.-])\\]'), line, re.I) if m: references.append(m.group(1)) @@ -70,14 +76,14 @@ def mangle_docstrings(app, what, name, obj, options, lines, if references: for i, line in enumerate(lines): for r in references: - if re.match(u'^\\d+$', r): - new_r = u"R%d" % (reference_offset[0] + int(r)) + if re.match(sixu('^\\d+$'), r): + new_r = sixu("R%d") % (reference_offset[0] + int(r)) else: - new_r = u"%s%d" % (r, reference_offset[0]) - lines[i] = lines[i].replace(u'[%s]_' % r, - u'[%s]_' % new_r) - lines[i] = lines[i].replace(u'.. [%s]' % r, - u'.. [%s]' % new_r) + new_r = sixu("%s%d") % (r, reference_offset[0]) + lines[i] = lines[i].replace(sixu('[%s]_') % r, + sixu('[%s]_') % new_r) + lines[i] = lines[i].replace(sixu('.. [%s]') % r, + sixu('.. [%s]') % new_r) reference_offset[0] += len(references) @@ -93,8 +99,8 @@ def mangle_signature(app, what, name, obj, options, sig, retann): doc = SphinxDocString(pydoc.getdoc(obj)) if doc['Signature']: - sig = re.sub(u"^[^(]*", u"", doc['Signature']) - return sig, u'' + sig = re.sub(sixu("^[^(]*"), sixu(""), doc['Signature']) + return sig, sixu('') def setup(app, get_doc_object_=get_doc_object): if not hasattr(app, 'add_config_value'): diff --git a/doc/sphinxext/numpydoc/tests/test_docscrape.py b/doc/sphinxext/numpydoc/tests/test_docscrape.py index 60a9749ba..45bedc88a 100644 --- a/doc/sphinxext/numpydoc/tests/test_docscrape.py +++ b/doc/sphinxext/numpydoc/tests/test_docscrape.py @@ -7,6 +7,12 @@ from numpydoc.docscrape import NumpyDocString, FunctionDoc, ClassDoc from numpydoc.docscrape_sphinx import SphinxDocString, SphinxClassDoc from nose.tools import * +if sys.version_info[0] >= 3: + sixu = lambda s: s +else: + sixu = lambda s: unicode(s, 'unicode_escape') + + doc_txt = '''\ numpy.multivariate_normal(mean, cov, shape=None, spam=None) @@ -220,12 +226,12 @@ spam : parrot Raises ------ -RuntimeError : +RuntimeError : Some error Warns ----- -RuntimeWarning : +RuntimeWarning : Some warning Warnings @@ -324,7 +330,7 @@ of the one-dimensional normal distribution to higher dimensions. The drawn samples, arranged according to `shape`. If the shape given is (m,n,...), then the shape of `out` is is (m,n,...,N). - + In other words, each entry ``out[i,j,...,:]`` is an N-dimensional value drawn from the distribution. @@ -333,16 +339,16 @@ of the one-dimensional normal distribution to higher dimensions. **spam** : parrot A parrot off its mortal coil. - + :Raises: - **RuntimeError** : + **RuntimeError** : Some error :Warns: - **RuntimeWarning** : + **RuntimeWarning** : Some warning @@ -351,12 +357,12 @@ of the one-dimensional normal distribution to higher dimensions. Certain warnings apply. .. seealso:: - + :obj:`some`, :obj:`other`, :obj:`funcs` - + :obj:`otherfunc` relationship - + .. rubric:: Notes Instead of specifying the full covariance matrix, popular @@ -403,7 +409,7 @@ standard deviation: [True, True] """) - + doc2 = NumpyDocString(""" Returns array of indices of the maximum values of along the given axis. @@ -558,9 +564,9 @@ def test_unicode(): """) assert isinstance(doc['Summary'][0], str) if sys.version_info[0] >= 3: - assert doc['Summary'][0] == u'öäöäöäöäöåååå' + assert doc['Summary'][0] == sixu('öäöäöäöäöåååå') else: - assert doc['Summary'][0] == u'öäöäöäöäöåååå'.encode('utf-8') + assert doc['Summary'][0] == sixu('öäöäöäöäöåååå').encode('utf-8') def test_plot_examples(): cfg = dict(use_plots=True) @@ -578,7 +584,7 @@ def test_plot_examples(): Examples -------- .. plot:: - + import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,6]) plt.show() @@ -695,13 +701,13 @@ def test_class_members_doc(): Methods ------- - a : + a : - b : + b : - c : + c : - .. index:: + .. index:: """) @@ -728,16 +734,16 @@ def test_class_members_doc_sphinx(): .. rubric:: Attributes === ========== - t (float) Current time. - y (ndarray) Current variable values. + t (float) Current time. + y (ndarray) Current variable values. === ========== .. rubric:: Methods === ========== - a - b - c + a + b + c === ========== """) @@ -745,4 +751,3 @@ def test_class_members_doc_sphinx(): if __name__ == "__main__": import nose nose.run() - diff --git a/numpy/__init__.py b/numpy/__init__.py index 0689bc08a..bbbecd6f6 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -164,7 +164,8 @@ else: # Make these accessible from numpy name-space # but not imported in from numpy import * if sys.version_info[0] >= 3: - from builtins import bool, int, float, complex, object, unicode, str + from builtins import bool, int, float, complex, object, str + unicode = str else: from __builtin__ import bool, int, float, complex, object, unicode, str diff --git a/numpy/compat/_inspect.py b/numpy/compat/_inspect.py index 557a3da32..6a499e727 100644 --- a/numpy/compat/_inspect.py +++ b/numpy/compat/_inspect.py @@ -151,7 +151,7 @@ def joinseq(seq): def strseq(object, convert, join=joinseq): """Recursively walk a sequence, stringifying each element.""" - if type(object) in [types.ListType, types.TupleType]: + if type(object) in [list, tuple]: return join([strseq(_o, convert, join) for _o in object]) else: return convert(object) diff --git a/numpy/compat/py3k.py b/numpy/compat/py3k.py index ce79edde8..e7ab9bf0f 100644 --- a/numpy/compat/py3k.py +++ b/numpy/compat/py3k.py @@ -6,7 +6,7 @@ from __future__ import division, absolute_import, print_function __all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar', 'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested', - 'asstr', 'open_latin1', 'long', 'basestring'] + 'asstr', 'open_latin1', 'long', 'basestring', 'sixu'] import sys @@ -16,8 +16,8 @@ if sys.version_info[0] >= 3: long = int integer_types = (int,) basestring = str - bytes = bytes unicode = str + bytes = bytes def asunicode(s): if isinstance(s, bytes): @@ -40,14 +40,17 @@ if sys.version_info[0] >= 3: def open_latin1(filename, mode='r'): return open(filename, mode=mode, encoding='iso-8859-1') + def sixu(s): + return s + strchar = 'U' else: bytes = str - unicode = unicode long = long basestring = basestring + unicode = unicode integer_types = (int, long) asbytes = str asstr = str @@ -65,6 +68,9 @@ else: def open_latin1(filename, mode='r'): return open(filename, mode=mode) + def sixu(s): + return unicode(s, 'unicode_escape') + def getexception(): return sys.exc_info()[1] diff --git a/numpy/core/code_generators/genapi.py b/numpy/core/code_generators/genapi.py index 85d7b04f6..62e4f9fc8 100644 --- a/numpy/core/code_generators/genapi.py +++ b/numpy/core/code_generators/genapi.py @@ -15,8 +15,7 @@ try: except ImportError: import md5 md5new = md5.new -if sys.version_info[:2] < (2, 6): - from sets import Set as set + import textwrap from os.path import join diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py index 874a295ef..2a8b31616 100644 --- a/numpy/core/defchararray.py +++ b/numpy/core/defchararray.py @@ -375,69 +375,42 @@ def capitalize(a): a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'capitalize') -if sys.version_info >= (2, 4): - def center(a, width, fillchar=' '): - """ - Return a copy of `a` with its elements centered in a string of - length `width`. - - Calls `str.center` element-wise. - - Parameters - ---------- - a : array_like of str or unicode - width : int - The length of the resulting strings - fillchar : str or unicode, optional - The padding character to use (default is space). +def center(a, width, fillchar=' '): + """ + Return a copy of `a` with its elements centered in a string of + length `width`. - Returns - ------- - out : ndarray - Output array of str or unicode, depending on input - types + Calls `str.center` element-wise. - See also - -------- - str.center + Parameters + ---------- + a : array_like of str or unicode - """ - a_arr = numpy.asarray(a) - width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) - if numpy.issubdtype(a_arr.dtype, numpy.string_): - fillchar = asbytes(fillchar) - return _vec_string( - a_arr, (a_arr.dtype.type, size), 'center', (width_arr, fillchar)) -else: - def center(a, width): - """ - Return an array with the elements of `a` centered in a string - of length width. + width : int + The length of the resulting strings + fillchar : str or unicode, optional + The padding character to use (default is space). - Calls `str.center` element-wise. + Returns + ------- + out : ndarray + Output array of str or unicode, depending on input + types - Parameters - ---------- - a : array_like of str or unicode - width : int - The length of the resulting strings + See also + -------- + str.center - Returns - ------- - out : ndarray, str or unicode - Output array of str or unicode, depending on input types + """ + a_arr = numpy.asarray(a) + width_arr = numpy.asarray(width) + size = long(numpy.max(width_arr.flat)) + if numpy.issubdtype(a_arr.dtype, numpy.string_): + fillchar = asbytes(fillchar) + return _vec_string( + a_arr, (a_arr.dtype.type, size), 'center', (width_arr, fillchar)) - See also - -------- - str.center - """ - a_arr = numpy.asarray(a) - width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) - return _vec_string( - a_arr, (a_arr.dtype.type, size), 'center', (width_arr,)) def count(a, sub, start=0, end=None): """ @@ -484,6 +457,7 @@ def count(a, sub, start=0, end=None): """ return _vec_string(a, integer, 'count', [sub, start] + _clean_args(end)) + def decode(a, encoding=None, errors=None): """ Calls `str.decode` element-wise. @@ -529,6 +503,7 @@ def decode(a, encoding=None, errors=None): return _to_string_or_unicode_array( _vec_string(a, object_, 'decode', _clean_args(encoding, errors))) + def encode(a, encoding=None, errors=None): """ Calls `str.encode` element-wise. @@ -563,6 +538,7 @@ def encode(a, encoding=None, errors=None): return _to_string_or_unicode_array( _vec_string(a, object_, 'encode', _clean_args(encoding, errors))) + def endswith(a, suffix, start=0, end=None): """ Returns a boolean array which is `True` where the string element @@ -606,6 +582,7 @@ def endswith(a, suffix, start=0, end=None): return _vec_string( a, bool_, 'endswith', [suffix, start] + _clean_args(end)) + def expandtabs(a, tabsize=8): """ Return a copy of each string element where all tab characters are @@ -640,6 +617,7 @@ def expandtabs(a, tabsize=8): return _to_string_or_unicode_array( _vec_string(a, object_, 'expandtabs', (tabsize,))) + def find(a, sub, start=0, end=None): """ For each element, return the lowest index in the string where @@ -674,10 +652,6 @@ def find(a, sub, start=0, end=None): return _vec_string( a, integer, 'find', [sub, start] + _clean_args(end)) -# if sys.version_info >= (2.6): -# def format(a, *args, **kwargs): -# # _vec_string doesn't support kwargs at present -# raise NotImplementedError def index(a, sub, start=0, end=None): """ @@ -901,68 +875,41 @@ def join(sep, seq): return _to_string_or_unicode_array( _vec_string(sep, object_, 'join', (seq,))) -if sys.version_info >= (2, 4): - def ljust(a, width, fillchar=' '): - """ - Return an array with the elements of `a` left-justified in a - string of length `width`. - - Calls `str.ljust` element-wise. - Parameters - ---------- - a : array_like of str or unicode - - width : int - The length of the resulting strings - fillchar : str or unicode, optional - The character to use for padding +def ljust(a, width, fillchar=' '): + """ + Return an array with the elements of `a` left-justified in a + string of length `width`. - Returns - ------- - out : ndarray - Output array of str or unicode, depending on input type + Calls `str.ljust` element-wise. - See also - -------- - str.ljust + Parameters + ---------- + a : array_like of str or unicode - """ - a_arr = numpy.asarray(a) - width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) - if numpy.issubdtype(a_arr.dtype, numpy.string_): - fillchar = asbytes(fillchar) - return _vec_string( - a_arr, (a_arr.dtype.type, size), 'ljust', (width_arr, fillchar)) -else: - def ljust(a, width): - """ - Return an array with the elements of `a` left-justified in a - string of length `width`. + width : int + The length of the resulting strings + fillchar : str or unicode, optional + The character to use for padding - Calls `str.ljust` element-wise. + Returns + ------- + out : ndarray + Output array of str or unicode, depending on input type - Parameters - ---------- - a : array_like of str or unicode - width : int - The length of the resulting strings + See also + -------- + str.ljust - Returns - ------- - out : ndarray - Output array of str or unicode, depending on input type + """ + a_arr = numpy.asarray(a) + width_arr = numpy.asarray(width) + size = long(numpy.max(width_arr.flat)) + if numpy.issubdtype(a_arr.dtype, numpy.string_): + fillchar = asbytes(fillchar) + return _vec_string( + a_arr, (a_arr.dtype.type, size), 'ljust', (width_arr, fillchar)) - See also - -------- - str.ljust - """ - a_arr = numpy.asarray(a) - width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) - return _vec_string( - a_arr, (a_arr.dtype.type, size), 'ljust', (width_arr,)) def lower(a): """ @@ -999,6 +946,7 @@ def lower(a): a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'lower') + def lstrip(a, chars=None): """ For each element in `a`, return a copy with the leading characters @@ -1055,40 +1003,41 @@ def lstrip(a, chars=None): a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'lstrip', (chars,)) -if sys.version_info >= (2, 5): - def partition(a, sep): - """ - Partition each element in `a` around `sep`. - Calls `str.partition` element-wise. +def partition(a, sep): + """ + Partition each element in `a` around `sep`. - For each element in `a`, split the element as the first - occurrence of `sep`, and return 3 strings containing the part - before the separator, the separator itself, and the part after - the separator. If the separator is not found, return 3 strings - containing the string itself, followed by two empty strings. + Calls `str.partition` element-wise. - Parameters - ---------- - a : array_like, {str, unicode} - Input array - sep : {str, unicode} - Separator to split each string element in `a`. + For each element in `a`, split the element as the first + occurrence of `sep`, and return 3 strings containing the part + before the separator, the separator itself, and the part after + the separator. If the separator is not found, return 3 strings + containing the string itself, followed by two empty strings. - Returns - ------- - out : ndarray, {str, unicode} - Output array of str or unicode, depending on input type. - The output array will have an extra dimension with 3 - elements per input element. + Parameters + ---------- + a : array_like, {str, unicode} + Input array + sep : {str, unicode} + Separator to split each string element in `a`. - See also - -------- - str.partition + Returns + ------- + out : ndarray, {str, unicode} + Output array of str or unicode, depending on input type. + The output array will have an extra dimension with 3 + elements per input element. + + See also + -------- + str.partition + + """ + return _to_string_or_unicode_array( + _vec_string(a, object_, 'partition', (sep,))) - """ - return _to_string_or_unicode_array( - _vec_string(a, object_, 'partition', (sep,))) def replace(a, old, new, count=None): """ @@ -1121,6 +1070,7 @@ def replace(a, old, new, count=None): _vec_string( a, object_, 'replace', [old, new] +_clean_args(count))) + def rfind(a, sub, start=0, end=None): """ For each element in `a`, return the highest index in the string @@ -1152,6 +1102,7 @@ def rfind(a, sub, start=0, end=None): return _vec_string( a, integer, 'rfind', [sub, start] + _clean_args(end)) + def rindex(a, sub, start=0, end=None): """ Like `rfind`, but raises `ValueError` when the substring `sub` is @@ -1180,140 +1131,113 @@ def rindex(a, sub, start=0, end=None): return _vec_string( a, integer, 'rindex', [sub, start] + _clean_args(end)) -if sys.version_info >= (2, 4): - def rjust(a, width, fillchar=' '): - """ - Return an array with the elements of `a` right-justified in a - string of length `width`. - Calls `str.rjust` element-wise. +def rjust(a, width, fillchar=' '): + """ + Return an array with the elements of `a` right-justified in a + string of length `width`. - Parameters - ---------- - a : array_like of str or unicode + Calls `str.rjust` element-wise. - width : int - The length of the resulting strings - fillchar : str or unicode, optional - The character to use for padding + Parameters + ---------- + a : array_like of str or unicode - Returns - ------- - out : ndarray - Output array of str or unicode, depending on input type + width : int + The length of the resulting strings + fillchar : str or unicode, optional + The character to use for padding - See also - -------- - str.rjust + Returns + ------- + out : ndarray + Output array of str or unicode, depending on input type - """ - a_arr = numpy.asarray(a) - width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) - if numpy.issubdtype(a_arr.dtype, numpy.string_): - fillchar = asbytes(fillchar) - return _vec_string( - a_arr, (a_arr.dtype.type, size), 'rjust', (width_arr, fillchar)) -else: - def rjust(a, width): - """ - Return an array with the elements of `a` right-justified in a - string of length `width`. + See also + -------- + str.rjust - Calls `str.rjust` element-wise. + """ + a_arr = numpy.asarray(a) + width_arr = numpy.asarray(width) + size = long(numpy.max(width_arr.flat)) + if numpy.issubdtype(a_arr.dtype, numpy.string_): + fillchar = asbytes(fillchar) + return _vec_string( + a_arr, (a_arr.dtype.type, size), 'rjust', (width_arr, fillchar)) - Parameters - ---------- - a : array_like of str or unicode - width : int - The length of the resulting strings - Returns - ------- - out : ndarray - Output array of str or unicode, depending on input type +def rpartition(a, sep): + """ + Partition (split) each element around the right-most separator. - See also - -------- - str.rjust - """ - a_arr = numpy.asarray(a) - width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) - return _vec_string( - a_arr, (a_arr.dtype.type, size), 'rjust', (width,)) + Calls `str.rpartition` element-wise. -if sys.version_info >= (2, 5): - def rpartition(a, sep): - """ - Partition (split) each element around the right-most separator. + For each element in `a`, split the element as the last + occurrence of `sep`, and return 3 strings containing the part + before the separator, the separator itself, and the part after + the separator. If the separator is not found, return 3 strings + containing the string itself, followed by two empty strings. - Calls `str.rpartition` element-wise. + Parameters + ---------- + a : array_like of str or unicode + Input array + sep : str or unicode + Right-most separator to split each element in array. - For each element in `a`, split the element as the last - occurrence of `sep`, and return 3 strings containing the part - before the separator, the separator itself, and the part after - the separator. If the separator is not found, return 3 strings - containing the string itself, followed by two empty strings. + Returns + ------- + out : ndarray + Output array of string or unicode, depending on input + type. The output array will have an extra dimension with + 3 elements per input element. - Parameters - ---------- - a : array_like of str or unicode - Input array - sep : str or unicode - Right-most separator to split each element in array. + See also + -------- + str.rpartition - Returns - ------- - out : ndarray - Output array of string or unicode, depending on input - type. The output array will have an extra dimension with - 3 elements per input element. + """ + return _to_string_or_unicode_array( + _vec_string(a, object_, 'rpartition', (sep,))) - See also - -------- - str.rpartition - """ - return _to_string_or_unicode_array( - _vec_string(a, object_, 'rpartition', (sep,))) +def rsplit(a, sep=None, maxsplit=None): + """ + For each element in `a`, return a list of the words in the + string, using `sep` as the delimiter string. -if sys.version_info >= (2, 4): - def rsplit(a, sep=None, maxsplit=None): - """ - For each element in `a`, return a list of the words in the - string, using `sep` as the delimiter string. + Calls `str.rsplit` element-wise. - Calls `str.rsplit` element-wise. + Except for splitting from the right, `rsplit` + behaves like `split`. - Except for splitting from the right, `rsplit` - behaves like `split`. + Parameters + ---------- + a : array_like of str or unicode - Parameters - ---------- - a : array_like of str or unicode + sep : str or unicode, optional + If `sep` is not specified or `None`, any whitespace string + is a separator. + maxsplit : int, optional + If `maxsplit` is given, at most `maxsplit` splits are done, + the rightmost ones. - sep : str or unicode, optional - If `sep` is not specified or `None`, any whitespace string - is a separator. - maxsplit : int, optional - If `maxsplit` is given, at most `maxsplit` splits are done, - the rightmost ones. + Returns + ------- + out : ndarray + Array of list objects - Returns - ------- - out : ndarray - Array of list objects + See also + -------- + str.rsplit, split - See also - -------- - str.rsplit, split + """ + # This will return an array of lists of different sizes, so we + # leave it as an object array + return _vec_string( + a, object_, 'rsplit', [sep] + _clean_args(maxsplit)) - """ - # This will return an array of lists of different sizes, so we - # leave it as an object array - return _vec_string( - a, object_, 'rsplit', [sep] + _clean_args(maxsplit)) def rstrip(a, chars=None): """ @@ -1358,6 +1282,7 @@ def rstrip(a, chars=None): a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'rstrip', (chars,)) + def split(a, sep=None, maxsplit=None): """ For each element in `a`, return a list of the words in the @@ -1391,6 +1316,7 @@ def split(a, sep=None, maxsplit=None): return _vec_string( a, object_, 'split', [sep] + _clean_args(maxsplit)) + def splitlines(a, keepends=None): """ For each element in `a`, return a list of the lines in the @@ -1419,6 +1345,7 @@ def splitlines(a, keepends=None): return _vec_string( a, object_, 'splitlines', _clean_args(keepends)) + def startswith(a, prefix, start=0, end=None): """ Returns a boolean array which is `True` where the string element @@ -1449,6 +1376,7 @@ def startswith(a, prefix, start=0, end=None): return _vec_string( a, bool_, 'startswith', [prefix, start] + _clean_args(end)) + def strip(a, chars=None): """ For each element in `a`, return a copy with the leading and @@ -1496,6 +1424,7 @@ def strip(a, chars=None): a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'strip', _clean_args(chars)) + def swapcase(a): """ Return element-wise a copy of the string with @@ -1532,6 +1461,7 @@ def swapcase(a): a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'swapcase') + def title(a): """ Return element-wise title cased version of string or unicode. @@ -1570,6 +1500,7 @@ def title(a): a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'title') + def translate(a, table, deletechars=None): """ For each element in `a`, return a copy of the string where all @@ -1605,6 +1536,7 @@ def translate(a, table, deletechars=None): return _vec_string( a_arr, a_arr.dtype, 'translate', [table] + _clean_args(deletechars)) + def upper(a): """ Return an array with the elements converted to uppercase. @@ -1640,6 +1572,7 @@ def upper(a): a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'upper') + def zfill(a, width): """ Return the numeric string left-filled with zeros @@ -1669,6 +1602,7 @@ def zfill(a, width): return _vec_string( a_arr, (a_arr.dtype.type, size), 'zfill', (width_arr,)) + def isnumeric(a): """ For each element, return True if there are only numeric @@ -1699,6 +1633,7 @@ def isnumeric(a): raise TypeError("isnumeric is only available for Unicode strings and arrays") return _vec_string(a, bool_, 'isnumeric') + def isdecimal(a): """ For each element, return True if there are only decimal @@ -2079,28 +2014,16 @@ class chararray(ndarray): """ return asarray(capitalize(self)) - if sys.version_info >= (2, 4): - def center(self, width, fillchar=' '): - """ - Return a copy of `self` with its elements centered in a - string of length `width`. - - See also - -------- - center - """ - return asarray(center(self, width, fillchar)) - else: - def center(self, width): - """ - Return a copy of `self` with its elements centered in a - string of length `width`. + def center(self, width, fillchar=' '): + """ + Return a copy of `self` with its elements centered in a + string of length `width`. - See also - -------- - center - """ - return asarray(center(self, width)) + See also + -------- + center + """ + return asarray(center(self, width, fillchar)) def count(self, sub, start=0, end=None): """ @@ -2285,29 +2208,17 @@ class chararray(ndarray): """ return join(self, seq) - if sys.version_info >= (2, 4): - def ljust(self, width, fillchar=' '): - """ - Return an array with the elements of `self` left-justified in a - string of length `width`. - - See also - -------- - char.ljust + def ljust(self, width, fillchar=' '): + """ + Return an array with the elements of `self` left-justified in a + string of length `width`. - """ - return asarray(ljust(self, width, fillchar)) - else: - def ljust(self, width): - """ - Return an array with the elements of `self` left-justified in a - string of length `width`. + See also + -------- + char.ljust - See also - -------- - ljust - """ - return asarray(ljust(self, width)) + """ + return asarray(ljust(self, width, fillchar)) def lower(self): """ @@ -2333,16 +2244,15 @@ class chararray(ndarray): """ return asarray(lstrip(self, chars)) - if sys.version_info >= (2, 5): - def partition(self, sep): - """ - Partition each element in `self` around `sep`. + def partition(self, sep): + """ + Partition each element in `self` around `sep`. - See also - -------- - partition - """ - return asarray(partition(self, sep)) + See also + -------- + partition + """ + return asarray(partition(self, sep)) def replace(self, old, new, count=None): """ @@ -2381,53 +2291,39 @@ class chararray(ndarray): """ return rindex(self, sub, start, end) - if sys.version_info >= (2, 4): - def rjust(self, width, fillchar=' '): - """ - Return an array with the elements of `self` - right-justified in a string of length `width`. + def rjust(self, width, fillchar=' '): + """ + Return an array with the elements of `self` + right-justified in a string of length `width`. + + See also + -------- + char.rjust - See also - -------- - char.rjust + """ + return asarray(rjust(self, width, fillchar)) - """ - return asarray(rjust(self, width, fillchar)) - else: - def rjust(self, width): - """ - Return an array with the elements of `self` - right-justified in a string of length `width`. - - See also - -------- - rjust - """ - return asarray(rjust(self, width)) - - if sys.version_info >= (2, 5): - def rpartition(self, sep): - """ - Partition each element in `self` around `sep`. - - See also - -------- - rpartition - """ - return asarray(rpartition(self, sep)) - - if sys.version_info >= (2, 4): - def rsplit(self, sep=None, maxsplit=None): - """ - For each element in `self`, return a list of the words in - the string, using `sep` as the delimiter string. - - See also - -------- - char.rsplit - - """ - return rsplit(self, sep, maxsplit) + def rpartition(self, sep): + """ + Partition each element in `self` around `sep`. + + See also + -------- + rpartition + """ + return asarray(rpartition(self, sep)) + + def rsplit(self, sep=None, maxsplit=None): + """ + For each element in `self`, return a list of the words in + the string, using `sep` as the delimiter string. + + See also + -------- + char.rsplit + + """ + return rsplit(self, sep, maxsplit) def rstrip(self, chars=None): """ diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index a301bb7e9..84e45a6a5 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -3,7 +3,16 @@ """ from __future__ import division, absolute_import, print_function -__docformat__ = "restructuredtext en" +import types + +from . import multiarray as mu +from . import umath as um +from . import numerictypes as nt +from .numeric import asarray, array, asanyarray, concatenate +from . import _methods + +_dt_ = nt.sctype2char + # functions that are now methods __all__ = ['take', 'reshape', 'choose', 'repeat', 'put', @@ -16,19 +25,11 @@ __all__ = ['take', 'reshape', 'choose', 'repeat', 'put', 'amax', 'amin', ] -from . import multiarray as mu -from . import umath as um -from . import numerictypes as nt -from .numeric import asarray, array, asanyarray, concatenate -from . import _methods -_dt_ = nt.sctype2char - -import types try: _gentype = types.GeneratorType except AttributeError: - _gentype = types.NoneType + _gentype = type(None) # save away Python sum _sum_ = sum diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index a4f049f2c..e6cbe7a71 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -248,14 +248,10 @@ class memmap(ndarray): else: acc = mmap.ACCESS_WRITE - if sys.version_info[:2] >= (2,6): - # The offset keyword in mmap.mmap needs Python >= 2.6 - start = offset - offset % mmap.ALLOCATIONGRANULARITY - bytes -= start - offset -= start - mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start) - else: - mm = mmap.mmap(fid.fileno(), bytes, access=acc) + start = offset - offset % mmap.ALLOCATIONGRANULARITY + bytes -= start + offset -= start + mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start) self = ndarray.__new__(subtype, shape, dtype=descr, buffer=mm, offset=offset, order=order) diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index e164427af..1a5f31e4e 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -102,7 +102,8 @@ from numpy.compat import bytes, long # we don't export these for import *, but we do want them accessible # as numerictypes.bool, etc. if sys.version_info[0] >= 3: - from builtins import bool, int, float, complex, object, unicode, str + from builtins import bool, int, float, complex, object, str + unicode = str else: from __builtin__ import bool, int, float, complex, object, unicode, str diff --git a/numpy/core/records.py b/numpy/core/records.py index 4f520ae66..4fe1f8444 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -36,18 +36,18 @@ Record arrays allow us to access fields as properties:: """ from __future__ import division, absolute_import, print_function -# All of the functions allow formats to be a dtype -__all__ = ['record', 'recarray', 'format_parser'] +import sys +import os from . import numeric as sb from .defchararray import chararray from . import numerictypes as nt -import types -import os -import sys - from numpy.compat import isfileobj, bytes, long +# All of the functions allow formats to be a dtype +__all__ = ['record', 'recarray', 'format_parser'] + + ndarray = sb.ndarray _byteorderconv = {'b':'>', @@ -171,7 +171,7 @@ class format_parser: attribute """ if (names): - if (type(names) in [types.ListType, types.TupleType]): + if (type(names) in [list, tuple]): pass elif (type(names) == str): names = names.split(',') diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 4e4630eb0..0b2ecfe67 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -154,11 +154,10 @@ def check_math_capabilities(config, moredefs, mathlibs): # config.h in the public namespace, so we have a clash for the common # functions we test. We remove every function tested by python's # autoconf, hoping their own test are correct - if sys.version_info[:2] >= (2, 5): - for f in OPTIONAL_STDFUNCS_MAYBE: - if config.check_decl(fname2def(f), - headers=["Python.h", "math.h"]): - OPTIONAL_STDFUNCS.remove(f) + for f in OPTIONAL_STDFUNCS_MAYBE: + if config.check_decl(fname2def(f), + headers=["Python.h", "math.h"]): + OPTIONAL_STDFUNCS.remove(f) check_funcs(OPTIONAL_STDFUNCS) @@ -222,19 +221,16 @@ def check_ieee_macros(config): # functions we test. We remove every function tested by python's # autoconf, hoping their own test are correct _macros = ["isnan", "isinf", "signbit", "isfinite"] - if sys.version_info[:2] >= (2, 6): - for f in _macros: - py_symbol = fname2def("decl_%s" % f) - already_declared = config.check_decl(py_symbol, - headers=["Python.h", "math.h"]) - if already_declared: - if config.check_macro_true(py_symbol, - headers=["Python.h", "math.h"]): - pub.append('NPY_%s' % fname2def("decl_%s" % f)) - else: - macros.append(f) - else: - macros = _macros[:] + for f in _macros: + py_symbol = fname2def("decl_%s" % f) + already_declared = config.check_decl(py_symbol, + headers=["Python.h", "math.h"]) + if already_declared: + if config.check_macro_true(py_symbol, + headers=["Python.h", "math.h"]): + pub.append('NPY_%s' % fname2def("decl_%s" % f)) + else: + macros.append(f) # Normally, isnan and isinf are macro (C99), but some platforms only have # func, or both func and macro version. Check for macro only, and define # replacement ones if not found. diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c index e1483f4e1..957a74fde 100644 --- a/numpy/core/src/multiarray/convert_datatype.c +++ b/numpy/core/src/multiarray/convert_datatype.c @@ -141,6 +141,12 @@ PyArray_AdaptFlexibleDType(PyObject *data_obj, PyArray_Descr *data_dtype, { PyArray_DatetimeMetaData *meta; int flex_type_num; + PyArrayObject *arr = NULL, *ret; + PyArray_Descr *dtype = NULL; + int ndim = 0; + npy_intp dims[NPY_MAXDIMS]; + PyObject *list = NULL; + int result; if (*flex_dtype == NULL) { if (!PyErr_Occurred()) { @@ -220,6 +226,37 @@ PyArray_AdaptFlexibleDType(PyObject *data_obj, PyArray_Descr *data_dtype, break; case NPY_OBJECT: size = 64; + /* + * If we're adapting a string dtype for an array of string + * objects, call GetArrayParamsFromObject to figure out + * maximum string size, and use that as new dtype size. + */ + if ((flex_type_num == NPY_STRING || + flex_type_num == NPY_UNICODE) && + data_obj != NULL) { + /* + * Convert data array to list of objects since + * GetArrayParamsFromObject won't iterate through + * items in an array. + */ + list = PyArray_ToList(data_obj); + if (list != NULL) { + result = PyArray_GetArrayParamsFromObject( + list, + flex_dtype, + 0, &dtype, + &ndim, dims, &arr, NULL); + if (result == 0 && dtype != NULL) { + if (flex_type_num == NPY_UNICODE) { + size = dtype->elsize / 4; + } + else { + size = dtype->elsize; + } + } + Py_DECREF(list); + } + } break; case NPY_STRING: case NPY_VOID: diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index 1d4b93b0f..8ab48f2d1 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -6,6 +6,7 @@ import numpy as np from numpy.testing import * from numpy.testing.utils import WarningManager import warnings +from numpy.compat import sixu # Switch between new behaviour when NPY_RELAXED_STRIDES_CHECKING is set. NPY_RELAXED_STRIDES_CHECKING = np.ones((10,1), order='C').flags.f_contiguous @@ -89,6 +90,27 @@ def test_array_astype(): assert_(not (a is b)) assert_(type(b) != np.matrix) + # Make sure converting from string object to fixed length string + # does not truncate. + a = np.array([b'a'*100], dtype='O') + b = a.astype('S') + assert_equal(a, b) + assert_equal(b.dtype, np.dtype('S100')) + a = np.array([sixu('a')*100], dtype='O') + b = a.astype('U') + assert_equal(a, b) + assert_equal(b.dtype, np.dtype('U100')) + + # Same test as above but for strings shorter than 64 characters + a = np.array([b'a'*10], dtype='O') + b = a.astype('S') + assert_equal(a, b) + assert_equal(b.dtype, np.dtype('S10')) + a = np.array([sixu('a')*10], dtype='O') + b = a.astype('U') + assert_equal(a, b) + assert_equal(b.dtype, np.dtype('U10')) + def test_copyto_fromscalar(): a = np.arange(6, dtype='f4').reshape(2,3) diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 3c024d934..44bf5f397 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -5,6 +5,7 @@ from __future__ import division, absolute_import, print_function import sys import numpy as np from numpy.testing import * +from numpy.compat import sixu class TestArrayRepr(object): def test_nan_inf(self): @@ -157,7 +158,7 @@ def test_unicode_object_array(): expected = "array(['é'], dtype=object)" else: expected = "array([u'\\xe9'], dtype=object)" - x = np.array([u'\xe9'], dtype=object) + x = np.array([sixu('\xe9')], dtype=object) assert_equal(repr(x), expected) diff --git a/numpy/core/tests/test_defchararray.py b/numpy/core/tests/test_defchararray.py index be8abb526..4656c842f 100644 --- a/numpy/core/tests/test_defchararray.py +++ b/numpy/core/tests/test_defchararray.py @@ -6,7 +6,7 @@ import numpy as np import sys from numpy.core.multiarray import _vec_string -from numpy.compat import asbytes, asbytes_nested +from numpy.compat import asbytes, asbytes_nested, sixu kw_unicode_true = {'unicode': True} # make 2to3 work properly kw_unicode_false = {'unicode': False} @@ -21,12 +21,12 @@ class TestBasic(TestCase): ['long', '0123456789']])) def test_from_object_array_unicode(self): - A = np.array([['abc', u'Sigma \u03a3'], + A = np.array([['abc', sixu('Sigma \u03a3')], ['long ', '0123456789']], dtype='O') self.assertRaises(ValueError, np.char.array, (A,)) B = np.char.array(A, **kw_unicode_true) assert_equal(B.dtype.itemsize, 10 * np.array('a', 'U').dtype.itemsize) - assert_array_equal(B, [['abc', u'Sigma \u03a3'], + assert_array_equal(B, [['abc', sixu('Sigma \u03a3')], ['long', '0123456789']]) def test_from_string_array(self): @@ -47,7 +47,7 @@ class TestBasic(TestCase): assert_(C[0,0] == A[0,0]) def test_from_unicode_array(self): - A = np.array([['abc', u'Sigma \u03a3'], + A = np.array([['abc', sixu('Sigma \u03a3')], ['long ', '0123456789']]) assert_equal(A.dtype.type, np.unicode_) B = np.char.array(A) @@ -64,7 +64,7 @@ class TestBasic(TestCase): def test_unicode_upconvert(self): A = np.char.array(['abc']) - B = np.char.array([u'\u03a3']) + B = np.char.array([sixu('\u03a3')]) assert_(issubclass((A + B).dtype.type, np.unicode_)) def test_from_string(self): @@ -74,7 +74,7 @@ class TestBasic(TestCase): assert_(issubclass(A.dtype.type, np.string_)) def test_from_unicode(self): - A = np.char.array(u'\u03a3') + A = np.char.array(sixu('\u03a3')) assert_equal(len(A), 1) assert_equal(len(A[0]), 1) assert_equal(A.itemsize, 4) @@ -186,9 +186,9 @@ class TestInformation(TestCase): self.A = np.array([[' abc ', ''], ['12345', 'MixedCase'], ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray) - self.B = np.array([[u' \u03a3 ', u''], - [u'12345', u'MixedCase'], - [u'123 \t 345 \0 ', u'UPPER']]).view(np.chararray) + self.B = np.array([[sixu(' \u03a3 '), sixu('')], + [sixu('12345'), sixu('MixedCase')], + [sixu('123 \t 345 \0 '), sixu('UPPER')]]).view(np.chararray) def test_len(self): assert_(issubclass(np.char.str_len(self.A).dtype.type, np.integer)) @@ -285,9 +285,9 @@ class TestMethods(TestCase): ['12345', 'MixedCase'], ['123 \t 345 \0 ', 'UPPER']], dtype='S').view(np.chararray) - self.B = np.array([[u' \u03a3 ', u''], - [u'12345', u'MixedCase'], - [u'123 \t 345 \0 ', u'UPPER']]).view(np.chararray) + self.B = np.array([[sixu(' \u03a3 '), sixu('')], + [sixu('12345'), sixu('MixedCase')], + [sixu('123 \t 345 \0 '), sixu('UPPER')]]).view(np.chararray) def test_capitalize(self): assert_(issubclass(self.A.capitalize().dtype.type, np.string_)) @@ -297,7 +297,7 @@ class TestMethods(TestCase): ['123 \t 345 \0 ', 'Upper']])) assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_)) assert_array_equal(self.B.capitalize(), [ - [u' \u03c3 ', ''], + [sixu(' \u03c3 '), ''], ['12345', 'Mixedcase'], ['123 \t 345 \0 ', 'Upper']]) @@ -325,7 +325,7 @@ class TestMethods(TestCase): def test_encode(self): B = self.B.encode('unicode_escape') - assert_(B[0][0] == asbytes(r' \u03a3 ')) + assert_(B[0][0] == str(' \\u03a3 ').encode('latin1')) def test_expandtabs(self): T = self.A.expandtabs() @@ -373,9 +373,9 @@ class TestMethods(TestCase): ['123 \t 345 \0 ', 'upper']])) assert_(issubclass(self.B.lower().dtype.type, np.unicode_)) assert_array_equal(self.B.lower(), [ - [u' \u03c3 ', u''], - [u'12345', u'mixedcase'], - [u'123 \t 345 \0 ', u'upper']]) + [sixu(' \u03c3 '), sixu('')], + [sixu('12345'), sixu('mixedcase')], + [sixu('123 \t 345 \0 '), sixu('upper')]]) def test_lstrip(self): assert_(issubclass(self.A.lstrip().dtype.type, np.string_)) @@ -390,18 +390,17 @@ class TestMethods(TestCase): ['23 \t 345 \x00', 'UPPER']])) assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_)) assert_array_equal(self.B.lstrip(), [ - [u'\u03a3 ', ''], + [sixu('\u03a3 '), ''], ['12345', 'MixedCase'], ['123 \t 345 \0 ', 'UPPER']]) def test_partition(self): - if sys.version_info >= (2, 5): - P = self.A.partition(asbytes_nested(['3', 'M'])) - assert_(issubclass(P.dtype.type, np.string_)) - assert_array_equal(P, asbytes_nested([ - [(' abc ', '', ''), ('', '', '')], - [('12', '3', '45'), ('', 'M', 'ixedCase')], - [('12', '3', ' \t 345 \0 '), ('UPPER', '', '')]])) + P = self.A.partition(asbytes_nested(['3', 'M'])) + assert_(issubclass(P.dtype.type, np.string_)) + assert_array_equal(P, asbytes_nested([ + [(' abc ', '', ''), ('', '', '')], + [('12', '3', '45'), ('', 'M', 'ixedCase')], + [('12', '3', ' \t 345 \0 '), ('UPPER', '', '')]])) def test_replace(self): R = self.A.replace(asbytes_nested(['3', 'a']), @@ -414,11 +413,11 @@ class TestMethods(TestCase): if sys.version_info[0] < 3: # NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3 - R = self.A.replace(asbytes('a'), u'\u03a3') + R = self.A.replace(asbytes('a'), sixu('\u03a3')) assert_(issubclass(R.dtype.type, np.unicode_)) assert_array_equal(R, [ - [u' \u03a3bc ', ''], - ['12345', u'MixedC\u03a3se'], + [sixu(' \u03a3bc '), ''], + ['12345', sixu('MixedC\u03a3se')], ['123 \t 345 \x00', 'UPPER']]) def test_rjust(self): @@ -437,13 +436,12 @@ class TestMethods(TestCase): [' FOO', ' FOO']])) def test_rpartition(self): - if sys.version_info >= (2, 5): - P = self.A.rpartition(asbytes_nested(['3', 'M'])) - assert_(issubclass(P.dtype.type, np.string_)) - assert_array_equal(P, asbytes_nested([ - [('', '', ' abc '), ('', '', '')], - [('12', '3', '45'), ('', 'M', 'ixedCase')], - [('123 \t ', '3', '45 \0 '), ('', '', 'UPPER')]])) + P = self.A.rpartition(asbytes_nested(['3', 'M'])) + assert_(issubclass(P.dtype.type, np.string_)) + assert_array_equal(P, asbytes_nested([ + [('', '', ' abc '), ('', '', '')], + [('12', '3', '45'), ('', 'M', 'ixedCase')], + [('123 \t ', '3', '45 \0 '), ('', '', 'UPPER')]])) def test_rsplit(self): A = self.A.rsplit(asbytes('3')) @@ -466,7 +464,7 @@ class TestMethods(TestCase): ['123 \t 345 \x00', 'UPP']])) assert_(issubclass(self.B.rstrip().dtype.type, np.unicode_)) assert_array_equal(self.B.rstrip(), [ - [u' \u03a3', ''], + [sixu(' \u03a3'), ''], ['12345', 'MixedCase'], ['123 \t 345', 'UPPER']]) @@ -483,7 +481,7 @@ class TestMethods(TestCase): ['23 \t 345 \x00', 'UPP']])) assert_(issubclass(self.B.strip().dtype.type, np.unicode_)) assert_array_equal(self.B.strip(), [ - [u'\u03a3', ''], + [sixu('\u03a3'), ''], ['12345', 'MixedCase'], ['123 \t 345', 'UPPER']]) @@ -509,9 +507,9 @@ class TestMethods(TestCase): ['123 \t 345 \0 ', 'upper']])) assert_(issubclass(self.B.swapcase().dtype.type, np.unicode_)) assert_array_equal(self.B.swapcase(), [ - [u' \u03c3 ', u''], - [u'12345', u'mIXEDcASE'], - [u'123 \t 345 \0 ', u'upper']]) + [sixu(' \u03c3 '), sixu('')], + [sixu('12345'), sixu('mIXEDcASE')], + [sixu('123 \t 345 \0 '), sixu('upper')]]) def test_title(self): assert_(issubclass(self.A.title().dtype.type, np.string_)) @@ -521,9 +519,9 @@ class TestMethods(TestCase): ['123 \t 345 \0 ', 'Upper']])) assert_(issubclass(self.B.title().dtype.type, np.unicode_)) assert_array_equal(self.B.title(), [ - [u' \u03a3 ', u''], - [u'12345', u'Mixedcase'], - [u'123 \t 345 \0 ', u'Upper']]) + [sixu(' \u03a3 '), sixu('')], + [sixu('12345'), sixu('Mixedcase')], + [sixu('123 \t 345 \0 '), sixu('Upper')]]) def test_upper(self): assert_(issubclass(self.A.upper().dtype.type, np.string_)) @@ -533,9 +531,9 @@ class TestMethods(TestCase): ['123 \t 345 \0 ', 'UPPER']])) assert_(issubclass(self.B.upper().dtype.type, np.unicode_)) assert_array_equal(self.B.upper(), [ - [u' \u03a3 ', u''], - [u'12345', u'MIXEDCASE'], - [u'123 \t 345 \0 ', u'UPPER']]) + [sixu(' \u03a3 '), sixu('')], + [sixu('12345'), sixu('MIXEDCASE')], + [sixu('123 \t 345 \0 '), sixu('UPPER')]]) def test_isnumeric(self): def fail(): diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 5661deb02..9b77a1fc1 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -86,7 +86,6 @@ class TestFloatScalarIndexDeprecation(object): category=DeprecationWarning) - @dec.skipif(sys.version_info[:2] < (2, 5)) def test_deprecations(self): a = np.array([[[5]]]) @@ -159,7 +158,6 @@ class TestFloatSliceParameterDeprecation(object): category=DeprecationWarning) - @dec.skipif(sys.version_info[:2] < (2, 5)) def test_deprecations(self): a = np.array([[5]]) diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 21af2e82c..7493c20e2 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -440,7 +440,7 @@ class TestString(TestCase): assert_equal(repr(dt), "dtype([('a', '<M8[D]'), ('b', '<m8[us]')])") - @dec.skipif(sys.version_info[0] > 2) + @dec.skipif(sys.version_info[0] >= 3) def test_dtype_str_with_long_in_shape(self): # Pull request #376 dt = np.dtype('(1L,)i4') diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 746eb73b5..e3520f123 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -4,12 +4,12 @@ import tempfile import sys import os import warnings + import numpy as np from nose import SkipTest from numpy.core import * -from numpy.compat import asbytes from numpy.testing.utils import WarningManager -from numpy.compat import asbytes, getexception, strchar +from numpy.compat import asbytes, getexception, strchar, sixu from test_print import in_foreign_locale from numpy.core.multiarray_tests import ( test_neighborhood_iterator, test_neighborhood_iterator_oob, @@ -31,7 +31,7 @@ if sys.version_info[:2] > (3, 2): # is an empty tuple instead of None. # http://docs.python.org/dev/whatsnew/3.3.html#api-changes EMPTY = () -else: +else: EMPTY = None @@ -1246,8 +1246,8 @@ class TestStringCompare(TestCase): def test_unicode(self): - g1 = array([u"This",u"is",u"example"]) - g2 = array([u"This",u"was",u"example"]) + g1 = array([sixu("This"),sixu("is"),sixu("example")]) + g2 = array([sixu("This"),sixu("was"),sixu("example")]) assert_array_equal(g1 == g2, [g1[i] == g2[i] for i in [0,1,2]]) assert_array_equal(g1 != g2, [g1[i] != g2[i] for i in [0,1,2]]) assert_array_equal(g1 <= g2, [g1[i] <= g2[i] for i in [0,1,2]]) @@ -2000,8 +2000,8 @@ class TestRecord(TestCase): raise SkipTest('non ascii unicode field indexing skipped; ' 'raises segfault on python 2.x') else: - assert_raises(ValueError, a.__setitem__, u'\u03e0', 1) - assert_raises(ValueError, a.__getitem__, u'\u03e0') + assert_raises(ValueError, a.__setitem__, sixu('\u03e0'), 1) + assert_raises(ValueError, a.__getitem__, sixu('\u03e0')) def test_field_names_deprecation(self): import warnings @@ -2497,369 +2497,367 @@ class TestMinScalarType(object): def test_usigned_short(self): dt = np.min_scalar_type(2**16-1) wanted = np.dtype('uint16') - assert_equal(wanted, dt) + assert_equal(wanted, dt) def test_usigned_int(self): dt = np.min_scalar_type(2**32-1) wanted = np.dtype('uint32') - assert_equal(wanted, dt) + assert_equal(wanted, dt) def test_usigned_longlong(self): dt = np.min_scalar_type(2**63-1) wanted = np.dtype('uint64') - assert_equal(wanted, dt) + assert_equal(wanted, dt) def test_object(self): dt = np.min_scalar_type(2**64) wanted = np.dtype('O') assert_equal(wanted, dt) - -if sys.version_info >= (2, 6): - - if sys.version_info[:2] == (2, 6): - from numpy.core.multiarray import memorysimpleview as memoryview - - from numpy.core._internal import _dtype_from_pep3118 - - class TestPEP3118Dtype(object): - def _check(self, spec, wanted): - dt = np.dtype(wanted) - if isinstance(wanted, list) and isinstance(wanted[-1], tuple): - if wanted[-1][0] == '': - names = list(dt.names) - names[-1] = '' - dt.names = tuple(names) - assert_equal(_dtype_from_pep3118(spec), dt, - err_msg="spec %r != dtype %r" % (spec, wanted)) - - def test_native_padding(self): - align = np.dtype('i').alignment - for j in range(8): - if j == 0: - s = 'bi' - else: - s = 'b%dxi' % j - self._check('@'+s, {'f0': ('i1', 0), - 'f1': ('i', align*(1 + j//align))}) - self._check('='+s, {'f0': ('i1', 0), - 'f1': ('i', 1+j)}) - - def test_native_padding_2(self): - # Native padding should work also for structs and sub-arrays - self._check('x3T{xi}', {'f0': (({'f0': ('i', 4)}, (3,)), 4)}) - self._check('^x3T{xi}', {'f0': (({'f0': ('i', 1)}, (3,)), 1)}) - - def test_trailing_padding(self): - # Trailing padding should be included, *and*, the item size - # should match the alignment if in aligned mode - align = np.dtype('i').alignment - def VV(n): - return 'V%d' % (align*(1 + (n-1)//align)) - - self._check('ix', [('f0', 'i'), ('', VV(1))]) - self._check('ixx', [('f0', 'i'), ('', VV(2))]) - self._check('ixxx', [('f0', 'i'), ('', VV(3))]) - self._check('ixxxx', [('f0', 'i'), ('', VV(4))]) - self._check('i7x', [('f0', 'i'), ('', VV(7))]) - - self._check('^ix', [('f0', 'i'), ('', 'V1')]) - self._check('^ixx', [('f0', 'i'), ('', 'V2')]) - self._check('^ixxx', [('f0', 'i'), ('', 'V3')]) - self._check('^ixxxx', [('f0', 'i'), ('', 'V4')]) - self._check('^i7x', [('f0', 'i'), ('', 'V7')]) - - def test_native_padding_3(self): - dt = np.dtype( - [('a', 'b'), ('b', 'i'), - ('sub', np.dtype('b,i')), ('c', 'i')], - align=True) - self._check("T{b:a:xxxi:b:T{b:f0:=i:f1:}:sub:xxxi:c:}", dt) - - dt = np.dtype( - [('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'), - ('e', 'b'), ('sub', np.dtype('b,i', align=True))]) - self._check("T{b:a:=i:b:b:c:b:d:b:e:T{b:f0:xxxi:f1:}:sub:}", dt) - - def test_padding_with_array_inside_struct(self): - dt = np.dtype( - [('a', 'b'), ('b', 'i'), ('c', 'b', (3,)), - ('d', 'i')], - align=True) - self._check("T{b:a:xxxi:b:3b:c:xi:d:}", dt) - - def test_byteorder_inside_struct(self): - # The byte order after @T{=i} should be '=', not '@'. - # Check this by noting the absence of native alignment. - self._check('@T{^i}xi', {'f0': ({'f0': ('i', 0)}, 0), - 'f1': ('i', 5)}) - - def test_intra_padding(self): - # Natively aligned sub-arrays may require some internal padding - align = np.dtype('i').alignment - def VV(n): - return 'V%d' % (align*(1 + (n-1)//align)) - - self._check('(3)T{ix}', ({'f0': ('i', 0), '': (VV(1), 4)}, (3,))) - - class TestNewBufferProtocol(object): - def _check_roundtrip(self, obj): - obj = np.asarray(obj) - x = memoryview(obj) - y = np.asarray(x) - y2 = np.array(x) - assert_(not y.flags.owndata) - assert_(y2.flags.owndata) - assert_equal(y.dtype, obj.dtype) - assert_array_equal(obj, y) - assert_equal(y2.dtype, obj.dtype) - assert_array_equal(obj, y2) - - def test_roundtrip(self): - x = np.array([1,2,3,4,5], dtype='i4') - self._check_roundtrip(x) - x = np.array([[1,2],[3,4]], dtype=np.float64) - self._check_roundtrip(x) +if sys.version_info[:2] == (2, 6): + from numpy.core.multiarray import memorysimpleview as memoryview - x = np.zeros((3,3,3), dtype=np.float32)[:,0,:] - self._check_roundtrip(x) +from numpy.core._internal import _dtype_from_pep3118 - dt = [('a', 'b'), - ('b', 'h'), - ('c', 'i'), - ('d', 'l'), - ('dx', 'q'), - ('e', 'B'), - ('f', 'H'), - ('g', 'I'), - ('h', 'L'), - ('hx', 'Q'), - ('i', np.single), - ('j', np.double), - ('k', np.longdouble), - ('ix', np.csingle), - ('jx', np.cdouble), - ('kx', np.clongdouble), - ('l', 'S4'), - ('m', 'U4'), - ('n', 'V3'), - ('o', '?'), - ('p', np.half), - ] - x = np.array( - [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - asbytes('aaaa'), 'bbbb', asbytes('xxx'), True, 1.0)], - dtype=dt) - self._check_roundtrip(x) +class TestPEP3118Dtype(object): + def _check(self, spec, wanted): + dt = np.dtype(wanted) + if isinstance(wanted, list) and isinstance(wanted[-1], tuple): + if wanted[-1][0] == '': + names = list(dt.names) + names[-1] = '' + dt.names = tuple(names) + assert_equal(_dtype_from_pep3118(spec), dt, + err_msg="spec %r != dtype %r" % (spec, wanted)) - x = np.array(([[1,2],[3,4]],), dtype=[('a', (int, (2,2)))]) - self._check_roundtrip(x) + def test_native_padding(self): + align = np.dtype('i').alignment + for j in range(8): + if j == 0: + s = 'bi' + else: + s = 'b%dxi' % j + self._check('@'+s, {'f0': ('i1', 0), + 'f1': ('i', align*(1 + j//align))}) + self._check('='+s, {'f0': ('i1', 0), + 'f1': ('i', 1+j)}) + + def test_native_padding_2(self): + # Native padding should work also for structs and sub-arrays + self._check('x3T{xi}', {'f0': (({'f0': ('i', 4)}, (3,)), 4)}) + self._check('^x3T{xi}', {'f0': (({'f0': ('i', 1)}, (3,)), 1)}) + + def test_trailing_padding(self): + # Trailing padding should be included, *and*, the item size + # should match the alignment if in aligned mode + align = np.dtype('i').alignment + def VV(n): + return 'V%d' % (align*(1 + (n-1)//align)) + + self._check('ix', [('f0', 'i'), ('', VV(1))]) + self._check('ixx', [('f0', 'i'), ('', VV(2))]) + self._check('ixxx', [('f0', 'i'), ('', VV(3))]) + self._check('ixxxx', [('f0', 'i'), ('', VV(4))]) + self._check('i7x', [('f0', 'i'), ('', VV(7))]) + + self._check('^ix', [('f0', 'i'), ('', 'V1')]) + self._check('^ixx', [('f0', 'i'), ('', 'V2')]) + self._check('^ixxx', [('f0', 'i'), ('', 'V3')]) + self._check('^ixxxx', [('f0', 'i'), ('', 'V4')]) + self._check('^i7x', [('f0', 'i'), ('', 'V7')]) + + def test_native_padding_3(self): + dt = np.dtype( + [('a', 'b'), ('b', 'i'), + ('sub', np.dtype('b,i')), ('c', 'i')], + align=True) + self._check("T{b:a:xxxi:b:T{b:f0:=i:f1:}:sub:xxxi:c:}", dt) + + dt = np.dtype( + [('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'), + ('e', 'b'), ('sub', np.dtype('b,i', align=True))]) + self._check("T{b:a:=i:b:b:c:b:d:b:e:T{b:f0:xxxi:f1:}:sub:}", dt) + + def test_padding_with_array_inside_struct(self): + dt = np.dtype( + [('a', 'b'), ('b', 'i'), ('c', 'b', (3,)), + ('d', 'i')], + align=True) + self._check("T{b:a:xxxi:b:3b:c:xi:d:}", dt) + + def test_byteorder_inside_struct(self): + # The byte order after @T{=i} should be '=', not '@'. + # Check this by noting the absence of native alignment. + self._check('@T{^i}xi', {'f0': ({'f0': ('i', 0)}, 0), + 'f1': ('i', 5)}) + + def test_intra_padding(self): + # Natively aligned sub-arrays may require some internal padding + align = np.dtype('i').alignment + def VV(n): + return 'V%d' % (align*(1 + (n-1)//align)) + + self._check('(3)T{ix}', ({'f0': ('i', 0), '': (VV(1), 4)}, (3,))) + +class TestNewBufferProtocol(object): + def _check_roundtrip(self, obj): + obj = np.asarray(obj) + x = memoryview(obj) + y = np.asarray(x) + y2 = np.array(x) + assert_(not y.flags.owndata) + assert_(y2.flags.owndata) + assert_equal(y.dtype, obj.dtype) + assert_array_equal(obj, y) + assert_equal(y2.dtype, obj.dtype) + assert_array_equal(obj, y2) - x = np.array([1,2,3], dtype='>i2') + def test_roundtrip(self): + x = np.array([1,2,3,4,5], dtype='i4') + self._check_roundtrip(x) + + x = np.array([[1,2],[3,4]], dtype=np.float64) + self._check_roundtrip(x) + + x = np.zeros((3,3,3), dtype=np.float32)[:,0,:] + self._check_roundtrip(x) + + dt = [('a', 'b'), + ('b', 'h'), + ('c', 'i'), + ('d', 'l'), + ('dx', 'q'), + ('e', 'B'), + ('f', 'H'), + ('g', 'I'), + ('h', 'L'), + ('hx', 'Q'), + ('i', np.single), + ('j', np.double), + ('k', np.longdouble), + ('ix', np.csingle), + ('jx', np.cdouble), + ('kx', np.clongdouble), + ('l', 'S4'), + ('m', 'U4'), + ('n', 'V3'), + ('o', '?'), + ('p', np.half), + ] + x = np.array( + [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + asbytes('aaaa'), 'bbbb', asbytes('xxx'), True, 1.0)], + dtype=dt) + self._check_roundtrip(x) + + x = np.array(([[1,2],[3,4]],), dtype=[('a', (int, (2,2)))]) + self._check_roundtrip(x) + + x = np.array([1,2,3], dtype='>i2') + self._check_roundtrip(x) + + x = np.array([1,2,3], dtype='<i2') + self._check_roundtrip(x) + + x = np.array([1,2,3], dtype='>i4') + self._check_roundtrip(x) + + x = np.array([1,2,3], dtype='<i4') + self._check_roundtrip(x) + + # Native-only data types can be passed through the buffer interface + # only in native byte order + if sys.byteorder == 'little': + x = np.array([1,2,3], dtype='>q') + assert_raises(ValueError, self._check_roundtrip, x) + x = np.array([1,2,3], dtype='<q') self._check_roundtrip(x) - - x = np.array([1,2,3], dtype='<i2') + else: + x = np.array([1,2,3], dtype='>q') self._check_roundtrip(x) + x = np.array([1,2,3], dtype='<q') + assert_raises(ValueError, self._check_roundtrip, x) + + def test_roundtrip_half(self): + half_list = [ + 1.0, + -2.0, + 6.5504 * 10**4, # (max half precision) + 2**-14, # ~= 6.10352 * 10**-5 (minimum positive normal) + 2**-24, # ~= 5.96046 * 10**-8 (minimum strictly positive subnormal) + 0.0, + -0.0, + float('+inf'), + float('-inf'), + 0.333251953125, # ~= 1/3 + ] - x = np.array([1,2,3], dtype='>i4') - self._check_roundtrip(x) + x = np.array(half_list, dtype='>e') + self._check_roundtrip(x) + x = np.array(half_list, dtype='<e') + self._check_roundtrip(x) + + def test_export_simple_1d(self): + x = np.array([1,2,3,4,5], dtype='i') + y = memoryview(x) + assert_equal(y.format, 'i') + assert_equal(y.shape, (5,)) + assert_equal(y.ndim, 1) + assert_equal(y.strides, (4,)) + assert_equal(y.suboffsets, EMPTY) + assert_equal(y.itemsize, 4) + + def test_export_simple_nd(self): + x = np.array([[1,2],[3,4]], dtype=np.float64) + y = memoryview(x) + assert_equal(y.format, 'd') + assert_equal(y.shape, (2, 2)) + assert_equal(y.ndim, 2) + assert_equal(y.strides, (16, 8)) + assert_equal(y.suboffsets, EMPTY) + assert_equal(y.itemsize, 8) + + def test_export_discontiguous(self): + x = np.zeros((3,3,3), dtype=np.float32)[:,0,:] + y = memoryview(x) + assert_equal(y.format, 'f') + assert_equal(y.shape, (3, 3)) + assert_equal(y.ndim, 2) + assert_equal(y.strides, (36, 4)) + assert_equal(y.suboffsets, EMPTY) + assert_equal(y.itemsize, 4) + + def test_export_record(self): + dt = [('a', 'b'), + ('b', 'h'), + ('c', 'i'), + ('d', 'l'), + ('dx', 'q'), + ('e', 'B'), + ('f', 'H'), + ('g', 'I'), + ('h', 'L'), + ('hx', 'Q'), + ('i', np.single), + ('j', np.double), + ('k', np.longdouble), + ('ix', np.csingle), + ('jx', np.cdouble), + ('kx', np.clongdouble), + ('l', 'S4'), + ('m', 'U4'), + ('n', 'V3'), + ('o', '?'), + ('p', np.half), + ] + x = np.array( + [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + asbytes('aaaa'), 'bbbb', asbytes(' '), True, 1.0)], + dtype=dt) + y = memoryview(x) + assert_equal(y.shape, (1,)) + assert_equal(y.ndim, 1) + assert_equal(y.suboffsets, EMPTY) + + sz = sum([dtype(b).itemsize for a, b in dt]) + if dtype('l').itemsize == 4: + assert_equal(y.format, 'T{b:a:=h:b:i:c:l:d:^q:dx:B:e:@H:f:=I:g:L:h:^Q:hx:=f:i:d:j:^g:k:=Zf:ix:Zd:jx:^Zg:kx:4s:l:=4w:m:3x:n:?:o:@e:p:}') + else: + assert_equal(y.format, 'T{b:a:=h:b:i:c:q:d:^q:dx:B:e:@H:f:=I:g:Q:h:^Q:hx:=f:i:d:j:^g:k:=Zf:ix:Zd:jx:^Zg:kx:4s:l:=4w:m:3x:n:?:o:@e:p:}') + # Cannot test if NPY_RELAXED_STRIDES_CHECKING changes the strides + if not (np.ones(1).strides[0] == np.iinfo(np.intp).max): + assert_equal(y.strides, (sz,)) + assert_equal(y.itemsize, sz) + + def test_export_subarray(self): + x = np.array(([[1,2],[3,4]],), dtype=[('a', ('i', (2,2)))]) + y = memoryview(x) + assert_equal(y.format, 'T{(2,2)i:a:}') + assert_equal(y.shape, EMPTY) + assert_equal(y.ndim, 0) + assert_equal(y.strides, EMPTY) + assert_equal(y.suboffsets, EMPTY) + assert_equal(y.itemsize, 16) + + def test_export_endian(self): + x = np.array([1,2,3], dtype='>i') + y = memoryview(x) + if sys.byteorder == 'little': + assert_equal(y.format, '>i') + else: + assert_equal(y.format, 'i') - x = np.array([1,2,3], dtype='<i4') - self._check_roundtrip(x) + x = np.array([1,2,3], dtype='<i') + y = memoryview(x) + if sys.byteorder == 'little': + assert_equal(y.format, 'i') + else: + assert_equal(y.format, '<i') - # Native-only data types can be passed through the buffer interface - # only in native byte order - if sys.byteorder == 'little': - x = np.array([1,2,3], dtype='>q') - assert_raises(ValueError, self._check_roundtrip, x) - x = np.array([1,2,3], dtype='<q') - self._check_roundtrip(x) - else: - x = np.array([1,2,3], dtype='>q') - self._check_roundtrip(x) - x = np.array([1,2,3], dtype='<q') - assert_raises(ValueError, self._check_roundtrip, x) - - def test_roundtrip_half(self): - half_list = [ - 1.0, - -2.0, - 6.5504 * 10**4, # (max half precision) - 2**-14, # ~= 6.10352 * 10**-5 (minimum positive normal) - 2**-24, # ~= 5.96046 * 10**-8 (minimum strictly positive subnormal) - 0.0, - -0.0, - float('+inf'), - float('-inf'), - 0.333251953125, # ~= 1/3 - ] - - x = np.array(half_list, dtype='>e') - self._check_roundtrip(x) - x = np.array(half_list, dtype='<e') + def test_padding(self): + for j in range(8): + x = np.array([(1,),(2,)], dtype={'f0': (int, j)}) self._check_roundtrip(x) - def test_export_simple_1d(self): - x = np.array([1,2,3,4,5], dtype='i') - y = memoryview(x) - assert_equal(y.format, 'i') - assert_equal(y.shape, (5,)) - assert_equal(y.ndim, 1) - assert_equal(y.strides, (4,)) - assert_equal(y.suboffsets, EMPTY) - assert_equal(y.itemsize, 4) - - def test_export_simple_nd(self): - x = np.array([[1,2],[3,4]], dtype=np.float64) - y = memoryview(x) - assert_equal(y.format, 'd') - assert_equal(y.shape, (2, 2)) - assert_equal(y.ndim, 2) - assert_equal(y.strides, (16, 8)) - assert_equal(y.suboffsets, EMPTY) - assert_equal(y.itemsize, 8) - - def test_export_discontiguous(self): - x = np.zeros((3,3,3), dtype=np.float32)[:,0,:] - y = memoryview(x) - assert_equal(y.format, 'f') - assert_equal(y.shape, (3, 3)) - assert_equal(y.ndim, 2) - assert_equal(y.strides, (36, 4)) - assert_equal(y.suboffsets, EMPTY) - assert_equal(y.itemsize, 4) - - def test_export_record(self): - dt = [('a', 'b'), - ('b', 'h'), - ('c', 'i'), - ('d', 'l'), - ('dx', 'q'), - ('e', 'B'), - ('f', 'H'), - ('g', 'I'), - ('h', 'L'), - ('hx', 'Q'), - ('i', np.single), - ('j', np.double), - ('k', np.longdouble), - ('ix', np.csingle), - ('jx', np.cdouble), - ('kx', np.clongdouble), - ('l', 'S4'), - ('m', 'U4'), - ('n', 'V3'), - ('o', '?'), - ('p', np.half), - ] - x = np.array( - [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - asbytes('aaaa'), 'bbbb', asbytes(' '), True, 1.0)], - dtype=dt) - y = memoryview(x) - assert_equal(y.shape, (1,)) - assert_equal(y.ndim, 1) - assert_equal(y.suboffsets, EMPTY) - - sz = sum([dtype(b).itemsize for a, b in dt]) - if dtype('l').itemsize == 4: - assert_equal(y.format, 'T{b:a:=h:b:i:c:l:d:^q:dx:B:e:@H:f:=I:g:L:h:^Q:hx:=f:i:d:j:^g:k:=Zf:ix:Zd:jx:^Zg:kx:4s:l:=4w:m:3x:n:?:o:@e:p:}') - else: - assert_equal(y.format, 'T{b:a:=h:b:i:c:q:d:^q:dx:B:e:@H:f:=I:g:Q:h:^Q:hx:=f:i:d:j:^g:k:=Zf:ix:Zd:jx:^Zg:kx:4s:l:=4w:m:3x:n:?:o:@e:p:}') - # Cannot test if NPY_RELAXED_STRIDES_CHECKING changes the strides - if not (np.ones(1).strides[0] == np.iinfo(np.intp).max): - assert_equal(y.strides, (sz,)) - assert_equal(y.itemsize, sz) - - def test_export_subarray(self): - x = np.array(([[1,2],[3,4]],), dtype=[('a', ('i', (2,2)))]) - y = memoryview(x) - assert_equal(y.format, 'T{(2,2)i:a:}') - assert_equal(y.shape, EMPTY) - assert_equal(y.ndim, 0) - assert_equal(y.strides, EMPTY) - assert_equal(y.suboffsets, EMPTY) - assert_equal(y.itemsize, 16) - - def test_export_endian(self): - x = np.array([1,2,3], dtype='>i') - y = memoryview(x) - if sys.byteorder == 'little': - assert_equal(y.format, '>i') - else: - assert_equal(y.format, 'i') - - x = np.array([1,2,3], dtype='<i') - y = memoryview(x) - if sys.byteorder == 'little': - assert_equal(y.format, 'i') - else: - assert_equal(y.format, '<i') - - def test_padding(self): - for j in range(8): - x = np.array([(1,),(2,)], dtype={'f0': (int, j)}) - self._check_roundtrip(x) - - def test_reference_leak(self): - count_1 = sys.getrefcount(np.core._internal) - a = np.zeros(4) - b = memoryview(a) - c = np.asarray(b) - count_2 = sys.getrefcount(np.core._internal) - assert_equal(count_1, count_2) - - def test_padded_struct_array(self): - dt1 = np.dtype( - [('a', 'b'), ('b', 'i'), ('sub', np.dtype('b,i')), ('c', 'i')], - align=True) - x1 = np.arange(dt1.itemsize, dtype=np.int8).view(dt1) - self._check_roundtrip(x1) - - dt2 = np.dtype( - [('a', 'b'), ('b', 'i'), ('c', 'b', (3,)), ('d', 'i')], - align=True) - x2 = np.arange(dt2.itemsize, dtype=np.int8).view(dt2) - self._check_roundtrip(x2) - - dt3 = np.dtype( - [('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'), - ('e', 'b'), ('sub', np.dtype('b,i', align=True))]) - x3 = np.arange(dt3.itemsize, dtype=np.int8).view(dt3) - self._check_roundtrip(x3) - - - class TestArrayAttributeDeletion(object): - - def test_multiarray_writable_attributes_deletion(self): - """ticket #2046, should not seqfault, raise AttributeError""" - a = np.ones(2) - attr = ['shape', 'strides', 'data', 'dtype', 'real', 'imag', 'flat'] - for s in attr: - assert_raises(AttributeError, delattr, a, s) - - - def test_multiarray_not_writable_attributes_deletion(self): - a = np.ones(2) - attr = ["ndim", "flags", "itemsize", "size", "nbytes", "base", - "ctypes", "T", "__array_interface__", "__array_struct__", - "__array_priority__", "__array_finalize__"] - for s in attr: - assert_raises(AttributeError, delattr, a, s) - - - def test_multiarray_flags_writable_attribute_deletion(self): - a = np.ones(2).flags - attr = ['updateifcopy', 'aligned', 'writeable'] - for s in attr: - assert_raises(AttributeError, delattr, a, s) - - - def test_multiarray_flags_not_writable_attribute_deletion(self): - a = np.ones(2).flags - attr = ["contiguous", "c_contiguous", "f_contiguous", "fortran", - "owndata", "fnc", "forc", "behaved", "carray", "farray", - "num"] - for s in attr: - assert_raises(AttributeError, delattr, a, s) + def test_reference_leak(self): + count_1 = sys.getrefcount(np.core._internal) + a = np.zeros(4) + b = memoryview(a) + c = np.asarray(b) + count_2 = sys.getrefcount(np.core._internal) + assert_equal(count_1, count_2) + + def test_padded_struct_array(self): + dt1 = np.dtype( + [('a', 'b'), ('b', 'i'), ('sub', np.dtype('b,i')), ('c', 'i')], + align=True) + x1 = np.arange(dt1.itemsize, dtype=np.int8).view(dt1) + self._check_roundtrip(x1) + + dt2 = np.dtype( + [('a', 'b'), ('b', 'i'), ('c', 'b', (3,)), ('d', 'i')], + align=True) + x2 = np.arange(dt2.itemsize, dtype=np.int8).view(dt2) + self._check_roundtrip(x2) + + dt3 = np.dtype( + [('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'), + ('e', 'b'), ('sub', np.dtype('b,i', align=True))]) + x3 = np.arange(dt3.itemsize, dtype=np.int8).view(dt3) + self._check_roundtrip(x3) + + +class TestArrayAttributeDeletion(object): + + def test_multiarray_writable_attributes_deletion(self): + """ticket #2046, should not seqfault, raise AttributeError""" + a = np.ones(2) + attr = ['shape', 'strides', 'data', 'dtype', 'real', 'imag', 'flat'] + for s in attr: + assert_raises(AttributeError, delattr, a, s) + + + def test_multiarray_not_writable_attributes_deletion(self): + a = np.ones(2) + attr = ["ndim", "flags", "itemsize", "size", "nbytes", "base", + "ctypes", "T", "__array_interface__", "__array_struct__", + "__array_priority__", "__array_finalize__"] + for s in attr: + assert_raises(AttributeError, delattr, a, s) + + + def test_multiarray_flags_writable_attribute_deletion(self): + a = np.ones(2).flags + attr = ['updateifcopy', 'aligned', 'writeable'] + for s in attr: + assert_raises(AttributeError, delattr, a, s) + + + def test_multiarray_flags_not_writable_attribute_deletion(self): + a = np.ones(2).flags + attr = ["contiguous", "c_contiguous", "f_contiguous", "fortran", + "owndata", "fnc", "forc", "behaved", "carray", "farray", + "num"] + for s in attr: + assert_raises(AttributeError, delattr, a, s) def test_array_interface(): # Test scalar coercion within the array interface diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index 31e5a5f10..76d85618f 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -4,7 +4,7 @@ import sys, warnings import numpy as np from numpy import array, arange, nditer, all -from numpy.compat import asbytes +from numpy.compat import asbytes, sixu from numpy.testing import * @@ -2008,7 +2008,7 @@ def test_iter_buffering_string(): assert_raises(TypeError,nditer,a,['buffered'],['readonly'], op_dtypes='U2') i = nditer(a, ['buffered'], ['readonly'], op_dtypes='U6') - assert_equal(i[0], u'abc') + assert_equal(i[0], sixu('abc')) assert_equal(i[0].dtype, np.dtype('U6')) def test_iter_buffering_growinner(): diff --git a/numpy/core/tests/test_print.py b/numpy/core/tests/test_print.py index c31771ad7..342ca27c6 100644 --- a/numpy/core/tests/test_print.py +++ b/numpy/core/tests/test_print.py @@ -24,11 +24,7 @@ def check_float_type(tp): assert_equal(str(tp(1e10)), str(float('1e10')), err_msg='Failed str formatting for type %s' % tp) else: - if sys.platform == 'win32' and sys.version_info[0] <= 2 and \ - sys.version_info[1] <= 5: - ref = '1e+010' - else: - ref = '1e+10' + ref = '1e+10' assert_equal(str(tp(1e10)), ref, err_msg='Failed str formatting for type %s' % tp) @@ -72,11 +68,7 @@ def check_complex_type(tp): assert_equal(str(tp(1e10)), str(complex(1e10)), err_msg='Failed str formatting for type %s' % tp) else: - if sys.platform == 'win32' and sys.version_info[0] <= 2 and \ - sys.version_info[1] <= 5: - ref = '(1e+010+0j)' - else: - ref = '(1e+10+0j)' + ref = '(1e+10+0j)' assert_equal(str(tp(1e10)), ref, err_msg='Failed str formatting for type %s' % tp) @@ -93,44 +85,24 @@ def test_complex_types(): def test_complex_inf_nan(): """Check inf/nan formatting of complex types.""" - if sys.version_info[:2] >= (2, 6): - TESTS = { - complex(np.inf, 0): "(inf+0j)", - complex(0, np.inf): "inf*j", - complex(-np.inf, 0): "(-inf+0j)", - complex(0, -np.inf): "-inf*j", - complex(np.inf, 1): "(inf+1j)", - complex(1, np.inf): "(1+inf*j)", - complex(-np.inf, 1): "(-inf+1j)", - complex(1, -np.inf): "(1-inf*j)", - complex(np.nan, 0): "(nan+0j)", - complex(0, np.nan): "nan*j", - complex(-np.nan, 0): "(nan+0j)", - complex(0, -np.nan): "nan*j", - complex(np.nan, 1): "(nan+1j)", - complex(1, np.nan): "(1+nan*j)", - complex(-np.nan, 1): "(nan+1j)", - complex(1, -np.nan): "(1+nan*j)", - } - else: - TESTS = { - complex(np.inf, 0): "(inf+0j)", - complex(0, np.inf): "infj", - complex(-np.inf, 0): "(-inf+0j)", - complex(0, -np.inf): "-infj", - complex(np.inf, 1): "(inf+1j)", - complex(1, np.inf): "(1+infj)", - complex(-np.inf, 1): "(-inf+1j)", - complex(1, -np.inf): "(1-infj)", - complex(np.nan, 0): "(nan+0j)", - complex(0, np.nan): "nanj", - complex(-np.nan, 0): "(nan+0j)", - complex(0, -np.nan): "nanj", - complex(np.nan, 1): "(nan+1j)", - complex(1, np.nan): "(1+nanj)", - complex(-np.nan, 1): "(nan+1j)", - complex(1, -np.nan): "(1+nanj)", - } + TESTS = { + complex(np.inf, 0): "(inf+0j)", + complex(0, np.inf): "inf*j", + complex(-np.inf, 0): "(-inf+0j)", + complex(0, -np.inf): "-inf*j", + complex(np.inf, 1): "(inf+1j)", + complex(1, np.inf): "(1+inf*j)", + complex(-np.inf, 1): "(-inf+1j)", + complex(1, -np.inf): "(1-inf*j)", + complex(np.nan, 0): "(nan+0j)", + complex(0, np.nan): "nan*j", + complex(-np.nan, 0): "(nan+0j)", + complex(0, -np.nan): "nan*j", + complex(np.nan, 1): "(nan+1j)", + complex(1, np.nan): "(1+nan*j)", + complex(-np.nan, 1): "(nan+1j)", + complex(1, -np.nan): "(1+nan*j)", + } for tp in [np.complex64, np.cdouble, np.clongdouble]: for c, s in TESTS.items(): yield _check_complex_inf_nan, c, s, tp @@ -167,11 +139,7 @@ def check_float_type_print(tp): if tp(1e10).itemsize > 4: _test_redirected_print(float(1e10), tp) else: - if sys.platform == 'win32' and sys.version_info[0] <= 2 and \ - sys.version_info[1] <= 5: - ref = '1e+010' - else: - ref = '1e+10' + ref = '1e+10' _test_redirected_print(float(1e10), tp, ref) def check_complex_type_print(tp): @@ -183,11 +151,7 @@ def check_complex_type_print(tp): if tp(1e10).itemsize > 8: _test_redirected_print(complex(1e10), tp) else: - if sys.platform == 'win32' and sys.version_info[0] <= 2 and \ - sys.version_info[1] <= 5: - ref = '(1e+010+0j)' - else: - ref = '(1e+10+0j)' + ref = '(1e+10+0j)' _test_redirected_print(complex(1e10), tp, ref) _test_redirected_print(complex(np.inf, 1), tp, '(inf+1j)') @@ -204,7 +168,6 @@ def test_complex_type_print(): for t in [np.complex64, np.cdouble, np.clongdouble] : yield check_complex_type_print, t -@dec.skipif(sys.version_info[:2] < (2, 6)) def test_scalar_format(): """Test the str.format method with NumPy scalar types""" tests = [('{0}', True, np.bool_), diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index d22868999..cb8415ee7 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -17,7 +17,7 @@ from numpy.testing import ( assert_raises, assert_warns, dec ) from numpy.testing.utils import _assert_valid_refcount, WarningManager -from numpy.compat import asbytes, asunicode, asbytes_nested, long +from numpy.compat import asbytes, asunicode, asbytes_nested, long, sixu rlevel = 1 @@ -138,7 +138,7 @@ class TestRegression(TestCase): def test_unicode_swapping(self,level=rlevel): """Ticket #79""" ulen = 1 - ucs_value = u'\U0010FFFF' + ucs_value = sixu('\U0010FFFF') ua = np.array([[[ucs_value*ulen]*2]*3]*4, dtype='U%s' % ulen) ua2 = ua.newbyteorder() @@ -1107,7 +1107,7 @@ class TestRegression(TestCase): for i in range(1,9) : msg = 'unicode offset: %d chars'%i t = np.dtype([('a','S%d'%i),('b','U2')]) - x = np.array([(asbytes('a'),u'b')], dtype=t) + x = np.array([(asbytes('a'),sixu('b'))], dtype=t) if sys.version_info[0] >= 3: assert_equal(str(x), "[(b'a', 'b')]", err_msg=msg) else: @@ -1314,21 +1314,24 @@ class TestRegression(TestCase): def test_unicode_to_string_cast(self): """Ticket #1240.""" - a = np.array([[u'abc', u'\u03a3'], [u'asdf', u'erw']], dtype='U') + a = np.array( + [ [sixu('abc'), sixu('\u03a3')], + [sixu('asdf'), sixu('erw')] + ], dtype='U') def fail(): b = np.array(a, 'S4') self.assertRaises(UnicodeEncodeError, fail) def test_mixed_string_unicode_array_creation(self): - a = np.array(['1234', u'123']) + a = np.array(['1234', sixu('123')]) assert_(a.itemsize == 16) - a = np.array([u'123', '1234']) + a = np.array([sixu('123'), '1234']) assert_(a.itemsize == 16) - a = np.array(['1234', u'123', '12345']) + a = np.array(['1234', sixu('123'), '12345']) assert_(a.itemsize == 20) - a = np.array([u'123', '1234', u'12345']) + a = np.array([sixu('123'), '1234', sixu('12345')]) assert_(a.itemsize == 20) - a = np.array([u'123', '1234', u'1234']) + a = np.array([sixu('123'), '1234', sixu('1234')]) assert_(a.itemsize == 16) def test_misaligned_objects_segfault(self): @@ -1844,7 +1847,7 @@ class TestRegression(TestCase): if sys.version_info[0] >= 3: a = np.array(['abcd']) else: - a = np.array([u'abcd']) + a = np.array([sixu('abcd')]) assert_equal(a.dtype.itemsize, 16) def test_unique_stable(self): diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 173707351..ea92ce7de 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -447,9 +447,6 @@ class TestLdexp(TestCase): self._check_ldexp('i') self._check_ldexp('l') - @dec.knownfailureif(sys.platform == 'win32' and sys.version_info < (2, 6), - "python.org < 2.6 binaries have broken ldexp in the " - "C runtime") def test_ldexp_overflow(self): # silence warning emitted on overflow err = np.seterr(over="ignore") @@ -916,12 +913,6 @@ class TestComplexFunctions(object): def test_against_cmath(self): import cmath, sys - # cmath.asinh is broken in some versions of Python, see - # http://bugs.python.org/issue1381 - broken_cmath_asinh = False - if sys.version_info < (2,6): - broken_cmath_asinh = True - points = [-1-1j, -1+1j, +1-1j, +1+1j] name_map = {'arcsin': 'asin', 'arccos': 'acos', 'arctan': 'atan', 'arcsinh': 'asinh', 'arccosh': 'acosh', 'arctanh': 'atanh'} @@ -936,10 +927,6 @@ class TestComplexFunctions(object): for p in points: a = complex(func(np.complex_(p))) b = cfunc(p) - - if cname == 'asinh' and broken_cmath_asinh: - continue - assert_(abs(a - b) < atol, "%s %s: %s; cmath: %s"%(fname,p,a,b)) def check_loss_of_precision(self, dtype): diff --git a/numpy/core/tests/test_unicode.py b/numpy/core/tests/test_unicode.py index c1ea4b06e..b29fe2010 100644 --- a/numpy/core/tests/test_unicode.py +++ b/numpy/core/tests/test_unicode.py @@ -4,7 +4,7 @@ import sys from numpy.testing import * from numpy.core import * -from numpy.compat import asbytes +from numpy.compat import asbytes, sixu # Guess the UCS length for this python interpreter if sys.version_info[:2] >= (3, 3): @@ -31,7 +31,7 @@ elif sys.version_info[0] >= 3: else: return prod(v.shape) * v.itemsize else: - if len(buffer(u'u')) == 4: + if len(buffer(sixu('u'))) == 4: ucs4 = True else: ucs4 = False @@ -43,9 +43,9 @@ else: # In both cases below we need to make sure that the byte swapped value (as # UCS4) is still a valid unicode: # Value that can be represented in UCS2 interpreters -ucs2_value = u'\u0900' +ucs2_value = sixu('\u0900') # Value that cannot be represented in UCS2 interpreters (but can in UCS4) -ucs4_value = u'\U00100900' +ucs4_value = sixu('\U00100900') ############################################################ @@ -62,7 +62,7 @@ class create_zeros(object): # Check the length of the data buffer self.assertTrue(buffer_length(ua) == nbytes) # Small check that data in array element is ok - self.assertTrue(ua_scalar == u'') + self.assertTrue(ua_scalar == sixu('')) # Encode to ascii and double check self.assertTrue(ua_scalar.encode('ascii') == asbytes('')) # Check buffer lengths for scalars diff --git a/numpy/f2py/auxfuncs.py b/numpy/f2py/auxfuncs.py index b7d32976a..e391430f5 100644 --- a/numpy/f2py/auxfuncs.py +++ b/numpy/f2py/auxfuncs.py @@ -16,17 +16,16 @@ Pearu Peterson """ from __future__ import division, absolute_import, print_function -__version__ = "$Revision: 1.65 $"[10:-1] - -from . import __version__ -f2py_version = __version__.version - import pprint import sys import types from functools import reduce + +from . import __version__ from . import cfuncs +f2py_version = __version__.version + errmess=sys.stderr.write #outmess=sys.stdout.write @@ -599,7 +598,7 @@ def gentitle(name): return '/*%s %s %s*/'%(l*'*',name,l*'*') def flatlist(l): - if type(l)==types.ListType: + if type(l)==list: return reduce(lambda x,y,f=flatlist:x+f(y),l,[]) return [l] @@ -608,9 +607,9 @@ def stripcomma(s): return s def replace(str,d,defaultsep=''): - if type(d)==types.ListType: + if type(d)==list: return [replace(str, _m, defaultsep) for _m in d] - if type(str)==types.ListType: + if type(str)==list: return [replace(_m, d, defaultsep) for _m in str] for k in 2*list(d.keys()): if k=='separatorsfor': @@ -619,14 +618,14 @@ def replace(str,d,defaultsep=''): sep=d['separatorsfor'][k] else: sep=defaultsep - if type(d[k])==types.ListType: + if type(d[k])==list: str=str.replace('#%s#'%(k),sep.join(flatlist(d[k]))) else: str=str.replace('#%s#'%(k),d[k]) return str def dictappend(rd,ar): - if type(ar)==types.ListType: + if type(ar)==list: for a in ar: rd=dictappend(rd,a) return rd @@ -636,13 +635,13 @@ def dictappend(rd,ar): if k in rd: if type(rd[k])==str: rd[k]=[rd[k]] - if type(rd[k])==types.ListType: - if type(ar[k])==types.ListType: + if type(rd[k])==list: + if type(ar[k])==list: rd[k]=rd[k]+ar[k] else: rd[k].append(ar[k]) - elif type(rd[k])==types.DictType: - if type(ar[k])==types.DictType: + elif type(rd[k])==dict: + if type(ar[k])==dict: if k=='separatorsfor': for k1 in ar[k].keys(): if k1 not in rd[k]: @@ -655,7 +654,7 @@ def dictappend(rd,ar): def applyrules(rules,d,var={}): ret={} - if type(rules)==types.ListType: + if type(rules)==list: for r in rules: rr=applyrules(r,d,var) ret=dictappend(ret,rr) @@ -674,7 +673,7 @@ def applyrules(rules,d,var={}): ret[k]=rules[k]; continue if type(rules[k])==str: ret[k]=replace(rules[k],d) - elif type(rules[k])==types.ListType: + elif type(rules[k])==list: ret[k]=[] for i in rules[k]: ar=applyrules({k:i},d,var) @@ -682,13 +681,13 @@ def applyrules(rules,d,var={}): ret[k].append(ar[k]) elif k[0]=='_': continue - elif type(rules[k])==types.DictType: + elif type(rules[k])==dict: ret[k]=[] for k1 in rules[k].keys(): if type(k1)==types.FunctionType and k1(var): - if type(rules[k][k1])==types.ListType: + if type(rules[k][k1])==list: for i in rules[k][k1]: - if type(i)==types.DictType: + if type(i)==dict: res=applyrules({'supertext':i},d,var) if 'supertext' in res: i=res['supertext'] @@ -696,7 +695,7 @@ def applyrules(rules,d,var={}): ret[k].append(replace(i,d)) else: i=rules[k][k1] - if type(i)==types.DictType: + if type(i)==dict: res=applyrules({'supertext':i},d) if 'supertext' in res: i=res['supertext'] @@ -704,7 +703,7 @@ def applyrules(rules,d,var={}): ret[k].append(replace(i,d)) else: errmess('applyrules: ignoring rule %s.\n'%repr(rules[k])) - if type(ret[k])==types.ListType: + if type(ret[k])==list: if len(ret[k])==1: ret[k]=ret[k][0] if ret[k]==[]: diff --git a/numpy/f2py/cb_rules.py b/numpy/f2py/cb_rules.py index eb8cc6fc6..85e679060 100644 --- a/numpy/f2py/cb_rules.py +++ b/numpy/f2py/cb_rules.py @@ -15,21 +15,19 @@ Pearu Peterson """ from __future__ import division, absolute_import, print_function -__version__ = "$Revision: 1.53 $"[10:-1] +import pprint +import sys from . import __version__ -f2py_version = __version__.version +from .auxfuncs import * +from . import cfuncs +f2py_version = __version__.version -import pprint -import sys -import types errmess=sys.stderr.write outmess=sys.stdout.write show=pprint.pprint -from .auxfuncs import * -from . import cfuncs ################## Rules for callback function ############## @@ -484,7 +482,7 @@ def buildcallback(rout,um): , #endif """] - if type(rd['docreturn'])==types.ListType: + if type(rd['docreturn'])==list: rd['docreturn']=stripcomma(replace('#docreturn#',{'docreturn':rd['docreturn']})) optargs=stripcomma(replace('#docsignopt#', {'docsignopt':rd['docsignopt']} @@ -501,10 +499,10 @@ def buildcallback(rout,um): rd['docstrsigns']=[] rd['latexdocstrsigns']=[] for k in ['docstrreq','docstropt','docstrout','docstrcbs']: - if k in rd and type(rd[k])==types.ListType: + if k in rd and type(rd[k])==list: rd['docstrsigns']=rd['docstrsigns']+rd[k] k='latex'+k - if k in rd and type(rd[k])==types.ListType: + if k in rd and type(rd[k])==list: rd['latexdocstrsigns']=rd['latexdocstrsigns']+rd[k][0:1]+\ ['\\begin{description}']+rd[k][1:]+\ ['\\end{description}'] diff --git a/numpy/f2py/cfuncs.py b/numpy/f2py/cfuncs.py index 4e61b0634..a16a07d59 100644 --- a/numpy/f2py/cfuncs.py +++ b/numpy/f2py/cfuncs.py @@ -16,15 +16,13 @@ Pearu Peterson """ from __future__ import division, absolute_import, print_function -__version__ = "$Revision: 1.75 $"[10:-1] +import sys +import copy from . import __version__ -f2py_version = __version__.version -import types -import sys -import copy -errmess=sys.stderr.write +f2py_version = __version__.version +errmess = sys.stderr.write ##################### Definitions ################## @@ -1130,7 +1128,7 @@ def buildcfuncs(): def append_needs(need,flag=1): global outneeds,needs - if type(need)==types.ListType: + if type(need)==list: for n in need: append_needs(n,flag) elif type(need)==str: @@ -1162,7 +1160,7 @@ def append_needs(need,flag=1): if need in needs: for nn in needs[need]: t=append_needs(nn,0) - if type(t)==types.DictType: + if type(t)==dict: for nnn in t.keys(): if nnn in tmp: tmp[nnn]=tmp[nnn]+t[nnn] @@ -1178,7 +1176,7 @@ def append_needs(need,flag=1): if need in needs: for nn in needs[need]: t=append_needs(nn,flag) - if type(t)==types.DictType: + if type(t)==dict: for nnn in t.keys(): if nnn in tmp: tmp[nnn]=t[nnn]+tmp[nnn] diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py index f60e2525c..c86a15407 100755 --- a/numpy/f2py/crackfortran.py +++ b/numpy/f2py/crackfortran.py @@ -140,12 +140,6 @@ TODO: """ from __future__ import division, absolute_import, print_function -__version__ = "$Revision: 1.177 $"[10:-1] -import platform -from . import __version__ -f2py_version = __version__.version - -# import sys import string import fileinput @@ -153,8 +147,13 @@ import re import pprint import os import copy +import platform + +from . import __version__ from .auxfuncs import * +f2py_version = __version__.version + # Global flags: strictf77=1 # Ignore `!' comments unless line[0]=='!' sourcecodeform='fix' # 'fix','free' @@ -1523,7 +1522,7 @@ def postcrack2(block,tab='',param_map=None): global f90modulevars if not f90modulevars: return block - if type(block)==types.ListType: + if type(block)==list: ret = [] for g in block: g = postcrack2(g,tab=tab+'\t',param_map=param_map) @@ -1560,7 +1559,7 @@ def postcrack(block,args=None,tab=''): determine expression types if in argument list """ global usermodules,onlyfunctions - if type(block)==types.ListType: + if type(block)==list: gret=[] uret=[] for g in block: @@ -1572,7 +1571,7 @@ def postcrack(block,args=None,tab=''): gret.append(g) return uret+gret setmesstext(block) - if (not type(block)==types.DictType) and 'block' not in block: + if (not type(block)==dict) and 'block' not in block: raise Exception('postcrack: Expected block dictionary instead of ' + \ str(block)) if 'name' in block and not block['name']=='unknown_interface': diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py index 80569ecc6..98f58e333 100755 --- a/numpy/f2py/f2py2e.py +++ b/numpy/f2py/f2py2e.py @@ -16,17 +16,10 @@ Pearu Peterson """ from __future__ import division, absolute_import, print_function -from . import __version__ -f2py_version = __version__.version - import sys import os import pprint -import types import re -errmess=sys.stderr.write -#outmess=sys.stdout.write -show=pprint.pprint from . import crackfortran from . import rules @@ -34,7 +27,12 @@ from . import cb_rules from . import auxfuncs from . import cfuncs from . import f90mod_rules +from . import __version__ +f2py_version = __version__.version +errmess = sys.stderr.write +#outmess=sys.stdout.write +show = pprint.pprint outmess = auxfuncs.outmess try: @@ -341,7 +339,7 @@ def dict_append(d_out,d_in): for (k,v) in d_in.items(): if k not in d_out: d_out[k] = [] - if type(v) is types.ListType: + if type(v) is list: d_out[k] = d_out[k] + v else: d_out[k].append(v) diff --git a/numpy/f2py/rules.py b/numpy/f2py/rules.py index 250caf55f..a76401ac9 100644 --- a/numpy/f2py/rules.py +++ b/numpy/f2py/rules.py @@ -60,11 +60,7 @@ f2py_version = __version__.version import pprint import sys import time -import types import copy -errmess=sys.stderr.write -outmess=sys.stdout.write -show=pprint.pprint from .auxfuncs import * from . import capi_maps @@ -74,8 +70,12 @@ from . import common_rules from . import use_rules from . import f90mod_rules from . import func2subr -options={} +errmess = sys.stderr.write +outmess = sys.stdout.write +show = pprint.pprint + +options={} sepdict={} #for k in ['need_cfuncs']: sepdict[k]=',' for k in ['decl', @@ -1388,9 +1388,9 @@ def buildapi(rout): vrd['check']=c ar=applyrules(check_rules,vrd,var[a]) rd=dictappend(rd,ar) - if type(rd['cleanupfrompyobj']) is types.ListType: + if type(rd['cleanupfrompyobj']) is list: rd['cleanupfrompyobj'].reverse() - if type(rd['closepyobjfrom']) is types.ListType: + if type(rd['closepyobjfrom']) is list: rd['closepyobjfrom'].reverse() rd['docsignature']=stripcomma(replace('#docsign##docsignopt##docsignxa#', {'docsign':rd['docsign'], @@ -1415,15 +1415,15 @@ def buildapi(rout): else: rd['callcompaqfortran']=cfs rd['callfortran']=cfs - if type(rd['docreturn'])==types.ListType: + if type(rd['docreturn'])==list: rd['docreturn']=stripcomma(replace('#docreturn#',{'docreturn':rd['docreturn']}))+' = ' rd['docstrsigns']=[] rd['latexdocstrsigns']=[] for k in ['docstrreq','docstropt','docstrout','docstrcbs']: - if k in rd and type(rd[k])==types.ListType: + if k in rd and type(rd[k])==list: rd['docstrsigns']=rd['docstrsigns']+rd[k] k='latex'+k - if k in rd and type(rd[k])==types.ListType: + if k in rd and type(rd[k])==list: rd['latexdocstrsigns']=rd['latexdocstrsigns']+rd[k][0:1]+\ ['\\begin{description}']+rd[k][1:]+\ ['\\end{description}'] diff --git a/numpy/fft/helper.py b/numpy/fft/helper.py index 56d03619f..0a475153f 100644 --- a/numpy/fft/helper.py +++ b/numpy/fft/helper.py @@ -4,14 +4,15 @@ Discrete Fourier Transforms - helper.py """ from __future__ import division, absolute_import, print_function +import numpy.core.numerictypes as nt +from numpy.core import ( + asarray, concatenate, arange, take, integer, empty + ) + # Created by Pearu Peterson, September 2002 __all__ = ['fftshift', 'ifftshift', 'fftfreq', 'rfftfreq'] -from numpy.core import asarray, concatenate, arange, take, \ - integer, empty -import numpy.core.numerictypes as nt -import types def fftshift(x, axes=None): """ @@ -156,7 +157,7 @@ def fftfreq(n, d=1.0): array([ 0. , 1.25, 2.5 , 3.75, -5. , -3.75, -2.5 , -1.25]) """ - if not (isinstance(n,types.IntType) or isinstance(n, integer)): + if not (isinstance(n,int) or isinstance(n, integer)): raise ValueError("n should be an integer") val = 1.0 / (n * d) results = empty(n, int) @@ -212,7 +213,7 @@ def rfftfreq(n, d=1.0): array([ 0., 10., 20., 30., 40., 50.]) """ - if not (isinstance(n,types.IntType) or isinstance(n, integer)): + if not (isinstance(n,int) or isinstance(n, integer)): raise ValueError("n should be an integer") val = 1.0/(n*d) N = n//2 + 1 diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py index 827adac02..aa39e25a1 100644 --- a/numpy/lib/_iotools.py +++ b/numpy/lib/_iotools.py @@ -11,7 +11,8 @@ import numpy.core.numeric as nx from numpy.compat import asbytes, bytes, asbytes_nested, long, basestring if sys.version_info[0] >= 3: - from builtins import bool, int, float, complex, object, unicode, str + from builtins import bool, int, float, complex, object, str + unicode = str else: from __builtin__ import bool, int, float, complex, object, unicode, str diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 2154acdce..c13c7e94a 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -14,20 +14,24 @@ from operator import itemgetter from ._datasource import DataSource from ._compiled_base import packbits, unpackbits -from ._iotools import LineSplitter, NameValidator, StringConverter, \ - ConverterError, ConverterLockError, ConversionWarning, \ - _is_string_like, has_nested_fields, flatten_dtype, \ - easy_dtype, _bytes_to_name +from ._iotools import ( + LineSplitter, NameValidator, StringConverter, + ConverterError, ConverterLockError, ConversionWarning, + _is_string_like, has_nested_fields, flatten_dtype, + easy_dtype, _bytes_to_name + ) + +from numpy.compat import ( + asbytes, asstr, asbytes_nested, bytes, basestring, unicode + ) -from numpy.compat import asbytes, asstr, asbytes_nested, bytes, basestring from io import BytesIO if sys.version_info[0] >= 3: import pickle - imap = map else: import cPickle as pickle - imap = itertools.imap + from future_builtins import map loads = pickle.loads @@ -125,8 +129,7 @@ class BagObj(object): def zipfile_factory(*args, **kwargs): import zipfile - if sys.version_info >= (2, 5): - kwargs['allowZip64'] = True + kwargs['allowZip64'] = True return zipfile.ZipFile(*args, **kwargs) class NpzFile(object): @@ -1609,7 +1612,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, converter.iterupgrade(current_column) except ConverterLockError: errmsg = "Converter #%i is locked and cannot be upgraded: " % i - current_column = imap(itemgetter(i), rows) + current_column = map(itemgetter(i), rows) for (j, value) in enumerate(current_column): try: converter.upgrade(value) diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index ffa5140bc..827e60d70 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -17,10 +17,8 @@ from numpy.ma.mrecords import MaskedRecords from numpy.lib._iotools import _is_string_like from numpy.compat import basestring -if sys.version_info[0] >= 3: - izip = zip -else: - izip = itertools.izip +if sys.version_info[0] < 3: + from future_builtins import zip _check_fill_value = np.ma.core._check_fill_value @@ -293,7 +291,7 @@ def izip_records(seqarrays, fill_value=None, flatten=True): zipfunc = _izip_fields # try: - for tup in izip(*iters): + for tup in zip(*iters): yield tuple(zipfunc(tup)) except IndexError: pass @@ -418,7 +416,7 @@ def merge_arrays(seqarrays, seqmask = [] # If we expect some kind of MaskedArray, make a special loop. if usemask: - for (a, n) in izip(seqarrays, sizes): + for (a, n) in zip(seqarrays, sizes): nbmissing = (maxlength - n) # Get the data and mask data = a.ravel().__array__() @@ -447,7 +445,7 @@ def merge_arrays(seqarrays, output = output.view(MaskedRecords) else: # Same as before, without the mask we don't need... - for (a, n) in izip(seqarrays, sizes): + for (a, n) in zip(seqarrays, sizes): nbmissing = (maxlength - n) data = a.ravel().__array__() if nbmissing: diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 3b70d1ff0..1e9bacdf5 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -1,9 +1,11 @@ from __future__ import division, absolute_import, print_function import sys + +import numpy as np from numpy.testing import * from numpy.testing.utils import _assert_valid_refcount -import numpy as np +from numpy.compat import unicode rlevel = 1 diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 75ba5bca6..2519cd4d4 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -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): """ diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 0846bb6e5..f14891087 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -8,7 +8,6 @@ from __future__ import division, absolute_import, print_function __author__ = "Pierre GF Gerard-Marchant" -import types import warnings import sys import pickle @@ -836,7 +835,7 @@ class TestMaskedArrayArithmetic(TestCase): if sys.version_info[0] >= 3: assert_(isinstance(count(ott), np.integer)) else: - assert_(isinstance(count(ott), types.IntType)) + assert_(isinstance(count(ott), int)) assert_equal(3, count(ott)) assert_(getmask(count(ott, 0)) is nomask) assert_equal([1, 2], count(ott, 0)) diff --git a/numpy/ma/tests/test_old_ma.py b/numpy/ma/tests/test_old_ma.py index f5c9e426e..208086f7b 100644 --- a/numpy/ma/tests/test_old_ma.py +++ b/numpy/ma/tests/test_old_ma.py @@ -1,7 +1,6 @@ from __future__ import division, absolute_import, print_function import sys -import types from functools import reduce import numpy @@ -166,7 +165,7 @@ class TestMa(TestCase): if sys.version_info[0] >= 3: self.assertTrue(isinstance(count(ott), numpy.integer)) else: - self.assertTrue(isinstance(count(ott), types.IntType)) + self.assertTrue(isinstance(count(ott), int)) self.assertEqual(3, count(ott)) self.assertEqual(1, count(1)) self.assertTrue (eq(0, array(1, mask=[1]))) @@ -175,7 +174,7 @@ class TestMa(TestCase): if sys.version_info[0] >= 3: assert_(isinstance(count(ott), numpy.integer)) else: - assert_(isinstance(count(ott), types.IntType)) + assert_(isinstance(count(ott), int)) self.assertTrue (eq(3, count(ott))) assert_(getmask(count(ott, 0)) is nomask) self.assertTrue (eq([1, 2], count(ott, 0))) diff --git a/numpy/ma/tests/test_regression.py b/numpy/ma/tests/test_regression.py index 917c53969..f713a8a1a 100644 --- a/numpy/ma/tests/test_regression.py +++ b/numpy/ma/tests/test_regression.py @@ -1,8 +1,9 @@ from __future__ import division, absolute_import, print_function -from numpy.testing import * import numpy as np import numpy.ma as ma +from numpy.testing import * +from numpy.compat import sixu rlevel = 1 @@ -38,7 +39,7 @@ class TestRegression(TestCase): def test_masked_array_repr_unicode(self): """Ticket #1256""" - repr(np.ma.array(u"Unicode")) + repr(np.ma.array(sixu("Unicode"))) def test_atleast_2d(self): """Ticket #1559""" diff --git a/numpy/oldnumeric/ma.py b/numpy/oldnumeric/ma.py index 3b861709e..fbc0aca27 100644 --- a/numpy/oldnumeric/ma.py +++ b/numpy/oldnumeric/ma.py @@ -18,10 +18,12 @@ from functools import reduce import numpy.core.umath as umath import numpy.core.fromnumeric as fromnumeric +import numpy.core.numeric as numeric from numpy.core.numeric import newaxis, ndarray, inf from numpy.core.fromnumeric import amax, amin from numpy.core.numerictypes import bool_, typecodes import numpy.core.numeric as numeric +from numpy.compat import bytes, long if sys.version_info[0] >= 3: _MAXINT = sys.maxsize @@ -93,13 +95,13 @@ default_object_fill_value = '?' def default_fill_value (obj): "Function to calculate default fill value for an object." - if isinstance(obj, types.FloatType): + if isinstance(obj, float): return default_real_fill_value - elif isinstance(obj, types.IntType) or isinstance(obj, types.LongType): + elif isinstance(obj, int) or isinstance(obj, long): return default_integer_fill_value - elif isinstance(obj, types.StringType): + elif isinstance(obj, bytes): return default_character_fill_value - elif isinstance(obj, types.ComplexType): + elif isinstance(obj, complex): return default_complex_fill_value elif isinstance(obj, MaskedArray) or isinstance(obj, ndarray): x = obj.dtype.char @@ -119,9 +121,9 @@ def default_fill_value (obj): def minimum_fill_value (obj): "Function to calculate default fill value suitable for taking minima." - if isinstance(obj, types.FloatType): + if isinstance(obj, float): return numeric.inf - elif isinstance(obj, types.IntType) or isinstance(obj, types.LongType): + elif isinstance(obj, int) or isinstance(obj, long): return _MAXINT elif isinstance(obj, MaskedArray) or isinstance(obj, ndarray): x = obj.dtype.char @@ -136,9 +138,9 @@ def minimum_fill_value (obj): def maximum_fill_value (obj): "Function to calculate default fill value suitable for taking maxima." - if isinstance(obj, types.FloatType): + if isinstance(obj, float): return -inf - elif isinstance(obj, types.IntType) or isinstance(obj, types.LongType): + elif isinstance(obj, int) or isinstance(obj, long): return -_MAXINT elif isinstance(obj, MaskedArray) or isinstance(obj, ndarray): x = obj.dtype.char @@ -239,7 +241,7 @@ def filled (a, value = None): return a.filled(value) elif isinstance(a, ndarray) and a.flags['CONTIGUOUS']: return a - elif isinstance(a, types.DictType): + elif isinstance(a, dict): return numeric.array(a, 'O') else: return numeric.array(a) @@ -1543,7 +1545,7 @@ def new_repeat(a, repeats, axis=None): telling how many times to repeat each element. """ af = filled(a) - if isinstance(repeats, types.IntType): + if isinstance(repeats, int): if axis is None: num = af.size else: @@ -2153,10 +2155,9 @@ def asarray(data, dtype=None): # Add methods to support ndarray interface # XXX: I is better to to change the masked_*_operation adaptors # XXX: to wrap ndarray methods directly to create ma.array methods. -from types import MethodType def _m(f): - return MethodType(f, None, array) + return types.MethodType(f, None, array) def not_implemented(*args, **kwds): raise NotImplementedError("not yet implemented for numpy.ma arrays") @@ -2280,7 +2281,7 @@ array.std = _m(_std) array.view = _m(not_implemented) array.round = _m(around) -del _m, MethodType, not_implemented +del _m, not_implemented masked = MaskedArray(0, int, mask=1) diff --git a/numpy/polynomial/polyutils.py b/numpy/polynomial/polyutils.py index c8121e633..63743bb40 100644 --- a/numpy/polynomial/polyutils.py +++ b/numpy/polynomial/polyutils.py @@ -70,16 +70,6 @@ class PolyBase(object) : pass # -# We need the any function for python < 2.5 -# -if sys.version_info[:2] < (2,5) : - def any(iterable) : - for element in iterable: - if element : - return True - return False - -# # Helper functions to convert inputs to 1-D arrays # def trimseq(seq) : diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 1b69d0ed2..40c569c0f 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -8,7 +8,6 @@ import os import sys import re import operator -import types import warnings from .nosetester import import_nose @@ -54,7 +53,7 @@ def gisnan(x): This should be removed once this problem is solved at the Ufunc level.""" from numpy.core import isnan st = isnan(x) - if isinstance(st, types.NotImplementedType): + if isinstance(st, type(NotImplemented)): raise TypeError("isnan not supported for this type") return st @@ -73,7 +72,7 @@ def gisfinite(x): err = seterr(invalid='ignore') try: st = isfinite(x) - if isinstance(st, types.NotImplementedType): + if isinstance(st, type(NotImplemented)): raise TypeError("isfinite not supported for this type") finally: seterr(**err) @@ -94,7 +93,7 @@ def gisinf(x): err = seterr(invalid='ignore') try: st = isinf(x) - if isinstance(st, types.NotImplementedType): + if isinstance(st, type(NotImplemented)): raise TypeError("isinf not supported for this type") finally: seterr(**err) @@ -154,34 +154,7 @@ def configuration(parent_package='',top_path=None): def setup_package(): - # Perform 2to3 if needed - local_path = os.path.dirname(os.path.abspath(sys.argv[0])) - src_path = local_path - - if sys.version_info[0] == 3: - src_path = os.path.join(local_path, 'build', 'py3k') - sys.path.insert(0, os.path.join(local_path, 'tools')) - import py3tool - print("Converting to Python3 via 2to3...") - py3tool.sync_2to3('numpy', os.path.join(src_path, 'numpy')) - - site_cfg = os.path.join(local_path, 'site.cfg') - if os.path.isfile(site_cfg): - shutil.copy(site_cfg, src_path) - - # Ugly hack to make pip work with Python 3, see #1857. - # Explanation: pip messes with __file__ which interacts badly with the - # change in directory due to the 2to3 conversion. Therefore we restore - # __file__ to what it would have been otherwise. - global __file__ - __file__ = os.path.join(os.curdir, os.path.basename(__file__)) - if '--egg-base' in sys.argv: - # Change pip-egg-info entry to absolute path, so pip can find it - # after changing directory. - idx = sys.argv.index('--egg-base') - if sys.argv[idx + 1] == 'pip-egg-info': - sys.argv[idx + 1] = os.path.join(local_path, 'pip-egg-info') - + src_path = os.path.dirname(os.path.abspath(sys.argv[0])) old_path = os.getcwd() os.chdir(src_path) sys.path.insert(0, src_path) diff --git a/tools/py3tool.py b/tools/py3tool.py deleted file mode 100755 index 072b593a7..000000000 --- a/tools/py3tool.py +++ /dev/null @@ -1,301 +0,0 @@ -#!/usr/bin/env python3 -# -*- python -*- -""" -%prog SUBMODULE... - -Hack to pipe submodules of Numpy through 2to3 and build them in-place -one-by-one. - -Example usage: - - python3 tools/py3tool.py testing distutils core - -This will copy files to _py3k/numpy, add a dummy __init__.py and -version.py on the top level, and copy and 2to3 the files of the three -submodules. - -When running py3tool again, only changed files are re-processed, which -makes the test-bugfix cycle faster. - -""" -from __future__ import division, absolute_import, print_function - -from optparse import OptionParser -import shutil -import os -import sys -import re -import subprocess -import fnmatch - -if os.environ.get('USE_2TO3CACHE'): - import lib2to3cache - -BASE = os.path.normpath(os.path.join(os.path.dirname(__file__), '..')) -TEMP = os.path.normpath(os.path.join(BASE, '_py3k')) - -SCRIPT_2TO3 = os.path.join(BASE, 'tools', '2to3.py') - -EXTRA_2TO3_FLAGS = { - 'numpy/core/defchararray.py': '-x unicode', - 'numpy/compat/py3k.py': '-x unicode', - 'numpy/ma/timer_comparison.py': 'skip', -} - -# Names of fixers to skip when running 2to3. This is a complete list of -# available fixers, with fixers not currently skipped commented out. -FIXES_TO_SKIP = [ - 'apply', - 'basestring', - 'buffer', - 'callable', - 'dict', - 'exec', - 'execfile', - 'exitfunc', - 'filter', - 'funcattrs', - 'future', - 'getcwdu', - 'has_key', -# 'idioms', - 'import', - 'imports', - 'imports2', - 'input', - 'intern', - 'isinstance', - 'itertools', - 'itertools_imports', - 'long', - 'map', - 'metaclass', - 'methodattrs', - 'ne', - 'next', - 'nonzero', - 'numliterals', - 'operator', - 'paren', - 'print', - 'raise', - 'raw_input', - 'reduce', - 'renames', - 'repr', - 'setliteral', - 'standarderror', - 'sys_exc', - 'throw', - 'tuple_params', -# 'types', -# 'unicode', - 'urllib', -# 'ws_comma', - 'xrange', - 'xreadlines', - 'zip', -] - -skip_fixes= [] -for _t in FIXES_TO_SKIP: - skip_fixes.append('-x') - skip_fixes.append(_t) - - -def main(): - p = OptionParser(usage=__doc__.strip()) - p.add_option("--clean", "-c", action="store_true", - help="clean source directory") - options, args = p.parse_args() - - if not args: - p.error('no submodules given') - else: - dirs = ['numpy/%s' % x for x in map(os.path.basename, args)] - - # Prepare - if not os.path.isdir(TEMP): - os.makedirs(TEMP) - - # Set up dummy files (for building only submodules) - dummy_files = { - '__init__.py': 'from numpy.version import version as __version__', - 'version.py': 'version = "1.4.0.dev"' - } - - for fn, content in dummy_files.items(): - fn = os.path.join(TEMP, 'numpy', fn) - if not os.path.isfile(fn): - try: - os.makedirs(os.path.dirname(fn)) - except OSError: - pass - f = open(fn, 'wb+') - f.write(content.encode('ascii')) - f.close() - - # Environment - pp = [os.path.abspath(TEMP)] - def getenv(): - env = dict(os.environ) - env.update({'PYTHONPATH': ':'.join(pp)}) - return env - - # Copy - for d in dirs: - src = os.path.join(BASE, d) - dst = os.path.join(TEMP, d) - - # Run 2to3 - sync_2to3(dst=dst, - src=src, - patchfile=os.path.join(TEMP, os.path.basename(d) + '.patch'), - clean=options.clean) - - # Run setup.py, falling back to Pdb post-mortem on exceptions - setup_py = os.path.join(dst, 'setup.py') - if os.path.isfile(setup_py): - code = """\ -import pdb, sys, traceback -p = pdb.Pdb() -try: - import __main__ - __main__.__dict__.update({ - "__name__": "__main__", "__file__": "setup.py", - "__builtins__": __builtins__}) - fp = open("setup.py", "rb") - try: - exec(compile(fp.read(), "setup.py", 'exec')) - finally: - fp.close() -except SystemExit: - raise -except: - traceback.print_exc() - t = sys.exc_info()[2] - p.interaction(None, t) -""" - ret = subprocess.call([sys.executable, '-c', code, - 'build_ext', '-i'], - cwd=dst, - env=getenv()) - if ret != 0: - raise RuntimeError("Build failed.") - - # Run nosetests - subprocess.call(['nosetests3', '-v', d], cwd=TEMP) - - -def walk_sync(dir1, dir2, _seen=None): - if _seen is None: - seen = {} - else: - seen = _seen - - if not dir1.endswith(os.path.sep): - dir1 = dir1 + os.path.sep - - # Walk through stuff (which we haven't yet gone through) in dir1 - for root, dirs, files in os.walk(dir1): - sub = root[len(dir1):] - if sub in seen: - dirs = [x for x in dirs if x not in seen[sub][0]] - files = [x for x in files if x not in seen[sub][1]] - seen[sub][0].extend(dirs) - seen[sub][1].extend(files) - else: - seen[sub] = (dirs, files) - if not dirs and not files: - continue - yield os.path.join(dir1, sub), os.path.join(dir2, sub), dirs, files - - if _seen is None: - # Walk through stuff (which we haven't yet gone through) in dir2 - for root2, root1, dirs, files in walk_sync(dir2, dir1, _seen=seen): - yield root1, root2, dirs, files - -def sync_2to3(src, dst, patchfile=None, clean=False): - import lib2to3.main - from io import StringIO - - to_convert = [] - - for src_dir, dst_dir, dirs, files in walk_sync(src, dst): - for fn in dirs + files: - src_fn = os.path.join(src_dir, fn) - dst_fn = os.path.join(dst_dir, fn) - - # skip temporary etc. files - if fn.startswith('.#') or fn.endswith('~'): - continue - - # remove non-existing - if os.path.exists(dst_fn) and not os.path.exists(src_fn): - if clean: - if os.path.isdir(dst_fn): - shutil.rmtree(dst_fn) - else: - os.unlink(dst_fn) - continue - - # make directories - if os.path.isdir(src_fn): - if not os.path.isdir(dst_fn): - os.makedirs(dst_fn) - continue - - dst_dir = os.path.dirname(dst_fn) - if os.path.isfile(dst_fn) and not os.path.isdir(dst_dir): - os.makedirs(dst_dir) - - # don't replace up-to-date files - try: - if os.path.isfile(dst_fn) and \ - os.stat(dst_fn).st_mtime >= os.stat(src_fn).st_mtime: - continue - except OSError: - pass - - # copy file - shutil.copyfile(src_fn, dst_fn) - - # add .py files to 2to3 list - if dst_fn.endswith('.py'): - to_convert.append((src_fn, dst_fn)) - - # run 2to3 - flag_sets = {} - for fn, dst_fn in to_convert: - flag = '' - for pat, opt in EXTRA_2TO3_FLAGS.items(): - if fnmatch.fnmatch(fn, pat): - flag = opt - break - flag_sets.setdefault(flag, []).append(dst_fn) - - if patchfile: - p = open(patchfile, 'wb+') - else: - p = open(os.devnull, 'wb') - - for flags, filenames in flag_sets.items(): - if flags == 'skip': - continue - - _old_stdout = sys.stdout - try: - sys.stdout = StringIO() - opt = [] - opt.extend(['-w', '-n']) - opt.extend(skip_fixes) - opt.extend(flags.split()) - opt.extend(filenames) - lib2to3.main.main("lib2to3.fixes", opt) - finally: - sys.stdout = _old_stdout - - p.close() - -if __name__ == "__main__": - main() |