diff options
537 files changed, 4239 insertions, 5604 deletions
@@ -63,6 +63,7 @@ Christopher Hanley <chanley@gmail.com> chanley <chanley@gmail.com> Christoph Gohlke <cgohlke@uci.edu> cgholke <?@?> Christoph Gohlke <cgohlke@uci.edu> cgohlke <cgohlke@uci.edu> Christoph Gohlke <cgohlke@uci.edu> Christolph Gohlke <cgohlke@uci.edu> +Colin Snyder <47012605+colinsnyder@users.noreply.github.com> colinsnyder <47012605+colinsnyder@users.noreply.github.com> Daniel B Allan <daniel.b.allan@gmail.com> danielballan <daniel.b.allan@gmail.com> Daniel da Silva <mail@danieldasilva.org> Daniel da Silva <daniel@meltingwax.net> Daniel da Silva <mail@danieldasilva.org> Daniel da Silva <var.mail.daniel@gmail.com> @@ -126,6 +127,7 @@ Jérémie du Boisberranger <jeremie.du-boisberranger@inria.fr> jeremiedbb <34657 Jerome Kelleher <jerome.kelleher@ed.ac.uk> jeromekelleher <jerome.kelleher@ed.ac.uk> Johannes Hampp <johannes.hampp@zeu.uni-giessen.de> euronion <42553970+euronion@users.noreply.github.com> Johannes Schönberger <hannesschoenberger@gmail.com> Johannes Schönberger <jschoenberger@demuc.de> +Johann Faouzi <johann.faouzi@gmail.com> johann.faouzi <johann.faouzi@icm-institute.org> John Darbyshire <24256554+attack68@users.noreply.github.com> attack68 <24256554+attack68@users.noreply.github.com> Joseph Fox-Rabinovitz <jfoxrabinovitz@gmail.com> Joseph Fox-Rabinovitz <joseph.r.fox-rabinovitz@nasa.gov> Joseph Fox-Rabinovitz <jfoxrabinovitz@gmail.com> Joseph Fox-Rabinovitz <madphysicist@users.noreply.github.com> @@ -135,6 +137,7 @@ Julian Taylor <juliantaylor108@gmail.com> Julian Taylor <jtaylor.debian@googlema Julian Taylor <juliantaylor108@gmail.com> Julian Taylor <juliantaylor108@googlemail.com> Julien Lhermitte <jrmlhermitte@gmail.com> Julien Lhermitte <lhermitte@bnl.gov> Julien Schueller <julien.schueller@gmail.com> jschueller <julien.schueller@gmail.com> +Justus Magin <keewis@posteo.de> keewis <keewis@users.noreply.github.com> Kai Striega <kaistriega@gmail.com> kai <kaistriega@gmail.com> Kai Striega <kaistriega@gmail.com> kai-striega <kaistriega@gmail.com> Kai Striega <kaistriega@gmail.com> kai-striega <kaistriega+github@gmail.com> @@ -149,6 +152,8 @@ Lars Buitinck <larsmans@gmail.com> Lars Buitinck <L.J.Buitinck@uva.nl> Lars Grüter <lagru@mailbox.org> Lars G <lagru@mailbox.org> Luis Pedro Coelho <luis@luispedro.org> Luis Pedro Coelho <lpc@cmu.edu> Luke Zoltan Kelley <lkelley@cfa.harvard.edu> lzkelley <lkelley@cfa.harvard.edu> +Magdalena Proszewska <magdalena.proszewska@gmail.com> mpro <magdalena.proszewska@gmail.com> +Magdalena Proszewska <magdalena.proszewska@gmail.com> mproszewska <38814059+mproszewska@users.noreply.github.com> Manoj Kumar <manojkumarsivaraj334@gmail.com> MechCoder <manojkumarsivaraj334@gmail.com> Mark DePristo <mdepristo@synapdx.com> markdepristo <mdepristo@synapdx.com> Mark Weissman <mw9050@gmail.com> m-d-w <mw9050@gmail.com> @@ -157,6 +162,7 @@ Mark Wiebe <mwwiebe@gmail.com> Mark Wiebe <mwiebe@continuum.io> Mark Wiebe <mwwiebe@gmail.com> Mark Wiebe <mwiebe@enthought.com> Mark Wiebe <mwwiebe@gmail.com> Mark Wiebe <mwiebe@georg.(none)> Martin Goodson <martingoodson@gmail.com> martingoodson <martingoodson@gmail.com> +Martin Reinecke <martin@mpa-garching.mpg.de> mreineck <martin@mpa-garching.mpg.de> Martin Teichmann <martin.teichmann@xfel.eu> Martin Teichmann <lkb.teichmann@gmail.com> Martino Sorbaro <martino.sorbaro@ed.ac.uk> martinosorb <martino.sorbaro@ed.ac.uk> Mattheus Ueckermann <empeeu@yahoo.com> empeeu <empeeu@yahoo.com> @@ -232,7 +238,12 @@ Wojtek Ruszczewski <git@wr.waw.pl> wrwrwr <git@wr.waw.pl> Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com> kngwyu <yuji.kngw.80s.revive@gmail.com> Yury Kirienko <yury.kirienko@gmail.com> kirienko <yury.kirienko@gmail.com> Zixu Zhao <zixu.zhao.tireless@gmail.com> ZZhaoTireless <zixu.zhao.tireless@gmail.com> -Ziyan Zhou<ziyan.zhou@mujin.co.jp> Ziyan <ziyan.zhou@mujin.co.jp> +Ziyan Zhou <ziyan.zhou@mujin.co.jp> Ziyan <ziyan.zhou@mujin.co.jp> +Zieji Pohz <poh.ziji@gmail.com> jpoh <poh.zijie@gmail.com> +Zieji Pohz <poh.ziji@gmail.com> zjpoh <poh.zijie@gmail.com> +Zieji Pohz <poh.ziji@gmail.com> Zijie (ZJ) Poh <8103276+zjpoh@users.noreply.github.com> +Zolisa Bleki <zolisa.bleki@gmail.com> zoj613 <44142765+zoj613@users.noreply.github.com> +Zolisa Bleki <zolisa.bleki@gmail.com> RedRuM <44142765+zoj613@users.noreply.github.com> luzpaz <kunda@scribus.net> luz.paz <luzpaz@users.noreply.github.com> luzpaz <kunda@scribus.net> luzpaz <luzpaz@users.noreply.github.com> spacescientist <aspacescientist@protonmail.com> spacescientist <spacescientist@pm.me> diff --git a/.travis.yml b/.travis.yml index 4afac959d..73106b4c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ # http://lint.travis-ci.org/ language: python group: travis_latest +os: linux dist: xenial # Travis whitelists the installable packages, additions can be requested @@ -18,8 +19,6 @@ cache: directories: - $HOME/.cache/pip -stage: Comprehensive tests - stages: # Do the style check and a single test job, don't proceed if it fails - name: Initial tests @@ -37,13 +36,14 @@ env: iFWt9Ka92CaqYdU7nqfWp9VImSndPmssjmCXJ1v1IjZPAM\ ahp7Qnm0rWRmA0z9SomuRUQOJQ6s684vU=" -matrix: +jobs: include: # Do all python versions without environment variables set - stage: Initial tests python: 3.8 - - python: 3.6 + - stage: Comprehensive tests + python: 3.6 - python: 3.7 - python: 3.6 @@ -75,7 +75,17 @@ matrix: - USE_ASV=1 - python: 3.7 - env: NPY_RELAXED_STRIDES_CHECKING=0 + env: + - NPY_RELAXED_STRIDES_CHECKING=0 + # use custom symbol-suffixed openblas build, not system ATLAS + - DOWNLOAD_OPENBLAS=1 + - CHECK_BLAS=1 + - NPY_USE_BLAS_ILP64=1 + addons: + apt: + packages: + - gfortran + - eatmydata - python: 3.7 env: USE_WHEEL=1 NPY_RELAXED_STRIDES_DEBUG=1 @@ -94,6 +104,7 @@ matrix: arch: ppc64le env: # use ppc64le OpenBLAS build, not system ATLAS + - DOWNLOAD_OPENBLAS=1 - ATLAS=None - python: 3.7 diff --git a/INSTALL.rst.txt b/INSTALL.rst.txt index d6c42bad7..30f58fa6d 100644 --- a/INSTALL.rst.txt +++ b/INSTALL.rst.txt @@ -14,24 +14,17 @@ Prerequisites Building NumPy requires the following installed software: -1) For Python 3, Python__ 3.5.x or newer. +1) Python__ 3.6.x or newer. - On Debian and derivative (Ubuntu): python python-dev + Please note that the Python development headers also need to be installed, + e.g., on Debian/Ubuntu one needs to install both `python3` and + `python3-dev`. On Windows and macOS this is normally not an issue. - On Windows: the official python installer on Python__ is enough - - Make sure that the Python package distutils is installed before - continuing. For example, in Debian GNU/Linux, distutils is included - in the python-dev package. - - Python must also be compiled with the zlib module enabled. - -2) Cython >= 0.29.2 (for development versions of numpy, not for released - versions) +2) Cython >= 0.29.14 3) pytest__ (optional) 1.15 or later - This is required for testing numpy, but not for using it. + This is required for testing NumPy, but not for using it. Python__ http://www.python.org pytest__ http://pytest.readthedocs.io @@ -45,14 +38,14 @@ pytest__ http://pytest.readthedocs.io .. note:: - More extensive information on building NumPy (and Scipy) is maintained at + More extensive information on building NumPy (and SciPy) is maintained at https://scipy.github.io/devdocs/building/ Basic Installation ================== -To install numpy run:: +To install NumPy, run:: python setup.py build -j 4 install --prefix $HOME/.local @@ -88,18 +81,18 @@ installed then ``g77`` will be detected and used first. To explicitly select Windows ------- -On Windows, building from source can be difficult. Currently, the most robust -option is to use the Intel compilers, or alternatively MSVC (the same version -as used to build Python itself) with Intel ifort. Intel itself maintains a -good `application note <https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl>`_ +On Windows, building from source can be difficult (in particular if you need to +build SciPy as well, because that requires a Fortran compiler). Currently, the +most robust option is to use MSVC (for NumPy only). If you also need SciPy, +you can either use MSVC + Intel Fortran or the Intel compiler suite. +Intel itself maintains a good `application note +<https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl>`_ on this. -If you want to use a free compiler toolchain, the recommended compiler is MingwPy__. -The older MinGW32 compiler set used to produce older .exe installers for NumPy -itself is still available at https://github.com/numpy/numpy-vendor, but not -recommended for use anymore. - -MingwPy__ https://mingwpy.github.io +If you want to use a free compiler toolchain, our current recommendation is to +use Docker or Windows subsystem for Linux (WSL). See +https://scipy.github.io/devdocs/dev/contributor/contributor_toc.html#development-environment +for more details. Building with optimized BLAS support @@ -114,16 +107,16 @@ Windows ------- The Intel compilers work with Intel MKL, see the application note linked above. -MingwPy__ works with OpenBLAS. + For an overview of the state of BLAS/LAPACK libraries on Windows, see `here <https://mingwpy.github.io/blas_lapack.html>`_. -OS X ----- +macOS +----- -OS X ships the Accelerate framework, which NumPy can build against without any -manual configuration. Other BLAS/LAPACK implementations (OpenBLAS, Intel MKL, -ATLAS) will also work. +You will need to install a BLAS/LAPACK library. We recommend using OpenBLAS or +Intel MKL. Apple's Accelerate also still works, however it has bugs and we are +likely to drop support for it in the near future. Ubuntu/Debian ------------- diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 29fc6c614..1851df71a 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -40,8 +40,9 @@ stages: locale-gen fr_FR && update-locale && \ apt-get -y install gfortran-5 wget && \ target=\$(python3 tools/openblas_support.py) && \ - cp -r \$target/usr/local/lib/* /usr/lib && \ - cp \$target/usr/local/include/* /usr/include && \ + ls -lR \$target && \ + cp -r \$target/lib/* /usr/lib && \ + cp \$target/include/* /usr/include && \ python3 -m pip install --user --upgrade pip setuptools && \ python3 -m pip install --user -r test_requirements.txt && \ python3 -m pip install . && \ @@ -65,13 +66,21 @@ stages: # azure config for mac os -- Microsoft has indicated # they will patch this issue vmImage: macOS-10.14 + strategy: + maxParallel: 2 + matrix: + Python36: + PYTHON_VERSION: '3.6' + Python36-ILP64: + PYTHON_VERSION: '3.6' + NPY_USE_BLAS_ILP64: '1' steps: # the @0 refers to the (major) version of the *task* on Microsoft's # end, not the order in the build matrix nor anything to do # with version of Python selected - task: UsePythonVersion@0 inputs: - versionSpec: '3.6' + versionSpec: $(PYTHON_VERSION) addToPath: true architecture: 'x64' # NOTE: do we have a compelling reason to use older / newer @@ -97,9 +106,10 @@ stages: # primarily on file size / name details - script: | target=$(python tools/openblas_support.py) + ls -lR $target # manually link to appropriate system paths - cp $target/usr/local/lib/* /usr/local/lib/ - cp $target/usr/local/include/* /usr/local/include/ + cp $target/lib/* /usr/local/lib/ + cp $target/include/* /usr/local/include/ displayName: 'install pre-built openblas' - script: python -m pip install --upgrade pip setuptools wheel displayName: 'Install tools' @@ -171,6 +181,8 @@ stages: PYTHON_ARCH: 'x64' TEST_MODE: full BITS: 64 + NPY_USE_BLAS_ILP64: '1' + OPENBLAS_SUFFIX: '64_' steps: - template: azure-steps-windows.yml - job: Linux_PyPy3 diff --git a/azure-steps-windows.yml b/azure-steps-windows.yml index 26d7a667d..fdc969890 100644 --- a/azure-steps-windows.yml +++ b/azure-steps-windows.yml @@ -9,9 +9,9 @@ steps: - script: python -m pip install -r test_requirements.txt displayName: 'Install dependencies; some are optional to avoid test skips' - powershell: | - $pyversion = python -c "from __future__ import print_function; import sys; print(sys.version.split()[0])" + $pyversion = python -c "import sys; print(sys.version.split()[0])" Write-Host "Python Version: $pyversion" - $target = "C:\\hostedtoolcache\\windows\\Python\\$pyversion\\$(PYTHON_ARCH)\\lib\\openblas.a" + $target = "C:\\hostedtoolcache\\windows\\Python\\$pyversion\\$(PYTHON_ARCH)\\lib\\openblas$env:OPENBLAS_SUFFIX.a" Write-Host "target path: $target" $openblas = python tools/openblas_support.py cp $openblas $target @@ -53,4 +53,4 @@ steps: inputs: testResultsFiles: '**/test-*.xml' failTaskOnFailedTests: true - testRunTitle: 'Publish test results for Python $(PYTHON_VERSION) $(BITS)-bit $(TEST_MODE) Windows'
\ No newline at end of file + testRunTitle: 'Publish test results for Python $(PYTHON_VERSION) $(BITS)-bit $(TEST_MODE) Windows' diff --git a/benchmarks/benchmarks/__init__.py b/benchmarks/benchmarks/__init__.py index e8a859ff4..e4193cf05 100644 --- a/benchmarks/benchmarks/__init__.py +++ b/benchmarks/benchmarks/__init__.py @@ -1,3 +1 @@ -from __future__ import absolute_import, division, print_function - from . import common diff --git a/benchmarks/benchmarks/bench_app.py b/benchmarks/benchmarks/bench_app.py index ccf6e4c4a..2a649f39b 100644 --- a/benchmarks/benchmarks/bench_app.py +++ b/benchmarks/benchmarks/bench_app.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark import numpy as np diff --git a/benchmarks/benchmarks/bench_array_coercion.py b/benchmarks/benchmarks/bench_array_coercion.py new file mode 100644 index 000000000..2bae4c002 --- /dev/null +++ b/benchmarks/benchmarks/bench_array_coercion.py @@ -0,0 +1,57 @@ +from __future__ import absolute_import, division, print_function + +from .common import Benchmark + +import numpy as np + + +class ArrayCoercionSmall(Benchmark): + # More detailed benchmarks for array coercion, + # some basic benchmarks are in `bench_core.py`. + params = [[range(3), [1], 1, np.array([5], dtype=np.int64), np.int64(5)]] + param_names = ['array_like'] + int64 = np.dtype(np.int64) + + def time_array_invalid_kwarg(self, array_like): + try: + np.array(array_like, ndmin="not-integer") + except TypeError: + pass + + def time_array(self, array_like): + np.array(array_like) + + def time_array_dtype_not_kwargs(self, array_like): + np.array(array_like, self.int64) + + def time_array_no_copy(self, array_like): + np.array(array_like, copy=False) + + def time_array_subok(self, array_like): + np.array(array_like, subok=True) + + def time_array_all_kwargs(self, array_like): + np.array(array_like, dtype=self.int64, copy=False, order="F", + subok=False, ndmin=2) + + def time_asarray(self, array_like): + np.asarray(array_like) + + def time_asarray_dtype(self, array_like): + np.array(array_like, dtype=self.int64) + + def time_asarray_dtype(self, array_like): + np.array(array_like, dtype=self.int64, order="F") + + def time_asanyarray(self, array_like): + np.asarray(array_like) + + def time_asanyarray_dtype(self, array_like): + np.array(array_like, dtype=self.int64) + + def time_asanyarray_dtype(self, array_like): + np.array(array_like, dtype=self.int64, order="F") + + def time_ascontiguousarray(self, array_like): + np.ascontiguousarray(array_like) + diff --git a/benchmarks/benchmarks/bench_avx.py b/benchmarks/benchmarks/bench_avx.py index f7b524e43..023696b90 100644 --- a/benchmarks/benchmarks/bench_avx.py +++ b/benchmarks/benchmarks/bench_avx.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark import numpy as np @@ -31,4 +29,3 @@ class AVX_UFunc(Benchmark): def time_ufunc(self, ufuncname, stride, dtype): self.f(self.arr[::stride]) - diff --git a/benchmarks/benchmarks/bench_core.py b/benchmarks/benchmarks/bench_core.py index f7ce61b8f..94d3ad503 100644 --- a/benchmarks/benchmarks/bench_core.py +++ b/benchmarks/benchmarks/bench_core.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark import numpy as np diff --git a/benchmarks/benchmarks/bench_function_base.py b/benchmarks/benchmarks/bench_function_base.py index 2170c4fc4..b1e592749 100644 --- a/benchmarks/benchmarks/bench_function_base.py +++ b/benchmarks/benchmarks/bench_function_base.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark import numpy as np @@ -106,7 +104,7 @@ def memoize(f): return f -class SortGenerator(object): +class SortGenerator: # The size of the unsorted area in the "random unsorted area" # benchmarks AREA_SIZE = 100 diff --git a/benchmarks/benchmarks/bench_import.py b/benchmarks/benchmarks/bench_import.py index 83edecafe..4b6ecbc7b 100644 --- a/benchmarks/benchmarks/bench_import.py +++ b/benchmarks/benchmarks/bench_import.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from subprocess import call from sys import executable from timeit import default_timer diff --git a/benchmarks/benchmarks/bench_indexing.py b/benchmarks/benchmarks/bench_indexing.py index a62a2050e..bb7596d0a 100644 --- a/benchmarks/benchmarks/bench_indexing.py +++ b/benchmarks/benchmarks/bench_indexing.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark, get_squares_, get_indexes_, get_indexes_rand_ from os.path import join as pjoin diff --git a/benchmarks/benchmarks/bench_io.py b/benchmarks/benchmarks/bench_io.py index 439cd422f..d5ce9a271 100644 --- a/benchmarks/benchmarks/bench_io.py +++ b/benchmarks/benchmarks/bench_io.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark, get_squares import numpy as np diff --git a/benchmarks/benchmarks/bench_itemselection.py b/benchmarks/benchmarks/bench_itemselection.py new file mode 100644 index 000000000..27fc49e30 --- /dev/null +++ b/benchmarks/benchmarks/bench_itemselection.py @@ -0,0 +1,45 @@ +from __future__ import absolute_import, division, print_function + +from .common import Benchmark, TYPES1 + +import numpy as np + + +class Take(Benchmark): + params = [ + [(1000, 1), (1000, 2), (2, 1000, 1), (1000, 3)], + ["raise", "wrap", "clip"], + TYPES1] + param_names = ["shape", "mode", "dtype"] + + def setup(self, shape, mode, dtype): + self.arr = np.ones(shape, dtype) + self.indices = np.arange(1000) + + def time_contiguous(self, shape, mode, dtype): + self.arr.take(self.indices, axis=-2, mode=mode) + + +class PutMask(Benchmark): + params = [ + [True, False], + TYPES1] + param_names = ["values_is_scalar", "dtype"] + + def setup(self, values_is_scalar, dtype): + if values_is_scalar: + self.vals = np.array(1., dtype=dtype) + else: + self.vals = np.ones(1000, dtype=dtype) + + self.arr = np.ones(1000, dtype=dtype) + + self.dense_mask = np.ones(1000, dtype="bool") + self.sparse_mask = np.zeros(1000, dtype="bool") + + def time_dense(self, values_is_scalar, dtype): + np.putmask(self.arr, self.dense_mask, self.vals) + + def time_sparse(self, values_is_scalar, dtype): + np.putmask(self.arr, self.sparse_mask, self.vals) + diff --git a/benchmarks/benchmarks/bench_lib.py b/benchmarks/benchmarks/bench_lib.py index f65a96dad..c22ceaa5e 100644 --- a/benchmarks/benchmarks/bench_lib.py +++ b/benchmarks/benchmarks/bench_lib.py @@ -1,8 +1,6 @@ """Benchmarks for `numpy.lib`.""" -from __future__ import absolute_import, division, print_function - from .common import Benchmark import numpy as np diff --git a/benchmarks/benchmarks/bench_linalg.py b/benchmarks/benchmarks/bench_linalg.py index 5c44162a2..3abbe3670 100644 --- a/benchmarks/benchmarks/bench_linalg.py +++ b/benchmarks/benchmarks/bench_linalg.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark, get_squares_, get_indexes_rand, TYPES1 import numpy as np diff --git a/benchmarks/benchmarks/bench_ma.py b/benchmarks/benchmarks/bench_ma.py index aff78df0a..b214c0b86 100644 --- a/benchmarks/benchmarks/bench_ma.py +++ b/benchmarks/benchmarks/bench_ma.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark import numpy as np diff --git a/benchmarks/benchmarks/bench_overrides.py b/benchmarks/benchmarks/bench_overrides.py index 58572d07d..e44951785 100644 --- a/benchmarks/benchmarks/bench_overrides.py +++ b/benchmarks/benchmarks/bench_overrides.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark try: @@ -35,7 +33,7 @@ def mock_concatenate(arrays, axis=0, out=None): pass -class DuckArray(object): +class DuckArray: def __array_function__(self, func, types, args, kwargs): pass diff --git a/benchmarks/benchmarks/bench_random.py b/benchmarks/benchmarks/bench_random.py index c52b463e5..9a5125876 100644 --- a/benchmarks/benchmarks/bench_random.py +++ b/benchmarks/benchmarks/bench_random.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark import numpy as np diff --git a/benchmarks/benchmarks/bench_records.py b/benchmarks/benchmarks/bench_records.py index 41a6dd775..92e0fca16 100644 --- a/benchmarks/benchmarks/bench_records.py +++ b/benchmarks/benchmarks/bench_records.py @@ -1,4 +1,3 @@ -from __future__ import absolute_import, division, print_function import os from .common import Benchmark diff --git a/benchmarks/benchmarks/bench_reduce.py b/benchmarks/benchmarks/bench_reduce.py index 0043d5357..76c573a51 100644 --- a/benchmarks/benchmarks/bench_reduce.py +++ b/benchmarks/benchmarks/bench_reduce.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark, TYPES1, get_squares import numpy as np diff --git a/benchmarks/benchmarks/bench_shape_base.py b/benchmarks/benchmarks/bench_shape_base.py index 187b923cd..0c7dc4e72 100644 --- a/benchmarks/benchmarks/bench_shape_base.py +++ b/benchmarks/benchmarks/bench_shape_base.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark import numpy as np diff --git a/benchmarks/benchmarks/bench_ufunc.py b/benchmarks/benchmarks/bench_ufunc.py index 62e70782d..9f45a7257 100644 --- a/benchmarks/benchmarks/bench_ufunc.py +++ b/benchmarks/benchmarks/bench_ufunc.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - from .common import Benchmark, get_squares_ import numpy as np @@ -152,7 +150,7 @@ class Scalar(Benchmark): (self.y + self.z) -class ArgPack(object): +class ArgPack: __slots__ = ['args', 'kwargs'] def __init__(self, *args, **kwargs): self.args = args diff --git a/benchmarks/benchmarks/common.py b/benchmarks/benchmarks/common.py index 18a09fd40..3fd81a164 100644 --- a/benchmarks/benchmarks/common.py +++ b/benchmarks/benchmarks/common.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - import numpy import random @@ -112,5 +110,5 @@ def get_indexes_rand_(): return indexes_rand_ -class Benchmark(object): +class Benchmark: goal_time = 0.25 diff --git a/doc/DISTUTILS.rst.txt b/doc/DISTUTILS.rst.txt index bcef82500..677398baa 100644 --- a/doc/DISTUTILS.rst.txt +++ b/doc/DISTUTILS.rst.txt @@ -577,9 +577,6 @@ The header of a typical SciPy ``__init__.py`` is:: Package docstring, typically with a brief description and function listing. """ - # py3k related imports - from __future__ import division, print_function, absolute_import - # import functions into module namespace from .subpackage import * ... diff --git a/doc/HOWTO_RELEASE.rst.txt b/doc/HOWTO_RELEASE.rst.txt index f0231293f..f201d3a77 100644 --- a/doc/HOWTO_RELEASE.rst.txt +++ b/doc/HOWTO_RELEASE.rst.txt @@ -40,27 +40,23 @@ Release Scripts Supported platforms and versions ================================ -Python 2.7 and >=3.4 are the currently supported versions when building from -source. We test NumPy against all these versions every time we merge code to -master. Binary installers may be available for a subset of these versions (see -below). - +`NEP 29`_ outlines which Python versions are supported; For the first half of +2020, this will be Python >= 3.6. We test NumPy against all these versions +every time we merge code to master. Binary installers may be available for a +subset of these versions (see below). OS X ---- -Python 2.7 and >=3.4 are the versions for which we provide binary installers. -OS X versions >= 10.6 are supported. We build binary wheels for OSX that are -compatible with Python.org Python, system Python, homebrew and macports - see -this `OSX wheel building summary +OS X versions >= 10.9 are supported, for Python version support see `NEP 29`_. +We build binary wheels for OSX that are compatible with Python.org Python, +system Python, homebrew and macports - see this `OSX wheel building summary <https://github.com/MacPython/wiki/wiki/Spinning-wheels>`_ for details. Windows ------- -We build 32- and 64-bit wheels for Python 2.7, 3.4, 3.5 on Windows. Windows -XP, Vista, 7, 8 and 10 are supported. We build NumPy using the MSVC compilers -on Appveyor, but we are hoping to update to a `mingw-w64 toolchain -<https://mingwpy.github.io>`_. The Windows wheels use ATLAS for BLAS / LAPACK. +We build 32- and 64-bit wheels on Windows. Windows 7, 8 and 10 are supported. +We build NumPy using the `mingw-w64 toolchain`_ on Appveyor. Linux @@ -92,13 +88,24 @@ each platform. At the moment this means: - OS X builds on travis currently use `clang`. It appears that binary wheels for OSX >= 10.6 can be safely built from the travis-ci OSX 10.9 VMs when building against the Python from the Python.org installers; -- Windows builds use the MSVC version corresponding to the Python being built - against; +- Windows builds use the `mingw-w64 toolchain`_; - Manylinux1 wheels use the gcc provided on the Manylinux docker images. You will need Cython for building the binaries. Cython compiles the ``.pyx`` files in the NumPy distribution to ``.c`` files. +.. _mingw-w64 toolchain : https://mingwpy.github.io +.. _NEP 29 : https://numpy.org/neps/nep-0029-deprecation_policy.html + +OpenBLAS +------------ +All the wheels link to a version of OpenBLAS_ supplied via the openblas-libs_ repo. +The shared object (or DLL) is shipped with in the wheel, renamed to prevent name +collisions with other OpenBLAS shared objects that may exist in the filesystem. + +.. _OpenBLAS: https://github.com/xianyi/OpenBLAS +.. _openblas-libs: https://github.com/MacPython/openblas-libs + Building source archives and wheels ----------------------------------- @@ -106,9 +113,10 @@ You will need write permission for numpy-wheels in order to trigger wheel builds. - Python(s) from `python.org <https://python.org>`_ or linux distro. -- cython +- cython (pip) - virtualenv (pip) - Paver (pip) +- pandoc `pandoc.org <https://www.pandoc.org>`_ or linux distro. - numpy-wheels `<https://github.com/MacPython/numpy-wheels>`_ (clone) @@ -379,7 +387,7 @@ Make the release ---------------- Build the changelog and notes for upload with:: - paver write_release_and_log + paver write_release Build and archive documentation diff --git a/doc/Makefile b/doc/Makefile index 74272fa50..199a22c34 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -158,7 +158,7 @@ endif @# the instructions in doc/HOWTO_RELEASE.rst.txt @echo " " @echo New documentation archive added to ./build/merge. - @echo Now add/modify the appropiate section after + @echo Now add/modify the appropriate section after @echo " <!-- insert here -->" @echo in build/merge/index.html, @echo then \"git commit\", \"git push\" diff --git a/doc/Py3K.rst.txt b/doc/Py3K.rst.txt index b23536ca5..cde0394dd 100644 --- a/doc/Py3K.rst.txt +++ b/doc/Py3K.rst.txt @@ -355,9 +355,7 @@ The Py2/Py3 compatible structure definition looks like:: (binaryfunc)0, /*nb_true_divide*/ 0, /*nb_inplace_floor_divide*/ 0, /*nb_inplace_true_divide*/ - #if PY_VERSION_HEX >= 0x02050000 (unaryfunc)NULL, /*nb_index*/ - #endif }; @@ -394,14 +392,6 @@ There are a couple of places that need further attention: In some cases, this returns a buffer object on Python 2. On Python 3, there is no stand-alone buffer object, so we return a byte array instead. -- multiarray.int_asbuffer - - Converts an integer to a void* pointer -- in Python. - - Should we just remove this for Py3? It doesn't seem like it is used - anywhere, and it doesn't sound very useful. - - The Py2/Py3 compatible PyBufferMethods definition looks like:: NPY_NO_EXPORT PyBufferProcs array_as_buffer = { @@ -430,10 +420,6 @@ The Py2/Py3 compatible PyBufferMethods definition looks like:: .. todo:: - Figure out what to do with int_asbuffer - -.. todo:: - There's stuff to clean up in numarray/_capi.c diff --git a/doc/TESTS.rst.txt b/doc/TESTS.rst.txt index 14cb28df8..9023c7100 100644 --- a/doc/TESTS.rst.txt +++ b/doc/TESTS.rst.txt @@ -119,7 +119,7 @@ that makes it hard to identify the test from the output of running the test suite with ``verbose=2`` (or similar verbosity setting). Use plain comments (``#``) if necessary. -Labeling tests +Labeling tests -------------- As an alternative to ``pytest.mark.<label>``, there are a number of labels you @@ -174,7 +174,7 @@ name; thus:: print 'doing teardown' - class TestMe(object): + class TestMe: def setup(): """Class-level setup""" print 'doing setup' diff --git a/doc/cdoc/numpyfilter.py b/doc/cdoc/numpyfilter.py index 0ec50697e..65c801206 100755 --- a/doc/cdoc/numpyfilter.py +++ b/doc/cdoc/numpyfilter.py @@ -6,8 +6,6 @@ Interpret C comments as ReStructuredText, and replace them by the HTML output. Also, add Doxygen /** and /**< syntax automatically where appropriate. """ -from __future__ import division, absolute_import, print_function - import sys import re import os diff --git a/doc/changelog/1.16.6-changelog.rst b/doc/changelog/1.16.6-changelog.rst new file mode 100644 index 000000000..62ff46c34 --- /dev/null +++ b/doc/changelog/1.16.6-changelog.rst @@ -0,0 +1,36 @@ + +Contributors +============ + +A total of 10 people contributed to this release. + +* CakeWithSteak +* Charles Harris +* Chris Burr +* Eric Wieser +* Fernando Saravia +* Lars Grueter +* Matti Picus +* Maxwell Aladago +* Qiming Sun +* Warren Weckesser + +Pull requests merged +==================== + +A total of 14 pull requests were merged for this release. + +* `#14211 <https://github.com/numpy/numpy/pull/14211>`__: BUG: Fix uint-overflow if padding with linear_ramp and negative... +* `#14275 <https://github.com/numpy/numpy/pull/14275>`__: BUG: fixing to allow unpickling of PY3 pickles from PY2 +* `#14340 <https://github.com/numpy/numpy/pull/14340>`__: BUG: Fix misuse of .names and .fields in various places (backport... +* `#14423 <https://github.com/numpy/numpy/pull/14423>`__: BUG: test, fix regression in converting to ctypes. +* `#14434 <https://github.com/numpy/numpy/pull/14434>`__: BUG: Fixed maximum relative error reporting in assert_allclose +* `#14509 <https://github.com/numpy/numpy/pull/14509>`__: BUG: Fix regression in boolean matmul. +* `#14686 <https://github.com/numpy/numpy/pull/14686>`__: BUG: properly define PyArray_DescrCheck +* `#14853 <https://github.com/numpy/numpy/pull/14853>`__: BLD: add 'apt update' to shippable +* `#14854 <https://github.com/numpy/numpy/pull/14854>`__: BUG: Fix _ctypes class circular reference. (#13808) +* `#14856 <https://github.com/numpy/numpy/pull/14856>`__: BUG: Fix `np.einsum` errors on Power9 Linux and z/Linux +* `#14863 <https://github.com/numpy/numpy/pull/14863>`__: BLD: Prevent -flto from optimising long double representation... +* `#14864 <https://github.com/numpy/numpy/pull/14864>`__: BUG: lib: Fix histogram problem with signed integer arrays. +* `#15172 <https://github.com/numpy/numpy/pull/15172>`__: ENH: Backport improvements to testing functions. +* `#15191 <https://github.com/numpy/numpy/pull/15191>`__: REL: Prepare for 1.16.6 release. diff --git a/doc/changelog/1.17.5-changelog.rst b/doc/changelog/1.17.5-changelog.rst new file mode 100644 index 000000000..7ac758075 --- /dev/null +++ b/doc/changelog/1.17.5-changelog.rst @@ -0,0 +1,26 @@ + +Contributors +============ + +A total of 6 people contributed to this release. People with a "+" by their +names contributed a patch for the first time. + +* Charles Harris +* Eric Wieser +* Ilhan Polat +* Matti Picus +* Michael Hudson-Doyle +* Ralf Gommers + +Pull requests merged +==================== + +A total of 7 pull requests were merged for this release. + +* `#14593 <https://github.com/numpy/numpy/pull/14593>`__: MAINT: backport Cython API cleanup to 1.17.x, remove docs +* `#14937 <https://github.com/numpy/numpy/pull/14937>`__: BUG: fix integer size confusion in handling array's ndmin argument +* `#14939 <https://github.com/numpy/numpy/pull/14939>`__: BUILD: remove SSE2 flag from numpy.random builds +* `#14993 <https://github.com/numpy/numpy/pull/14993>`__: MAINT: Added Python3.8 branch to dll lib discovery +* `#15038 <https://github.com/numpy/numpy/pull/15038>`__: BUG: Fix refcounting in ufunc object loops +* `#15067 <https://github.com/numpy/numpy/pull/15067>`__: BUG: Exceptions tracebacks are dropped +* `#15175 <https://github.com/numpy/numpy/pull/15175>`__: ENH: Backport improvements to testing functions. diff --git a/doc/changelog/1.18.0-changelog.rst b/doc/changelog/1.18.0-changelog.rst new file mode 100644 index 000000000..b86b3614a --- /dev/null +++ b/doc/changelog/1.18.0-changelog.rst @@ -0,0 +1,533 @@ + +Contributors +============ + +A total of 114 people contributed to this release. People with a "+" by their +names contributed a patch for the first time. + +* Abhinav Sagar +* Alex Henrie + +* Alexander Jung + +* Allan Haldane +* Andrea Pattori +* Andrew Liu + +* Anis Ladram + +* Anne Bonner + +* Antoine Dechaume + +* Aryan Naraghi + +* Bastian Eichenberger + +* Brian Wignall + +* Brigitta Sipocz +* CakeWithSteak + +* Charles Harris +* Chris Barker +* Chris Burr + +* Chris Markiewicz + +* Christoph Gohlke +* Christopher Whelan +* Colin Snyder +* Dan Allan +* Daniel Ching +* David Stansby + +* David Zwicker + +* Dieter Werthmüller +* Disconnect3d + +* Dmytro + +* Doug Davis + +* Eric Larson +* Eric Wieser +* Esben Haabendal + +* Eugene Prilepin + +* Felix Divo + +* Gary Gurlaskie +* Gina + +* Giuseppe Cuccu + +* Grzegorz Bokota + +* Guanqun Lu + +* Guilherme Leobas + +* Guillaume Horel +* Géraud Le Falher + +* Hameer Abbasi +* Harmon +* Hiroyuki V. Yamazaki +* Huang, Guangtai + +* Hugo + +* Hyeonguk Ryu + +* Ilhan Polat + +* Isaac Virshup +* Jack J. Woehr + +* Jack Woehr + +* Jackie Leng +* Jaime Fernandez +* Jeff Hale + +* Johann Faouzi + +* Jon Dufresne + +* Joseph Fox-Rabinovitz +* Joseph R. Fox-Rabinovitz + +* João Marcos Gris + +* Justus Magin + +* Jérémie du Boisberranger +* Kai Striega +* Kevin Sheppard +* Kexuan Sun +* Kmol Yuan + +* Kriti Singh +* Larry Bradley + +* Lars Grueter +* Luis Pedro Coelho +* MSeifert04 +* Magdalena Proszewska + +* Manny + +* Mark Harfouche +* Martin Reinecke +* Martin Thoma +* Matt Haberland + +* Matt McCormick + +* Matthias Bussonnier +* Matti Picus +* Max Bolingbroke + +* Maxwell Aladago + +* Michael Hudson-Doyle + +* Oleksandr Pavlyk +* Omar Merghany + +* Pauli Virtanen +* Peter Andreas Entschev +* Peter Bell +* Peter Cock + +* Pradeep Reddy Raamana + +* Qiming Sun + +* Raghuveer Devulapalli +* Ralf Gommers +* Samesh + +* Samesh Lakhotia + +* Sebastian Berg +* Sergei Lebedev +* Seth Troisi + +* Siddhesh Poyarekar + +* Simon + +* Simon Notley + +* Stefan van der Walt +* Stephan Hoyer +* Steve Stagg +* Thomas A Caswell +* Thomas Kluyver +* Tim Hoffmann + +* Tirth Patel + +* Tyler Reddy +* Vladimir Pershin + +* Warren Weckesser +* Yadong Zhang + +* Zieji Pohz + +* Zolisa Bleki + + +Pull requests merged +==================== + +A total of 406 pull requests were merged for this release. + +* `#9301 <https://github.com/numpy/numpy/pull/9301>`__: DOC: added note to docstring of numpy.savez +* `#10151 <https://github.com/numpy/numpy/pull/10151>`__: BUG: Numpy scalar types sometimes have the same name +* `#12129 <https://github.com/numpy/numpy/pull/12129>`__: DOC: Improve axes shift description and example in np.tensordot +* `#12205 <https://github.com/numpy/numpy/pull/12205>`__: MAINT: avoid relying on `np.generic.__name__` in `np.dtype.name` +* `#12284 <https://github.com/numpy/numpy/pull/12284>`__: ENH: supply our version of numpy.pxd, requires cython>=0.29 +* `#12633 <https://github.com/numpy/numpy/pull/12633>`__: BUG: General fixes to f2py reference counts (dereferencing) +* `#12658 <https://github.com/numpy/numpy/pull/12658>`__: BUG: NaT now sorts to ends of arrays +* `#12828 <https://github.com/numpy/numpy/pull/12828>`__: DOC: Updates to nditer usage instructions +* `#13003 <https://github.com/numpy/numpy/pull/13003>`__: BUG: Do not crash on recursive `.dtype` attribute lookup. +* `#13368 <https://github.com/numpy/numpy/pull/13368>`__: ENH: Use AVX for float32 implementation of np.sin & np.cos +* `#13605 <https://github.com/numpy/numpy/pull/13605>`__: DEP: Deprecate silent ignoring of bad data in fromfile/fromstring +* `#13610 <https://github.com/numpy/numpy/pull/13610>`__: ENH: Always produce a consistent shape in the result of `argwhere` +* `#13673 <https://github.com/numpy/numpy/pull/13673>`__: DOC: array(obj, dtype=dt) can downcast +* `#13698 <https://github.com/numpy/numpy/pull/13698>`__: DOC: Document ma.filled behavior with non-scalar fill_value +* `#13710 <https://github.com/numpy/numpy/pull/13710>`__: DOC: Add note to irfft-like functions about the default sizes +* `#13739 <https://github.com/numpy/numpy/pull/13739>`__: BUG: Don't produce undefined behavior for a << b if b >= bitsof(a) +* `#13766 <https://github.com/numpy/numpy/pull/13766>`__: MAINT: Update NEP template. +* `#13794 <https://github.com/numpy/numpy/pull/13794>`__: ENH: random: Add the multivariate hypergeometric distribution. +* `#13799 <https://github.com/numpy/numpy/pull/13799>`__: DOC: Fix unrendered links +* `#13812 <https://github.com/numpy/numpy/pull/13812>`__: MAINT: Rewrite Floyd algorithm +* `#13825 <https://github.com/numpy/numpy/pull/13825>`__: DOC: Add missing macros to C-API documentation +* `#13829 <https://github.com/numpy/numpy/pull/13829>`__: ENH: Add axis argument to random.permutation and random.shuffle +* `#13847 <https://github.com/numpy/numpy/pull/13847>`__: DOC: Adds documentation of functions exposed in numpy namespace +* `#13860 <https://github.com/numpy/numpy/pull/13860>`__: BUG: Refcount fixes +* `#13871 <https://github.com/numpy/numpy/pull/13871>`__: MAINT: Ensure array_dealloc does not modify refcount of self +* `#13874 <https://github.com/numpy/numpy/pull/13874>`__: MAINT: Prepare master for 1.18.0 development. +* `#13876 <https://github.com/numpy/numpy/pull/13876>`__: MAINT,BUG,DOC: Fix errors in _add_newdocs +* `#13880 <https://github.com/numpy/numpy/pull/13880>`__: MAINT: Remove an unnessary backslash between two string literals +* `#13881 <https://github.com/numpy/numpy/pull/13881>`__: MAINT: Update pavement to use python3 in shell commands. +* `#13882 <https://github.com/numpy/numpy/pull/13882>`__: MAINT: Remove unnecessary backslashes (and replace others by... +* `#13883 <https://github.com/numpy/numpy/pull/13883>`__: MAINT: Replace integers in places where booleans are expected +* `#13884 <https://github.com/numpy/numpy/pull/13884>`__: DOC: Add missing parameter description for keepdims in MaskedArray +* `#13885 <https://github.com/numpy/numpy/pull/13885>`__: ENH: use AVX for float32 and float64 implementation of sqrt,... +* `#13886 <https://github.com/numpy/numpy/pull/13886>`__: DOC: reformat top-level release index +* `#13892 <https://github.com/numpy/numpy/pull/13892>`__: DOC : Refactor Array API documentation -- Array Structure and... +* `#13895 <https://github.com/numpy/numpy/pull/13895>`__: DOC: Fix typo in "make_mask" documentation +* `#13896 <https://github.com/numpy/numpy/pull/13896>`__: MAINT: Delete unused _aliased_types.py +* `#13901 <https://github.com/numpy/numpy/pull/13901>`__: BLD: Remove Trusty dist in Travis CI build +* `#13907 <https://github.com/numpy/numpy/pull/13907>`__: BUG: Handle weird bytestrings in dtype() +* `#13908 <https://github.com/numpy/numpy/pull/13908>`__: ENH: use towncrier to build the release note +* `#13913 <https://github.com/numpy/numpy/pull/13913>`__: ENH: improve error message for ragged-array creation failure +* `#13914 <https://github.com/numpy/numpy/pull/13914>`__: DOC: Update the description of byteswap +* `#13916 <https://github.com/numpy/numpy/pull/13916>`__: BUG: i0 Bessel function regression on array-likes supporting... +* `#13920 <https://github.com/numpy/numpy/pull/13920>`__: ENH, BUILD: refactor all OpenBLAS downloads into a single, testable... +* `#13922 <https://github.com/numpy/numpy/pull/13922>`__: MAINT: Remove unnecessary parenthesis in numpy.ma.core +* `#13925 <https://github.com/numpy/numpy/pull/13925>`__: MAINT: Fix wrong spelling of ufunc +* `#13926 <https://github.com/numpy/numpy/pull/13926>`__: DOC: Remove explicit .next method calls with built-in next function... +* `#13928 <https://github.com/numpy/numpy/pull/13928>`__: DOC: Don't override MaskedArray.view documentation with the one... +* `#13930 <https://github.com/numpy/numpy/pull/13930>`__: BUG: Fix incorrect GIL release in array.nonzero +* `#13935 <https://github.com/numpy/numpy/pull/13935>`__: MAINT: Warn if `_add_newdocs.py` is used to add docstrings to... +* `#13943 <https://github.com/numpy/numpy/pull/13943>`__: MAINT: Revert #13876, "MAINT,BUG,DOC: Fix errors in _add_newdocs" +* `#13944 <https://github.com/numpy/numpy/pull/13944>`__: MAINT,BUG,DOC: Fix errors in _add_newdocs +* `#13945 <https://github.com/numpy/numpy/pull/13945>`__: DOC, MAINT: emphasize random API changes, remove Generator.randint +* `#13946 <https://github.com/numpy/numpy/pull/13946>`__: DOC: Add a numpy-doc docstring to add_newdoc +* `#13947 <https://github.com/numpy/numpy/pull/13947>`__: DOC: Fix rst rendering in data types +* `#13948 <https://github.com/numpy/numpy/pull/13948>`__: DOC:Update the description of set_printoptions in quickstart... +* `#13950 <https://github.com/numpy/numpy/pull/13950>`__: Fixing failure on Python 2.7 on Windows 7 +* `#13952 <https://github.com/numpy/numpy/pull/13952>`__: Fix a typo related to the range of indices +* `#13959 <https://github.com/numpy/numpy/pull/13959>`__: DOC: add space between words across lines +* `#13964 <https://github.com/numpy/numpy/pull/13964>`__: BUG, DOC: add new recfunctions to `__all__` +* `#13967 <https://github.com/numpy/numpy/pull/13967>`__: DOC: Change (old) range() to np.arange() +* `#13968 <https://github.com/numpy/numpy/pull/13968>`__: DOC: improve np.sort docstring +* `#13970 <https://github.com/numpy/numpy/pull/13970>`__: DOC: spellcheck numpy/doc/broadcasting.py +* `#13976 <https://github.com/numpy/numpy/pull/13976>`__: MAINT, TST: remove test-installed-numpy.py +* `#13979 <https://github.com/numpy/numpy/pull/13979>`__: DOC: Document array_function at a higher level. +* `#13985 <https://github.com/numpy/numpy/pull/13985>`__: DOC: show workaround for backward compatibility +* `#13988 <https://github.com/numpy/numpy/pull/13988>`__: DOC: Add a call for contribution paragraph to the readme +* `#13989 <https://github.com/numpy/numpy/pull/13989>`__: BUG: Missing warnings import in polyutils +* `#13990 <https://github.com/numpy/numpy/pull/13990>`__: BUILD: adapt "make version-check" to "make dist" +* `#13991 <https://github.com/numpy/numpy/pull/13991>`__: DOC: emphasize need for matching numpy, git versions +* `#14002 <https://github.com/numpy/numpy/pull/14002>`__: TST, MAINT, BUG: expand OpenBLAS version checking +* `#14004 <https://github.com/numpy/numpy/pull/14004>`__: ENH: Chain exception for typed item assignment +* `#14005 <https://github.com/numpy/numpy/pull/14005>`__: MAINT: Fix spelling error in npy_tempita kwarg +* `#14010 <https://github.com/numpy/numpy/pull/14010>`__: DOC: Array API : Directory restructure and code cleanup +* `#14011 <https://github.com/numpy/numpy/pull/14011>`__: [DOC] Remove unused/deprecated functions +* `#14022 <https://github.com/numpy/numpy/pull/14022>`__: Update system_info.py +* `#14025 <https://github.com/numpy/numpy/pull/14025>`__: DOC:Link between the two indexing documentation pages +* `#14026 <https://github.com/numpy/numpy/pull/14026>`__: DOC: Update NumFOCUS subcommittee replacing Nathaniel with Sebastian +* `#14027 <https://github.com/numpy/numpy/pull/14027>`__: DOC: update "Contributing to NumPy" with more activities/roles +* `#14028 <https://github.com/numpy/numpy/pull/14028>`__: DOC: Improve quickstart documentation of new random Generator +* `#14030 <https://github.com/numpy/numpy/pull/14030>`__: DEP: Speed up WarnOnWrite deprecation in buffer interface +* `#14032 <https://github.com/numpy/numpy/pull/14032>`__: NEP: numpy.org website redesign +* `#14035 <https://github.com/numpy/numpy/pull/14035>`__: DOC: Fix docstring of numpy.allclose regarding NaNs +* `#14036 <https://github.com/numpy/numpy/pull/14036>`__: DEP: Raise warnings for deprecated functions PyArray_As1D, PyArray_As2D +* `#14039 <https://github.com/numpy/numpy/pull/14039>`__: DEP: Remove np.rank which has been deprecated for more than 5... +* `#14048 <https://github.com/numpy/numpy/pull/14048>`__: BUG, TEST: Adding validation test suite to validate float32 exp +* `#14051 <https://github.com/numpy/numpy/pull/14051>`__: ENH,DEP: Allow multiple axes in expand_dims +* `#14053 <https://github.com/numpy/numpy/pull/14053>`__: ENH: add pyproject.toml +* `#14060 <https://github.com/numpy/numpy/pull/14060>`__: DOC: Update cversions.py links and wording +* `#14062 <https://github.com/numpy/numpy/pull/14062>`__: DOC, BUILD: cleanups and fix (again) 'make dist' +* `#14063 <https://github.com/numpy/numpy/pull/14063>`__: BUG: Fix file-like object check when saving arrays +* `#14064 <https://github.com/numpy/numpy/pull/14064>`__: DOC: Resolve bad references in Sphinx warnings +* `#14068 <https://github.com/numpy/numpy/pull/14068>`__: MAINT: bump ARMv8 / POWER8 OpenBLAS in CI +* `#14069 <https://github.com/numpy/numpy/pull/14069>`__: DOC: Emphasize the need to run tests when building from source +* `#14070 <https://github.com/numpy/numpy/pull/14070>`__: DOC:Add example to clarify "numpy.save" behavior on already open... +* `#14072 <https://github.com/numpy/numpy/pull/14072>`__: DEP: Deprecate full and economic modes for linalg.qr +* `#14073 <https://github.com/numpy/numpy/pull/14073>`__: DOC: Doc release +* `#14074 <https://github.com/numpy/numpy/pull/14074>`__: BUG: fix build issue on icc 2016 +* `#14076 <https://github.com/numpy/numpy/pull/14076>`__: TST: Add 3.8-dev to travisCI testing. +* `#14085 <https://github.com/numpy/numpy/pull/14085>`__: DOC: Add blank line above doctest for intersect1d +* `#14086 <https://github.com/numpy/numpy/pull/14086>`__: ENH: Propose standard policy for dropping support of old Python... +* `#14089 <https://github.com/numpy/numpy/pull/14089>`__: DOC: Use `pip install .` where possible instead of calling setup.py +* `#14091 <https://github.com/numpy/numpy/pull/14091>`__: MAINT: adjustments to test_ufunc_noncontigous +* `#14092 <https://github.com/numpy/numpy/pull/14092>`__: MAINT: Improve NEP template +* `#14096 <https://github.com/numpy/numpy/pull/14096>`__: DOC: fix documentation of i and j for tri. +* `#14097 <https://github.com/numpy/numpy/pull/14097>`__: MAINT: Lazy import testing on python >=3.7 +* `#14100 <https://github.com/numpy/numpy/pull/14100>`__: DEP: Deprecate PyArray_FromDimsAndDataAndDescr, PyArray_FromDims +* `#14101 <https://github.com/numpy/numpy/pull/14101>`__: MAINT: Clearer error message while padding with stat_length=0 +* `#14106 <https://github.com/numpy/numpy/pull/14106>`__: MAINT: remove duplicate variable assignments +* `#14108 <https://github.com/numpy/numpy/pull/14108>`__: BUG: initialize variable that is passed by pointer +* `#14110 <https://github.com/numpy/numpy/pull/14110>`__: DOC: fix typo in c-api/array.rst doc +* `#14121 <https://github.com/numpy/numpy/pull/14121>`__: BUG: Add gcd/lcm definitions to npy_math.h +* `#14122 <https://github.com/numpy/numpy/pull/14122>`__: MAINT: Mark umath accuracy test xfail. +* `#14124 <https://github.com/numpy/numpy/pull/14124>`__: MAINT: Use equality instead of identity check with literal +* `#14130 <https://github.com/numpy/numpy/pull/14130>`__: MAINT: Fix small typo in quickstart docs +* `#14134 <https://github.com/numpy/numpy/pull/14134>`__: DOC, MAINT: Update master after 1.17.0 release. +* `#14141 <https://github.com/numpy/numpy/pull/14141>`__: ENH: add c-imported modules for freeze analysis in np.random +* `#14143 <https://github.com/numpy/numpy/pull/14143>`__: BUG: Fix DeprecationWarning in python 3.8 +* `#14144 <https://github.com/numpy/numpy/pull/14144>`__: BUG: Remove stray print that causes a SystemError on python 3.7... +* `#14145 <https://github.com/numpy/numpy/pull/14145>`__: BUG: Remove the broken clip wrapper +* `#14152 <https://github.com/numpy/numpy/pull/14152>`__: BUG: avx2_scalef_ps must be static +* `#14153 <https://github.com/numpy/numpy/pull/14153>`__: TST: Allow fuss in testing strided/non-strided exp/log loops +* `#14170 <https://github.com/numpy/numpy/pull/14170>`__: NEP: Proposal for __duckarray__ protocol +* `#14171 <https://github.com/numpy/numpy/pull/14171>`__: BUG: Make advanced indexing result on read-only subclass writeable +* `#14178 <https://github.com/numpy/numpy/pull/14178>`__: TST: Clean up of test_pocketfft.py +* `#14181 <https://github.com/numpy/numpy/pull/14181>`__: DEP: Deprecate np.alen +* `#14185 <https://github.com/numpy/numpy/pull/14185>`__: MAINT: Workaround for Intel compiler bug leading to failing test +* `#14190 <https://github.com/numpy/numpy/pull/14190>`__: DOC: Fix hermitian argument docs in `svd` +* `#14195 <https://github.com/numpy/numpy/pull/14195>`__: MAINT: Fix a docstring typo. +* `#14196 <https://github.com/numpy/numpy/pull/14196>`__: DOC: Fix links in `/.github/CONTRIBUTING.md`. +* `#14197 <https://github.com/numpy/numpy/pull/14197>`__: ENH: Multivariate normal speedups +* `#14203 <https://github.com/numpy/numpy/pull/14203>`__: MAINT: Improve mismatch message of np.testing.assert_array_equal +* `#14204 <https://github.com/numpy/numpy/pull/14204>`__: DOC,MAINT: Move towncrier files and fixup categories +* `#14207 <https://github.com/numpy/numpy/pull/14207>`__: BUG: Fixed default BitGenerator name +* `#14209 <https://github.com/numpy/numpy/pull/14209>`__: BUG: Fix uint-overflow if padding with linear_ramp and negative... +* `#14216 <https://github.com/numpy/numpy/pull/14216>`__: ENH: Enable huge pages in all Linux builds +* `#14217 <https://github.com/numpy/numpy/pull/14217>`__: BUG: Fix leak in the f2py-generated module init and `PyMem_Del`... +* `#14219 <https://github.com/numpy/numpy/pull/14219>`__: DOC: new nan_to_num keywords are from 1.17 onwards +* `#14223 <https://github.com/numpy/numpy/pull/14223>`__: TST: Add tests for deprecated C functions (PyArray_As1D, PyArray_As1D) +* `#14224 <https://github.com/numpy/numpy/pull/14224>`__: DOC: mention `take_along_axis` in `choose` +* `#14227 <https://github.com/numpy/numpy/pull/14227>`__: ENH: Parse complex number from string +* `#14231 <https://github.com/numpy/numpy/pull/14231>`__: DOC: update or remove outdated sourceforge links +* `#14234 <https://github.com/numpy/numpy/pull/14234>`__: MAINT: Better error message for norm +* `#14235 <https://github.com/numpy/numpy/pull/14235>`__: DOC: add backlinks to numpy.org +* `#14240 <https://github.com/numpy/numpy/pull/14240>`__: BUG: Don't fail when lexsorting some empty arrays. +* `#14241 <https://github.com/numpy/numpy/pull/14241>`__: BUG: Fix segfault in `random.permutation(x)` when x is a string. +* `#14245 <https://github.com/numpy/numpy/pull/14245>`__: Doc: fix a typo in NEP21 +* `#14249 <https://github.com/numpy/numpy/pull/14249>`__: DOC: set status of NEP 28 (website redesign) to Accepted +* `#14250 <https://github.com/numpy/numpy/pull/14250>`__: BLD: MAINT: change default behavior of build flag appending. +* `#14252 <https://github.com/numpy/numpy/pull/14252>`__: BUG: Fixes StopIteration error from 'np.genfromtext' for empty... +* `#14255 <https://github.com/numpy/numpy/pull/14255>`__: BUG: fix inconsistent axes ordering for axis in function `unique` +* `#14256 <https://github.com/numpy/numpy/pull/14256>`__: DEP: Deprecate load/dump functions in favour of pickle methods +* `#14257 <https://github.com/numpy/numpy/pull/14257>`__: MAINT: Update NEP-30 +* `#14259 <https://github.com/numpy/numpy/pull/14259>`__: DEP: Deprecate arrayprint formatting functions +* `#14266 <https://github.com/numpy/numpy/pull/14266>`__: DOC: remove scipy.org from the breadcrumb formattiong +* `#14270 <https://github.com/numpy/numpy/pull/14270>`__: BUG: Fix formatting error in exception message +* `#14272 <https://github.com/numpy/numpy/pull/14272>`__: DOC: Address typos in dispatch docs +* `#14279 <https://github.com/numpy/numpy/pull/14279>`__: BUG: Fix ZeroDivisionError for zero length arrays in pocketfft. +* `#14290 <https://github.com/numpy/numpy/pull/14290>`__: BUG: Fix misuse of .names and .fields in various places +* `#14291 <https://github.com/numpy/numpy/pull/14291>`__: TST, BUG: Use python3.6-dbg. +* `#14295 <https://github.com/numpy/numpy/pull/14295>`__: BUG: core: Handle large negative np.int64 args in binary_repr. +* `#14298 <https://github.com/numpy/numpy/pull/14298>`__: BUG: Fix numpy.random bug in platform detection +* `#14303 <https://github.com/numpy/numpy/pull/14303>`__: MAINT: random: Match type of SeedSequence.pool_size to DEFAULT_POOL_SIZE. +* `#14310 <https://github.com/numpy/numpy/pull/14310>`__: Bug: Fix behavior of structured_to_unstructured on non-trivial... +* `#14311 <https://github.com/numpy/numpy/pull/14311>`__: DOC: add two commas, move one word +* `#14313 <https://github.com/numpy/numpy/pull/14313>`__: DOC: Clarify rules about broadcasting when empty arrays are involved. +* `#14321 <https://github.com/numpy/numpy/pull/14321>`__: TST, MAINT: bump to OpenBLAS 0.3.7 stable +* `#14325 <https://github.com/numpy/numpy/pull/14325>`__: DEP: numpy.testing.rand +* `#14335 <https://github.com/numpy/numpy/pull/14335>`__: DEP: Deprecate class `SafeEval` +* `#14341 <https://github.com/numpy/numpy/pull/14341>`__: BUG: revert detecting and raising error on ragged arrays +* `#14342 <https://github.com/numpy/numpy/pull/14342>`__: DOC: Improve documentation of `isscalar`. +* `#14349 <https://github.com/numpy/numpy/pull/14349>`__: MAINT: Fix bloated mismatch error percentage in array comparisons. +* `#14351 <https://github.com/numpy/numpy/pull/14351>`__: DOC: Fix a minor typo in dispatch documentation. +* `#14352 <https://github.com/numpy/numpy/pull/14352>`__: MAINT: Remove redundant deprecation checks +* `#14353 <https://github.com/numpy/numpy/pull/14353>`__: MAINT: polynomial: Add an N-d vander implementation used under... +* `#14355 <https://github.com/numpy/numpy/pull/14355>`__: DOC: clarify that PytestTester is non-public +* `#14356 <https://github.com/numpy/numpy/pull/14356>`__: DOC: support and require sphinx>=2.2 +* `#14360 <https://github.com/numpy/numpy/pull/14360>`__: DOC: random: fix doc linking, was referencing private submodules. +* `#14364 <https://github.com/numpy/numpy/pull/14364>`__: MAINT: Fixes for prospective Python 3.10 and 4.0 +* `#14365 <https://github.com/numpy/numpy/pull/14365>`__: DOC: lib: Add more explanation of the weighted average calculation. +* `#14368 <https://github.com/numpy/numpy/pull/14368>`__: MAINT: Avoid BytesWarning in PyArray_DescrConverter() +* `#14369 <https://github.com/numpy/numpy/pull/14369>`__: MAINT: Post NumPy 1.17.1 update. +* `#14370 <https://github.com/numpy/numpy/pull/14370>`__: DOC: Fixed dtype docs for var, nanvar. +* `#14372 <https://github.com/numpy/numpy/pull/14372>`__: DOC: Document project as Python 3 only with a trove classifier +* `#14378 <https://github.com/numpy/numpy/pull/14378>`__: BUILD: move all test dependencies to ./test_requirements.txt +* `#14381 <https://github.com/numpy/numpy/pull/14381>`__: BUG: lib: Fix histogram problem with signed integer arrays. +* `#14385 <https://github.com/numpy/numpy/pull/14385>`__: REL: Update master after NumPy 1.16.5 release. +* `#14387 <https://github.com/numpy/numpy/pull/14387>`__: BUG: test, fix regression in converting to ctypes +* `#14389 <https://github.com/numpy/numpy/pull/14389>`__: NEP: Add initial draft of NEP-31: Context-local and global overrides... +* `#14390 <https://github.com/numpy/numpy/pull/14390>`__: DOC: document numpy/doc update process +* `#14392 <https://github.com/numpy/numpy/pull/14392>`__: DOC: update np.around docstring with note about floating-point... +* `#14393 <https://github.com/numpy/numpy/pull/14393>`__: BUG: view with fieldless dtype should raise if itemsize != 0 +* `#14395 <https://github.com/numpy/numpy/pull/14395>`__: DOC: fix issue with __new__ usage in subclassing doc. +* `#14398 <https://github.com/numpy/numpy/pull/14398>`__: DOC: Fix release notes table of contents +* `#14399 <https://github.com/numpy/numpy/pull/14399>`__: NEP 32: Remove the financial functions from NumPy +* `#14404 <https://github.com/numpy/numpy/pull/14404>`__: BLD: Update RELEASE_WALKTHROUGH and cythonize. +* `#14407 <https://github.com/numpy/numpy/pull/14407>`__: Bump pytest from 5.1.1 to 5.1.2 +* `#14408 <https://github.com/numpy/numpy/pull/14408>`__: TST: Remove build job since we now use Dependabot +* `#14410 <https://github.com/numpy/numpy/pull/14410>`__: BLD: Only allow using Cython module when cythonizing. +* `#14411 <https://github.com/numpy/numpy/pull/14411>`__: TST: Add dependabot config file. +* `#14416 <https://github.com/numpy/numpy/pull/14416>`__: BUG: Fix format statement associated with AttributeError. +* `#14417 <https://github.com/numpy/numpy/pull/14417>`__: BUG: Fix aradixsort indirect indexing. +* `#14426 <https://github.com/numpy/numpy/pull/14426>`__: DOC: add the reference to 'printoptions' +* `#14429 <https://github.com/numpy/numpy/pull/14429>`__: BUG: Do not show Override module in private error classes. +* `#14444 <https://github.com/numpy/numpy/pull/14444>`__: DOC: Make implementation bullet points consistent in NEP 29 +* `#14447 <https://github.com/numpy/numpy/pull/14447>`__: MAINT: Clarify policy language in NEP-29. +* `#14448 <https://github.com/numpy/numpy/pull/14448>`__: REL: Update master after 1.17.2 release. +* `#14452 <https://github.com/numpy/numpy/pull/14452>`__: MAINT: clean up pocketfft modules inside numpy.fft namespace +* `#14453 <https://github.com/numpy/numpy/pull/14453>`__: BLD: remove generated Cython files from sdist +* `#14454 <https://github.com/numpy/numpy/pull/14454>`__: MAINT: add test to prevent new public-looking modules being added +* `#14458 <https://github.com/numpy/numpy/pull/14458>`__: BUG: random.hypergeometic assumes npy_long is npy_int64, hangs... +* `#14459 <https://github.com/numpy/numpy/pull/14459>`__: ENH: Print the amount of memory that would be used by a failed... +* `#14460 <https://github.com/numpy/numpy/pull/14460>`__: MAINT: use test_requirements.txt in tox and shippable, ship it... +* `#14464 <https://github.com/numpy/numpy/pull/14464>`__: BUG: add a specialized loop for boolean matmul +* `#14469 <https://github.com/numpy/numpy/pull/14469>`__: BUG: Fix _ctypes class circular reference. (#13808) +* `#14472 <https://github.com/numpy/numpy/pull/14472>`__: BUG: core: Fix the str function of the rational dtype. +* `#14475 <https://github.com/numpy/numpy/pull/14475>`__: DOC: add timedelta64 signature +* `#14477 <https://github.com/numpy/numpy/pull/14477>`__: MAINT: Extract raising of MemoryError to a helper function +* `#14483 <https://github.com/numpy/numpy/pull/14483>`__: BUG,MAINT: Some fixes and minor cleanup based on clang analysis +* `#14484 <https://github.com/numpy/numpy/pull/14484>`__: MAINT: Add `NPY_UNUSED` and `const` qualified suggested by clang +* `#14485 <https://github.com/numpy/numpy/pull/14485>`__: MAINT: Silence integer comparison build warnings in assert statements +* `#14486 <https://github.com/numpy/numpy/pull/14486>`__: MAINT: distutils: Add newline at the end of printed warnings. +* `#14490 <https://github.com/numpy/numpy/pull/14490>`__: BUG: random: Revert gh-14458 and refix gh-14557. +* `#14493 <https://github.com/numpy/numpy/pull/14493>`__: DOC: Fix reference NPY_ARRAY_OWNDATA instead of NPY_OWNDATA. +* `#14495 <https://github.com/numpy/numpy/pull/14495>`__: ENH: Allow NPY_PKG_CONFIG_PATH environment variable override +* `#14498 <https://github.com/numpy/numpy/pull/14498>`__: MAINT: remove the entropy c-extension module +* `#14499 <https://github.com/numpy/numpy/pull/14499>`__: DOC: Add backslashes so PyUFunc_FromFuncAndDataAndSignatureAndIdentity... +* `#14500 <https://github.com/numpy/numpy/pull/14500>`__: DOC: Fix a minor typo in changelog readme +* `#14501 <https://github.com/numpy/numpy/pull/14501>`__: BUG: Fix randint when range is 2**32 +* `#14503 <https://github.com/numpy/numpy/pull/14503>`__: DOC: tweak np.round docstring to clarify floating-point error +* `#14508 <https://github.com/numpy/numpy/pull/14508>`__: DOC: Add warning to NPV function +* `#14510 <https://github.com/numpy/numpy/pull/14510>`__: API: Do not return None from recfunctions.drop_fields +* `#14511 <https://github.com/numpy/numpy/pull/14511>`__: BUG: Fix flatten_dtype so that nested 0-field structs are flattened... +* `#14514 <https://github.com/numpy/numpy/pull/14514>`__: DOC: Build release notes during CircleCI step +* `#14518 <https://github.com/numpy/numpy/pull/14518>`__: BUILD: Hide platform configuration probe behind --debug-configure +* `#14520 <https://github.com/numpy/numpy/pull/14520>`__: Mention that split() returns views into the original array +* `#14521 <https://github.com/numpy/numpy/pull/14521>`__: MAINT: Simplify lookfor function +* `#14523 <https://github.com/numpy/numpy/pull/14523>`__: MAINT: random: Remove a few duplicated C function prototypes. +* `#14525 <https://github.com/numpy/numpy/pull/14525>`__: BUILD, MAINT: run tests with verbose for PyPY, also do not leak... +* `#14526 <https://github.com/numpy/numpy/pull/14526>`__: BUG: fix release snippet failures caught only after merging +* `#14527 <https://github.com/numpy/numpy/pull/14527>`__: BLD: add warn-error option, adds -Werror to compiler +* `#14531 <https://github.com/numpy/numpy/pull/14531>`__: BUG: random: Create a legacy implementation of random.binomial. +* `#14534 <https://github.com/numpy/numpy/pull/14534>`__: MAINT: remove unused functions, rearrange headers (from CC=clang) +* `#14535 <https://github.com/numpy/numpy/pull/14535>`__: DOC: Fix a bit of code in 'Beyond the Basics' C API user guide. +* `#14536 <https://github.com/numpy/numpy/pull/14536>`__: MAINT: Cleanup old_defines in DOC +* `#14540 <https://github.com/numpy/numpy/pull/14540>`__: DOC: Added missing versionadded to diff(prepend) +* `#14543 <https://github.com/numpy/numpy/pull/14543>`__: BUG: Avoid ctypes in Generators +* `#14545 <https://github.com/numpy/numpy/pull/14545>`__: Changing ImportWarning to DeprecationWarning +* `#14548 <https://github.com/numpy/numpy/pull/14548>`__: MAINT: handle case where GIT_VERSION is empty string +* `#14554 <https://github.com/numpy/numpy/pull/14554>`__: MAINT: core: Remove duplicated inner loop ee->e from log, exp,... +* `#14555 <https://github.com/numpy/numpy/pull/14555>`__: DOC: clarify input types in basics.io.genfromtxt.rst +* `#14557 <https://github.com/numpy/numpy/pull/14557>`__: DOC: remove note about Pocketfft license file (non-existing here). +* `#14558 <https://github.com/numpy/numpy/pull/14558>`__: DOC: Fix code that generates the table in the 'Casting Rules'... +* `#14562 <https://github.com/numpy/numpy/pull/14562>`__: MAINT: don't install partial numpy.random C/Cython API. +* `#14564 <https://github.com/numpy/numpy/pull/14564>`__: TST: ensure coercion tables aren't printed on failing public... +* `#14567 <https://github.com/numpy/numpy/pull/14567>`__: DEP: remove deprecated (and private) numpy.testing submodules. +* `#14568 <https://github.com/numpy/numpy/pull/14568>`__: BLD, DOC: fix gh-14518, add release note +* `#14570 <https://github.com/numpy/numpy/pull/14570>`__: BUG: importing build_src breaks setuptools monkeypatch for msvc14 +* `#14572 <https://github.com/numpy/numpy/pull/14572>`__: DOC: Note runtests.py `-- -s` method to use pytests `-s` +* `#14573 <https://github.com/numpy/numpy/pull/14573>`__: DOC: update submodule docstrings, remove info.py files +* `#14576 <https://github.com/numpy/numpy/pull/14576>`__: DOC: Document the NPY_SCALARKIND values as C variables. +* `#14582 <https://github.com/numpy/numpy/pull/14582>`__: MAINT: Bump pytest from 5.1.2 to 5.1.3 +* `#14583 <https://github.com/numpy/numpy/pull/14583>`__: DEP: remove deprecated select behaviour +* `#14585 <https://github.com/numpy/numpy/pull/14585>`__: BUG: Add missing check for 0-sized array in ravel_multi_index +* `#14586 <https://github.com/numpy/numpy/pull/14586>`__: BUG: dtype refcount cleanups +* `#14587 <https://github.com/numpy/numpy/pull/14587>`__: DOC: Fix a minor typo in changelog entry +* `#14592 <https://github.com/numpy/numpy/pull/14592>`__: MAINT: Fix typo: remoge → remove +* `#14595 <https://github.com/numpy/numpy/pull/14595>`__: DOC: Change the promotion table checkmark to 'Y'. +* `#14596 <https://github.com/numpy/numpy/pull/14596>`__: DEP: Complete deprecation of invalid array/memory order +* `#14598 <https://github.com/numpy/numpy/pull/14598>`__: DOC: Add to doc that interp cannot contain NaN +* `#14600 <https://github.com/numpy/numpy/pull/14600>`__: NEP: Accept NEP 32. +* `#14601 <https://github.com/numpy/numpy/pull/14601>`__: NEP: Fix discrepancies in NEPs +* `#14603 <https://github.com/numpy/numpy/pull/14603>`__: NEP: Only list "Active" NEPs under "Meta-NEPs" +* `#14604 <https://github.com/numpy/numpy/pull/14604>`__: API: restructure and document numpy.random C-API +* `#14605 <https://github.com/numpy/numpy/pull/14605>`__: BUG: properly define PyArray_DescrCheck{,Exact} +* `#14607 <https://github.com/numpy/numpy/pull/14607>`__: MAINT: Remove duplicate files from .gitignore +* `#14608 <https://github.com/numpy/numpy/pull/14608>`__: API: rearrange the cython files in numpy.random +* `#14614 <https://github.com/numpy/numpy/pull/14614>`__: MAINT: Bump pytest from 5.1.3 to 5.2.0 +* `#14615 <https://github.com/numpy/numpy/pull/14615>`__: MAINT: Add "MAINT" tag to dependabot commit msg +* `#14616 <https://github.com/numpy/numpy/pull/14616>`__: DOC: Updated sphinx directive formatting +* `#14620 <https://github.com/numpy/numpy/pull/14620>`__: DEP: Finish deprecation of non-integer `num` in linspace +* `#14621 <https://github.com/numpy/numpy/pull/14621>`__: DOC: s/OR/AND/ in np.logical_and docstring +* `#14623 <https://github.com/numpy/numpy/pull/14623>`__: DOC: misleading np.sinc() documentation +* `#14629 <https://github.com/numpy/numpy/pull/14629>`__: DOC: clarify residual in np.polyfit +* `#14630 <https://github.com/numpy/numpy/pull/14630>`__: BUILD: change to build_src --verbose-cfg, runtests.py --debug-info +* `#14631 <https://github.com/numpy/numpy/pull/14631>`__: BUG: always free clean_sep +* `#14634 <https://github.com/numpy/numpy/pull/14634>`__: DOC: Create `class Extension` docstring and add it to documentation. +* `#14636 <https://github.com/numpy/numpy/pull/14636>`__: DOC: add `printoptions` as a context manager to `set_printoptions` +* `#14639 <https://github.com/numpy/numpy/pull/14639>`__: DOC: Fix typo in NEP 29 +* `#14643 <https://github.com/numpy/numpy/pull/14643>`__: MAINT: Use scalar math power function directly +* `#14649 <https://github.com/numpy/numpy/pull/14649>`__: DOC: Add IPython to dependencies needed to build docs. +* `#14652 <https://github.com/numpy/numpy/pull/14652>`__: MAINT: Bump pytest-cov from 2.7.1 to 2.8.1 +* `#14653 <https://github.com/numpy/numpy/pull/14653>`__: MAINT: Bump pytest from 5.2.0 to 5.2.1 +* `#14654 <https://github.com/numpy/numpy/pull/14654>`__: MAINT: Bump pytz from 2019.2 to 2019.3 +* `#14656 <https://github.com/numpy/numpy/pull/14656>`__: MAINT: Use `extract_unit` throughout datetime +* `#14657 <https://github.com/numpy/numpy/pull/14657>`__: BUG: fix fromfile behavior when reading sub-array dtypes +* `#14662 <https://github.com/numpy/numpy/pull/14662>`__: BUG: random: Use correct length when axis is given to shuffle. +* `#14669 <https://github.com/numpy/numpy/pull/14669>`__: BUG: Do not rely on undefined behaviour to cast from float to... +* `#14674 <https://github.com/numpy/numpy/pull/14674>`__: NEP: add default-dtype-object-deprecation nep 34 +* `#14681 <https://github.com/numpy/numpy/pull/14681>`__: MAINT: Remove unused boolean negative/subtract loops +* `#14682 <https://github.com/numpy/numpy/pull/14682>`__: DEP: ufunc `out` argument must be a tuple for multiple outputs +* `#14693 <https://github.com/numpy/numpy/pull/14693>`__: BUG: Fix `np.einsum` errors on Power9 Linux and z/Linux +* `#14696 <https://github.com/numpy/numpy/pull/14696>`__: DOC: Note release notes process changes on devdocs start page +* `#14699 <https://github.com/numpy/numpy/pull/14699>`__: Doc warnings +* `#14705 <https://github.com/numpy/numpy/pull/14705>`__: DOC: Switch Markdown link to RST in NEP 29 +* `#14709 <https://github.com/numpy/numpy/pull/14709>`__: TST: Divide Azure CI Pipelines into stages. +* `#14710 <https://github.com/numpy/numpy/pull/14710>`__: DEP: Finish the out kwarg deprecation for ufunc calls +* `#14711 <https://github.com/numpy/numpy/pull/14711>`__: DOC: Removing mentions of appveyor +* `#14714 <https://github.com/numpy/numpy/pull/14714>`__: BUG: Default start to 0 for timedelta arange +* `#14717 <https://github.com/numpy/numpy/pull/14717>`__: API: NaT (arg)min/max behavior +* `#14718 <https://github.com/numpy/numpy/pull/14718>`__: API: Forbid Q<->m safe casting +* `#14720 <https://github.com/numpy/numpy/pull/14720>`__: DEP: deprecate financial functions. +* `#14721 <https://github.com/numpy/numpy/pull/14721>`__: DOC: Move newsfragment to correct folder +* `#14723 <https://github.com/numpy/numpy/pull/14723>`__: DOC: cleaning up examples in maskedarray.generic +* `#14725 <https://github.com/numpy/numpy/pull/14725>`__: MAINT: umath: Change error message for unsupported bool subtraction. +* `#14730 <https://github.com/numpy/numpy/pull/14730>`__: ENH: Add complex number support for fromfile +* `#14732 <https://github.com/numpy/numpy/pull/14732>`__: TST: run refguide-check on rst files in doc/* +* `#14734 <https://github.com/numpy/numpy/pull/14734>`__: DOC: Edit NEP procedure for better discussion +* `#14736 <https://github.com/numpy/numpy/pull/14736>`__: DOC: Post 1.17.3 release update. +* `#14737 <https://github.com/numpy/numpy/pull/14737>`__: NEP: Accept NEP 29 as final +* `#14738 <https://github.com/numpy/numpy/pull/14738>`__: BUG: Don't narrow intp to int when producing error messages +* `#14742 <https://github.com/numpy/numpy/pull/14742>`__: DOC: lib: Fix deprecation markup in financial function docstrings. +* `#14743 <https://github.com/numpy/numpy/pull/14743>`__: DOC: Change from HTTP to HTTPS +* `#14745 <https://github.com/numpy/numpy/pull/14745>`__: BUG: clear only attribute errors in get_attr_string.h::maybe_get_attr +* `#14762 <https://github.com/numpy/numpy/pull/14762>`__: MAINT: doc: Remove doc/newdtype_example/ +* `#14763 <https://github.com/numpy/numpy/pull/14763>`__: Reword cautionary note about dtype.descr +* `#14769 <https://github.com/numpy/numpy/pull/14769>`__: BUG: fix integer size confusion in handling array's ndmin argument +* `#14771 <https://github.com/numpy/numpy/pull/14771>`__: TST, BUILD: add a gcc 4.8 run on ubuntu 18.04 +* `#14775 <https://github.com/numpy/numpy/pull/14775>`__: Update CLASSIFIERS with python 3.8 support +* `#14777 <https://github.com/numpy/numpy/pull/14777>`__: BUG: random: biased samples from integers() with 8 or 16 bit... +* `#14782 <https://github.com/numpy/numpy/pull/14782>`__: DOC: Add release note about changed random variate stream from... +* `#14786 <https://github.com/numpy/numpy/pull/14786>`__: DOC: Make changes to NEP procedure +* `#14790 <https://github.com/numpy/numpy/pull/14790>`__: DOC: random: Remove redundant 'See Also' entry in 'uniform' docstring. +* `#14791 <https://github.com/numpy/numpy/pull/14791>`__: MAINT: Minor typo fix +* `#14792 <https://github.com/numpy/numpy/pull/14792>`__: MAINT: Bump pytest from 5.2.1 to 5.2.2 +* `#14793 <https://github.com/numpy/numpy/pull/14793>`__: DOC: Adjust NEP-31 to new template. +* `#14794 <https://github.com/numpy/numpy/pull/14794>`__: DEP: issue deprecation warning when creating ragged array (NEP... +* `#14798 <https://github.com/numpy/numpy/pull/14798>`__: NEP: move 'NEP 29 random' from Accepted to Final +* `#14799 <https://github.com/numpy/numpy/pull/14799>`__: DOC: Add take_along_axis to the see also section in argmin, argmax... +* `#14800 <https://github.com/numpy/numpy/pull/14800>`__: ENH: change object-array comparisons to prefer OO->O unfuncs +* `#14805 <https://github.com/numpy/numpy/pull/14805>`__: TST: Don't construct Fraction instances from numpy scalars +* `#14814 <https://github.com/numpy/numpy/pull/14814>`__: Rename helper functions to not use the word rank +* `#14820 <https://github.com/numpy/numpy/pull/14820>`__: MAINT: Use templating to merge float loops +* `#14826 <https://github.com/numpy/numpy/pull/14826>`__: BUILD: ignore more build.log warnings +* `#14827 <https://github.com/numpy/numpy/pull/14827>`__: BLD: Prevent -flto from optimising long double representation... +* `#14829 <https://github.com/numpy/numpy/pull/14829>`__: BUG: raise ValueError for empty arrays passed to _pyarray_correlate +* `#14830 <https://github.com/numpy/numpy/pull/14830>`__: MAINT: move buffer.h -> npy_buffer.h to avoid conflicts +* `#14836 <https://github.com/numpy/numpy/pull/14836>`__: MAINT: Bump cython from 0.29.13 to 0.29.14 +* `#14841 <https://github.com/numpy/numpy/pull/14841>`__: ENH: add isinf, isnan, fmin, fmax loops for datetime64, timedelta64 +* `#14842 <https://github.com/numpy/numpy/pull/14842>`__: BLD: add 'apt update' to shippable +* `#14845 <https://github.com/numpy/numpy/pull/14845>`__: MAINT: revert gh-14800, which gave precedence to OO->O over OO->? +* `#14874 <https://github.com/numpy/numpy/pull/14874>`__: REL: Update master after 1.17.4 release. +* `#14878 <https://github.com/numpy/numpy/pull/14878>`__: BUILD: remove SSE2 flag from numpy.random builds +* `#14879 <https://github.com/numpy/numpy/pull/14879>`__: DOC: Update NEP29 with Python3.8 informations. +* `#14881 <https://github.com/numpy/numpy/pull/14881>`__: BUG: Remove builtins from __all__ +* `#14898 <https://github.com/numpy/numpy/pull/14898>`__: MAINT: Delete and ignore generated files +* `#14899 <https://github.com/numpy/numpy/pull/14899>`__: Update FUNDING.yml +* `#14901 <https://github.com/numpy/numpy/pull/14901>`__: MAINT: Remove uses of scalar aliases +* `#14903 <https://github.com/numpy/numpy/pull/14903>`__: NEP: move nep 34 to accepted +* `#14907 <https://github.com/numpy/numpy/pull/14907>`__: TST: Add s390x to the TravisCI test matrix. +* `#14912 <https://github.com/numpy/numpy/pull/14912>`__: DOC: Note FFT type promotion +* `#14914 <https://github.com/numpy/numpy/pull/14914>`__: TST: Test with Python3.8 on Windows. +* `#14915 <https://github.com/numpy/numpy/pull/14915>`__: TST: Update travis.yml +* `#14921 <https://github.com/numpy/numpy/pull/14921>`__: TST: add no_tracing decorator to refcount-sensitive codepath... +* `#14926 <https://github.com/numpy/numpy/pull/14926>`__: MAINT: Bump pytest from 5.2.2 to 5.2.4 +* `#14929 <https://github.com/numpy/numpy/pull/14929>`__: BUG: Fix step returned by linspace when num=1 and endpoint=False +* `#14932 <https://github.com/numpy/numpy/pull/14932>`__: DOC: Compare 'tolist' function to 'list' in example +* `#14935 <https://github.com/numpy/numpy/pull/14935>`__: DOC: Clarify return type for default_rng +* `#14944 <https://github.com/numpy/numpy/pull/14944>`__: MAINT: move numpy/random/examples -> numpy/random/_examples +* `#14947 <https://github.com/numpy/numpy/pull/14947>`__: DOC: testing: Note handling of scalars in assert_array_equal... +* `#14948 <https://github.com/numpy/numpy/pull/14948>`__: DOC, API: add random.__init__.pxd and document random.* functions +* `#14951 <https://github.com/numpy/numpy/pull/14951>`__: DOC: Clean up examples of low-level random access +* `#14954 <https://github.com/numpy/numpy/pull/14954>`__: TST. API: test using distributions.h via cffi +* `#14962 <https://github.com/numpy/numpy/pull/14962>`__: TST: skip if cython is not available +* `#14967 <https://github.com/numpy/numpy/pull/14967>`__: MAINT: Cleaned up mintypecode for Py3 +* `#14973 <https://github.com/numpy/numpy/pull/14973>`__: DOC: fix docstring of np.linalg.norm +* `#14974 <https://github.com/numpy/numpy/pull/14974>`__: MAINT: Added Python3.8 branch to dll lib discovery on Windows +* `#14976 <https://github.com/numpy/numpy/pull/14976>`__: DEV: update asv.conf.json +* `#14978 <https://github.com/numpy/numpy/pull/14978>`__: MAINT: Bump pytest from 5.2.4 to 5.3.0 +* `#14982 <https://github.com/numpy/numpy/pull/14982>`__: MAINT: Fix typos +* `#14983 <https://github.com/numpy/numpy/pull/14983>`__: REV: "ENH: Improved performance of PyArray_FromAny for sequences... +* `#14994 <https://github.com/numpy/numpy/pull/14994>`__: BUG: warn when saving dtype with metadata +* `#14996 <https://github.com/numpy/numpy/pull/14996>`__: DEP: Deprecate the axis argument to masked_rows and masked_cols +* `#15004 <https://github.com/numpy/numpy/pull/15004>`__: MAINT: Fix long name of PCG64 +* `#15007 <https://github.com/numpy/numpy/pull/15007>`__: DOC, API: improve the C-API/Cython documentation and interfaces... +* `#15009 <https://github.com/numpy/numpy/pull/15009>`__: DOC: Fix typo in numpy.loadtxt and numpy.genfromtxt documentation +* `#15012 <https://github.com/numpy/numpy/pull/15012>`__: ENH: allow using symbol-suffixed 64-bit BLAS/LAPACK for numpy.dot... +* `#15014 <https://github.com/numpy/numpy/pull/15014>`__: DOC: add a more useful comment to compat.py3k.py +* `#15019 <https://github.com/numpy/numpy/pull/15019>`__: DOC: lib: Use a clearer example of ddof in the notes of the cov... +* `#15021 <https://github.com/numpy/numpy/pull/15021>`__: TST: machinery for tests requiring large memory + lapack64 smoketest +* `#15023 <https://github.com/numpy/numpy/pull/15023>`__: MAINT: Only copy input array in _replace_nan() if there are nans... +* `#15025 <https://github.com/numpy/numpy/pull/15025>`__: MAINT: Bump pytest from 5.3.0 to 5.3.1 +* `#15027 <https://github.com/numpy/numpy/pull/15027>`__: REV: "ENH: Improved performance of PyArray_FromAny for sequences... +* `#15031 <https://github.com/numpy/numpy/pull/15031>`__: REL: Prepare for 1.18 branch +* `#15032 <https://github.com/numpy/numpy/pull/15032>`__: MAINT: Cleaned up mintypecode for Py3 (pt. 2) +* `#15036 <https://github.com/numpy/numpy/pull/15036>`__: BUG: Fix refcounting in ufunc object loops +* `#15039 <https://github.com/numpy/numpy/pull/15039>`__: BUG: Exceptions tracebacks are dropped +* `#15053 <https://github.com/numpy/numpy/pull/15053>`__: REV: Revert "Merge pull request #14794 from mattip/nep-0034-impl" +* `#15058 <https://github.com/numpy/numpy/pull/15058>`__: API, DOC: change names to multivariate_hypergeometric, improve docs +* `#15059 <https://github.com/numpy/numpy/pull/15059>`__: REL: Prepare for NumPy 1.18.0 release. +* `#15109 <https://github.com/numpy/numpy/pull/15109>`__: TST: Check requires_memory immediately before the test +* `#15111 <https://github.com/numpy/numpy/pull/15111>`__: ENH: Add support to sort timedelta64 `NaT` to end of the array +* `#15112 <https://github.com/numpy/numpy/pull/15112>`__: MAINT: follow-up cleanup for blas64 PR +* `#15113 <https://github.com/numpy/numpy/pull/15113>`__: ENH: f2py: add --f2cmap option for specifying the name of .f2py_f2cmap +* `#15114 <https://github.com/numpy/numpy/pull/15114>`__: ENH: add support for ILP64 OpenBLAS (without symbol suffix) +* `#15146 <https://github.com/numpy/numpy/pull/15146>`__: REL: Prepare for 1.18.0 release. diff --git a/doc/changelog/1.18.1-changelog.rst b/doc/changelog/1.18.1-changelog.rst new file mode 100644 index 000000000..d3df29198 --- /dev/null +++ b/doc/changelog/1.18.1-changelog.rst @@ -0,0 +1,33 @@ + +Contributors +============ + +A total of 7 people contributed to this release. People with a "+" by their +names contributed a patch for the first time. + +* Charles Harris +* Matti Picus +* Maxwell Aladago +* Pauli Virtanen +* Ralf Gommers +* Tyler Reddy +* Warren Weckesser + +Pull requests merged +==================== + +A total of 13 pull requests were merged for this release. + +* `#15158 <https://github.com/numpy/numpy/pull/15158>`__: MAINT: Update pavement.py for towncrier. +* `#15159 <https://github.com/numpy/numpy/pull/15159>`__: DOC: add moved modules to 1.18 release note +* `#15161 <https://github.com/numpy/numpy/pull/15161>`__: MAINT, DOC: Minor backports and updates for 1.18.x +* `#15176 <https://github.com/numpy/numpy/pull/15176>`__: TST: Add assert_array_equal test for big integer arrays +* `#15184 <https://github.com/numpy/numpy/pull/15184>`__: BUG: use tmp dir and check version for cython test (#15170) +* `#15220 <https://github.com/numpy/numpy/pull/15220>`__: BUG: distutils: fix msvc+gfortran openblas handling corner case +* `#15221 <https://github.com/numpy/numpy/pull/15221>`__: BUG: remove -std=c99 for c++ compilation (#15194) +* `#15222 <https://github.com/numpy/numpy/pull/15222>`__: MAINT: unskip test on win32 +* `#15223 <https://github.com/numpy/numpy/pull/15223>`__: TST: add BLAS ILP64 run in Travis & Azure +* `#15245 <https://github.com/numpy/numpy/pull/15245>`__: MAINT: only add --std=c99 where needed +* `#15246 <https://github.com/numpy/numpy/pull/15246>`__: BUG: lib: Fix handling of integer arrays by gradient. +* `#15247 <https://github.com/numpy/numpy/pull/15247>`__: MAINT: Do not use private Python function in testing +* `#15250 <https://github.com/numpy/numpy/pull/15250>`__: REL: Prepare for the NumPy 1.18.1 release. diff --git a/doc/example.py b/doc/example.py index 8a5f9948f..5e3d79807 100644 --- a/doc/example.py +++ b/doc/example.py @@ -8,8 +8,6 @@ extend over multiple lines, the closing three quotation marks must be on a line by itself, preferably preceded by a blank line. """ -from __future__ import division, absolute_import, print_function - import os # standard library imports first # Do NOT import using *, e.g. from numpy import * diff --git a/doc/neps/nep-0023-backwards-compatibility.rst b/doc/neps/nep-0023-backwards-compatibility.rst index 158b08f1f..92974ad6e 100644 --- a/doc/neps/nep-0023-backwards-compatibility.rst +++ b/doc/neps/nep-0023-backwards-compatibility.rst @@ -198,7 +198,7 @@ Policy can be made, *provided the benefit is worth the cost* and suitable deprecation warnings have been raised first. 3. Deprecation warnings are in all cases warnings that functionality will be removed. - If there is no intent to remove functionlity, then deprecation in documentation + If there is no intent to remove functionality, then deprecation in documentation only or other types of warnings shall be used. 4. Deprecations for stylistic reasons (e.g. consistency between functions) are strongly discouraged. diff --git a/doc/neps/nep-0028-website-redesign.rst b/doc/neps/nep-0028-website-redesign.rst index b418ca831..dcd182d55 100644 --- a/doc/neps/nep-0028-website-redesign.rst +++ b/doc/neps/nep-0028-website-redesign.rst @@ -166,7 +166,7 @@ have a similar deployment to GitHub Pages or Netlify. Analytics ~~~~~~~~~ -It's benefical to maintainers to know how many visitors are coming to +It's beneficial to maintainers to know how many visitors are coming to numpy.org. Google Analytics offers visitor counts and locations. This will help to support and deploy more strategically, and help maintainers understand where traffic is coming from. diff --git a/doc/neps/nep-0037-array-module.rst b/doc/neps/nep-0037-array-module.rst new file mode 100644 index 000000000..387356490 --- /dev/null +++ b/doc/neps/nep-0037-array-module.rst @@ -0,0 +1,550 @@ +=================================================== +NEP 37 — A dispatch protocol for NumPy-like modules +=================================================== + +:Author: Stephan Hoyer <shoyer@google.com> +:Author: Hameer Abbasi +:Author: Sebastian Berg +:Status: Draft +:Type: Standards Track +:Created: 2019-12-29 + +Abstract +-------- + +NEP-18's ``__array_function__`` has been a mixed success. Some projects (e.g., +dask, CuPy, xarray, sparse, Pint) have enthusiastically adopted it. Others +(e.g., PyTorch, JAX, SciPy) have been more reluctant. Here we propose a new +protocol, ``__array_module__``, that we expect could eventually subsume most +use-cases for ``__array_function__``. The protocol requires explicit adoption +by both users and library authors, which ensures backwards compatibility, and +is also significantly simpler than ``__array_function__``, both of which we +expect will make it easier to adopt. + +Why ``__array_function__`` hasn't been enough +--------------------------------------------- + +There are two broad ways in which NEP-18 has fallen short of its goals: + +1. **Maintainability concerns**. `__array_function__` has significant + implications for libraries that use it: + + - Projects like `PyTorch + <https://github.com/pytorch/pytorch/issues/22402>`_, `JAX + <https://github.com/google/jax/issues/1565>`_ and even `scipy.sparse + <https://github.com/scipy/scipy/issues/10362>`_ have been reluctant to + implement `__array_function__` in part because they are concerned about + **breaking existing code**: users expect NumPy functions like + ``np.concatenate`` to return NumPy arrays. This is a fundamental + limitation of the ``__array_function__`` design, which we chose to allow + overriding the existing ``numpy`` namespace. + - ``__array_function__`` currently requires an "all or nothing" approach to + implementing NumPy's API. There is no good pathway for **incremental + adoption**, which is particularly problematic for established projects + for which adopting ``__array_function__`` would result in breaking + changes. + - It is no longer possible to use **aliases to NumPy functions** within + modules that support overrides. For example, both CuPy and JAX set + ``result_type = np.result_type``. + - Implementing **fall-back mechanisms** for unimplemented NumPy functions + by using NumPy's implementation is hard to get right (but see the + `version from dask <https://github.com/dask/dask/pull/5043>`_), because + ``__array_function__`` does not present a consistent interface. + Converting all arguments of array type requires recursing into generic + arguments of the form ``*args, **kwargs``. + +2. **Limitations on what can be overridden.** ``__array_function__`` has some + important gaps, most notably array creation and coercion functions: + + - **Array creation** routines (e.g., ``np.arange`` and those in + ``np.random``) need some other mechanism for indicating what type of + arrays to create. `NEP 36 <https://github.com/numpy/numpy/pull/14715>`_ + proposed adding optional ``like=`` arguments to functions without + existing array arguments. However, we still lack any mechanism to + override methods on objects, such as those needed by + ``np.random.RandomState``. + - **Array conversion** can't reuse the existing coercion functions like + ``np.asarray``, because ``np.asarray`` sometimes means "convert to an + exact ``np.ndarray``" and other times means "convert to something _like_ + a NumPy array." This led to the `NEP 30 + <https://numpy.org/neps/nep-0030-duck-array-protocol.html>`_ proposal for + a separate ``np.duckarray`` function, but this still does not resolve how + to cast one duck array into a type matching another duck array. + +``get_array_module`` and the ``__array_module__`` protocol +---------------------------------------------------------- + +We propose a new user-facing mechanism for dispatching to a duck-array +implementation, ``numpy.get_array_module``. ``get_array_module`` performs the +same type resolution as ``__array_function__`` and returns a module with an API +promised to match the standard interface of ``numpy`` that can implement +operations on all provided array types. + +The protocol itself is both simpler and more powerful than +``__array_function__``, because it doesn't need to worry about actually +implementing functions. We believe it resolves most of the maintainability and +functionality limitations of ``__array_function__``. + +The new protocol is opt-in, explicit and with local control; see +:ref:`appendix-design-choices` for discussion on the importance of these design +features. + +The array module contract +========================= + +Modules returned by ``get_array_module``/``__array_module__`` should make a +best effort to implement NumPy's core functionality on new array types(s). +Unimplemented functionality should simply be omitted (e.g., accessing an +unimplemented function should raise ``AttributeError``). In the future, we +anticipate codifying a protocol for requesting restricted subsets of ``numpy``; +see :ref:`requesting-restricted-subsets` for more details. + +How to use ``get_array_module`` +=============================== + +Code that wants to support generic duck arrays should explicitly call +``get_array_module`` to determine an appropriate array module from which to +call functions, rather than using the ``numpy`` namespace directly. For +example: + +.. code:: python + + # calls the appropriate version of np.something for x and y + module = np.get_array_module(x, y) + module.something(x, y) + +Both array creation and array conversion are supported, because dispatching is +handled by ``get_array_module`` rather than via the types of function +arguments. For example, to use random number generation functions or methods, +we can simply pull out the appropriate submodule: + +.. code:: python + + def duckarray_add_random(array): + module = np.get_array_module(array) + noise = module.random.randn(*array.shape) + return array + noise + +We can also write the duck-array ``stack`` function from `NEP 30 +<https://numpy.org/neps/nep-0030-duck-array-protocol.html>`_, without the need +for a new ``np.duckarray`` function: + +.. code:: python + + def duckarray_stack(arrays): + module = np.get_array_module(*arrays) + arrays = [module.asarray(arr) for arr in arrays] + shapes = {arr.shape for arr in arrays} + if len(shapes) != 1: + raise ValueError('all input arrays must have the same shape') + expanded_arrays = [arr[module.newaxis, ...] for arr in arrays] + return module.concatenate(expanded_arrays, axis=0) + +By default, ``get_array_module`` will return the ``numpy`` module if no +arguments are arrays. This fall-back can be explicitly controlled by providing +the ``module`` keyword-only argument. It is also possible to indicate that an +exception should be raised instead of returning a default array module by +setting ``module=None``. + +How to implement ``__array_module__`` +===================================== + +Libraries implementing a duck array type that want to support +``get_array_module`` need to implement the corresponding protocol, +``__array_module__``. This new protocol is based on Python's dispatch protocol +for arithmetic, and is essentially a simpler version of ``__array_function__``. + +Only one argument is passed into ``__array_module__``, a Python collection of +unique array types passed into ``get_array_module``, i.e., all arguments with +an ``__array_module__`` attribute. + +The special method should either return an namespace with an API matching +``numpy``, or ``NotImplemented``, indicating that it does not know how to +handle the operation: + +.. code:: python + + class MyArray: + def __array_module__(self, types): + if not all(issubclass(t, MyArray) for t in types): + return NotImplemented + return my_array_module + +Returning custom objects from ``__array_module__`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``my_array_module`` will typically, but need not always, be a Python module. +Returning a custom objects (e.g., with functions implemented via +``__getattr__``) may be useful for some advanced use cases. + +For example, custom objects could allow for partial implementations of duck +array modules that fall-back to NumPy (although this is not recommended in +general because such fall-back behavior can be error prone): + +.. code:: python + + class MyArray: + def __array_module__(self, types): + if all(issubclass(t, MyArray) for t in types): + return ArrayModule() + else: + return NotImplemented + + class ArrayModule: + def __getattr__(self, name): + import base_module + return getattr(base_module, name, getattr(numpy, name)) + +Subclassing from ``numpy.ndarray`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +All of the same guidance about well-defined type casting hierarchies from +NEP-18 still applies. ``numpy.ndarray`` itself contains a matching +implementation of ``__array_module__``, which is convenient for subclasses: + +.. code:: python + + class ndarray: + def __array_module__(self, types): + if all(issubclass(t, ndarray) for t in types): + return numpy + else: + return NotImplemented + +NumPy's internal machinery +========================== + +The type resolution rules of ``get_array_module`` follow the same model as +Python and NumPy's existing dispatch protocols: subclasses are called before +super-classes, and otherwise left to right. ``__array_module__`` is guaranteed +to be called only a single time on each unique type. + +The actual implementation of `get_array_module` will be in C, but should be +equivalent to this Python code: + +.. code:: python + + def get_array_module(*arrays, default=numpy): + implementing_arrays, types = _implementing_arrays_and_types(arrays) + if not implementing_arrays and default is not None: + return default + for array in implementing_arrays: + module = array.__array_module__(types) + if module is not NotImplemented: + return module + raise TypeError("no common array module found") + + def _implementing_arrays_and_types(relevant_arrays): + types = [] + implementing_arrays = [] + for array in relevant_arrays: + t = type(array) + if t not in types and hasattr(t, '__array_module__'): + types.append(t) + # Subclasses before superclasses, otherwise left to right + index = len(implementing_arrays) + for i, old_array in enumerate(implementing_arrays): + if issubclass(t, type(old_array)): + index = i + break + implementing_arrays.insert(index, array) + return implementing_arrays, types + +Relationship with ``__array_ufunc__`` and ``__array_function__`` +---------------------------------------------------------------- + +These older protocols have distinct use-cases and should remain +=============================================================== + +``__array_module__`` is intended to resolve limitations of +``__array_function__``, so it is natural to consider whether it could entirely +replace ``__array_function__``. This would offer dual benefits: (1) simplifying +the user-story about how to override NumPy and (2) removing the slowdown +associated with checking for dispatch when calling every NumPy function. + +However, ``__array_module__`` and ``__array_function__`` are pretty different +from a user perspective: it requires explicit calls to ``get_array_function``, +rather than simply reusing original ``numpy`` functions. This is probably fine +for *libraries* that rely on duck-arrays, but may be frustratingly verbose for +interactive use. + +Some of the dispatching use-cases for ``__array_ufunc__`` are also solved by +``__array_module__``, but not all of them. For example, it is still useful to +be able to define non-NumPy ufuncs (e.g., from Numba or SciPy) in a generic way +on non-NumPy arrays (e.g., with dask.array). + +Given their existing adoption and distinct use cases, we don't think it makes +sense to remove or deprecate ``__array_function__`` and ``__array_ufunc__`` at +this time. + +Mixin classes to implement ``__array_function__`` and ``__array_ufunc__`` +========================================================================= + +Despite the user-facing differences, ``__array_module__`` and a module +implementing NumPy's API still contain sufficient functionality needed to +implement dispatching with the existing duck array protocols. + +For example, the following mixin classes would provide sensible defaults for +these special methods in terms of ``get_array_module`` and +``__array_module__``: + +.. code:: python + + class ArrayUfuncFromModuleMixin: + + def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + arrays = inputs + kwargs.get('out', ()) + try: + array_module = np.get_array_module(*arrays) + except TypeError: + return NotImplemented + + try: + # Note this may have false positive matches, if ufunc.__name__ + # matches the name of a ufunc defined by NumPy. Unfortunately + # there is no way to determine in which module a ufunc was + # defined. + new_ufunc = getattr(array_module, ufunc.__name__) + except AttributeError: + return NotImplemented + + try: + callable = getattr(new_ufunc, method) + except AttributeError: + return NotImplemented + + return callable(*inputs, **kwargs) + + class ArrayFunctionFromModuleMixin: + + def __array_function__(self, func, types, args, kwargs): + array_module = self.__array_module__(types) + if array_module is NotImplemented: + return NotImplemented + + # Traverse submodules to find the appropriate function + modules = func.__module__.split('.') + assert modules[0] == 'numpy' + for submodule in modules[1:]: + module = getattr(module, submodule, None) + new_func = getattr(module, func.__name__, None) + if new_func is None: + return NotImplemented + + return new_func(*args, **kwargs) + +To make it easier to write duck arrays, we could also add these mixin classes +into ``numpy.lib.mixins`` (but the examples above may suffice). + +Alternatives considered +----------------------- + +Naming +====== + +We like the name ``__array_module__`` because it mirrors the existing +``__array_function__`` and ``__array_ufunc__`` protocols. Another reasonable +choice could be ``__array_namespace__``. + +It is less clear what the NumPy function that calls this protocol should be +called (``get_array_module`` in this proposal). Some possible alternatives: +``array_module``, ``common_array_module``, ``resolve_array_module``, +``get_namespace``, ``get_numpy``, ``get_numpylike_module``, +``get_duck_array_module``. + +.. _requesting-restricted-subsets: + +Requesting restricted subsets of NumPy's API +============================================ + +Over time, NumPy has accumulated a very large API surface, with over 600 +attributes in the top level ``numpy`` module alone. It is unlikely that any +duck array library could or would want to implement all of these functions and +classes, because the frequently used subset of NumPy is much smaller. + +We think it would be useful exercise to define "minimal" subset(s) of NumPy's +API, omitting rarely used or non-recommended functionality. For example, +minimal NumPy might include ``stack``, but not the other stacking functions +``column_stack``, ``dstack``, ``hstack`` and ``vstack``. This could clearly +indicate to duck array authors and users want functionality is core and what +functionality they can skip. + +Support for requesting a restricted subset of NumPy's API would be a natural +feature to include in ``get_array_function`` and ``__array_module__``, e.g., + +.. code:: python + + # array_module is only guaranteed to contain "minimal" NumPy + array_module = np.get_array_module(*arrays, request='minimal') + +To facilitate testing with NumPy and use with any valid duck array library, +NumPy itself would return restricted versions of the ``numpy`` module when +``get_array_module`` is called only on NumPy arrays. Omitted functions would +simply not exist. + +Unfortunately, we have not yet figured out what these restricted subsets should +be, so it doesn't make sense to do this yet. When/if we do, we could either add +new keyword arguments to ``get_array_module`` or add new top level functions, +e.g., ``get_minimal_array_module``. We would also need to add either a new +protocol patterned off of ``__array_module__`` (e.g., +``__array_module_minimal__``), or could add an optional second argument to +``__array_module__`` (catching errors with ``try``/``except``). + +A new namespace for implicit dispatch +===================================== + +Instead of supporting overrides in the main `numpy` namespace with +``__array_function__``, we could create a new opt-in namespace, e.g., +``numpy.api``, with versions of NumPy functions that support dispatching. These +overrides would need new opt-in protocols, e.g., ``__array_function_api__`` +patterned off of ``__array_function__``. + +This would resolve the biggest limitations of ``__array_function__`` by being +opt-in and would also allow for unambiguously overriding functions like +``asarray``, because ``np.api.asarray`` would always mean "convert an +array-like object." But it wouldn't solve all the dispatching needs met by +``__array_module__``, and would leave us with supporting a considerably more +complex protocol both for array users and implementors. + +We could potentially implement such a new namespace *via* the +``__array_module__`` protocol. Certainly some users would find this convenient, +because it is slightly less boilerplate. But this would leave users with a +confusing choice: when should they use `get_array_module` vs. +`np.api.something`. Also, we would have to add and maintain a whole new module, +which is considerably more expensive than merely adding a function. + +Dispatching on both types and arrays instead of only types +========================================================== + +Instead of supporting dispatch only via unique array types, we could also +support dispatch via array objects, e.g., by passing an ``arrays`` argument as +part of the ``__array_module__`` protocol. This could potentially be useful for +dispatch for arrays with metadata, such provided by Dask and Pint, but would +impose costs in terms of type safety and complexity. + +For example, a library that supports arrays on both CPUs and GPUs might decide +on which device to create a new arrays from functions like ``ones`` based on +input arguments: + +.. code:: python + + class Array: + def __array_module__(self, types, arrays): + useful_arrays = tuple(a in arrays if isinstance(a, Array)) + if not useful_arrays: + return NotImplemented + prefer_gpu = any(a.prefer_gpu for a in useful_arrays) + return ArrayModule(prefer_gpu) + + class ArrayModule: + def __init__(self, prefer_gpu): + self.prefer_gpu = prefer_gpu + + def __getattr__(self, name): + import base_module + base_func = getattr(base_module, name) + return functools.partial(base_func, prefer_gpu=self.prefer_gpu) + +This might be useful, but it's not clear if we really need it. Pint seems to +get along OK without any explicit array creation routines (favoring +multiplication by units, e.g., ``np.ones(5) * ureg.m``), and for the most part +Dask is also OK with existing ``__array_function__`` style overides (e.g., +favoring ``np.ones_like`` over ``np.ones``). Choosing whether to place an array +on the CPU or GPU could be solved by `making array creation lazy +<https://github.com/google/jax/pull/1668>`_. + +.. _appendix-design-choices: + +Appendix: design choices for API overrides +------------------------------------------ + +There is a large range of possible design choices for overriding NumPy's API. +Here we discuss three major axes of the design decision that guided our design +for ``__array_module__``. + +Opt-in vs. opt-out for users +============================ + +The ``__array_ufunc__`` and ``__array_function__`` protocols provide a +mechanism for overriding NumPy functions *within NumPy's existing namespace*. +This means that users need to explicitly opt-out if they do not want any +overridden behavior, e.g., by casting arrays with ``np.asarray()``. + +In theory, this approach lowers the barrier for adopting these protocols in +user code and libraries, because code that uses the standard NumPy namespace is +automatically compatible. But in practice, this hasn't worked out. For example, +most well-maintained libraries that use NumPy follow the best practice of +casting all inputs with ``np.asarray()``, which they would have to explicitly +relax to use ``__array_function__``. Our experience has been that making a +library compatible with a new duck array type typically requires at least a +small amount of work to accommodate differences in the data model and operations +that can be implemented efficiently. + +These opt-out approaches also considerably complicate backwards compatibility +for libraries that adopt these protocols, because by opting in as a library +they also opt-in their users, whether they expect it or not. For winning over +libraries that have been unable to adopt ``__array_function__``, an opt-in +approach seems like a must. + +Explicit vs. implicit choice of implementation +============================================== + +Both ``__array_ufunc__`` and ``__array_function__`` have implicit control over +dispatching: the dispatched functions are determined via the appropriate +protocols in every function call. This generalizes well to handling many +different types of objects, as evidenced by its use for implementing arithmetic +operators in Python, but it has two downsides: + +1. *Speed*: it imposes additional overhead in every function call, because each + function call needs to inspect each of its arguments for overrides. This is + why arithmetic on builtin Python numbers is slow. +2. *Readability*: it is not longer immediately evident to readers of code what + happens when a function is called, because the function's implementation + could be overridden by any of its arguments. + +In contrast, importing a new library (e.g., ``import dask.array as da``) with +an API matching NumPy is entirely explicit. There is no overhead from dispatch +or ambiguity about which implementation is being used. + +Explicit and implicit choice of implementations are not mutually exclusive +options. Indeed, most implementations of NumPy API overrides via +``__array_function__`` that we are familiar with (namely, dask, CuPy and +sparse, but not Pint) also include an explicit way to use their version of +NumPy's API by importing a module directly (``dask.array``, ``cupy`` or +``sparse``, respectively). + +Local vs. non-local vs. global control +====================================== + +The final design axis is how users control the choice of API: + +- **Local control**, as exemplified by multiple dispatch and Python protocols for + arithmetic, determines which implementation to use either by checking types + or calling methods on the direct arguments of a function. +- **Non-local control** such as `np.errstate + <https://docs.scipy.org/doc/numpy/reference/generated/numpy.errstate.html>`_ + overrides behavior with global-state via function decorators or + context-managers. Control is determined hierarchically, via the inner-most + context. +- **Global control** provides a mechanism for users to set default behavior, + either via function calls or configuration files. For example, matplotlib + allows setting a global choice of plotting backend. + +Local control is generally considered a best practice for API design, because +control flow is entirely explicit, which makes it the easiest to understand. +Non-local and global control are occasionally used, but generally either due to +ignorance or a lack of better alternatives. + +In the case of duck typing for NumPy's public API, we think non-local or global +control would be mistakes, mostly because they **don't compose well**. If one +library sets/needs one set of overrides and then internally calls a routine +that expects another set of overrides, the resulting behavior may be very +surprising. Higher order functions are especially problematic, because the +context in which functions are evaluated may not be the context in which they +are defined. + +One class of override use cases where we think non-local and global control are +appropriate is for choosing a backend system that is guaranteed to have an +entirely consistent interface, such as a faster alternative implementation of +``numpy.fft`` on NumPy arrays. However, these are out of scope for the current +proposal, which is focused on duck arrays. diff --git a/doc/postprocess.py b/doc/postprocess.py index 2e50c115e..b6d067437 100755 --- a/doc/postprocess.py +++ b/doc/postprocess.py @@ -6,8 +6,6 @@ Post-processes HTML and Latex files output by Sphinx. MODE is either 'html' or 'tex'. """ -from __future__ import division, absolute_import, print_function - import re import optparse import io diff --git a/doc/release/upcoming_changes/14933.compatibility.rst b/doc/release/upcoming_changes/14933.compatibility.rst new file mode 100644 index 000000000..1b5f1b113 --- /dev/null +++ b/doc/release/upcoming_changes/14933.compatibility.rst @@ -0,0 +1,10 @@ +Scalar promotion in ``PyArray_ConvertToCommonType`` +--------------------------------------------------- + +The promotion of mixed scalars and arrays in ``PyArray_ConvertToCommonType`` +has been changed to adhere to those used by ``np.result_type``. +This means that input such as ``(1000, np.array([1], dtype=np.uint8)))`` +will now return ``uint16`` dtypes. In most cases the behaviour is unchanged. +Note that the use of this C-API function is generally discouraged. +This also fixes ``np.choose`` to behave the same way as the rest of NumPy +in this respect. diff --git a/doc/release/upcoming_changes/15106.new_feature.rst b/doc/release/upcoming_changes/15106.new_feature.rst deleted file mode 100644 index 9f1d0b247..000000000 --- a/doc/release/upcoming_changes/15106.new_feature.rst +++ /dev/null @@ -1,4 +0,0 @@ -Add ``--f2cmap`` option to F2PY -------------------------------- -Allow specifying a file to load Fortran-to-C type map -customizations from. diff --git a/doc/release/upcoming_changes/15218.improvement.rst b/doc/release/upcoming_changes/15218.improvement.rst new file mode 100644 index 000000000..ccbbbd66f --- /dev/null +++ b/doc/release/upcoming_changes/15218.improvement.rst @@ -0,0 +1,6 @@ +Use 64-bit integer size on 64-bit platforms in fallback lapack_lite +------------------------------------------------------------------- + +Use 64-bit integer size on 64-bit platforms in the fallback LAPACK library, +which is used when the system has no LAPACK installed, allowing it to deal with +linear algebra for large arrays. diff --git a/doc/release/upcoming_changes/15229.compatibility.rst b/doc/release/upcoming_changes/15229.compatibility.rst new file mode 100644 index 000000000..404f7774f --- /dev/null +++ b/doc/release/upcoming_changes/15229.compatibility.rst @@ -0,0 +1,7 @@ +Removed ``multiarray.int_asbuffer`` +----------------------------------- + +As part of the continued removal of Python 2 compatibility, +``multiarray.int_asbuffer`` was removed. On Python 3, it threw a +``NotImplementedError`` and was unused internally. It is expected that there +are no downstream use cases for this method with Python 3. diff --git a/doc/release/upcoming_changes/15233.highlight.rst b/doc/release/upcoming_changes/15233.highlight.rst new file mode 100644 index 000000000..df96ee871 --- /dev/null +++ b/doc/release/upcoming_changes/15233.highlight.rst @@ -0,0 +1,4 @@ +* Code compatibility with Python versions < 3.5 (including Python 2) was + dropped from both the python and C code. The shims in numpy.compat will + remain to support third-party packages, but they may be deprecated in a + future release. diff --git a/doc/release/upcoming_changes/15251.c_api.rst b/doc/release/upcoming_changes/15251.c_api.rst new file mode 100644 index 000000000..f391c904b --- /dev/null +++ b/doc/release/upcoming_changes/15251.c_api.rst @@ -0,0 +1,10 @@ +Better support for ``const`` dimensions in API functions +-------------------------------------------------------- +The following functions now accept a constant array of ``npy_intp``: + +* `PyArray_BroadcastToShape` +* `PyArray_IntTupleFromIntp` +* `PyArray_OverflowMultiplyList` + +Previously the caller would have to cast away the const-ness to call these +functions. diff --git a/doc/release/upcoming_changes/15255.compatibility.rst b/doc/release/upcoming_changes/15255.compatibility.rst new file mode 100644 index 000000000..e360eeeb3 --- /dev/null +++ b/doc/release/upcoming_changes/15255.compatibility.rst @@ -0,0 +1,12 @@ +``numpy.distutils.compat`` has been removed +------------------------------------------- +This module contained only the function ``get_exception()``, which was used as:: + + try: + ... + except Exception: + e = get_exception() + +Its purpose was to handle the change in syntax introduced in Python 2.6, from +``except Exception, e:`` to ``except Exception as e:``, meaning it was only +necessary for codebases supporting Python 2.5 and older. diff --git a/doc/source/conf.py b/doc/source/conf.py index 09770535b..7e3a145f5 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import division, absolute_import, print_function - import sys, os, re # Minimum version, enforced by sphinx diff --git a/doc/source/dev/development_workflow.rst b/doc/source/dev/development_workflow.rst index 900431374..9f2ecede6 100644 --- a/doc/source/dev/development_workflow.rst +++ b/doc/source/dev/development_workflow.rst @@ -303,7 +303,7 @@ Suppose that the commit history looks like this:: 2dec1ac Fix a few bugs + disable 13d7934 First implementation 6ad92e5 * masked is now an instance of a new object, MaskedConstant - 29001ed Add pre-nep for a copule of structured_array_extensions. + 29001ed Add pre-nep for a couple of structured_array_extensions. ... and ``6ad92e5`` is the last commit in the ``master`` branch. Suppose we diff --git a/doc/source/dev/governance/governance.rst b/doc/source/dev/governance/governance.rst index 54e52363c..d8719700f 100644 --- a/doc/source/dev/governance/governance.rst +++ b/doc/source/dev/governance/governance.rst @@ -301,7 +301,7 @@ its interactions with NumFOCUS. or technical direction of the Project. - This Subcommittee will have 5 members, 4 of whom will be current Council Members and 1 of whom will be external to the Steering - Council. No more than 2 Subcommitee Members can report to one person + Council. No more than 2 Subcommittee Members can report to one person through employment or contracting work (including the reportee, i.e. the reportee + 1 is the max). This avoids effective majorities resting on one person. diff --git a/doc/source/dev/index.rst b/doc/source/dev/index.rst index 306c15069..1aacc2c49 100644 --- a/doc/source/dev/index.rst +++ b/doc/source/dev/index.rst @@ -231,6 +231,22 @@ Requirements `Sphinx <http://www.sphinx-doc.org/en/stable/>`__ is needed to build the documentation. Matplotlib, SciPy, and IPython are also required. +The numpy documentation also depends on the +`numpydoc <https://numpydoc.readthedocs.io/en/latest/>`__ sphinx extension +as well as an external sphinx theme. +These extensions are included as git submodules and must be initialized +before building the docs. +From the ``doc/`` directory: + +.. code:: sh + + git submodule update --init + +The documentation includes mathematical formulae with LaTeX formatting. +A working LaTeX document production system +(e.g. `texlive <https://www.tug.org/texlive/>`__) is required for the +proper rendering of the LaTeX math in the documentation. + Fixing Warnings ~~~~~~~~~~~~~~~ diff --git a/doc/source/f2py/setup_example.py b/doc/source/f2py/setup_example.py index 54af77299..479acc004 100644 --- a/doc/source/f2py/setup_example.py +++ b/doc/source/f2py/setup_example.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.distutils.core import Extension ext1 = Extension(name = 'scalar', diff --git a/doc/source/reference/arrays.ndarray.rst b/doc/source/reference/arrays.ndarray.rst index 831d211bc..47692c8b4 100644 --- a/doc/source/reference/arrays.ndarray.rst +++ b/doc/source/reference/arrays.ndarray.rst @@ -512,10 +512,6 @@ Arithmetic: - Any third argument to :func:`pow()` is silently ignored, as the underlying :func:`ufunc <power>` takes only two arguments. - - The three division operators are all defined; :obj:`div` is active - by default, :obj:`truediv` is active when - :obj:`__future__` division is in effect. - - Because :class:`ndarray` is a built-in type (written in C), the ``__r{op}__`` special methods are not directly defined. diff --git a/doc/source/reference/c-api/array.rst b/doc/source/reference/c-api/array.rst index 0530a5747..2eaf3a27a 100644 --- a/doc/source/reference/c-api/array.rst +++ b/doc/source/reference/c-api/array.rst @@ -1255,14 +1255,18 @@ Converting data types Convert a sequence of Python objects contained in *op* to an array of ndarrays each having the same data type. The type is selected - based on the typenumber (larger type number is chosen over a - smaller one) ignoring objects that are only scalars. The length of - the sequence is returned in *n*, and an *n* -length array of - :c:type:`PyArrayObject` pointers is the return value (or ``NULL`` if an - error occurs). The returned array must be freed by the caller of - this routine (using :c:func:`PyDataMem_FREE` ) and all the array objects - in it ``DECREF`` 'd or a memory-leak will occur. The example - template-code below shows a typically usage: + in the same way as `PyArray_ResultType`. The length of the sequence is + returned in *n*, and an *n* -length array of :c:type:`PyArrayObject` + pointers is the return value (or ``NULL`` if an error occurs). + The returned array must be freed by the caller of this routine + (using :c:func:`PyDataMem_FREE` ) and all the array objects in it + ``DECREF`` 'd or a memory-leak will occur. The example template-code + below shows a typically usage: + + .. versionchanged:: 1.18.0 + A mix of scalars and zero-dimensional arrays now produces a type + capable of holding the scalar value. + Previously priority was given to the dtype of the arrays. .. code-block:: c @@ -2510,7 +2514,7 @@ this useful approach to looping over an array. stride and that axis will be used. .. c:function:: PyObject *PyArray_BroadcastToShape( \ - PyObject* arr, npy_intp *dimensions, int nd) + PyObject* arr, npy_intp const *dimensions, int nd) Return an array iterator that is broadcast to iterate as an array of the shape provided by *dimensions* and *nd*. diff --git a/doc/source/reference/c-api/coremath.rst b/doc/source/reference/c-api/coremath.rst index 7e00322f9..4200f4ba8 100644 --- a/doc/source/reference/c-api/coremath.rst +++ b/doc/source/reference/c-api/coremath.rst @@ -193,7 +193,7 @@ Those can be useful for precise floating point comparison. .. c:function:: int npy_get_floatstatus_barrier(char*) Get floating point status. A pointer to a local variable is passed in to - prevent aggressive compiler optimizations from reodering this function call + prevent aggressive compiler optimizations from reordering this function call relative to the code setting the status, which could lead to incorrect results. @@ -219,7 +219,7 @@ Those can be useful for precise floating point comparison. .. c:function:: int npy_clear_floatstatus_barrier(char*) Clears the floating point status. A pointer to a local variable is passed in to - prevent aggressive compiler optimizations from reodering this function call. + prevent aggressive compiler optimizations from reordering this function call. Returns the previous status mask. .. versionadded:: 1.15.0 diff --git a/doc/source/reference/random/extending.rst b/doc/source/reference/random/extending.rst index 4adb90c06..52063ae5b 100644 --- a/doc/source/reference/random/extending.rst +++ b/doc/source/reference/random/extending.rst @@ -41,8 +41,8 @@ wrap around, providing array alignment information -- still apply. :language: cython :end-before: example 2 -The BitGenerator can also be directly accessed using the members of the basic -RNG structure. +The BitGenerator can also be directly accessed using the members of the ``bitgen_t`` +struct. .. literalinclude:: ../../../../numpy/random/_examples/cython/extending_distributions.pyx :language: cython @@ -70,9 +70,9 @@ directly from the ``_generator`` shared object, using the `BitGenerator.cffi` in :start-after: dlopen -New Basic RNGs -============== -`~Generator` can be used with other user-provided BitGenerators. The simplest +New Bit Generators +================== +`~Generator` can be used with user-provided `~BitGenerator`\ s. The simplest way to write a new BitGenerator is to examine the pyx file of one of the existing BitGenerators. The key structure that must be provided is the ``capsule`` which contains a ``PyCapsule`` to a struct pointer of type diff --git a/doc/source/reference/random/legacy.rst b/doc/source/reference/random/legacy.rst index 922d76a9a..91b91dac8 100644 --- a/doc/source/reference/random/legacy.rst +++ b/doc/source/reference/random/legacy.rst @@ -125,7 +125,7 @@ Distributions Functions in `numpy.random` =========================== Many of the RandomState methods above are exported as functions in -`numpy.random` This usage is discouraged, as it is implemented via a gloabl +`numpy.random` This usage is discouraged, as it is implemented via a global `RandomState` instance which is not advised on two counts: - It uses global state, which means results will change as the code changes diff --git a/doc/source/reference/random/multithreading.rst b/doc/source/reference/random/multithreading.rst index a0a31d0ea..8502429ce 100644 --- a/doc/source/reference/random/multithreading.rst +++ b/doc/source/reference/random/multithreading.rst @@ -24,7 +24,7 @@ seed will produce the same outputs. import concurrent.futures import numpy as np - class MultithreadedRNG(object): + class MultithreadedRNG: def __init__(self, n, seed=None, threads=None): rg = PCG64(seed) if threads is None: diff --git a/doc/source/reference/routines.linalg.rst b/doc/source/reference/routines.linalg.rst index d42e77ad8..86e168b26 100644 --- a/doc/source/reference/routines.linalg.rst +++ b/doc/source/reference/routines.linalg.rst @@ -18,6 +18,18 @@ or specify the processor architecture. .. _OpenBLAS: https://www.openblas.net/ .. _threadpoolctl: https://github.com/joblib/threadpoolctl +The SciPy library also contains a `~scipy.linalg` submodule, and there is +overlap in the functionality provided by the SciPy and NumPy submodules. SciPy +contains functions not found in `numpy.linalg`, such as functions related to +LU decomposition and the Schur decomposition, multiple ways of calculating the +pseudoinverse, and matrix transcendentals such as the matrix logarithm. Some +functions that exist in both have augmented functionality in `scipy.linalg`. +For example, `scipy.linalg.eig` can take a second matrix argument for solving +generalized eigenvalue problems. Some functions in NumPy, however, have more +flexible broadcasting options. For example, `numpy.linalg.solve` can handle +"stacked" arrays, while `scipy.linalg.solve` accepts only a single square +array as its first argument. + .. currentmodule:: numpy Matrix and vector products diff --git a/doc/source/reference/ufuncs.rst b/doc/source/reference/ufuncs.rst index 361cf11b9..20c89e0b3 100644 --- a/doc/source/reference/ufuncs.rst +++ b/doc/source/reference/ufuncs.rst @@ -569,6 +569,7 @@ Math operations add subtract multiply + matmul divide logaddexp logaddexp2 @@ -577,6 +578,7 @@ Math operations negative positive power + float_power remainder mod fmod @@ -635,6 +637,8 @@ The ratio of degrees to radians is :math:`180^{\circ}/\pi.` arcsinh arccosh arctanh + degrees + radians deg2rad rad2deg diff --git a/doc/source/release.rst b/doc/source/release.rst index 26373ad07..7d12bae41 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -6,12 +6,15 @@ Release Notes :maxdepth: 3 1.19.0 <release/1.19.0-notes> + 1.18.1 <release/1.18.1-notes> 1.18.0 <release/1.18.0-notes> + 1.17.5 <release/1.17.5-notes> 1.17.4 <release/1.17.4-notes> 1.17.3 <release/1.17.3-notes> 1.17.2 <release/1.17.2-notes> 1.17.1 <release/1.17.1-notes> 1.17.0 <release/1.17.0-notes> + 1.16.6 <release/1.16.6-notes> 1.16.5 <release/1.16.5-notes> 1.16.4 <release/1.16.4-notes> 1.16.3 <release/1.16.3-notes> diff --git a/doc/source/release/1.16.6-notes.rst b/doc/source/release/1.16.6-notes.rst new file mode 100644 index 000000000..0aeba3cd3 --- /dev/null +++ b/doc/source/release/1.16.6-notes.rst @@ -0,0 +1,87 @@ +.. currentmodule:: numpy + +========================== +NumPy 1.16.6 Release Notes +========================== + +The NumPy 1.16.6 release fixes bugs reported against the 1.16.5 release, and +also backports several enhancements from master that seem appropriate for a +release series that is the last to support Python 2.7. The wheels on PyPI are +linked with OpenBLAS v0.3.7, which should fix errors on Skylake series +cpus. + +Downstream developers building this release should use Cython >= 0.29.2 and, if +using OpenBLAS, OpenBLAS >= v0.3.7. The supported Python versions are 2.7 and +3.5-3.7. + +Highlights +========== + +- The ``np.testing.utils`` functions have been updated from 1.19.0-dev0. + This improves the function documentation and error messages as well + extending the ``assert_array_compare`` function to additional types. + + +New functions +============= + +Allow matmul (`@` operator) to work with object arrays. +------------------------------------------------------- +This is an enhancement that was added in NumPy 1.17 and seems reasonable to +include in the LTS 1.16 release series. + + +Compatibility notes +=================== + +Fix regression in matmul (`@` operator) for boolean types +--------------------------------------------------------- +Booleans were being treated as integers rather than booleans, +which was a regression from previous behavior. + + +Improvements +============ + +Array comparison assertions include maximum differences +------------------------------------------------------- +Error messages from array comparison tests such as ``testing.assert_allclose`` +now include "max absolute difference" and "max relative difference," in +addition to the previous "mismatch" percentage. This information makes it +easier to update absolute and relative error tolerances. + +Contributors +============ + +A total of 10 people contributed to this release. + +* CakeWithSteak +* Charles Harris +* Chris Burr +* Eric Wieser +* Fernando Saravia +* Lars Grueter +* Matti Picus +* Maxwell Aladago +* Qiming Sun +* Warren Weckesser + +Pull requests merged +==================== + +A total of 14 pull requests were merged for this release. + +* `#14211 <https://github.com/numpy/numpy/pull/14211>`__: BUG: Fix uint-overflow if padding with linear_ramp and negative... +* `#14275 <https://github.com/numpy/numpy/pull/14275>`__: BUG: fixing to allow unpickling of PY3 pickles from PY2 +* `#14340 <https://github.com/numpy/numpy/pull/14340>`__: BUG: Fix misuse of .names and .fields in various places (backport... +* `#14423 <https://github.com/numpy/numpy/pull/14423>`__: BUG: test, fix regression in converting to ctypes. +* `#14434 <https://github.com/numpy/numpy/pull/14434>`__: BUG: Fixed maximum relative error reporting in assert_allclose +* `#14509 <https://github.com/numpy/numpy/pull/14509>`__: BUG: Fix regression in boolean matmul. +* `#14686 <https://github.com/numpy/numpy/pull/14686>`__: BUG: properly define PyArray_DescrCheck +* `#14853 <https://github.com/numpy/numpy/pull/14853>`__: BLD: add 'apt update' to shippable +* `#14854 <https://github.com/numpy/numpy/pull/14854>`__: BUG: Fix _ctypes class circular reference. (#13808) +* `#14856 <https://github.com/numpy/numpy/pull/14856>`__: BUG: Fix `np.einsum` errors on Power9 Linux and z/Linux +* `#14863 <https://github.com/numpy/numpy/pull/14863>`__: BLD: Prevent -flto from optimising long double representation... +* `#14864 <https://github.com/numpy/numpy/pull/14864>`__: BUG: lib: Fix histogram problem with signed integer arrays. +* `#15172 <https://github.com/numpy/numpy/pull/15172>`__: ENH: Backport improvements to testing functions. +* `#15191 <https://github.com/numpy/numpy/pull/15191>`__: REL: Prepare for 1.16.6 release. diff --git a/doc/source/release/1.17.0-notes.rst b/doc/source/release/1.17.0-notes.rst index a0e737982..a93eb2186 100644 --- a/doc/source/release/1.17.0-notes.rst +++ b/doc/source/release/1.17.0-notes.rst @@ -297,7 +297,7 @@ mergesort. Due to the need to maintain backward compatibility, the sorting ``kind`` options ``"stable"`` and ``"mergesort"`` have been made aliases of each other with the actual sort implementation depending on the array type. Radix sort is used for small integer types of 16 bits or less and timsort for -the remaining types. Timsort features improved performace on data containing +the remaining types. Timsort features improved performance on data containing already or nearly sorted data and performs like mergesort on random data and requires :math:`O(n/2)` working space. Details of the timsort algorithm can be found at `CPython listsort.txt diff --git a/doc/source/release/1.17.5-notes.rst b/doc/source/release/1.17.5-notes.rst new file mode 100644 index 000000000..0f1d3e1a5 --- /dev/null +++ b/doc/source/release/1.17.5-notes.rst @@ -0,0 +1,45 @@ +.. currentmodule:: numpy + +========================== +NumPy 1.17.5 Release Notes +========================== + +This release contains fixes for bugs reported against NumPy 1.17.4 along with +some build improvements. The Python versions supported in this release +are 3.5-3.8. + +Downstream developers should use Cython >= 0.29.14 for Python 3.8 support and +OpenBLAS >= 3.7 to avoid errors on the Skylake architecture. + +It is recommended that developers interested in the new random bit generators +upgrade to the NumPy 1.18.x series, as it has updated documentation and +many small improvements. + + +Contributors +============ + +A total of 6 people contributed to this release. People with a "+" by their +names contributed a patch for the first time. + +* Charles Harris +* Eric Wieser +* Ilhan Polat +* Matti Picus +* Michael Hudson-Doyle +* Ralf Gommers + + +Pull requests merged +==================== + +A total of 8 pull requests were merged for this release. + +* `#14593 <https://github.com/numpy/numpy/pull/14593>`__: MAINT: backport Cython API cleanup to 1.17.x, remove docs +* `#14937 <https://github.com/numpy/numpy/pull/14937>`__: BUG: fix integer size confusion in handling array's ndmin argument +* `#14939 <https://github.com/numpy/numpy/pull/14939>`__: BUILD: remove SSE2 flag from numpy.random builds +* `#14993 <https://github.com/numpy/numpy/pull/14993>`__: MAINT: Added Python3.8 branch to dll lib discovery +* `#15038 <https://github.com/numpy/numpy/pull/15038>`__: BUG: Fix refcounting in ufunc object loops +* `#15067 <https://github.com/numpy/numpy/pull/15067>`__: BUG: Exceptions tracebacks are dropped +* `#15175 <https://github.com/numpy/numpy/pull/15175>`__: ENH: Backport improvements to testing functions. +* `#15213 <https://github.com/numpy/numpy/pull/15213>`__: REL: Prepare for the NumPy 1.17.5 release. diff --git a/doc/source/release/1.18.0-notes.rst b/doc/source/release/1.18.0-notes.rst index dc0c4ce84..d59f985cd 100644 --- a/doc/source/release/1.18.0-notes.rst +++ b/doc/source/release/1.18.0-notes.rst @@ -1,14 +1,33 @@ -================================ -NumPy NumPy 1.18.0 Release Notes -================================ +.. currentmodule:: numpy + +========================== +NumPy 1.18.0 Release Notes +========================== + +In addition to the usual bug fixes, this NumPy release cleans up and documents +the new random C-API, expires a large number of old deprecations, and improves +the appearance of the documentation. The Python versions supported are 3.5-3.8. +This is the last NumPy release series that will support Python 3.5. + +Downstream developers should use Cython >= 0.29.14 for Python 3.8 support and +OpenBLAS >= 3.7 to avoid problems on the Skylake +architecture. + + +Highlights +========== + +* The C-API for ``numpy.random`` has been defined and documented. +* Basic infrastructure for linking with 64 bit BLAS and LAPACK libraries. +* Many documentation improvements. New functions ============= -Multivariate hypergeometric distribution added to `numpy.random` ----------------------------------------------------------------- -The method `multivariate_hypergeometric` has been added to the class +Multivariate hypergeometric distribution added to ``numpy.random`` +------------------------------------------------------------------ +The method ``multivariate_hypergeometric`` has been added to the class `numpy.random.Generator`. This method generates random variates from the multivariate hypergeometric probability distribution. (`gh-13794 <https://github.com/numpy/numpy/pull/13794>`__) @@ -17,15 +36,14 @@ the multivariate hypergeometric probability distribution. Deprecations ============ -`np.fromfile` and `np.fromstring` will error on bad data --------------------------------------------------------- +``np.fromfile`` and ``np.fromstring`` will error on bad data +------------------------------------------------------------ -In future numpy releases, the functions `np.fromfile` and `np.fromstring` +In future numpy releases, the functions ``np.fromfile`` and ``np.fromstring`` will throw an error when parsing bad data. This will now give a ``DeprecationWarning`` where previously partial or even invalid data was silently returned. This deprecation also affects -the C defined functions c:func:`PyArray_FromString`` and -c:func:`PyArray_FromFile` +the C defined functions ``PyArray_FromString`` and ``PyArray_FromFile`` (`gh-13605 <https://github.com/numpy/numpy/pull/13605>`__) Deprecate non-scalar arrays as fill values in ``ma.fill_value`` @@ -35,29 +53,29 @@ since the logic to broadcast the fill value to the array is fragile, especially when slicing. (`gh-13698 <https://github.com/numpy/numpy/pull/13698>`__) -Deprecate `PyArray_As1D`, `PyArray_As2D` ----------------------------------------- -`PyArray_As1D`, `PyArray_As2D` are deprecated, use -`PyArray_AsCArray` instead +Deprecate ``PyArray_As1D``, ``PyArray_As2D`` +-------------------------------------------- +``PyArray_As1D``, ``PyArray_As2D`` are deprecated, use +``PyArray_AsCArray`` instead (`gh-14036 <https://github.com/numpy/numpy/pull/14036>`__) -Deprecate `np.alen` -------------------- -`np.alen` was deprecated. Use `len` instead. +Deprecate ``np.alen`` +--------------------- +``np.alen`` was deprecated. Use ``len`` instead. (`gh-14181 <https://github.com/numpy/numpy/pull/14181>`__) Deprecate the financial functions --------------------------------- In accordance with `NEP-32 <https://numpy.org/neps/nep-0032-remove-financial-functions.html>`_, -the functions `fv`, `ipmt`, `irr`, `mirr`, `nper`, `npv`, `pmt`, `ppmt`, -`pv` and `rate` are deprecated, and will be removed from NumPy 1.20. -The replacement for these functions is the Python package +the financial functions ``fv`` ``ipmt``, ``irr``, ``mirr``, ``nper``, +``npv``, ``pmt``, ``ppmt``, ``pv`` and ``rate`` are deprecated, and will be +removed from NumPy 1.20.The replacement for these functions is the Python package `numpy-financial <https://pypi.org/project/numpy-financial>`_. (`gh-14720 <https://github.com/numpy/numpy/pull/14720>`__) -The ``axis`` argument to `numpy.ma.mask_cols` and `numpy.ma.mask_row` is deprecated ------------------------------------------------------------------------------------ +The ``axis`` argument to ``numpy.ma.mask_cols`` and ``numpy.ma.mask_row`` is deprecated +--------------------------------------------------------------------------------------- This argument was always ignored. (`gh-14996 <https://github.com/numpy/numpy/pull/14996>`__) @@ -89,8 +107,10 @@ Expired deprecations * ``arrayprint.FloatFormat``, ``arrayprint.LongFloatFormat`` has been removed, use ``FloatingFormat`` instead + * ``arrayprint.ComplexFormat``, ``arrayprint.LongComplexFormat`` has been removed, use ``ComplexFloatingFormat`` instead + * ``arrayprint.StructureFormat`` has been removed, use ``StructureVoidFormat`` instead (`gh-14259 <https://github.com/numpy/numpy/pull/14259>`__) @@ -99,22 +119,22 @@ Expired deprecations and has been replaced by ``np.random.rand``. (`gh-14325 <https://github.com/numpy/numpy/pull/14325>`__) -* Class ``SafeEval`` in ``numpy/lib/utils.py`` has been removed. This was deprecated in NumPy 1.10. - Use ``np.safe_eval`` instead. +* Class ``SafeEval`` in ``numpy/lib/utils.py`` has been removed. + This was deprecated in NumPy 1.10. Use ``np.safe_eval`` instead. (`gh-14335 <https://github.com/numpy/numpy/pull/14335>`__) * Remove deprecated support for boolean and empty condition lists in - `numpy.select` + ``np.select`` (`gh-14583 <https://github.com/numpy/numpy/pull/14583>`__) * Array order only accepts 'C', 'F', 'A', and 'K'. More permissive options were deprecated in NumPy 1.11. (`gh-14596 <https://github.com/numpy/numpy/pull/14596>`__) -* np.linspace param num must be an integer. This was deprecated in NumPy 1.12. +* np.linspace parameter ``num`` must be an integer. Deprecated in NumPy 1.12. (`gh-14620 <https://github.com/numpy/numpy/pull/14620>`__) -* UFuncs with multiple outputs must use a tuple for the `out` kwarg. This +* UFuncs with multiple outputs must use a tuple for the ``out`` kwarg. This finishes a deprecation started in NumPy 1.10. (`gh-14682 <https://github.com/numpy/numpy/pull/14682>`__) @@ -151,7 +171,6 @@ converting the empty recarray to None ``np.can_cast(np.uint64, np.timedelta64, casting='safe')`` is now ``False`` --------------------------------------------------------------------------- - Previously this was ``True`` - however, this was inconsistent with ``uint64`` not being safely castable to ``int64``, and resulting in strange type resolution. @@ -159,9 +178,9 @@ resolution. If this impacts your code, cast ``uint64`` to ``int64`` first. (`gh-14718 <https://github.com/numpy/numpy/pull/14718>`__) -Changed random variate stream from `numpy.random.Generator.integers` --------------------------------------------------------------------- -There was a bug in `numpy.random.Generator.integers` that caused biased +Changed random variate stream from ``numpy.random.Generator.integers`` +---------------------------------------------------------------------- +There was a bug in ``numpy.random.Generator.integers`` that caused biased sampling of 8 and 16 bit integer types. Fixing that bug has changed the output stream from what it was in previous releases. (`gh-14777 <https://github.com/numpy/numpy/pull/14777>`__) @@ -171,20 +190,30 @@ Add more ufunc loops for ``datetime64``, ``timedelta64`` ``np.datetime('NaT')`` should behave more like ``float('Nan')``. Add needed infrastructure so ``np.isinf(a)`` and ``np.isnan(a)`` will run on ``datetime64`` and ``timedelta64`` dtypes. Also added specific loops for -`numpy.fmin` and `numpy.fmax` that mask ``NaT``. This may require adjustment to user- -facing code. Specifically, code that either disallowed the calls to -`numpy.isinf` or `numpy.isnan` or checked that they raised an exception will -require adaptation, and code that mistakenly called `numpy.fmax` and -`numpy.fmin` instead of `numpy.maximum` or `numpy.minimum` respectively will -requre adjustment. This also affects `numpy.nanmax` and `numpy.nanmin`. +``numpy.fmin`` and ``numpy.fmax`` that mask ``NaT``. This may require +adjustment to user- facing code. Specifically, code that either disallowed the +calls to ``numpy.isinf`` or ``numpy.isnan`` or checked that they raised an +exception will require adaptation, and code that mistakenly called +``numpy.fmax`` and ``numpy.fmin`` instead of ``numpy.maximum`` or +``numpy.minimum`` respectively will requre adjustment. This also affects +``numpy.nanmax`` and ``numpy.nanmin``. (`gh-14841 <https://github.com/numpy/numpy/pull/14841>`__) +Moved modules in ``numpy.random`` +--------------------------------- +As part of the API cleanup, the submodules in ``numpy.random`` +``bit_generator``, ``philox``, ``pcg64``, ``sfc64, ``common``, ``generator``, +and ``bounded_integers`` were moved to ``_bit_generator``, ``_philox``, +``_pcg64``, ``_sfc64, ``_common``, ``_generator``, and ``_bounded_integers`` +respectively to indicate that they are not part of the public interface. +(`gh-14608 <https://github.com/numpy/numpy/pull/14608>`__) + C API changes ============= -PyDataType_ISUNSIZED(descr) now returns False for structured datatypes ----------------------------------------------------------------------- +``PyDataType_ISUNSIZED(descr)`` now returns False for structured datatypes +-------------------------------------------------------------------------- Previously this returned True for any datatype of itemsize 0, but now this returns false for the non-flexible datatype with itemsize 0, ``np.dtype([])``. (`gh-14393 <https://github.com/numpy/numpy/pull/14393>`__) @@ -193,15 +222,14 @@ returns false for the non-flexible datatype with itemsize 0, ``np.dtype([])``. New Features ============ - Add our own ``*.pxd`` cython import file --------------------------------------------- -Added a ``numpy/__init__.pxd`` file. It will be used for `cimport numpy` +---------------------------------------- +Added a ``numpy/__init__.pxd`` file. It will be used for ``cimport numpy`` (`gh-12284 <https://github.com/numpy/numpy/pull/12284>`__) A tuple of axes can now be input to ``expand_dims`` --------------------------------------------------- -The `numpy.expand_dims` ``axis`` keyword can now accept a tuple of +The ``numpy.expand_dims`` ``axis`` keyword can now accept a tuple of axes. Previously, ``axis`` was required to be an integer. (`gh-14051 <https://github.com/numpy/numpy/pull/14051>`__) @@ -209,6 +237,14 @@ Support for 64-bit OpenBLAS --------------------------- Added support for 64-bit (ILP64) OpenBLAS. See ``site.cfg.example`` for details. +(`gh-15012 <https://github.com/numpy/numpy/pull/15012>`__) + +Add ``--f2cmap`` option to F2PY +------------------------------- +Allow specifying a file to load Fortran-to-C type map +customizations from. +(`gh-15113 <https://github.com/numpy/numpy/pull/15113>`__) + Improvements ============ @@ -226,7 +262,7 @@ These types now always print with a unique ``__name__``. ``argwhere`` now produces a consistent result on 0d arrays ---------------------------------------------------------- -On N-d arrays, `numpy.argwhere` now always produces an array of shape +On N-d arrays, ``numpy.argwhere`` now always produces an array of shape ``(n_non_zero, arr.ndim)``, even when ``arr.ndim == 0``. Previously, the last axis would have a dimension of 1 in this case. (`gh-13610 <https://github.com/numpy/numpy/pull/13610>`__) @@ -239,10 +275,10 @@ can only shuffle an array along the first axis; they now have a new argument ``axis`` which allows shuffle along a specified axis. (`gh-13829 <https://github.com/numpy/numpy/pull/13829>`__) -``method`` keyword argument for `np.random.multivariate_normal` ---------------------------------------------------------------- +``method`` keyword argument for ``np.random.multivariate_normal`` +----------------------------------------------------------------- A ``method`` keyword argument is now available for -`np.random.multivariate_normal` with possible values +``np.random.multivariate_normal`` with possible values ``{'svd', 'eigh', 'cholesky'}``. To use it, write ``np.random.multivariate_normal(..., method=<method>)``. (`gh-14197 <https://github.com/numpy/numpy/pull/14197>`__) @@ -252,21 +288,21 @@ Add complex number support for ``numpy.fromstring`` Now ``numpy.fromstring`` can read complex numbers. (`gh-14227 <https://github.com/numpy/numpy/pull/14227>`__) -`numpy.unique` has consistent axes order (except the chosen one) when ``axis`` is not None ------------------------------------------------------------------------------------------- -Using ``moveaxis`` instead of ``swapaxes`` in `numpy.unique`, so that the ordering of axes +``numpy.unique`` has consistent axes order when ``axis`` is not None +-------------------------------------------------------------------- +Using ``moveaxis`` instead of ``swapaxes`` in ``numpy.unique``, so that the ordering of axes except the axis in arguments will not be broken. (`gh-14255 <https://github.com/numpy/numpy/pull/14255>`__) -`numpy.matmul` with boolean output now converts to boolean values ------------------------------------------------------------------ -Calling `numpy.matmul` where the output is a boolean array would fill the array +``numpy.matmul`` with boolean output now converts to boolean values +------------------------------------------------------------------- +Calling ``numpy.matmul`` where the output is a boolean array would fill the array with uint8 equivalents of the result, rather than 0/1. Now it forces the output to 0 or 1 (``NPY_TRUE`` or ``NPY_FALSE``). (`gh-14464 <https://github.com/numpy/numpy/pull/14464>`__) -`numpy.random.randint` produced incorrect value when the range was ``2**32`` ----------------------------------------------------------------------------- +``numpy.random.randint`` produced incorrect value when the range was ``2**32`` +------------------------------------------------------------------------------ The implementation introduced in 1.17.0 had an incorrect check when determining whether to use the 32-bit path or the full 64-bit path that incorrectly redirected random integer generation with a high - low @@ -289,6 +325,7 @@ automatically add the code if the compiler name has ``gcc`` in it. Changes ======= + ``NaT`` now sorts to the end of arrays -------------------------------------- ``NaT`` is now effectively treated as the largest integer for sorting @@ -305,14 +342,14 @@ for non-numeric types and ``ValueError`` for ``nan`` values. Warn when saving a dtype with metadata -------------------------------------- -A ``UserWarning`` will be emitted when saving an array via `numpy.save` with +A ``UserWarning`` will be emitted when saving an array via ``numpy.save`` with ``metadata``. Saving such an array may not preserve metadata, and if metadata is preserved, loading it will cause a ``ValueError``. This shortcoming in save and load will be addressed in a future release. (`gh-14142 <https://github.com/numpy/numpy/pull/14142>`__) -`numpy.distutils`: append behavior changed for LDFLAGS and similar ------------------------------------------------------------------- +``numpy.distutils`` append behavior changed for LDFLAGS and similar +------------------------------------------------------------------- `numpy.distutils` has always overridden rather than appended to ``LDFLAGS`` and other similar such environment variables for compiling Fortran extensions. Now the default behavior has changed to appending - which is the expected behavior @@ -326,9 +363,9 @@ change in behavior would have affected the compile flags used. Remove ``numpy.random.entropy`` without a deprecation ----------------------------------------------------- -``numpy.random.entropy`` was added to the `numpy.random` namespace in 1.17.0. +``numpy.random.entropy`` was added to the ``numpy.random`` namespace in 1.17.0. It was meant to be a private c-extension module, but was exposed as public. -It has been replaced by `numpy.random.SeedSequence` so the module was +It has been replaced by ``numpy.random.SeedSequence`` so the module was completely removed. (`gh-14498 <https://github.com/numpy/numpy/pull/14498>`__) @@ -338,9 +375,9 @@ Added two new configuration options. During the ``build_src`` subcommand, as part of configuring NumPy, the files ``_numpyconfig.h`` and ``config.h`` are created by probing support for various runtime functions and routines. Previously, the very verbose compiler output during this stage clouded more -important information. By default the output is silenced. Running ``runtests.py ---debug-info`` will add ``--verbose-cfg`` to the ``build_src`` subcommand, -which will restore the previous behaviour. +important information. By default the output is silenced. Running +``runtests.py --debug-info`` will add ``--verbose-cfg`` to the ``build_src`` +subcommand,which will restore the previous behaviour. Adding ``CFLAGS=-Werror`` to turn warnings into errors would trigger errors during the configuration. Now ``runtests.py --warn-error`` will add diff --git a/doc/source/release/1.18.1-notes.rst b/doc/source/release/1.18.1-notes.rst new file mode 100644 index 000000000..8bc502ecb --- /dev/null +++ b/doc/source/release/1.18.1-notes.rst @@ -0,0 +1,52 @@ +.. currentmodule:: numpy + +========================== +NumPy 1.18.1 Release Notes +========================== + +This release contains fixes for bugs reported against NumPy 1.18.0. Two bugs +in particular that caused widespread problems downstream were: + +- The cython random extension test was not using a temporary directory for + building, resulting in a permission violation. Fixed. + +- Numpy distutils was appending `-std=c99` to all C compiler runs, leading to + changed behavior and compile problems downstream. That flag is now only + applied when building numpy C code. + +The Python versions supported in this release are 3.5-3.8. Downstream +developers should use Cython >= 0.29.14 for Python 3.8 support and OpenBLAS >= +3.7 to avoid errors on the Skylake architecture. + +Contributors +============ + +A total of 7 people contributed to this release. People with a "+" by their +names contributed a patch for the first time. + +* Charles Harris +* Matti Picus +* Maxwell Aladago +* Pauli Virtanen +* Ralf Gommers +* Tyler Reddy +* Warren Weckesser + +Pull requests merged +==================== + +A total of 13 pull requests were merged for this release. + +* `#15158 <https://github.com/numpy/numpy/pull/15158>`__: MAINT: Update pavement.py for towncrier. +* `#15159 <https://github.com/numpy/numpy/pull/15159>`__: DOC: add moved modules to 1.18 release note +* `#15161 <https://github.com/numpy/numpy/pull/15161>`__: MAINT, DOC: Minor backports and updates for 1.18.x +* `#15176 <https://github.com/numpy/numpy/pull/15176>`__: TST: Add assert_array_equal test for big integer arrays +* `#15184 <https://github.com/numpy/numpy/pull/15184>`__: BUG: use tmp dir and check version for cython test (#15170) +* `#15220 <https://github.com/numpy/numpy/pull/15220>`__: BUG: distutils: fix msvc+gfortran openblas handling corner case +* `#15221 <https://github.com/numpy/numpy/pull/15221>`__: BUG: remove -std=c99 for c++ compilation (#15194) +* `#15222 <https://github.com/numpy/numpy/pull/15222>`__: MAINT: unskip test on win32 +* `#15223 <https://github.com/numpy/numpy/pull/15223>`__: TST: add BLAS ILP64 run in Travis & Azure +* `#15245 <https://github.com/numpy/numpy/pull/15245>`__: MAINT: only add --std=c99 where needed +* `#15246 <https://github.com/numpy/numpy/pull/15246>`__: BUG: lib: Fix handling of integer arrays by gradient. +* `#15247 <https://github.com/numpy/numpy/pull/15247>`__: MAINT: Do not use private Python function in testing +* `#15250 <https://github.com/numpy/numpy/pull/15250>`__: REL: Prepare for the NumPy 1.18.1 release. diff --git a/doc/source/release/1.19.0-notes.rst b/doc/source/release/1.19.0-notes.rst new file mode 100644 index 000000000..6e7fd69d4 --- /dev/null +++ b/doc/source/release/1.19.0-notes.rst @@ -0,0 +1,6 @@ +.. currentmodule:: numpy + +========================== +NumPy 1.19.0 Release Notes +========================== + diff --git a/doc/source/user/building.rst b/doc/source/user/building.rst index 1588de964..fefbb4e0b 100644 --- a/doc/source/user/building.rst +++ b/doc/source/user/building.rst @@ -11,19 +11,11 @@ Prerequisites Building NumPy requires the following software installed: -1) Python 2.7.x, 3.4.x or newer +1) Python 3.6.x or newer - On Debian and derivatives (Ubuntu): python, python-dev (or python3-dev) - - On Windows: the official python installer at - `www.python.org <https://www.python.org>`_ is enough - - Make sure that the Python package distutils is installed before - continuing. For example, in Debian GNU/Linux, installing python-dev - also installs distutils. - - Python must also be compiled with the zlib module enabled. This is - practically always the case with pre-packaged Pythons. + Please note that the Python development headers also need to be installed, + e.g., on Debian/Ubuntu one needs to install both `python3` and + `python3-dev`. On Windows and macOS this is normally not an issue. 2) Compilers @@ -42,19 +34,16 @@ Building NumPy requires the following software installed: NumPy does not require any external linear algebra libraries to be installed. However, if these are available, NumPy's setup script can detect them and use them for building. A number of different LAPACK library setups - can be used, including optimized LAPACK libraries such as ATLAS, MKL or the - Accelerate/vecLib framework on OS X. + can be used, including optimized LAPACK libraries such as OpenBLAS or MKL. 4) Cython - To build development versions of NumPy, you'll need a recent version of - Cython. Released NumPy sources on PyPi include the C files generated from - Cython code, so for released versions having Cython installed isn't needed. + For building NumPy, you'll need a recent version of Cython. Basic Installation ------------------ -To install NumPy run:: +To install NumPy, run:: pip install . @@ -62,10 +51,6 @@ To perform an in-place build that can be run from the source folder run:: python setup.py build_ext --inplace -The NumPy build system uses ``setuptools`` (from numpy 1.11.0, before that it -was plain ``distutils``) and ``numpy.distutils``. -Using ``virtualenv`` should work as expected. - *Note: for build instructions to do development work on NumPy itself, see* :ref:`development-environment`. @@ -83,7 +68,7 @@ For detailed info on testing, see :ref:`testing-builds`. Parallel builds ~~~~~~~~~~~~~~~ -From NumPy 1.10.0 on it's also possible to do a parallel build with:: +It's possible to do a parallel build with:: python setup.py build -j 4 install --prefix $HOME/.local @@ -95,22 +80,11 @@ to perform a parallel in-place build, run:: The number of build jobs can also be specified via the environment variable ``NPY_NUM_BUILD_JOBS``. - -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. This applies for most other cases -where different FORTRAN compilers might have been used. - Choosing the fortran compiler ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -To build with gfortran:: +Compilers are auto-detected; building with a particular compiler can be done +with ``--fcompiler``. E.g. to select gfortran:: python setup.py build --fcompiler=gnu95 @@ -118,14 +92,14 @@ For more information see:: python setup.py build --help-fcompiler -How to check the ABI of blas/lapack/atlas -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +How to check the ABI of BLAS/LAPACK libraries +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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. +means that g77 has been used (note: g77 is no longer supported for building NumPy). +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. Accelerated BLAS/LAPACK libraries --------------------------------- @@ -145,7 +119,6 @@ The default order for the libraries are: 5. Accelerate (MacOS) 6. BLAS (NetLIB) - If you wish to build against OpenBLAS but you also have BLIS available one may predefine the order of searching via the environment variable ``NPY_BLAS_ORDER`` which is a comma-separated list of the above names which @@ -235,14 +208,3 @@ Additional compiler flags can be supplied by setting the ``OPT``, ``FOPT`` (for Fortran), and ``CC`` environment variables. When providing options that should improve the performance of the code ensure that you also set ``-DNDEBUG`` so that debugging code is not executed. - - -Building with ATLAS support ---------------------------- - -Ubuntu -~~~~~~ - -You can install the necessary package for optimized ATLAS with this command:: - - sudo apt-get install libatlas-base-dev diff --git a/doc/source/user/c-info.ufunc-tutorial.rst b/doc/source/user/c-info.ufunc-tutorial.rst index 96a73f9a6..8ff45a934 100644 --- a/doc/source/user/c-info.ufunc-tutorial.rst +++ b/doc/source/user/c-info.ufunc-tutorial.rst @@ -137,7 +137,6 @@ the module. /* This initiates the module using the above definitions. */ - #if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "spam", @@ -159,17 +158,6 @@ the module. } return m; } - #else - PyMODINIT_FUNC initspam(void) - { - PyObject *m; - - m = Py_InitModule("spam", SpamMethods); - if (m == NULL) { - return; - } - } - #endif To use the setup.py file, place setup.py and spammodule.c in the same folder. Then python setup.py build will build the module to import, @@ -322,7 +310,6 @@ the primary thing that must be changed to create your own ufunc. static void *data[1] = {NULL}; - #if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "npufunc", @@ -357,30 +344,6 @@ the primary thing that must be changed to create your own ufunc. return m; } - #else - PyMODINIT_FUNC initnpufunc(void) - { - PyObject *m, *logit, *d; - - - m = Py_InitModule("npufunc", LogitMethods); - if (m == NULL) { - return; - } - - import_array(); - import_umath(); - - logit = PyUFunc_FromFuncAndData(funcs, data, types, 1, 1, 1, - PyUFunc_None, "logit", - "logit_docstring", 0); - - d = PyModule_GetDict(m); - - PyDict_SetItemString(d, "logit", logit); - Py_DECREF(logit); - } - #endif This is a setup.py file for the above code. As before, the module can be build via calling python setup.py build at the command prompt, @@ -601,7 +564,6 @@ the primary thing that must be changed to create your own ufunc. NPY_LONGDOUBLE, NPY_LONGDOUBLE}; static void *data[4] = {NULL, NULL, NULL, NULL}; - #if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "npufunc", @@ -636,30 +598,6 @@ the primary thing that must be changed to create your own ufunc. return m; } - #else - PyMODINIT_FUNC initnpufunc(void) - { - PyObject *m, *logit, *d; - - - m = Py_InitModule("npufunc", LogitMethods); - if (m == NULL) { - return; - } - - import_array(); - import_umath(); - - logit = PyUFunc_FromFuncAndData(funcs, data, types, 4, 1, 1, - PyUFunc_None, "logit", - "logit_docstring", 0); - - d = PyModule_GetDict(m); - - PyDict_SetItemString(d, "logit", logit); - Py_DECREF(logit); - } - #endif This is a setup.py file for the above code. As before, the module can be build via calling python setup.py build at the command prompt, @@ -824,7 +762,6 @@ as well as all other properties of a ufunc. static void *data[1] = {NULL}; - #if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "npufunc", @@ -859,30 +796,6 @@ as well as all other properties of a ufunc. return m; } - #else - PyMODINIT_FUNC initnpufunc(void) - { - PyObject *m, *logit, *d; - - - m = Py_InitModule("npufunc", LogitMethods); - if (m == NULL) { - return; - } - - import_array(); - import_umath(); - - logit = PyUFunc_FromFuncAndData(funcs, data, types, 1, 2, 2, - PyUFunc_None, "logit", - "logit_docstring", 0); - - d = PyModule_GetDict(m); - - PyDict_SetItemString(d, "logit", logit); - Py_DECREF(logit); - } - #endif .. _`sec:NumPy-struct-dtype`: @@ -976,7 +889,6 @@ The C file is given below. static void *data[1] = {NULL}; - #if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "struct_ufunc_test", @@ -988,31 +900,18 @@ The C file is given below. NULL, NULL }; - #endif - #if defined(NPY_PY3K) PyMODINIT_FUNC PyInit_struct_ufunc_test(void) - #else - PyMODINIT_FUNC initstruct_ufunc_test(void) - #endif { PyObject *m, *add_triplet, *d; PyObject *dtype_dict; PyArray_Descr *dtype; PyArray_Descr *dtypes[3]; - #if defined(NPY_PY3K) m = PyModule_Create(&moduledef); - #else - m = Py_InitModule("struct_ufunc_test", StructUfuncTestMethods); - #endif if (m == NULL) { - #if defined(NPY_PY3K) return NULL; - #else - return; - #endif } import_array(); @@ -1043,9 +942,7 @@ The C file is given below. PyDict_SetItemString(d, "add_triplet", add_triplet); Py_DECREF(add_triplet); - #if defined(NPY_PY3K) return m; - #endif } .. index:: diff --git a/doc/source/user/quickstart.rst b/doc/source/user/quickstart.rst index 6211d0c69..34e327d75 100644 --- a/doc/source/user/quickstart.rst +++ b/doc/source/user/quickstart.rst @@ -117,9 +117,8 @@ from the type of the elements in the sequences. >>> b.dtype dtype('float64') -A frequent error consists in calling ``array`` with multiple numeric -arguments, rather than providing a single list of numbers as an -argument. +A frequent error consists in calling ``array`` with multiple arguments, +rather than providing a single sequence as an argument. :: @@ -174,8 +173,9 @@ state of the memory. By default, the dtype of the created array is array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]]) -To create sequences of numbers, NumPy provides a function analogous to -``range`` that returns arrays instead of lists. +To create sequences of numbers, NumPy provides the ``arange`` function +which is analogous to the Python built-in ``range``, but returns an +array. :: @@ -300,9 +300,9 @@ elementwise in NumPy arrays. The matrix product can be performed using the ``@`` operator (in python >=3.5) or the ``dot`` function or method:: >>> A = np.array( [[1,1], - ... [0,1]] ) + ... [0,1]] ) >>> B = np.array( [[2,0], - ... [3,4]] ) + ... [3,4]] ) >>> A * B # elementwise product array([[2, 0], [0, 4]]) @@ -437,7 +437,7 @@ operate elementwise on an array, producing an array as output. `dot`, `floor`, `inner`, - `inv`, + `invert`, `lexsort`, `max`, `maximum`, @@ -732,8 +732,14 @@ stacks 1D arrays as columns into a 2D array. It is equivalent to array([[ 4., 3.], [ 2., 8.]]) -On the other hand, the function `ma.row_stack` is equivalent to `vstack` -for any input arrays. +On the other hand, the function `row_stack` is equivalent to `vstack` +for any input arrays. In fact, `row_stack` is an alias for `vstack`:: + + >>> np.column_stack is np.hstack + False + >>> np.row_stack is np.vstack + True + In general, for arrays with more than two dimensions, `hstack` stacks along their second axes, `vstack` stacks along their @@ -917,7 +923,7 @@ Array Creation `ogrid`, `ones`, `ones_like`, - `r`, + `r_`, `zeros`, `zeros_like` Conversions @@ -1431,11 +1437,15 @@ functions ``column_stack``, ``dstack``, ``hstack`` and ``vstack``, depending on the dimension in which the stacking is to be done. For example:: - x = np.arange(0,10,2) # x=([0,2,4,6,8]) - y = np.arange(5) # y=([0,1,2,3,4]) - m = np.vstack([x,y]) # m=([[0,2,4,6,8], - # [0,1,2,3,4]]) - xy = np.hstack([x,y]) # xy =([0,2,4,6,8,0,1,2,3,4]) + >>> x = np.arange(0,10,2) + >>> y = np.arange(5) + >>> m = np.vstack([x,y]) + >>> m + array([[0, 2, 4, 6, 8], + [0, 1, 2, 3, 4]]) + >>> xy = np.hstack([x,y]) + >>> xy + array([0, 2, 4, 6, 8, 0, 1, 2, 3, 4]) The logic behind those functions in more than two dimensions can be strange. @@ -1448,7 +1458,7 @@ Histograms ---------- The NumPy ``histogram`` function applied to an array returns a pair of -vectors: the histogram of the array and the vector of bins. Beware: +vectors: the histogram of the array and a vector of the bin edges. Beware: ``matplotlib`` also has a function to build histograms (called ``hist``, as in Matlab) that differs from the one in NumPy. The main difference is that ``pylab.hist`` plots the histogram automatically, while diff --git a/doc/summarize.py b/doc/summarize.py index cfce2713e..9b02a408c 100755 --- a/doc/summarize.py +++ b/doc/summarize.py @@ -5,8 +5,6 @@ summarize.py Show a summary about which NumPy functions are documented and which are not. """ -from __future__ import division, absolute_import, print_function - import os, glob, re, sys, inspect, optparse try: # Accessing collections abstract classes from collections diff --git a/numpy/__init__.py b/numpy/__init__.py index 349914b2f..73e979a18 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -104,8 +104,6 @@ available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``. Exceptions to this rule are documented. """ -from __future__ import division, absolute_import, print_function - import sys import warnings diff --git a/numpy/_build_utils/__init__.py b/numpy/_build_utils/__init__.py index 1d0f69b67..e69de29bb 100644 --- a/numpy/_build_utils/__init__.py +++ b/numpy/_build_utils/__init__.py @@ -1 +0,0 @@ -from __future__ import division, absolute_import, print_function diff --git a/numpy/_build_utils/apple_accelerate.py b/numpy/_build_utils/apple_accelerate.py index 36dd7584a..b26aa12ad 100644 --- a/numpy/_build_utils/apple_accelerate.py +++ b/numpy/_build_utils/apple_accelerate.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys import re diff --git a/numpy/_globals.py b/numpy/_globals.py index f5c0761b5..9f44c7729 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -15,8 +15,6 @@ That was not the case when the singleton classes were defined in the numpy motivated this module. """ -from __future__ import division, absolute_import, print_function - __ALL__ = [ 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', '_NoValue' ] @@ -56,7 +54,7 @@ class VisibleDeprecationWarning(UserWarning): VisibleDeprecationWarning.__module__ = 'numpy' -class _NoValueType(object): +class _NoValueType: """Special keyword value. The instance of this class may be used as the default value assigned to a diff --git a/numpy/_pytesttester.py b/numpy/_pytesttester.py index b25224c20..8b6e3217e 100644 --- a/numpy/_pytesttester.py +++ b/numpy/_pytesttester.py @@ -15,7 +15,7 @@ Warnings filtering and other runtime settings should be dealt with in the whether or not that file is found as follows: * ``pytest.ini`` is present (develop mode) - All warnings except those explicily filtered out are raised as error. + All warnings except those explicitly filtered out are raised as error. * ``pytest.ini`` is absent (release mode) DeprecationWarnings and PendingDeprecationWarnings are ignored, other warnings are passed through. @@ -27,8 +27,6 @@ This module is imported by every numpy subpackage, so lies at the top level to simplify circular import issues. For the same reason, it contains no numpy imports at module scope, instead importing numpy within function calls. """ -from __future__ import division, absolute_import, print_function - import sys import os @@ -44,7 +42,7 @@ def _show_numpy_info(): print("NumPy relaxed strides checking option:", relaxed_strides) -class PytestTester(object): +class PytestTester: """ Pytest test runner. @@ -166,9 +164,6 @@ class PytestTester(object): # Ignore python2.7 -3 warnings pytest_args += [ - r"-W ignore:sys\.exc_clear\(\) not supported in 3\.x:DeprecationWarning", - r"-W ignore:in 3\.x, __setslice__:DeprecationWarning", - r"-W ignore:in 3\.x, __getslice__:DeprecationWarning", r"-W ignore:buffer\(\) not supported in 3\.x:DeprecationWarning", r"-W ignore:CObject type is not supported in 3\.x:DeprecationWarning", r"-W ignore:comparing unequal types not supported in 3\.x:DeprecationWarning", diff --git a/numpy/compat/__init__.py b/numpy/compat/__init__.py index 5b371f5c0..afee621b8 100644 --- a/numpy/compat/__init__.py +++ b/numpy/compat/__init__.py @@ -8,8 +8,6 @@ extensions, which may be included for the following reasons: * we may only need a small subset of the copied library/module """ -from __future__ import division, absolute_import, print_function - from . import _inspect from . import py3k from ._inspect import getargspec, formatargspec diff --git a/numpy/compat/_inspect.py b/numpy/compat/_inspect.py index 439d0d2c2..9a874a71d 100644 --- a/numpy/compat/_inspect.py +++ b/numpy/compat/_inspect.py @@ -5,8 +5,6 @@ significantly contributes to numpy import times. Importing this copy has almost no overhead. """ -from __future__ import division, absolute_import, print_function - import types __all__ = ['getargspec', 'formatargspec'] diff --git a/numpy/compat/py3k.py b/numpy/compat/py3k.py index 90e17d6d6..3d3002744 100644 --- a/numpy/compat/py3k.py +++ b/numpy/compat/py3k.py @@ -1,7 +1,7 @@ """ Python 3.X compatibility tools. -While this file was originally intented for Python 2 -> 3 transition, +While this file was originally intended for Python 2 -> 3 transition, it is now used to create a compatibility layer between different minor versions of Python 3. @@ -113,7 +113,7 @@ def is_pathlib_path(obj): return Path is not None and isinstance(obj, Path) # from Python 3.7 -class contextlib_nullcontext(object): +class contextlib_nullcontext: """Context manager that does no additional processing. Used as a stand-in for a normal context manager, when a particular diff --git a/numpy/compat/setup.py b/numpy/compat/setup.py index 882857428..afa511673 100644 --- a/numpy/compat/setup.py +++ b/numpy/compat/setup.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration diff --git a/numpy/compat/tests/test_compat.py b/numpy/compat/tests/test_compat.py index 1543aafaf..2b8acbaa0 100644 --- a/numpy/compat/tests/test_compat.py +++ b/numpy/compat/tests/test_compat.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from os.path import join from numpy.compat import isfileobj diff --git a/numpy/conftest.py b/numpy/conftest.py index 1baf4adda..a843f725f 100644 --- a/numpy/conftest.py +++ b/numpy/conftest.py @@ -1,8 +1,6 @@ """ Pytest configuration and fixtures for the Numpy test suite. """ -from __future__ import division, absolute_import, print_function - import os import pytest diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index c3b3f0392..815c61924 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -6,8 +6,6 @@ are available in the main ``numpy`` namespace - use that instead. """ -from __future__ import division, absolute_import, print_function - from numpy.version import version as __version__ import os diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index 2f1273904..f36c6941f 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -8,8 +8,6 @@ NOTE: Many of the methods of ndarray have corresponding functions. core/fromnumeric.py, core/defmatrix.py up-to-date. """ -from __future__ import division, absolute_import, print_function - import sys from numpy.core import numerictypes as _numerictypes @@ -1036,7 +1034,7 @@ add_newdoc('numpy.core.multiarray', 'fromstring', A string containing the data. dtype : data-type, optional The data type of the array; default: float. For binary input data, - the data must be in exactly this format. Most builtin numeric types are + the data must be in exactly this format. Most builtin numeric types are supported and extension types may be supported. .. versionadded:: 1.18.0 @@ -1486,59 +1484,6 @@ add_newdoc('numpy.core.multiarray', 'promote_types', """) -if sys.version_info.major < 3: - add_newdoc('numpy.core.multiarray', 'newbuffer', - """ - newbuffer(size) - - Return a new uninitialized buffer object. - - Parameters - ---------- - size : int - Size in bytes of returned buffer object. - - Returns - ------- - newbuffer : buffer object - Returned, uninitialized buffer object of `size` bytes. - - """) - - add_newdoc('numpy.core.multiarray', 'getbuffer', - """ - getbuffer(obj [,offset[, size]]) - - Create a buffer object from the given object referencing a slice of - length size starting at offset. - - Default is the entire buffer. A read-write buffer is attempted followed - by a read-only buffer. - - Parameters - ---------- - obj : object - - offset : int, optional - - size : int, optional - - Returns - ------- - buffer_obj : buffer - - Examples - -------- - >>> buf = np.getbuffer(np.ones(5), 1, 3) - >>> len(buf) - 3 - >>> buf[0] - '\\x00' - >>> buf - <read-write buffer for 0x8af1e70, size 3, offset 1 at 0x8ba4ec0> - - """) - add_newdoc('numpy.core.multiarray', 'c_einsum', """ c_einsum(subscripts, *operands, out=None, dtype=None, order='K', @@ -2507,7 +2452,7 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('T', add_newdoc('numpy.core.multiarray', 'ndarray', ('__array__', - """ a.__array__(|dtype) -> reference if type unchanged, copy otherwise. + """ a.__array__([dtype], /) -> reference if type unchanged, copy otherwise. Returns either a new reference to self if dtype is not given or a new array of provided data type if dtype is different from the current dtype of the @@ -3953,7 +3898,7 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('tolist', Examples -------- - For a 1D array, ``a.tolist()`` is almost the same as ``list(a)``, + For a 1D array, ``a.tolist()`` is almost the same as ``list(a)``, except that ``tolist`` changes numpy scalars to Python scalars: >>> a = np.uint32([1, 2]) @@ -4124,21 +4069,26 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('var', add_newdoc('numpy.core.multiarray', 'ndarray', ('view', """ - a.view(dtype=None, type=None) + a.view([dtype][, type]) New view of array with the same data. + .. note:: + Passing None for ``dtype`` is different from omitting the parameter, + since the former invokes ``dtype(None)`` which is an alias for + ``dtype('float_')``. + Parameters ---------- dtype : data-type or ndarray sub-class, optional - Data-type descriptor of the returned view, e.g., float32 or int16. The - default, None, results in the view having the same data-type as `a`. + Data-type descriptor of the returned view, e.g., float32 or int16. + Omitting it results in the view having the same data-type as `a`. This argument can also be specified as an ndarray sub-class, which then specifies the type of the returned object (this is equivalent to setting the ``type`` parameter). type : Python type, optional - Type of the returned view, e.g., ndarray or matrix. Again, the - default None results in type preservation. + Type of the returned view, e.g., ndarray or matrix. Again, omission + of the parameter results in type preservation. Notes ----- @@ -6871,4 +6821,3 @@ for float_name in ('half', 'single', 'double', 'longdouble'): >>> np.{ftype}(-.25).as_integer_ratio() (-1, 4) """.format(ftype=float_name))) - diff --git a/numpy/core/_asarray.py b/numpy/core/_asarray.py index 0ad4161f4..df569f22d 100644 --- a/numpy/core/_asarray.py +++ b/numpy/core/_asarray.py @@ -3,8 +3,6 @@ Functions in the ``as*array`` family that promote array-likes into arrays. `require` fits this category despite its name not matching this pattern. """ -from __future__ import division, absolute_import, print_function - from .overrides import set_module from .multiarray import array diff --git a/numpy/core/_dtype.py b/numpy/core/_dtype.py index df1ff180e..fa39dfcd4 100644 --- a/numpy/core/_dtype.py +++ b/numpy/core/_dtype.py @@ -3,8 +3,6 @@ A place for code to be called from the implementation of np.dtype String handling is much easier to do correctly in python. """ -from __future__ import division, absolute_import, print_function - import sys import numpy as np diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 05e401e0b..f8de6dd09 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -4,8 +4,7 @@ A place for internal code Some things are more easily handled Python. """ -from __future__ import division, absolute_import, print_function - +import ast import re import sys import platform @@ -20,9 +19,9 @@ except ImportError: IS_PYPY = platform.python_implementation() == 'PyPy' if (sys.byteorder == 'little'): - _nbo = b'<' + _nbo = '<' else: - _nbo = b'>' + _nbo = '>' def _makenames_list(adict, align): allfields = [] @@ -145,16 +144,16 @@ def _reconstruct(subtype, shape, dtype): # format_re was originally from numarray by J. Todd Miller -format_re = re.compile(br'(?P<order1>[<>|=]?)' - br'(?P<repeats> *[(]?[ ,0-9]*[)]? *)' - br'(?P<order2>[<>|=]?)' - br'(?P<dtype>[A-Za-z0-9.?]*(?:\[[a-zA-Z0-9,.]+\])?)') -sep_re = re.compile(br'\s*,\s*') -space_re = re.compile(br'\s+$') +format_re = re.compile(r'(?P<order1>[<>|=]?)' + r'(?P<repeats> *[(]?[ ,0-9]*[)]? *)' + r'(?P<order2>[<>|=]?)' + r'(?P<dtype>[A-Za-z0-9.?]*(?:\[[a-zA-Z0-9,.]+\])?)') +sep_re = re.compile(r'\s*,\s*') +space_re = re.compile(r'\s+$') # astr is a string (perhaps comma separated) -_convorder = {b'=': _nbo} +_convorder = {'=': _nbo} def _commastring(astr): startindex = 0 @@ -179,9 +178,9 @@ def _commastring(astr): (len(result)+1, astr)) startindex = mo.end() - if order2 == b'': + if order2 == '': order = order1 - elif order1 == b'': + elif order1 == '': order = order2 else: order1 = _convorder.get(order1, order1) @@ -192,18 +191,18 @@ def _commastring(astr): (order1, order2)) order = order1 - if order in [b'|', b'=', _nbo]: - order = b'' + if order in ['|', '=', _nbo]: + order = '' dtype = order + dtype - if (repeats == b''): + if (repeats == ''): newitem = dtype else: - newitem = (dtype, eval(repeats)) + newitem = (dtype, ast.literal_eval(repeats)) result.append(newitem) return result -class dummy_ctype(object): +class dummy_ctype: def __init__(self, cls): self._cls = cls def __mul__(self, other): @@ -238,16 +237,16 @@ _getintp_ctype.cache = None # Used for .ctypes attribute of ndarray -class _missing_ctypes(object): +class _missing_ctypes: def cast(self, num, obj): return num.value - class c_void_p(object): + class c_void_p: def __init__(self, ptr): self.value = ptr -class _ctypes(object): +class _ctypes: def __init__(self, array, ptr=None): self._arr = array @@ -523,7 +522,7 @@ _pep3118_unsupported_map = { 'X': 'function pointers', } -class _Stream(object): +class _Stream: def __init__(self, s): self.s = s self.byteorder = '@' @@ -845,7 +844,7 @@ def npy_ctypes_check(cls): return False -class recursive(object): +class recursive: ''' A decorator class for recursive nested functions. Naive recursive nested functions hold a reference to themselves: diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index 269e509b8..694523b20 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -3,8 +3,6 @@ Array methods which are called by both the C-code for the method and the Python code for the NumPy-namespace function """ -from __future__ import division, absolute_import, print_function - import warnings from numpy.core import multiarray as mu diff --git a/numpy/core/_ufunc_config.py b/numpy/core/_ufunc_config.py index c3951cc09..4872a5385 100644 --- a/numpy/core/_ufunc_config.py +++ b/numpy/core/_ufunc_config.py @@ -3,8 +3,6 @@ Functions for changing global ufunc configuration This provides helpers which wrap `umath.geterrobj` and `umath.seterrobj` """ -from __future__ import division, absolute_import, print_function - try: # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 @@ -290,7 +288,7 @@ def seterrcall(func): Log error message: - >>> class Log(object): + >>> class Log: ... def write(self, msg): ... print("LOG: %s" % msg) ... @@ -365,7 +363,7 @@ def geterrcall(): return umath.geterrobj()[2] -class _unspecified(object): +class _unspecified: pass @@ -431,11 +429,9 @@ class errstate(contextlib.ContextDecorator): OrderedDict([('divide', 'ignore'), ('invalid', 'ignore'), ('over', 'ignore'), ('under', 'ignore')]) """ - # Note that we don't want to run the above doctests because they will fail - # without a from __future__ import with_statement - def __init__(self, **kwargs): - self.call = kwargs.pop('call', _Unspecified) + def __init__(self, *, call=_Unspecified, **kwargs): + self.call = call self.kwargs = kwargs def __enter__(self): diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 401018015..918da4a72 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -3,8 +3,6 @@ $Id: arrayprint.py,v 1.9 2005/09/13 13:58:44 teoliphant Exp $ """ -from __future__ import division, absolute_import, print_function - __all__ = ["array2string", "array_str", "array_repr", "set_string_function", "set_printoptions", "get_printoptions", "printoptions", "format_float_positional", "format_float_scientific"] @@ -100,7 +98,7 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None, @set_module('numpy') def set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, - formatter=None, sign=None, floatmode=None, **kwarg): + formatter=None, sign=None, floatmode=None, *, legacy=None): """ Set printing options. @@ -249,11 +247,6 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, array([ 0. , 1.11, 2.22, ..., 7.78, 8.89, 10. ]) """ - legacy = kwarg.pop('legacy', None) - if kwarg: - msg = "set_printoptions() got unexpected keyword argument '{}'" - raise TypeError(msg.format(kwarg.popitem()[0])) - opt = _make_options_dict(precision, threshold, edgeitems, linewidth, suppress, nanstr, infstr, sign, formatter, floatmode, legacy) @@ -369,23 +362,22 @@ def repr_format(x): def str_format(x): return str(x) -def _get_formatdict(data, **opt): - prec, fmode = opt['precision'], opt['floatmode'] - supp, sign = opt['suppress'], opt['sign'] - legacy = opt['legacy'] +def _get_formatdict(data, *, precision, floatmode, suppress, sign, legacy, + formatter, **kwargs): + # note: extra arguments in kwargs are ignored # wrapped in lambdas to avoid taking a code path with the wrong type of data formatdict = { 'bool': lambda: BoolFormat(data), 'int': lambda: IntegerFormat(data), - 'float': lambda: - FloatingFormat(data, prec, fmode, supp, sign, legacy=legacy), - 'longfloat': lambda: - FloatingFormat(data, prec, fmode, supp, sign, legacy=legacy), - 'complexfloat': lambda: - ComplexFloatingFormat(data, prec, fmode, supp, sign, legacy=legacy), - 'longcomplexfloat': lambda: - ComplexFloatingFormat(data, prec, fmode, supp, sign, legacy=legacy), + 'float': lambda: FloatingFormat( + data, precision, floatmode, suppress, sign, legacy=legacy), + 'longfloat': lambda: FloatingFormat( + data, precision, floatmode, suppress, sign, legacy=legacy), + 'complexfloat': lambda: ComplexFloatingFormat( + data, precision, floatmode, suppress, sign, legacy=legacy), + 'longcomplexfloat': lambda: ComplexFloatingFormat( + data, precision, floatmode, suppress, sign, legacy=legacy), 'datetime': lambda: DatetimeFormat(data, legacy=legacy), 'timedelta': lambda: TimedeltaFormat(data), 'object': lambda: _object_format, @@ -398,7 +390,6 @@ def _get_formatdict(data, **opt): def indirect(x): return lambda: x - formatter = opt['formatter'] if formatter is not None: fkeys = [k for k in formatter.keys() if formatter[k] is not None] if 'all' in fkeys: @@ -525,7 +516,7 @@ def _array2string_dispatcher( suppress_small=None, separator=None, prefix=None, style=None, formatter=None, threshold=None, edgeitems=None, sign=None, floatmode=None, suffix=None, - **kwarg): + *, legacy=None): return (a,) @@ -534,7 +525,7 @@ def array2string(a, max_line_width=None, precision=None, suppress_small=None, separator=' ', prefix="", style=np._NoValue, formatter=None, threshold=None, edgeitems=None, sign=None, floatmode=None, suffix="", - **kwarg): + *, legacy=None): """ Return a string representation of an array. @@ -679,10 +670,6 @@ def array2string(a, max_line_width=None, precision=None, '[0x0 0x1 0x2]' """ - legacy = kwarg.pop('legacy', None) - if kwarg: - msg = "array2string() got unexpected keyword argument '{}'" - raise TypeError(msg.format(kwarg.popitem()[0])) overrides = _make_options_dict(precision, threshold, edgeitems, max_line_width, suppress_small, None, None, @@ -851,15 +838,15 @@ def _none_or_positive_arg(x, name): raise ValueError("{} must be >= 0".format(name)) return x -class FloatingFormat(object): +class FloatingFormat: """ Formatter for subtypes of np.floating """ def __init__(self, data, precision, floatmode, suppress_small, sign=False, - **kwarg): + *, legacy=None): # for backcompatibility, accept bools if isinstance(sign, bool): sign = '+' if sign else '-' - self._legacy = kwarg.get('legacy', False) + self._legacy = legacy if self._legacy == '1.13': # when not 0d, legacy does not support '-' if data.shape != () and sign == '-': @@ -1140,7 +1127,7 @@ def format_float_positional(x, precision=None, unique=True, pad_right=pad_right) -class IntegerFormat(object): +class IntegerFormat: def __init__(self, data): if data.size > 0: max_str_len = max(len(str(np.max(data))), @@ -1153,7 +1140,7 @@ class IntegerFormat(object): return self.format % x -class BoolFormat(object): +class BoolFormat: def __init__(self, data, **kwargs): # add an extra space so " True" and "False" have the same length and # array elements align nicely when printed, except in 0d arrays @@ -1163,23 +1150,27 @@ class BoolFormat(object): return self.truestr if x else "False" -class ComplexFloatingFormat(object): +class ComplexFloatingFormat: """ Formatter for subtypes of np.complexfloating """ def __init__(self, x, precision, floatmode, suppress_small, - sign=False, **kwarg): + sign=False, *, legacy=None): # for backcompatibility, accept bools if isinstance(sign, bool): sign = '+' if sign else '-' floatmode_real = floatmode_imag = floatmode - if kwarg.get('legacy', False) == '1.13': + if legacy == '1.13': floatmode_real = 'maxprec_equal' floatmode_imag = 'maxprec' - self.real_format = FloatingFormat(x.real, precision, floatmode_real, - suppress_small, sign=sign, **kwarg) - self.imag_format = FloatingFormat(x.imag, precision, floatmode_imag, - suppress_small, sign='+', **kwarg) + self.real_format = FloatingFormat( + x.real, precision, floatmode_real, suppress_small, + sign=sign, legacy=legacy + ) + self.imag_format = FloatingFormat( + x.imag, precision, floatmode_imag, suppress_small, + sign='+', legacy=legacy + ) def __call__(self, x): r = self.real_format(x.real) @@ -1192,7 +1183,7 @@ class ComplexFloatingFormat(object): return r + i -class _TimelikeFormat(object): +class _TimelikeFormat: def __init__(self, data): non_nat = data[~isnat(data)] if len(non_nat) > 0: @@ -1255,7 +1246,7 @@ class TimedeltaFormat(_TimelikeFormat): return str(x.astype('i8')) -class SubArrayFormat(object): +class SubArrayFormat: def __init__(self, format_function): self.format_function = format_function @@ -1265,7 +1256,7 @@ class SubArrayFormat(object): return "[" + ", ".join(self.__call__(a) for a in arr) + "]" -class StructuredVoidFormat(object): +class StructuredVoidFormat: """ Formatter for structured np.void objects. diff --git a/numpy/core/code_generators/__init__.py b/numpy/core/code_generators/__init__.py index 1d0f69b67..e69de29bb 100644 --- a/numpy/core/code_generators/__init__.py +++ b/numpy/core/code_generators/__init__.py @@ -1 +0,0 @@ -from __future__ import division, absolute_import, print_function diff --git a/numpy/core/code_generators/genapi.py b/numpy/core/code_generators/genapi.py index 22afa0320..4fa68a1f0 100644 --- a/numpy/core/code_generators/genapi.py +++ b/numpy/core/code_generators/genapi.py @@ -6,8 +6,6 @@ See ``find_function`` for how functions should be formatted, and specified. """ -from __future__ import division, absolute_import, print_function - from numpy.distutils.conv_template import process_file as process_c_file import sys, os, re @@ -76,7 +74,7 @@ def _repl(str): return str.replace('Bool', 'npy_bool') -class StealRef(object): +class StealRef: def __init__(self, arg): self.arg = arg # counting from 1 @@ -87,7 +85,7 @@ class StealRef(object): return 'NPY_STEALS_REF_TO_ARG(%d)' % self.arg -class NonNull(object): +class NonNull: def __init__(self, arg): self.arg = arg # counting from 1 @@ -98,7 +96,7 @@ class NonNull(object): return 'NPY_GCC_NONNULL(%d)' % self.arg -class Function(object): +class Function: def __init__(self, name, return_type, args, doc=''): self.name = name self.return_type = _repl(return_type) @@ -309,7 +307,7 @@ def write_file(filename, data): # Those *Api classes instances know how to output strings for the generated code -class TypeApi(object): +class TypeApi: def __init__(self, name, index, ptr_cast, api_name): self.index = index self.name = name @@ -331,7 +329,7 @@ extern NPY_NO_EXPORT PyTypeObject %(type)s; """ % {'type': self.name} return astr -class GlobalVarApi(object): +class GlobalVarApi: def __init__(self, name, index, type, api_name): self.name = name self.index = index @@ -355,7 +353,7 @@ extern NPY_NO_EXPORT %(type)s %(name)s; # Dummy to be able to consistently use *Api instances for all items in the # array api -class BoolValuesApi(object): +class BoolValuesApi: def __init__(self, name, index, api_name): self.name = name self.index = index @@ -377,7 +375,7 @@ extern NPY_NO_EXPORT PyBoolScalarObject _PyArrayScalar_BoolValues[2]; """ return astr -class FunctionApi(object): +class FunctionApi: def __init__(self, name, index, annotations, return_type, args, api_name): self.name = name self.index = index diff --git a/numpy/core/code_generators/generate_numpy_api.py b/numpy/core/code_generators/generate_numpy_api.py index 5e04fb86d..fe21bc543 100644 --- a/numpy/core/code_generators/generate_numpy_api.py +++ b/numpy/core/code_generators/generate_numpy_api.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - import os import genapi @@ -59,21 +57,12 @@ _import_array(void) return -1; } -#if PY_VERSION_HEX >= 0x03000000 if (!PyCapsule_CheckExact(c_api)) { PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is not PyCapsule object"); Py_DECREF(c_api); return -1; } PyArray_API = (void **)PyCapsule_GetPointer(c_api, NULL); -#else - if (!PyCObject_Check(c_api)) { - PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is not PyCObject object"); - Py_DECREF(c_api); - return -1; - } - PyArray_API = (void **)PyCObject_AsVoidPtr(c_api); -#endif Py_DECREF(c_api); if (PyArray_API == NULL) { PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is NULL pointer"); @@ -120,13 +109,7 @@ _import_array(void) return 0; } -#if PY_VERSION_HEX >= 0x03000000 -#define NUMPY_IMPORT_ARRAY_RETVAL NULL -#else -#define NUMPY_IMPORT_ARRAY_RETVAL -#endif - -#define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } } +#define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NULL; } } #define import_array1(ret) {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return ret; } } diff --git a/numpy/core/code_generators/generate_ufunc_api.py b/numpy/core/code_generators/generate_ufunc_api.py index 1b0143e88..c32cf3e44 100644 --- a/numpy/core/code_generators/generate_ufunc_api.py +++ b/numpy/core/code_generators/generate_ufunc_api.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - import os import genapi @@ -51,21 +49,12 @@ _import_umath(void) return -1; } -#if PY_VERSION_HEX >= 0x03000000 if (!PyCapsule_CheckExact(c_api)) { PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is not PyCapsule object"); Py_DECREF(c_api); return -1; } PyUFunc_API = (void **)PyCapsule_GetPointer(c_api, NULL); -#else - if (!PyCObject_Check(c_api)) { - PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is not PyCObject object"); - Py_DECREF(c_api); - return -1; - } - PyUFunc_API = (void **)PyCObject_AsVoidPtr(c_api); -#endif Py_DECREF(c_api); if (PyUFunc_API == NULL) { PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is NULL pointer"); @@ -74,12 +63,6 @@ _import_umath(void) return 0; } -#if PY_VERSION_HEX >= 0x03000000 -#define NUMPY_IMPORT_UMATH_RETVAL NULL -#else -#define NUMPY_IMPORT_UMATH_RETVAL -#endif - #define import_umath() \ do {\ UFUNC_NOFPE\ @@ -87,7 +70,7 @@ _import_umath(void) PyErr_Print();\ PyErr_SetString(PyExc_ImportError,\ "numpy.core.umath failed to import");\ - return NUMPY_IMPORT_UMATH_RETVAL;\ + return NULL;\ }\ } while(0) diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index 6d76f7ca2..f5691d950 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - import os import re import struct @@ -21,16 +19,16 @@ ReorderableNone = "(Py_INCREF(Py_None), Py_None)" # Sentinel value to specify using the full type description in the # function name -class FullTypeDescr(object): +class FullTypeDescr: pass -class FuncNameSuffix(object): +class FuncNameSuffix: """Stores the suffix to append when generating functions names. """ def __init__(self, suffix): self.suffix = suffix -class TypeDescription(object): +class TypeDescription: """Type signature for a ufunc. Attributes @@ -120,7 +118,7 @@ def TD(types, f=None, astype=None, in_=None, out=None, simd=None): tds.append(TypeDescription(t, f=fd, in_=i, out=o, astype=astype, simd=simdt)) return tds -class Ufunc(object): +class Ufunc: """Description of a ufunc. Attributes @@ -132,7 +130,7 @@ class Ufunc(object): type_descriptions : list of TypeDescription objects """ def __init__(self, nin, nout, identity, docstring, typereso, - *type_descriptions, **kwargs): + *type_descriptions, signature=None): self.nin = nin self.nout = nout if identity is None: @@ -141,13 +139,11 @@ class Ufunc(object): self.docstring = docstring self.typereso = typereso self.type_descriptions = [] - self.signature = kwargs.pop('signature', None) + self.signature = signature for td in type_descriptions: self.type_descriptions.extend(td) for td in self.type_descriptions: td.finish_signature(self.nin, self.nout) - if kwargs: - raise ValueError('unknown kwargs %r' % str(kwargs)) # String-handling utilities to avoid locale-dependence. @@ -306,17 +302,6 @@ defdict = { ], TD(O, f='PyNumber_Multiply'), ), -'divide': - Ufunc(2, 1, None, # One is only a unit to the right, not the left - docstrings.get('numpy.core.umath.divide'), - 'PyUFunc_MixedDivisionTypeResolver', - TD(intfltcmplx), - [TypeDescription('m', FullTypeDescr, 'mq', 'm'), - TypeDescription('m', FullTypeDescr, 'md', 'm'), - TypeDescription('m', FullTypeDescr, 'mm', 'd'), - ], - TD(O, f='PyNumber_Divide'), - ), 'floor_divide': Ufunc(2, 1, None, # One is only a unit to the right, not the left docstrings.get('numpy.core.umath.floor_divide'), @@ -957,10 +942,6 @@ defdict = { ), } -if sys.version_info[0] >= 3: - # Will be aliased to true_divide in umathmodule.c.src:InitOtherOperators - del defdict['divide'] - def indent(st, spaces): indentation = ' '*spaces indented = indentation + st.replace('\n', '\n'+indentation) diff --git a/numpy/core/code_generators/numpy_api.py b/numpy/core/code_generators/numpy_api.py index a71c236fd..916fb537e 100644 --- a/numpy/core/code_generators/numpy_api.py +++ b/numpy/core/code_generators/numpy_api.py @@ -13,8 +13,6 @@ When adding a function, make sure to use the next integer not used as an index exception, so it should hopefully not get unnoticed). """ -from __future__ import division, absolute_import, print_function - from code_generators.genapi import StealRef, NonNull # index, type diff --git a/numpy/core/code_generators/ufunc_docstrings.py b/numpy/core/code_generators/ufunc_docstrings.py index 9560eb31b..33ad1502d 100644 --- a/numpy/core/code_generators/ufunc_docstrings.py +++ b/numpy/core/code_generators/ufunc_docstrings.py @@ -9,7 +9,6 @@ for the ufuncs in numpy.core at the C level when the ufuncs are created at compile time. """ -from __future__ import division, absolute_import, print_function import textwrap docdict = {} @@ -3826,12 +3825,7 @@ add_newdoc('numpy.core.umath', 'true_divide', Notes ----- - The floor division operator ``//`` was added in Python 2.2 making - ``//`` and ``/`` equivalent operators. The default floor division - operation of ``/`` can be replaced by true division with ``from - __future__ import division``. - - In Python 3.0, ``//`` is the floor division operator and ``/`` the + In Python, ``//`` is the floor division operator and ``/`` the true division operator. The ``true_divide(x1, x2)`` function is equivalent to true division in Python. @@ -3844,7 +3838,6 @@ add_newdoc('numpy.core.umath', 'true_divide', >>> x//4 array([0, 0, 0, 0, 1]) - >>> from __future__ import division >>> x/4 array([ 0. , 0.25, 0.5 , 0.75, 1. ]) >>> x//4 diff --git a/numpy/core/cversions.py b/numpy/core/cversions.py index 7995dd993..00159c3a8 100644 --- a/numpy/core/cversions.py +++ b/numpy/core/cversions.py @@ -3,8 +3,6 @@ The API has is defined by numpy_api_order and ufunc_api_order. """ -from __future__ import division, absolute_import, print_function - from os.path import dirname from code_generators.genapi import fullapi_hash diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py index 2d89d6fe0..ff1474d9d 100644 --- a/numpy/core/defchararray.py +++ b/numpy/core/defchararray.py @@ -15,8 +15,6 @@ available in your version of Python. The preferred alias for `defchararray` is `numpy.char`. """ -from __future__ import division, absolute_import, print_function - import functools import sys from .numerictypes import string_, unicode_, integer, object_, bool_, character @@ -358,7 +356,7 @@ def _mod_dispatcher(a, values): def mod(a, values): """ Return (a % i), that is pre-Python 2.6 string formatting - (iterpolation), element-wise for a pair of array_likes of str + (interpolation), element-wise for a pair of array_likes of str or unicode. Parameters @@ -2112,7 +2110,7 @@ class chararray(ndarray): def __mod__(self, i): """ Return (self % i), that is pre-Python 2.6 string formatting - (iterpolation), element-wise for a pair of array_likes of `string_` + (interpolation), element-wise for a pair of array_likes of `string_` or `unicode_`. See also diff --git a/numpy/core/einsumfunc.py b/numpy/core/einsumfunc.py index 3412c3fd5..ec3eb19d2 100644 --- a/numpy/core/einsumfunc.py +++ b/numpy/core/einsumfunc.py @@ -2,8 +2,6 @@ Implementation of optimized einsum. """ -from __future__ import division, absolute_import, print_function - import itertools from numpy.compat import basestring @@ -691,7 +689,7 @@ def _parse_einsum_input(operands): return (input_subscripts, output_subscript, operands) -def _einsum_path_dispatcher(*operands, **kwargs): +def _einsum_path_dispatcher(*operands, optimize=None, einsum_call=None): # NOTE: technically, we should only dispatch on array-like arguments, not # subscripts (given as strings). But separating operands into # arrays/subscripts is a little tricky/slow (given einsum's two supported @@ -702,7 +700,7 @@ def _einsum_path_dispatcher(*operands, **kwargs): @array_function_dispatch(_einsum_path_dispatcher, module='numpy') -def einsum_path(*operands, **kwargs): +def einsum_path(*operands, optimize='greedy', einsum_call=False): """ einsum_path(subscripts, *operands, optimize='greedy') @@ -812,16 +810,8 @@ def einsum_path(*operands, **kwargs): 5 defg,hd->efgh efgh->efgh """ - # Make sure all keywords are valid - valid_contract_kwargs = ['optimize', 'einsum_call'] - unknown_kwargs = [k for (k, v) in kwargs.items() if k - not in valid_contract_kwargs] - if len(unknown_kwargs): - raise TypeError("Did not understand the following kwargs:" - " %s" % unknown_kwargs) - # Figure out what the path really is - path_type = kwargs.pop('optimize', True) + path_type = optimize if path_type is True: path_type = 'greedy' if path_type is None: @@ -847,7 +837,7 @@ def einsum_path(*operands, **kwargs): raise TypeError("Did not understand the path: %s" % str(path_type)) # Hidden option, only einsum should call this - einsum_call_arg = kwargs.pop("einsum_call", False) + einsum_call_arg = einsum_call # Python side parsing input_subscripts, output_subscript, operands = _parse_einsum_input(operands) @@ -992,17 +982,17 @@ def einsum_path(*operands, **kwargs): return (path, path_print) -def _einsum_dispatcher(*operands, **kwargs): +def _einsum_dispatcher(*operands, out=None, optimize=None, **kwargs): # Arguably we dispatch on more arguments that we really should; see note in # _einsum_path_dispatcher for why. for op in operands: yield op - yield kwargs.get('out') + yield out # Rewrite einsum to handle different cases @array_function_dispatch(_einsum_dispatcher, module='numpy') -def einsum(*operands, **kwargs): +def einsum(*operands, out=None, optimize=False, **kwargs): """ einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False) @@ -1347,39 +1337,29 @@ def einsum(*operands, **kwargs): ... _ = np.einsum('ijk,ilm,njm,nlk,abc->',a,a,a,a,a, optimize=path) """ - - # Grab non-einsum kwargs; do not optimize by default. - optimize_arg = kwargs.pop('optimize', False) + # Special handling if out is specified + specified_out = out is not None # If no optimization, run pure einsum - if optimize_arg is False: + if optimize is False: + if specified_out: + kwargs['out'] = out return c_einsum(*operands, **kwargs) - valid_einsum_kwargs = ['out', 'dtype', 'order', 'casting'] - einsum_kwargs = {k: v for (k, v) in kwargs.items() if - k in valid_einsum_kwargs} - - # Make sure all keywords are valid - valid_contract_kwargs = ['optimize'] + valid_einsum_kwargs + # Check the kwargs to avoid a more cryptic error later, without having to + # repeat default values here + valid_einsum_kwargs = ['dtype', 'order', 'casting'] unknown_kwargs = [k for (k, v) in kwargs.items() if - k not in valid_contract_kwargs] - + k not in valid_einsum_kwargs] if len(unknown_kwargs): raise TypeError("Did not understand the following kwargs: %s" % unknown_kwargs) - # Special handeling if out is specified - specified_out = False - out_array = einsum_kwargs.pop('out', None) - if out_array is not None: - specified_out = True # Build the contraction list and operand - operands, contraction_list = einsum_path(*operands, optimize=optimize_arg, + operands, contraction_list = einsum_path(*operands, optimize=optimize, einsum_call=True) - handle_out = False - # Start contraction loop for num, contraction in enumerate(contraction_list): inds, idx_rm, einsum_str, remaining, blas = contraction @@ -1410,23 +1390,23 @@ def einsum(*operands, **kwargs): # Build a new view if needed if (tensor_result != results_index) or handle_out: if handle_out: - einsum_kwargs["out"] = out_array - new_view = c_einsum(tensor_result + '->' + results_index, new_view, **einsum_kwargs) + kwargs["out"] = out + new_view = c_einsum(tensor_result + '->' + results_index, new_view, **kwargs) # Call einsum else: # If out was specified if handle_out: - einsum_kwargs["out"] = out_array + kwargs["out"] = out # Do the contraction - new_view = c_einsum(einsum_str, *tmp_operands, **einsum_kwargs) + new_view = c_einsum(einsum_str, *tmp_operands, **kwargs) # Append new items and dereference what we can operands.append(new_view) del tmp_operands, new_view if specified_out: - return out_array + return out else: return operands[0] diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 33b3b5703..213a4438f 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -1,8 +1,6 @@ """Module containing non-deprecated functions borrowed from Numeric. """ -from __future__ import division, absolute_import, print_function - import functools import types import warnings diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index 538ac8b84..8f92a4f71 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import functools import warnings import operator diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index 31fa6b9bf..b00ef64bd 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -1,8 +1,6 @@ """Machine limits for Float32 and Float64 and (long double) if available... """ -from __future__ import division, absolute_import, print_function - __all__ = ['finfo', 'iinfo'] import warnings @@ -31,12 +29,12 @@ def _fr1(a): a.shape = () return a -class MachArLike(object): +class MachArLike: """ Object to simulate MachAr instance """ def __init__(self, ftype, - **kwargs): + *, eps, epsneg, huge, tiny, ibeta, **kwargs): params = _MACHAR_PARAMS[ftype] float_conv = lambda v: array([v], ftype) float_to_float = lambda v : _fr1(float_conv(v)) @@ -44,11 +42,11 @@ class MachArLike(object): self.title = params['title'] # Parameter types same as for discovered MachAr object. - self.epsilon = self.eps = float_to_float(kwargs.pop('eps')) - self.epsneg = float_to_float(kwargs.pop('epsneg')) - self.xmax = self.huge = float_to_float(kwargs.pop('huge')) - self.xmin = self.tiny = float_to_float(kwargs.pop('tiny')) - self.ibeta = params['itype'](kwargs.pop('ibeta')) + self.epsilon = self.eps = float_to_float(eps) + self.epsneg = float_to_float(epsneg) + self.xmax = self.huge = float_to_float(huge) + self.xmin = self.tiny = float_to_float(tiny) + self.ibeta = params['itype'](ibeta) self.__dict__.update(kwargs) self.precision = int(-log10(self.eps)) self.resolution = float_to_float(float_conv(10) ** (-self.precision)) @@ -291,7 +289,7 @@ def _discovered_machar(ftype): @set_module('numpy') -class finfo(object): +class finfo: """ finfo(dtype) @@ -442,7 +440,7 @@ class finfo(object): @set_module('numpy') -class iinfo(object): +class iinfo: """ iinfo(type) diff --git a/numpy/core/include/numpy/ndarrayobject.h b/numpy/core/include/numpy/ndarrayobject.h index 95e9cb060..b18d75f35 100644 --- a/numpy/core/include/numpy/ndarrayobject.h +++ b/numpy/core/include/numpy/ndarrayobject.h @@ -45,7 +45,6 @@ extern "C" { #define PyArray_CheckScalar(m) (PyArray_IsScalar(m, Generic) || \ PyArray_IsZeroDim(m)) -#if PY_MAJOR_VERSION >= 3 #define PyArray_IsPythonNumber(obj) \ (PyFloat_Check(obj) || PyComplex_Check(obj) || \ PyLong_Check(obj) || PyBool_Check(obj)) @@ -54,17 +53,6 @@ extern "C" { #define PyArray_IsPythonScalar(obj) \ (PyArray_IsPythonNumber(obj) || PyBytes_Check(obj) || \ PyUnicode_Check(obj)) -#else -#define PyArray_IsPythonNumber(obj) \ - (PyInt_Check(obj) || PyFloat_Check(obj) || PyComplex_Check(obj) || \ - PyLong_Check(obj) || PyBool_Check(obj)) -#define PyArray_IsIntegerScalar(obj) (PyInt_Check(obj) \ - || PyLong_Check(obj) \ - || PyArray_IsScalar((obj), Integer)) -#define PyArray_IsPythonScalar(obj) \ - (PyArray_IsPythonNumber(obj) || PyString_Check(obj) || \ - PyUnicode_Check(obj)) -#endif #define PyArray_IsAnyScalar(obj) \ (PyArray_IsScalar(obj, Generic) || PyArray_IsPythonScalar(obj)) @@ -248,11 +236,6 @@ NPY_TITLE_KEY_check(PyObject *key, PyObject *value) if (PyUnicode_Check(title) && PyUnicode_Check(key)) { return PyUnicode_Compare(title, key) == 0 ? 1 : 0; } -#if PY_VERSION_HEX < 0x03000000 - if (PyString_Check(title) && PyString_Check(key)) { - return PyObject_Compare(title, key) == 0 ? 1 : 0; - } -#endif #endif return 0; } diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index ad98d562b..bec6fcf30 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -353,21 +353,12 @@ struct NpyAuxData_tag { #define NPY_USE_PYMEM 1 + #if NPY_USE_PYMEM == 1 - /* numpy sometimes calls PyArray_malloc() with the GIL released. On Python - 3.3 and older, it was safe to call PyMem_Malloc() with the GIL released. - On Python 3.4 and newer, it's better to use PyMem_RawMalloc() to be able - to use tracemalloc. On Python 3.6, calling PyMem_Malloc() with the GIL - released is now a fatal error in debug mode. */ -# if PY_VERSION_HEX >= 0x03040000 -# define PyArray_malloc PyMem_RawMalloc -# define PyArray_free PyMem_RawFree -# define PyArray_realloc PyMem_RawRealloc -# else -# define PyArray_malloc PyMem_Malloc -# define PyArray_free PyMem_Free -# define PyArray_realloc PyMem_Realloc -# endif +/* use the Raw versions which are safe to call with the GIL released */ +#define PyArray_malloc PyMem_RawMalloc +#define PyArray_free PyMem_RawFree +#define PyArray_realloc PyMem_RawRealloc #else #define PyArray_malloc malloc #define PyArray_free free diff --git a/numpy/core/include/numpy/npy_1_7_deprecated_api.h b/numpy/core/include/numpy/npy_1_7_deprecated_api.h index a6ee21219..440458010 100644 --- a/numpy/core/include/numpy/npy_1_7_deprecated_api.h +++ b/numpy/core/include/numpy/npy_1_7_deprecated_api.h @@ -69,18 +69,11 @@ #define PyArray_DEFAULT NPY_DEFAULT_TYPE /* These DATETIME bits aren't used internally */ -#if PY_VERSION_HEX >= 0x03000000 #define PyDataType_GetDatetimeMetaData(descr) \ ((descr->metadata == NULL) ? NULL : \ ((PyArray_DatetimeMetaData *)(PyCapsule_GetPointer( \ PyDict_GetItemString( \ descr->metadata, NPY_METADATA_DTSTR), NULL)))) -#else -#define PyDataType_GetDatetimeMetaData(descr) \ - ((descr->metadata == NULL) ? NULL : \ - ((PyArray_DatetimeMetaData *)(PyCObject_AsVoidPtr( \ - PyDict_GetItemString(descr->metadata, NPY_METADATA_DTSTR))))) -#endif /* * Deprecated as of NumPy 1.7, this kind of shortcut doesn't diff --git a/numpy/core/include/numpy/npy_3kcompat.h b/numpy/core/include/numpy/npy_3kcompat.h index 7ed263796..dbb5bd506 100644 --- a/numpy/core/include/numpy/npy_3kcompat.h +++ b/numpy/core/include/numpy/npy_3kcompat.h @@ -13,11 +13,9 @@ #include <Python.h> #include <stdio.h> -#if PY_VERSION_HEX >= 0x03000000 #ifndef NPY_PY3K #define NPY_PY3K 1 #endif -#endif #include "numpy/npy_common.h" #include "numpy/ndarrayobject.h" @@ -62,13 +60,7 @@ static NPY_INLINE int PyInt_Check(PyObject *op) { PySlice_GetIndicesEx((PySliceObject *)op, nop, start, end, step, slicelength) #endif -/* <2.7.11 and <3.4.4 have the wrong argument type for Py_EnterRecursiveCall */ -#if (PY_VERSION_HEX < 0x02070B00) || \ - ((0x03000000 <= PY_VERSION_HEX) && (PY_VERSION_HEX < 0x03040400)) - #define Npy_EnterRecursiveCall(x) Py_EnterRecursiveCall((char *)(x)) -#else - #define Npy_EnterRecursiveCall(x) Py_EnterRecursiveCall(x) -#endif +#define Npy_EnterRecursiveCall(x) Py_EnterRecursiveCall(x) /* Py_SETREF was added in 3.5.2, and only if Py_LIMITED_API is absent */ #if PY_VERSION_HEX < 0x03050200 @@ -490,8 +482,6 @@ PyObject_Cmp(PyObject *i1, PyObject *i2, int *cmp) * The main job here is to get rid of the improved error handling * of PyCapsules. It's a shame... */ -#if PY_VERSION_HEX >= 0x03000000 - static NPY_INLINE PyObject * NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *)) { @@ -536,41 +526,6 @@ NpyCapsule_Check(PyObject *ptr) return PyCapsule_CheckExact(ptr); } -#else - -static NPY_INLINE PyObject * -NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *)) -{ - return PyCObject_FromVoidPtr(ptr, dtor); -} - -static NPY_INLINE PyObject * -NpyCapsule_FromVoidPtrAndDesc(void *ptr, void* context, - void (*dtor)(void *, void *)) -{ - return PyCObject_FromVoidPtrAndDesc(ptr, context, dtor); -} - -static NPY_INLINE void * -NpyCapsule_AsVoidPtr(PyObject *ptr) -{ - return PyCObject_AsVoidPtr(ptr); -} - -static NPY_INLINE void * -NpyCapsule_GetDesc(PyObject *obj) -{ - return PyCObject_GetDesc(obj); -} - -static NPY_INLINE int -NpyCapsule_Check(PyObject *ptr) -{ - return PyCObject_Check(ptr); -} - -#endif - #ifdef __cplusplus } #endif diff --git a/numpy/core/include/numpy/npy_common.h b/numpy/core/include/numpy/npy_common.h index 27b83f7b5..c2e755958 100644 --- a/numpy/core/include/numpy/npy_common.h +++ b/numpy/core/include/numpy/npy_common.h @@ -369,18 +369,8 @@ typedef long npy_long; typedef float npy_float; typedef double npy_double; -/* - * Hash value compatibility. - * As of Python 3.2 hash values are of type Py_hash_t. - * Previous versions use C long. - */ -#if PY_VERSION_HEX < 0x03020000 -typedef long npy_hash_t; -#define NPY_SIZEOF_HASH_T NPY_SIZEOF_LONG -#else typedef Py_hash_t npy_hash_t; #define NPY_SIZEOF_HASH_T NPY_SIZEOF_INTP -#endif /* * Disabling C99 complex usage: a lot of C code in numpy/scipy rely on being diff --git a/numpy/core/machar.py b/numpy/core/machar.py index 202580bdb..a48dc3d50 100644 --- a/numpy/core/machar.py +++ b/numpy/core/machar.py @@ -5,8 +5,6 @@ floating-point arithmetic system Author: Pearu Peterson, September 2003 """ -from __future__ import division, absolute_import, print_function - __all__ = ['MachAr'] from numpy.core.fromnumeric import any @@ -16,7 +14,7 @@ from numpy.core.overrides import set_module # Need to speed this up...especially for longfloat @set_module('numpy') -class MachAr(object): +class MachAr: """ Diagnosing machine parameters. diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index 062645551..ad0d7ad79 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from .numeric import uint8, ndarray, dtype from numpy.compat import ( diff --git a/numpy/core/multiarray.py b/numpy/core/multiarray.py index c0fcc10ff..5749afdcc 100644 --- a/numpy/core/multiarray.py +++ b/numpy/core/multiarray.py @@ -33,7 +33,7 @@ __all__ = [ 'digitize', 'dot', 'dragon4_positional', 'dragon4_scientific', 'dtype', 'empty', 'empty_like', 'error', 'flagsobj', 'flatiter', 'format_longfloat', 'frombuffer', 'fromfile', 'fromiter', 'fromstring', 'inner', - 'int_asbuffer', 'interp', 'interp_complex', 'is_busday', 'lexsort', + 'interp', 'interp_complex', 'is_busday', 'lexsort', 'matmul', 'may_share_memory', 'min_scalar_type', 'ndarray', 'nditer', 'nested_iters', 'normalize_axis_index', 'packbits', 'promote_types', 'putmask', 'ravel_multi_index', 'result_type', 'scalar', @@ -41,8 +41,6 @@ __all__ = [ 'set_string_function', 'set_typeDict', 'shares_memory', 'test_interrupt', 'tracemalloc_domain', 'typeinfo', 'unpackbits', 'unravel_index', 'vdot', 'where', 'zeros'] -if sys.version_info.major < 3: - __all__ += ['newbuffer', 'getbuffer'] # For backward compatibility, make sure pickle imports these functions from here _reconstruct.__module__ = 'numpy.core.multiarray' diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 1e011e2e7..f18ab6336 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import functools import itertools import operator @@ -17,12 +15,10 @@ from .multiarray import ( WRAP, arange, array, broadcast, can_cast, compare_chararrays, concatenate, copyto, dot, dtype, empty, empty_like, flatiter, frombuffer, fromfile, fromiter, fromstring, - inner, int_asbuffer, lexsort, matmul, may_share_memory, + inner, lexsort, matmul, may_share_memory, min_scalar_type, ndarray, nditer, nested_iters, promote_types, putmask, result_type, set_numeric_ops, shares_memory, vdot, where, zeros, normalize_axis_index) -if sys.version_info[0] < 3: - from .multiarray import newbuffer, getbuffer from . import overrides from . import umath @@ -52,7 +48,7 @@ array_function_dispatch = functools.partial( __all__ = [ 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', 'arange', 'array', 'zeros', 'count_nonzero', 'empty', 'broadcast', 'dtype', - 'fromstring', 'fromfile', 'frombuffer', 'int_asbuffer', 'where', + 'fromstring', 'fromfile', 'frombuffer', 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', 'min_scalar_type', 'result_type', 'isfortran', 'empty_like', 'zeros_like', 'ones_like', @@ -67,9 +63,6 @@ __all__ = [ 'matmul', 'shares_memory', 'may_share_memory', 'MAY_SHARE_BOUNDS', 'MAY_SHARE_EXACT', 'TooHardError', 'AxisError'] -if sys.version_info[0] < 3: - __all__.extend(['getbuffer', 'newbuffer']) - @set_module('numpy') class ComplexWarning(RuntimeWarning): @@ -1723,7 +1716,7 @@ def indices(dimensions, dtype=int, sparse=False): @set_module('numpy') -def fromfunction(function, shape, **kwargs): +def fromfunction(function, shape, *, dtype=float, **kwargs): """ Construct an array by executing a function over each coordinate. @@ -1774,7 +1767,6 @@ def fromfunction(function, shape, **kwargs): [2, 3, 4]]) """ - dtype = kwargs.pop('dtype', float) args = indices(shape, dtype=dtype) return function(*args, **kwargs) diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 761c7087c..c63ea08c7 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -79,8 +79,6 @@ Exported symbols include: \\-> object_ (not used much) (kind=O) """ -from __future__ import division, absolute_import, print_function - import types as _types import sys import numbers diff --git a/numpy/core/records.py b/numpy/core/records.py index a1cad9075..6717dc69b 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -33,8 +33,6 @@ Record arrays allow us to access fields as properties:: array([2., 2.]) """ -from __future__ import division, absolute_import, print_function - import sys import os import warnings @@ -98,7 +96,7 @@ def find_duplicate(list): @set_module('numpy') -class format_parser(object): +class format_parser: """ Class to convert formats, names, titles description to a dtype. diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 974ec4628..e376d5a23 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - import os import sys import pickle @@ -38,7 +36,7 @@ NPY_RELAXED_STRIDES_DEBUG = NPY_RELAXED_STRIDES_DEBUG and NPY_RELAXED_STRIDES_CH # Use pickle in all cases, as cPickle is gone in python3 and the difference # in time is only in build. -- Charles Harris, 2013-03-30 -class CallOnceOnly(object): +class CallOnceOnly: def __init__(self): self._check_types = None self._check_ieee_macros = None @@ -469,10 +467,6 @@ def configuration(parent_package='',top_path=None): moredefs.append('NPY_DO_NOT_OPTIMIZE_LONGLONG_right_shift') moredefs.append('NPY_DO_NOT_OPTIMIZE_ULONGLONG_right_shift') - # Py3K check - if sys.version_info[0] >= 3: - moredefs.append(('NPY_PY3K', 1)) - # Generate the config.h file from moredefs with open(target, 'w') as target_f: for d in moredefs: diff --git a/numpy/core/setup_common.py b/numpy/core/setup_common.py index 6356f08ba..a947f7a3d 100644 --- a/numpy/core/setup_common.py +++ b/numpy/core/setup_common.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - # Code common to build tools import sys import warnings diff --git a/numpy/core/shape_base.py b/numpy/core/shape_base.py index 31b1c20b9..d2f26149b 100644 --- a/numpy/core/shape_base.py +++ b/numpy/core/shape_base.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __all__ = ['atleast_1d', 'atleast_2d', 'atleast_3d', 'block', 'hstack', 'stack', 'vstack'] diff --git a/numpy/core/src/common/array_assign.c b/numpy/core/src/common/array_assign.c index 0ac1b01c6..d626d1260 100644 --- a/numpy/core/src/common/array_assign.c +++ b/numpy/core/src/common/array_assign.c @@ -27,9 +27,9 @@ /* See array_assign.h for parameter documentation */ NPY_NO_EXPORT int -broadcast_strides(int ndim, npy_intp *shape, - int strides_ndim, npy_intp *strides_shape, npy_intp *strides, - char *strides_name, +broadcast_strides(int ndim, npy_intp const *shape, + int strides_ndim, npy_intp const *strides_shape, npy_intp const *strides, + char const *strides_name, npy_intp *out_strides) { int idim, idim_start = ndim - strides_ndim; @@ -84,8 +84,8 @@ broadcast_error: { /* See array_assign.h for parameter documentation */ NPY_NO_EXPORT int -raw_array_is_aligned(int ndim, npy_intp *shape, - char *data, npy_intp *strides, int alignment) +raw_array_is_aligned(int ndim, npy_intp const *shape, + char *data, npy_intp const *strides, int alignment) { /* diff --git a/numpy/core/src/common/array_assign.h b/numpy/core/src/common/array_assign.h index 69ef56bb4..f5d884dd9 100644 --- a/numpy/core/src/common/array_assign.h +++ b/numpy/core/src/common/array_assign.h @@ -44,8 +44,8 @@ PyArray_AssignRawScalar(PyArrayObject *dst, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -raw_array_assign_scalar(int ndim, npy_intp *shape, - PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, +raw_array_assign_scalar(int ndim, npy_intp const *shape, + PyArray_Descr *dst_dtype, char *dst_data, npy_intp const *dst_strides, PyArray_Descr *src_dtype, char *src_data); /* @@ -55,11 +55,11 @@ raw_array_assign_scalar(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -raw_array_wheremasked_assign_scalar(int ndim, npy_intp *shape, - PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, +raw_array_wheremasked_assign_scalar(int ndim, npy_intp const *shape, + PyArray_Descr *dst_dtype, char *dst_data, npy_intp const *dst_strides, PyArray_Descr *src_dtype, char *src_data, PyArray_Descr *wheremask_dtype, char *wheremask_data, - npy_intp *wheremask_strides); + npy_intp const *wheremask_strides); /******** LOW-LEVEL ARRAY MANIPULATION HELPERS ********/ @@ -80,9 +80,9 @@ raw_array_wheremasked_assign_scalar(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -broadcast_strides(int ndim, npy_intp *shape, - int strides_ndim, npy_intp *strides_shape, npy_intp *strides, - char *strides_name, +broadcast_strides(int ndim, npy_intp const *shape, + int strides_ndim, npy_intp const *strides_shape, npy_intp const *strides, + char const *strides_name, npy_intp *out_strides); /* @@ -93,8 +93,8 @@ broadcast_strides(int ndim, npy_intp *shape, * cannot-be-aligned, in which case 0 (false) is always returned. */ NPY_NO_EXPORT int -raw_array_is_aligned(int ndim, npy_intp *shape, - char *data, npy_intp *strides, int alignment); +raw_array_is_aligned(int ndim, npy_intp const *shape, + char *data, npy_intp const *strides, int alignment); /* * Checks if an array is aligned to its "true alignment" diff --git a/numpy/core/src/common/get_attr_string.h b/numpy/core/src/common/get_attr_string.h index d3401aea6..8b7cf1c5b 100644 --- a/numpy/core/src/common/get_attr_string.h +++ b/numpy/core/src/common/get_attr_string.h @@ -7,9 +7,6 @@ _is_basic_python_type(PyTypeObject *tp) return ( /* Basic number types */ tp == &PyBool_Type || -#if !defined(NPY_PY3K) - tp == &PyInt_Type || -#endif tp == &PyLong_Type || tp == &PyFloat_Type || tp == &PyComplex_Type || @@ -22,9 +19,6 @@ _is_basic_python_type(PyTypeObject *tp) tp == &PyFrozenSet_Type || tp == &PyUnicode_Type || tp == &PyBytes_Type || -#if !defined(NPY_PY3K) - tp == &PyString_Type || -#endif /* other builtins */ tp == &PySlice_Type || @@ -50,25 +44,21 @@ _is_basic_python_type(PyTypeObject *tp) * there is no such attribute, and NULL with an exception on failure. */ static NPY_INLINE PyObject * -maybe_get_attr(PyObject *obj, char *name) +maybe_get_attr(PyObject *obj, char const *name) { PyTypeObject *tp = Py_TYPE(obj); PyObject *res = (PyObject *)NULL; /* Attribute referenced by (char *)name */ if (tp->tp_getattr != NULL) { - res = (*tp->tp_getattr)(obj, name); + res = (*tp->tp_getattr)(obj, (char *)name); if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); } } /* Attribute referenced by (PyObject *)name */ else if (tp->tp_getattro != NULL) { -#if defined(NPY_PY3K) PyObject *w = PyUnicode_InternFromString(name); -#else - PyObject *w = PyString_InternFromString(name); -#endif if (w == NULL) { return (PyObject *)NULL; } @@ -91,7 +81,7 @@ maybe_get_attr(PyObject *obj, char *name) * In future, could be made more like _Py_LookupSpecial */ static NPY_INLINE PyObject * -PyArray_LookupSpecial(PyObject *obj, char *name) +PyArray_LookupSpecial(PyObject *obj, char const *name) { PyTypeObject *tp = Py_TYPE(obj); @@ -111,7 +101,7 @@ PyArray_LookupSpecial(PyObject *obj, char *name) * Kept for backwards compatibility. In future, we should deprecate this. */ static NPY_INLINE PyObject * -PyArray_LookupSpecial_OnInstance(PyObject *obj, char *name) +PyArray_LookupSpecial_OnInstance(PyObject *obj, char const *name) { PyTypeObject *tp = Py_TYPE(obj); diff --git a/numpy/core/src/common/lowlevel_strided_loops.h b/numpy/core/src/common/lowlevel_strided_loops.h index bacd27473..9208d5499 100644 --- a/numpy/core/src/common/lowlevel_strided_loops.h +++ b/numpy/core/src/common/lowlevel_strided_loops.h @@ -306,30 +306,30 @@ PyArray_CastRawArrays(npy_intp count, NPY_NO_EXPORT npy_intp PyArray_TransferNDimToStrided(npy_intp ndim, char *dst, npy_intp dst_stride, - char *src, npy_intp *src_strides, npy_intp src_strides_inc, - npy_intp *coords, npy_intp coords_inc, - npy_intp *shape, npy_intp shape_inc, + char *src, npy_intp const *src_strides, npy_intp src_strides_inc, + npy_intp const *coords, npy_intp coords_inc, + npy_intp const *shape, npy_intp shape_inc, npy_intp count, npy_intp src_itemsize, PyArray_StridedUnaryOp *stransfer, NpyAuxData *transferdata); NPY_NO_EXPORT npy_intp PyArray_TransferStridedToNDim(npy_intp ndim, - char *dst, npy_intp *dst_strides, npy_intp dst_strides_inc, + char *dst, npy_intp const *dst_strides, npy_intp dst_strides_inc, char *src, npy_intp src_stride, - npy_intp *coords, npy_intp coords_inc, - npy_intp *shape, npy_intp shape_inc, + npy_intp const *coords, npy_intp coords_inc, + npy_intp const *shape, npy_intp shape_inc, npy_intp count, npy_intp src_itemsize, PyArray_StridedUnaryOp *stransfer, NpyAuxData *transferdata); NPY_NO_EXPORT npy_intp PyArray_TransferMaskedStridedToNDim(npy_intp ndim, - char *dst, npy_intp *dst_strides, npy_intp dst_strides_inc, + char *dst, npy_intp const *dst_strides, npy_intp dst_strides_inc, char *src, npy_intp src_stride, npy_bool *mask, npy_intp mask_stride, - npy_intp *coords, npy_intp coords_inc, - npy_intp *shape, npy_intp shape_inc, + npy_intp const *coords, npy_intp coords_inc, + npy_intp const *shape, npy_intp shape_inc, npy_intp count, npy_intp src_itemsize, PyArray_MaskedStridedUnaryOp *stransfer, NpyAuxData *data); @@ -365,8 +365,8 @@ mapiter_set(PyArrayMapIterObject *mit); * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -PyArray_PrepareOneRawArrayIter(int ndim, npy_intp *shape, - char *data, npy_intp *strides, +PyArray_PrepareOneRawArrayIter(int ndim, npy_intp const *shape, + char *data, npy_intp const *strides, int *out_ndim, npy_intp *out_shape, char **out_data, npy_intp *out_strides); @@ -387,9 +387,9 @@ PyArray_PrepareOneRawArrayIter(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -PyArray_PrepareTwoRawArrayIter(int ndim, npy_intp *shape, - char *dataA, npy_intp *stridesA, - char *dataB, npy_intp *stridesB, +PyArray_PrepareTwoRawArrayIter(int ndim, npy_intp const *shape, + char *dataA, npy_intp const *stridesA, + char *dataB, npy_intp const *stridesB, int *out_ndim, npy_intp *out_shape, char **out_dataA, npy_intp *out_stridesA, char **out_dataB, npy_intp *out_stridesB); @@ -411,10 +411,10 @@ PyArray_PrepareTwoRawArrayIter(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -PyArray_PrepareThreeRawArrayIter(int ndim, npy_intp *shape, - char *dataA, npy_intp *stridesA, - char *dataB, npy_intp *stridesB, - char *dataC, npy_intp *stridesC, +PyArray_PrepareThreeRawArrayIter(int ndim, npy_intp const *shape, + char *dataA, npy_intp const *stridesA, + char *dataB, npy_intp const *stridesB, + char *dataC, npy_intp const *stridesC, int *out_ndim, npy_intp *out_shape, char **out_dataA, npy_intp *out_stridesA, char **out_dataB, npy_intp *out_stridesB, diff --git a/numpy/core/src/common/npy_longdouble.c b/numpy/core/src/common/npy_longdouble.c index c580e0cce..260e02a64 100644 --- a/numpy/core/src/common/npy_longdouble.c +++ b/numpy/core/src/common/npy_longdouble.c @@ -100,16 +100,12 @@ done: static PyObject * _PyLong_Bytes(PyObject *long_obj) { PyObject *bytes; -#if defined(NPY_PY3K) PyObject *unicode = PyObject_Str(long_obj); if (unicode == NULL) { return NULL; } bytes = PyUnicode_AsUTF8String(unicode); Py_DECREF(unicode); -#else - bytes = PyObject_Str(long_obj); -#endif return bytes; } diff --git a/numpy/core/src/common/ucsnarrow.c b/numpy/core/src/common/ucsnarrow.c index 8e293e9f2..946a72257 100644 --- a/numpy/core/src/common/ucsnarrow.c +++ b/numpy/core/src/common/ucsnarrow.c @@ -31,7 +31,7 @@ * Values above 0xffff are converted to surrogate pairs. */ NPY_NO_EXPORT int -PyUCS2Buffer_FromUCS4(Py_UNICODE *ucs2, npy_ucs4 *ucs4, int ucs4length) +PyUCS2Buffer_FromUCS4(Py_UNICODE *ucs2, npy_ucs4 const *ucs4, int ucs4length) { int i; int numucs2 = 0; @@ -63,7 +63,7 @@ PyUCS2Buffer_FromUCS4(Py_UNICODE *ucs2, npy_ucs4 *ucs4, int ucs4length) * The return value is the actual size of the used part of the ucs4 buffer. */ NPY_NO_EXPORT int -PyUCS2Buffer_AsUCS4(Py_UNICODE *ucs2, npy_ucs4 *ucs4, int ucs2len, int ucs4len) +PyUCS2Buffer_AsUCS4(Py_UNICODE const *ucs2, npy_ucs4 *ucs4, int ucs2len, int ucs4len) { int i; npy_ucs4 chr; @@ -107,11 +107,11 @@ PyUCS2Buffer_AsUCS4(Py_UNICODE *ucs2, npy_ucs4 *ucs4, int ucs2len, int ucs4len) * new_reference: PyUnicodeObject */ NPY_NO_EXPORT PyUnicodeObject * -PyUnicode_FromUCS4(char *src, Py_ssize_t size, int swap, int align) +PyUnicode_FromUCS4(char const *src_char, Py_ssize_t size, int swap, int align) { Py_ssize_t ucs4len = size / sizeof(npy_ucs4); - npy_ucs4 *buf = (npy_ucs4 *)src; - int alloc = 0; + npy_ucs4 const *src = (npy_ucs4 const *)src_char; + npy_ucs4 *buf = NULL; PyUnicodeObject *ret; /* swap and align if needed */ @@ -121,22 +121,22 @@ PyUnicode_FromUCS4(char *src, Py_ssize_t size, int swap, int align) PyErr_NoMemory(); goto fail; } - alloc = 1; memcpy(buf, src, size); if (swap) { byte_swap_vector(buf, ucs4len, sizeof(npy_ucs4)); } + src = buf; } /* trim trailing zeros */ - while (ucs4len > 0 && buf[ucs4len - 1] == 0) { + while (ucs4len > 0 && src[ucs4len - 1] == 0) { ucs4len--; } /* produce PyUnicode object */ #ifdef Py_UNICODE_WIDE { - ret = (PyUnicodeObject *)PyUnicode_FromUnicode((Py_UNICODE*)buf, + ret = (PyUnicodeObject *)PyUnicode_FromUnicode((Py_UNICODE const*)src, (Py_ssize_t) ucs4len); if (ret == NULL) { goto fail; @@ -152,7 +152,7 @@ PyUnicode_FromUCS4(char *src, Py_ssize_t size, int swap, int align) PyErr_NoMemory(); goto fail; } - ucs2len = PyUCS2Buffer_FromUCS4(tmp, buf, ucs4len); + ucs2len = PyUCS2Buffer_FromUCS4(tmp, src, ucs4len); ret = (PyUnicodeObject *)PyUnicode_FromUnicode(tmp, (Py_ssize_t) ucs2len); free(tmp); if (ret == NULL) { @@ -161,13 +161,13 @@ PyUnicode_FromUCS4(char *src, Py_ssize_t size, int swap, int align) } #endif - if (alloc) { + if (buf) { free(buf); } return ret; fail: - if (alloc) { + if (buf) { free(buf); } return NULL; diff --git a/numpy/core/src/dummymodule.c b/numpy/core/src/dummymodule.c index 718199f70..e26875736 100644 --- a/numpy/core/src/dummymodule.c +++ b/numpy/core/src/dummymodule.c @@ -16,7 +16,6 @@ static struct PyMethodDef methods[] = { }; -#if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "dummy", @@ -28,10 +27,8 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif /* Initialization function for the module */ -#if defined(NPY_PY3K) PyMODINIT_FUNC PyInit__dummy(void) { PyObject *m; m = PyModule_Create(&moduledef); @@ -40,9 +37,3 @@ PyMODINIT_FUNC PyInit__dummy(void) { } return m; } -#else -PyMODINIT_FUNC -init_dummy(void) { - Py_InitModule("_dummy", methods); -} -#endif diff --git a/numpy/core/src/multiarray/_datetime.h b/numpy/core/src/multiarray/_datetime.h index 3db1254d4..20f7a132c 100644 --- a/numpy/core/src/multiarray/_datetime.h +++ b/numpy/core/src/multiarray/_datetime.h @@ -1,7 +1,7 @@ #ifndef _NPY_PRIVATE__DATETIME_H_ #define _NPY_PRIVATE__DATETIME_H_ -extern NPY_NO_EXPORT char *_datetime_strings[NPY_DATETIME_NUMUNITS]; +extern NPY_NO_EXPORT char const *_datetime_strings[NPY_DATETIME_NUMUNITS]; extern NPY_NO_EXPORT int _days_per_month_table[2][12]; NPY_NO_EXPORT void @@ -68,7 +68,7 @@ days_to_month_number(npy_datetime days); * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -parse_datetime_metadata_from_metastr(char *metastr, Py_ssize_t len, +parse_datetime_metadata_from_metastr(char const *metastr, Py_ssize_t len, PyArray_DatetimeMetaData *out_meta); @@ -78,7 +78,7 @@ parse_datetime_metadata_from_metastr(char *metastr, Py_ssize_t len, * contain its string length. */ NPY_NO_EXPORT PyArray_Descr * -parse_dtype_from_datetime_typestr(char *typestr, Py_ssize_t len); +parse_dtype_from_datetime_typestr(char const *typestr, Py_ssize_t len); /* * Converts a substring given by 'str' and 'len' into @@ -88,7 +88,7 @@ parse_dtype_from_datetime_typestr(char *typestr, Py_ssize_t len); * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT NPY_DATETIMEUNIT -parse_datetime_unit_from_string(char *str, Py_ssize_t len, char *metastr); +parse_datetime_unit_from_string(char const *str, Py_ssize_t len, char const *metastr); /* * Translate divisors into multiples of smaller units. @@ -99,7 +99,7 @@ parse_datetime_unit_from_string(char *str, Py_ssize_t len, char *metastr); */ NPY_NO_EXPORT int convert_datetime_divisor_to_multiple(PyArray_DatetimeMetaData *meta, - int den, char *metastr); + int den, char const *metastr); /* * Determines whether the 'divisor' metadata divides evenly into diff --git a/numpy/core/src/multiarray/_multiarray_tests.c.src b/numpy/core/src/multiarray/_multiarray_tests.c.src index fa2efb428..7007dd204 100644 --- a/numpy/core/src/multiarray/_multiarray_tests.c.src +++ b/numpy/core/src/multiarray/_multiarray_tests.c.src @@ -55,7 +55,7 @@ EXPORT(void*) forward_pointer(void *x) * #typenum = NPY_DOUBLE, NPY_INT# */ static int copy_@name@(PyArrayIterObject *itx, PyArrayNeighborhoodIterObject *niterx, - npy_intp *bounds, + npy_intp const *bounds, PyObject **out) { npy_intp i, j; @@ -97,7 +97,7 @@ static int copy_@name@(PyArrayIterObject *itx, PyArrayNeighborhoodIterObject *ni /**end repeat**/ static int copy_object(PyArrayIterObject *itx, PyArrayNeighborhoodIterObject *niterx, - npy_intp *bounds, + npy_intp const *bounds, PyObject **out) { npy_intp i, j; @@ -251,7 +251,7 @@ clean_ax: static int copy_double_double(PyArrayNeighborhoodIterObject *itx, PyArrayNeighborhoodIterObject *niterx, - npy_intp *bounds, + npy_intp const *bounds, PyObject **out) { npy_intp i, j; @@ -771,30 +771,6 @@ npy_discard(PyObject* NPY_UNUSED(self), PyObject* args) Py_RETURN_NONE; } -#if !defined(NPY_PY3K) -static PyObject * -int_subclass(PyObject *dummy, PyObject *args) -{ - - PyObject *result = NULL; - PyObject *scalar_object = NULL; - - if (!PyArg_UnpackTuple(args, "test_int_subclass", 1, 1, &scalar_object)) - return NULL; - - if (PyInt_Check(scalar_object)) - result = Py_True; - else - result = Py_False; - - Py_INCREF(result); - - return result; - -} -#endif - - /* * Create python string from a FLAG and or the corresponding PyBuf flag * for the use in get_buffer_info. @@ -1210,11 +1186,7 @@ array_solve_diophantine(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject for (j = 0; j < nterms; ++j) { PyObject *obj; -#if defined(NPY_PY3K) obj = PyLong_FromSsize_t(x[j]); -#else - obj = PyInt_FromSsize_t(x[j]); -#endif if (obj == NULL) { goto fail; } @@ -2018,11 +1990,6 @@ static PyMethodDef Multiarray_TestsMethods[] = { {"npy_discard", npy_discard, METH_O, NULL}, -#if !defined(NPY_PY3K) - {"test_int_subclass", - int_subclass, - METH_VARARGS, NULL}, -#endif {"get_buffer_info", get_buffer_info, METH_VARARGS, NULL}, @@ -2126,7 +2093,6 @@ static PyMethodDef Multiarray_TestsMethods[] = { }; -#if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_multiarray_tests", @@ -2138,33 +2104,21 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif -#if defined(NPY_PY3K) -#define RETVAL m PyMODINIT_FUNC PyInit__multiarray_tests(void) -#else -#define RETVAL -PyMODINIT_FUNC -init_multiarray_tests(void) -#endif { PyObject *m; -#if defined(NPY_PY3K) m = PyModule_Create(&moduledef); -#else - m = Py_InitModule("_multiarray_tests", Multiarray_TestsMethods); -#endif if (m == NULL) { - return RETVAL; + return m; } import_array(); if (PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "cannot load _multiarray_tests module."); } - return RETVAL; + return m; } NPY_NO_EXPORT int diff --git a/numpy/core/src/multiarray/alloc.c b/numpy/core/src/multiarray/alloc.c index a7f34cbe5..c2b7e9ca7 100644 --- a/numpy/core/src/multiarray/alloc.c +++ b/numpy/core/src/multiarray/alloc.c @@ -48,11 +48,6 @@ static cache_bucket datacache[NBUCKETS]; static cache_bucket dimcache[NBUCKETS_DIM]; /* as the cache is managed in global variables verify the GIL is held */ -#if defined(NPY_PY3K) -#define NPY_CHECK_GIL_HELD() PyGILState_Check() -#else -#define NPY_CHECK_GIL_HELD() 1 -#endif /* * very simplistic small memory block cache to avoid more expensive libc @@ -67,7 +62,7 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz, void * p; assert((esz == 1 && cache == datacache) || (esz == sizeof(npy_intp) && cache == dimcache)); - assert(NPY_CHECK_GIL_HELD()); + assert(PyGILState_Check()); if (nelem < msz) { if (cache[nelem].available > 0) { return cache[nelem].ptrs[--(cache[nelem].available)]; @@ -102,7 +97,7 @@ static NPY_INLINE void _npy_free_cache(void * p, npy_uintp nelem, npy_uint msz, cache_bucket * cache, void (*dealloc)(void *)) { - assert(NPY_CHECK_GIL_HELD()); + assert(PyGILState_Check()); if (p != NULL && nelem < msz) { if (cache[nelem].available < NCACHE) { cache[nelem].ptrs[cache[nelem].available++] = p; diff --git a/numpy/core/src/multiarray/array_assign_array.c b/numpy/core/src/multiarray/array_assign_array.c index 7ff33ebd7..e40b6c719 100644 --- a/numpy/core/src/multiarray/array_assign_array.c +++ b/numpy/core/src/multiarray/array_assign_array.c @@ -29,8 +29,8 @@ * elements, as required by the copy/casting code in lowlevel_strided_loops.c */ NPY_NO_EXPORT int -copycast_isaligned(int ndim, npy_intp *shape, - PyArray_Descr *dtype, char *data, npy_intp *strides) +copycast_isaligned(int ndim, npy_intp const *shape, + PyArray_Descr *dtype, char *data, npy_intp const *strides) { int aligned; int big_aln, small_aln; @@ -72,9 +72,9 @@ copycast_isaligned(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -raw_array_assign_array(int ndim, npy_intp *shape, - PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, - PyArray_Descr *src_dtype, char *src_data, npy_intp *src_strides) +raw_array_assign_array(int ndim, npy_intp const *shape, + PyArray_Descr *dst_dtype, char *dst_data, npy_intp const *dst_strides, + PyArray_Descr *src_dtype, char *src_data, npy_intp const *src_strides) { int idim; npy_intp shape_it[NPY_MAXDIMS]; @@ -152,11 +152,11 @@ raw_array_assign_array(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -raw_array_wheremasked_assign_array(int ndim, npy_intp *shape, - PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, - PyArray_Descr *src_dtype, char *src_data, npy_intp *src_strides, +raw_array_wheremasked_assign_array(int ndim, npy_intp const *shape, + PyArray_Descr *dst_dtype, char *dst_data, npy_intp const *dst_strides, + PyArray_Descr *src_dtype, char *src_data, npy_intp const *src_strides, PyArray_Descr *wheremask_dtype, char *wheremask_data, - npy_intp *wheremask_strides) + npy_intp const *wheremask_strides) { int idim; npy_intp shape_it[NPY_MAXDIMS]; diff --git a/numpy/core/src/multiarray/array_assign_scalar.c b/numpy/core/src/multiarray/array_assign_scalar.c index ecb5be47b..6bc9bcfee 100644 --- a/numpy/core/src/multiarray/array_assign_scalar.c +++ b/numpy/core/src/multiarray/array_assign_scalar.c @@ -30,8 +30,8 @@ * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -raw_array_assign_scalar(int ndim, npy_intp *shape, - PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, +raw_array_assign_scalar(int ndim, npy_intp const *shape, + PyArray_Descr *dst_dtype, char *dst_data, npy_intp const *dst_strides, PyArray_Descr *src_dtype, char *src_data) { int idim; @@ -101,11 +101,11 @@ raw_array_assign_scalar(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -raw_array_wheremasked_assign_scalar(int ndim, npy_intp *shape, - PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, +raw_array_wheremasked_assign_scalar(int ndim, npy_intp const *shape, + PyArray_Descr *dst_dtype, char *dst_data, npy_intp const *dst_strides, PyArray_Descr *src_dtype, char *src_data, PyArray_Descr *wheremask_dtype, char *wheremask_data, - npy_intp *wheremask_strides) + npy_intp const *wheremask_strides) { int idim; npy_intp shape_it[NPY_MAXDIMS], dst_strides_it[NPY_MAXDIMS]; diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index a5cebfbd8..0c554d31b 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -388,7 +388,7 @@ PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object) /*NUMPY_API */ NPY_NO_EXPORT int -PyArray_TypeNumFromName(char *str) +PyArray_TypeNumFromName(char const *str) { int i; PyArray_Descr *descr; @@ -614,7 +614,7 @@ PyArray_SetDatetimeParseFunction(PyObject *NPY_UNUSED(op)) /*NUMPY_API */ NPY_NO_EXPORT int -PyArray_CompareUCS4(npy_ucs4 *s1, npy_ucs4 *s2, size_t len) +PyArray_CompareUCS4(npy_ucs4 const *s1, npy_ucs4 const *s2, size_t len) { npy_ucs4 c1, c2; while(len-- > 0) { @@ -703,35 +703,40 @@ PyArray_FailUnlessWriteable(PyArrayObject *obj, const char *name) If they are NULL terminated, then stop comparison. */ static int -_myunincmp(npy_ucs4 *s1, npy_ucs4 *s2, int len1, int len2) +_myunincmp(npy_ucs4 const *s1, npy_ucs4 const *s2, int len1, int len2) { - npy_ucs4 *sptr; - npy_ucs4 *s1t=s1, *s2t=s2; + npy_ucs4 const *sptr; + npy_ucs4 *s1t = NULL; + npy_ucs4 *s2t = NULL; int val; npy_intp size; int diff; + /* Replace `s1` and `s2` with aligned copies if needed */ if ((npy_intp)s1 % sizeof(npy_ucs4) != 0) { size = len1*sizeof(npy_ucs4); s1t = malloc(size); memcpy(s1t, s1, size); + s1 = s1t; } if ((npy_intp)s2 % sizeof(npy_ucs4) != 0) { size = len2*sizeof(npy_ucs4); s2t = malloc(size); memcpy(s2t, s2, size); + s2 = s1t; } - val = PyArray_CompareUCS4(s1t, s2t, PyArray_MIN(len1,len2)); + + val = PyArray_CompareUCS4(s1, s2, PyArray_MIN(len1,len2)); if ((val != 0) || (len1 == len2)) { goto finish; } if (len2 > len1) { - sptr = s2t+len1; + sptr = s2+len1; val = -1; diff = len2-len1; } else { - sptr = s1t+len2; + sptr = s1+len2; val = 1; diff=len1-len2; } @@ -744,10 +749,11 @@ _myunincmp(npy_ucs4 *s1, npy_ucs4 *s2, int len1, int len2) val = 0; finish: - if (s1t != s1) { + /* Cleanup the aligned copies */ + if (s1t) { free(s1t); } - if (s2t != s2) { + if (s2t) { free(s2t); } return val; @@ -763,9 +769,9 @@ _myunincmp(npy_ucs4 *s1, npy_ucs4 *s2, int len1, int len2) * If they are NULL terminated, then stop comparison. */ static int -_mystrncmp(char *s1, char *s2, int len1, int len2) +_mystrncmp(char const *s1, char const *s2, int len1, int len2) { - char *sptr; + char const *sptr; int val; int diff; @@ -827,7 +833,7 @@ static void _unistripw(npy_ucs4 *s, int n) static char * -_char_copy_n_strip(char *original, char *temp, int nc) +_char_copy_n_strip(char const *original, char *temp, int nc) { if (nc > SMALL_STRING) { temp = malloc(nc); @@ -850,7 +856,7 @@ _char_release(char *ptr, int nc) } static char * -_uni_copy_n_strip(char *original, char *temp, int nc) +_uni_copy_n_strip(char const *original, char *temp, int nc) { if (nc*sizeof(npy_ucs4) > SMALL_STRING) { temp = malloc(nc*sizeof(npy_ucs4)); @@ -919,7 +925,7 @@ _compare_strings(PyArrayObject *result, PyArrayMultiIterObject *multi, int N1, N2; int (*compfunc)(void *, void *, int, int); void (*relfunc)(char *, int); - char* (*stripfunc)(char *, char *, int); + char* (*stripfunc)(char const *, char *, int); compfunc = func; dptr = (npy_bool *)PyArray_DATA(result); @@ -998,22 +1004,18 @@ _strings_richcompare(PyArrayObject *self, PyArrayObject *other, int cmp_op, { PyArrayObject *result; PyArrayMultiIterObject *mit; - int val, cast = 0; + int val; /* Cast arrays to a common type */ if (PyArray_TYPE(self) != PyArray_DESCR(other)->type_num) { -#if defined(NPY_PY3K) /* * Comparison between Bytes and Unicode is not defined in Py3K; * we follow. */ Py_INCREF(Py_NotImplemented); return Py_NotImplemented; -#else - cast = 1; -#endif /* define(NPY_PY3K) */ } - if (cast || (PyArray_ISNOTSWAPPED(self) != PyArray_ISNOTSWAPPED(other))) { + if (PyArray_ISNOTSWAPPED(self) != PyArray_ISNOTSWAPPED(other)) { PyObject *new; if (PyArray_TYPE(self) == NPY_STRING && PyArray_DESCR(other)->type_num == NPY_UNICODE) { @@ -1331,14 +1333,6 @@ _failed_comparison_workaround(PyArrayObject *self, PyObject *other, int cmp_op) * get us the desired TypeError, but on python 2, one gets strange * ordering, so we emit a warning. */ -#if !defined(NPY_PY3K) - /* 2015-05-14, 1.10 */ - if (DEPRECATE( - "unorderable dtypes; returning scalar but in " - "the future this will be an error") < 0) { - goto fail; - } -#endif Py_XDECREF(exc); Py_XDECREF(val); Py_XDECREF(tb); @@ -1601,7 +1595,7 @@ PyArray_ElementStrides(PyObject *obj) /*NUMPY_API*/ NPY_NO_EXPORT npy_bool PyArray_CheckStrides(int elsize, int nd, npy_intp numbytes, npy_intp offset, - npy_intp *dims, npy_intp *newstrides) + npy_intp const *dims, npy_intp const *newstrides) { npy_intp begin, end; npy_intp lower_offset; @@ -1788,12 +1782,7 @@ array_free(PyObject * v) NPY_NO_EXPORT PyTypeObject PyArray_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.ndarray", /* tp_name */ NPY_SIZEOF_PYARRAYOBJECT, /* tp_basicsize */ 0, /* tp_itemsize */ @@ -1802,11 +1791,7 @@ NPY_NO_EXPORT PyTypeObject PyArray_Type = { (printfunc)NULL, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif (reprfunc)array_repr, /* tp_repr */ &array_as_number, /* tp_as_number */ &array_as_sequence, /* tp_as_sequence */ @@ -1821,12 +1806,7 @@ NPY_NO_EXPORT PyTypeObject PyArray_Type = { (getattrofunc)0, /* tp_getattro */ (setattrofunc)0, /* tp_setattro */ &array_as_buffer, /* tp_as_buffer */ - (Py_TPFLAGS_DEFAULT -#if !defined(NPY_PY3K) - | Py_TPFLAGS_CHECKTYPES - | Py_TPFLAGS_HAVE_NEWBUFFER -#endif - | Py_TPFLAGS_BASETYPE), /* tp_flags */ + (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE), /* tp_flags */ 0, /* tp_doc */ (traverseproc)0, /* tp_traverse */ diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 9e108e3e1..3d6a5eda8 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -330,11 +330,7 @@ string_to_long_double(PyObject*op) /* Convert python long objects to a longdouble, without precision or range * loss via a double. */ - if ((PyLong_Check(op) && !PyBool_Check(op)) -#if !defined(NPY_PY3K) - || (PyInt_Check(op) && !PyBool_Check(op)) -#endif - ) { + if ((PyLong_Check(op) && !PyBool_Check(op))) { return npy_longdouble_from_PyLong(op); } @@ -470,7 +466,6 @@ UNICODE_setitem(PyObject *op, void *ov, void *vap) "setting an array element with a sequence"); return -1; } -#if defined(NPY_PY3K) if (PyBytes_Check(op)) { /* Try to decode from ASCII */ temp = PyUnicode_FromEncodedObject(op, "ASCII", "strict"); @@ -479,9 +474,6 @@ UNICODE_setitem(PyObject *op, void *ov, void *vap) } } else if ((temp=PyObject_Str(op)) == NULL) { -#else - if ((temp=PyObject_Unicode(op)) == NULL) { -#endif return -1; } ptr = PyUnicode_AS_UNICODE(temp); @@ -561,7 +553,6 @@ STRING_setitem(PyObject *op, void *ov, void *vap) "setting an array element with a sequence"); return -1; } -#if defined(NPY_PY3K) if (PyUnicode_Check(op)) { /* Assume ASCII codec -- function similarly as Python 2 */ temp = PyUnicode_AsASCIIString(op); @@ -588,11 +579,6 @@ STRING_setitem(PyObject *op, void *ov, void *vap) return -1; } } -#else - if ((temp = PyObject_Str(op)) == NULL) { - return -1; - } -#endif if (PyBytes_AsStringAndSize(temp, &ptr, &len) < 0) { Py_DECREF(temp); return -1; @@ -748,7 +734,7 @@ _setup_field(int i, PyArray_Descr *descr, PyArrayObject *arr, } ((PyArrayObject_fields *)(arr))->descr = new; - if ((new->alignment > 1) && + if ((new->alignment > 1) && ((((uintptr_t)dstdata + offset) % new->alignment) != 0)) { PyArray_CLEARFLAGS(arr, NPY_ARRAY_ALIGNED); } @@ -836,7 +822,7 @@ VOID_setitem(PyObject *op, void *input, void *vap) if (names_size != PyTuple_Size(op)) { errmsg = PyUString_FromFormat( "could not assign tuple of length %zd to structure " - "with %" NPY_INTP_FMT " fields.", + "with %" NPY_INTP_FMT " fields.", PyTuple_Size(op), names_size); PyErr_SetObject(PyExc_ValueError, errmsg); Py_DECREF(errmsg); @@ -919,7 +905,6 @@ VOID_setitem(PyObject *op, void *input, void *vap) * undiscerning case: It interprets any object as a buffer * and reads as many bytes as possible, padding with 0. */ -#if defined(NPY_PY3K) { Py_buffer view; @@ -933,20 +918,6 @@ VOID_setitem(PyObject *op, void *input, void *vap) PyBuffer_Release(&view); _dealloc_cached_buffer_info(op); } -#else - { - const void *buffer; - Py_ssize_t buflen; - - if (PyObject_AsReadBuffer(op, &buffer, &buflen) < 0) { - return -1; - } - memcpy(ip, buffer, PyArray_MIN(buflen, itemsize)); - if (itemsize > buflen) { - memset(ip + buflen, 0, itemsize - buflen); - } - } -#endif return 0; } @@ -1530,7 +1501,7 @@ OBJECT_to_@TOTYPE@(void *input, void *output, npy_intp n, * #convert = 1*18, 0*3, 1*2, * 1*18, 0*3, 1*2, * 0*23# - * #convstr = (Int*9, Long*2, Float*4, Complex*3, Tuple*3, Long*2)*3# + * #convstr = (Long*9, Long*2, Float*4, Complex*3, Tuple*3, Long*2)*3# */ #if @convert@ @@ -1556,7 +1527,7 @@ static void return; } -#if defined(NPY_PY3K) && defined(IS_STRING) +#if defined(IS_STRING) /* Work around some Python 3K */ new = PyUnicode_FromEncodedObject(temp, "ascii", "strict"); Py_DECREF(temp); @@ -1571,13 +1542,7 @@ static void /* call out to the Python builtin given by convstr */ args = Py_BuildValue("(N)", temp); -#if defined(NPY_PY3K) -#define PyInt_Type PyLong_Type -#endif new = Py@convstr@_Type.tp_new(&Py@convstr@_Type, args, NULL); -#if defined(NPY_PY3K) -#undef PyInt_Type -#endif Py_DECREF(args); temp = new; if (temp == NULL) { @@ -3000,7 +2965,7 @@ OBJECT_compare(PyObject **ip1, PyObject **ip2, PyArrayObject *NPY_UNUSED(ap)) ret = PyObject_RichCompareBool(*ip1, *ip2, Py_LT); if (ret < 0) { - /* error occurred, avoid the next call to PyObject_RichCompareBool */ + /* error occurred, avoid the next call to PyObject_RichCompareBool */ return 0; } if (ret == 1) { @@ -4193,7 +4158,7 @@ _datetime_dtype_metadata_clone(NpyAuxData *data) } /* - * Allcoate and initialize a PyArray_DatetimeDTypeMetaData object + * Allocate and initialize a PyArray_DatetimeDTypeMetaData object */ static NpyAuxData* _create_datetime_metadata(NPY_DATETIMEUNIT base, int num) @@ -4429,7 +4394,7 @@ static PyArray_ArrFuncs _Py@NAME@_ArrFuncs = { #if @rsort@ aradixsort_@suff@ #else - atimsort_@suff@ + atimsort_@suff@ #endif }, #else diff --git a/numpy/core/src/multiarray/buffer.c b/numpy/core/src/multiarray/buffer.c index 0edadee98..576186362 100644 --- a/numpy/core/src/multiarray/buffer.c +++ b/numpy/core/src/multiarray/buffer.c @@ -21,59 +21,6 @@ **************** Implement Buffer Protocol **************************** *************************************************************************/ -/* removed multiple segment interface */ - -#if !defined(NPY_PY3K) -static Py_ssize_t -array_getsegcount(PyArrayObject *self, Py_ssize_t *lenp) -{ - if (lenp) { - *lenp = PyArray_NBYTES(self); - } - if (PyArray_ISONESEGMENT(self)) { - return 1; - } - if (lenp) { - *lenp = 0; - } - return 0; -} - -static Py_ssize_t -array_getreadbuf(PyArrayObject *self, Py_ssize_t segment, void **ptrptr) -{ - if (segment != 0) { - PyErr_SetString(PyExc_ValueError, - "accessing non-existing array segment"); - return -1; - } - if (PyArray_ISONESEGMENT(self)) { - *ptrptr = PyArray_DATA(self); - return PyArray_NBYTES(self); - } - PyErr_SetString(PyExc_ValueError, "array is not a single segment"); - *ptrptr = NULL; - return -1; -} - - -static Py_ssize_t -array_getwritebuf(PyArrayObject *self, Py_ssize_t segment, void **ptrptr) -{ - if (PyArray_FailUnlessWriteable(self, "buffer source array") < 0) { - return -1; - } - return array_getreadbuf(self, segment, (void **) ptrptr); -} - -static Py_ssize_t -array_getcharbuf(PyArrayObject *self, Py_ssize_t segment, constchar **ptrptr) -{ - return array_getreadbuf(self, segment, (void **) ptrptr); -} -#endif /* !defined(NPY_PY3K) */ - - /************************************************************************* * PEP 3118 buffer protocol * @@ -151,13 +98,8 @@ _append_field_name(_tmp_string_t *str, PyObject *name) char *p; Py_ssize_t len; PyObject *tmp; -#if defined(NPY_PY3K) /* FIXME: XXX -- should it use UTF-8 here? */ tmp = PyUnicode_AsUTF8String(name); -#else - tmp = name; - Py_INCREF(tmp); -#endif if (tmp == NULL || PyBytes_AsStringAndSize(tmp, &p, &len) < 0) { PyErr_Clear(); PyErr_SetString(PyExc_ValueError, "invalid field name"); @@ -523,7 +465,7 @@ _buffer_info_new(PyObject *obj) /* * Special case datetime64 scalars to remain backward compatible. * This will change in a future version. - * Note arrays of datetime64 and strutured arrays with datetime64 + * Note arrays of datetime64 and structured arrays with datetime64 * fields will not hit this code path and are currently unsupported * in _buffer_format_string. */ @@ -952,12 +894,6 @@ _dealloc_cached_buffer_info(PyObject *self) /*************************************************************************/ NPY_NO_EXPORT PyBufferProcs array_as_buffer = { -#if !defined(NPY_PY3K) - (readbufferproc)array_getreadbuf, /*bf_getreadbuffer*/ - (writebufferproc)array_getwritebuf, /*bf_getwritebuffer*/ - (segcountproc)array_getsegcount, /*bf_getsegcount*/ - (charbufferproc)array_getcharbuf, /*bf_getcharbuffer*/ -#endif (getbufferproc)array_getbuffer, (releasebufferproc)0, }; @@ -968,13 +904,13 @@ NPY_NO_EXPORT PyBufferProcs array_as_buffer = { */ static int -_descriptor_from_pep3118_format_fast(char *s, PyObject **result); +_descriptor_from_pep3118_format_fast(char const *s, PyObject **result); static int _pep3118_letter_to_type(char letter, int native, int complex); NPY_NO_EXPORT PyArray_Descr* -_descriptor_from_pep3118_format(char *s) +_descriptor_from_pep3118_format(char const *s) { char *buf, *p; int in_name = 0; @@ -1059,7 +995,7 @@ _descriptor_from_pep3118_format(char *s) */ static int -_descriptor_from_pep3118_format_fast(char *s, PyObject **result) +_descriptor_from_pep3118_format_fast(char const *s, PyObject **result) { PyArray_Descr *descr; diff --git a/numpy/core/src/multiarray/calculation.c b/numpy/core/src/multiarray/calculation.c index 1d72a5227..b9ed5a9e8 100644 --- a/numpy/core/src/multiarray/calculation.c +++ b/numpy/core/src/multiarray/calculation.c @@ -772,11 +772,7 @@ PyArray_Mean(PyArrayObject *self, int axis, int rtype, PyArrayObject *out) return NULL; } if (!out) { -#if defined(NPY_PY3K) ret = PyNumber_TrueDivide(obj1, obj2); -#else - ret = PyNumber_Divide(obj1, obj2); -#endif } else { ret = PyObject_CallFunction(n_ops.divide, "OOO", out, obj2, out); diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c index c991f7428..3ec151368 100644 --- a/numpy/core/src/multiarray/common.c +++ b/numpy/core/src/multiarray/common.c @@ -165,22 +165,10 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims, if ((temp = PyObject_Str(obj)) == NULL) { goto fail; } -#if defined(NPY_PY3K) - #if PY_VERSION_HEX >= 0x03030000 itemsize = PyUnicode_GetLength(temp); - #else - itemsize = PyUnicode_GET_SIZE(temp); - #endif -#else - itemsize = PyString_GET_SIZE(temp); -#endif } else if (string_type == NPY_UNICODE) { -#if defined(NPY_PY3K) if ((temp = PyObject_Str(obj)) == NULL) { -#else - if ((temp = PyObject_Unicode(obj)) == NULL) { -#endif goto fail; } itemsize = PyUnicode_GET_DATA_SIZE(temp); @@ -221,22 +209,10 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims, if ((temp = PyObject_Str(obj)) == NULL) { goto fail; } -#if defined(NPY_PY3K) - #if PY_VERSION_HEX >= 0x03030000 itemsize = PyUnicode_GetLength(temp); - #else - itemsize = PyUnicode_GET_SIZE(temp); - #endif -#else - itemsize = PyString_GET_SIZE(temp); -#endif } else if (string_type == NPY_UNICODE) { -#if defined(NPY_PY3K) if ((temp = PyObject_Str(obj)) == NULL) { -#else - if ((temp = PyObject_Unicode(obj)) == NULL) { -#endif goto fail; } itemsize = PyUnicode_GET_DATA_SIZE(temp); @@ -340,24 +316,18 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims, if (ip != NULL) { if (PyDict_Check(ip)) { PyObject *typestr; -#if defined(NPY_PY3K) PyObject *tmp = NULL; -#endif typestr = PyDict_GetItemString(ip, "typestr"); -#if defined(NPY_PY3K) /* Allow unicode type strings */ if (typestr && PyUnicode_Check(typestr)) { tmp = PyUnicode_AsASCIIString(typestr); typestr = tmp; } -#endif if (typestr && PyBytes_Check(typestr)) { dtype =_array_typedescr_fromstr(PyBytes_AS_STRING(typestr)); -#if defined(NPY_PY3K) if (tmp == typestr) { Py_DECREF(tmp); } -#endif Py_DECREF(ip); if (dtype == NULL) { goto fail; @@ -397,19 +367,6 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims, PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */ } - /* The old buffer interface */ -#if !defined(NPY_PY3K) - if (PyBuffer_Check(obj)) { - dtype = PyArray_DescrNewFromType(NPY_VOID); - if (dtype == NULL) { - goto fail; - } - dtype->elsize = Py_TYPE(obj)->tp_as_sequence->sq_length(obj); - PyErr_Clear(); - goto promote_types; - } -#endif - /* The __array__ attribute */ ip = PyArray_LookupSpecial_OnInstance(obj, "__array__"); if (ip != NULL) { @@ -478,9 +435,6 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims, if (common_type != NULL && !string_type && (common_type == &PyFloat_Type || /* TODO: we could add longs if we add a range check */ -#if !defined(NPY_PY3K) - common_type == &PyInt_Type || -#endif common_type == &PyBool_Type || common_type == &PyComplex_Type)) { size = 1; @@ -554,7 +508,7 @@ fail: /* new reference */ NPY_NO_EXPORT PyArray_Descr * -_array_typedescr_fromstr(char *c_str) +_array_typedescr_fromstr(char const *c_str) { PyArray_Descr *descr = NULL; PyObject *stringobj = PyString_FromString(c_str); @@ -612,12 +566,7 @@ NPY_NO_EXPORT npy_bool _IsWriteable(PyArrayObject *ap) { PyObject *base = PyArray_BASE(ap); -#if defined(NPY_PY3K) Py_buffer view; -#else - void *dummy; - Py_ssize_t n; -#endif /* * C-data wrapping arrays may not own their data while not having a base; @@ -661,7 +610,6 @@ _IsWriteable(PyArrayObject *ap) assert(!PyArray_CHKFLAGS(ap, NPY_ARRAY_OWNDATA)); } -#if defined(NPY_PY3K) if (PyObject_GetBuffer(base, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) { PyErr_Clear(); return NPY_FALSE; @@ -675,12 +623,6 @@ _IsWriteable(PyArrayObject *ap) * _dealloc_cached_buffer_info, but in this case leave it in the cache to * speed up future calls to _IsWriteable. */ -#else - if (PyObject_AsWriteBuffer(base, &dummy, &n) < 0) { - PyErr_Clear(); - return NPY_FALSE; - } -#endif return NPY_TRUE; } @@ -695,7 +637,7 @@ _IsWriteable(PyArrayObject *ap) * @return Python unicode string */ NPY_NO_EXPORT PyObject * -convert_shape_to_string(npy_intp n, npy_intp *vals, char *ending) +convert_shape_to_string(npy_intp n, npy_intp const *vals, char *ending) { npy_intp i; PyObject *ret, *tmp; diff --git a/numpy/core/src/multiarray/common.h b/numpy/core/src/multiarray/common.h index 7eee9ddc5..e77e51f42 100644 --- a/numpy/core/src/multiarray/common.h +++ b/numpy/core/src/multiarray/common.h @@ -49,7 +49,7 @@ NPY_NO_EXPORT PyArray_Descr * _array_find_python_scalar_type(PyObject *op); NPY_NO_EXPORT PyArray_Descr * -_array_typedescr_fromstr(char *str); +_array_typedescr_fromstr(char const *str); NPY_NO_EXPORT char * index2ptr(PyArrayObject *mp, npy_intp i); @@ -61,7 +61,7 @@ NPY_NO_EXPORT npy_bool _IsWriteable(PyArrayObject *ap); NPY_NO_EXPORT PyObject * -convert_shape_to_string(npy_intp n, npy_intp *vals, char *ending); +convert_shape_to_string(npy_intp n, npy_intp const *vals, char *ending); /* * Sets ValueError with "matrices not aligned" message for np.dot and friends diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c index 055d3e60f..d4b9edd57 100644 --- a/numpy/core/src/multiarray/compiled_base.c +++ b/numpy/core/src/multiarray/compiled_base.c @@ -1159,12 +1159,12 @@ fail: } -/* +/* * Inner loop for unravel_index * order must be NPY_CORDER or NPY_FORTRANORDER */ static int -unravel_index_loop(int unravel_ndim, npy_intp *unravel_dims, +unravel_index_loop(int unravel_ndim, npy_intp const *unravel_dims, npy_intp unravel_size, npy_intp count, char *indices, npy_intp indices_stride, npy_intp *coords, NPY_ORDER order) @@ -1186,7 +1186,7 @@ unravel_index_loop(int unravel_ndim, npy_intp *unravel_dims, } idx = idx_start; for (i = 0; i < unravel_ndim; ++i) { - /* + /* * Using a local seems to enable single-divide optimization * but only if the / precedes the % */ @@ -1447,7 +1447,6 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *args) } } -#if defined(NPY_PY3K) if (!PyArg_ParseTuple(args, "OO!:add_docstring", &obj, &PyUnicode_Type, &str)) { return NULL; } @@ -1456,13 +1455,6 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *args) if (docstr == NULL) { return NULL; } -#else - if (!PyArg_ParseTuple(args, "OO!:add_docstring", &obj, &PyString_Type, &str)) { - return NULL; - } - - docstr = PyString_AS_STRING(str); -#endif #define _TESTDOC1(typebase) (Py_TYPE(obj) == &Py##typebase##_Type) #define _TESTDOC2(typebase) (Py_TYPE(obj) == Py##typebase##_TypePtr) diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index ca126b4b1..484a13134 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -152,11 +152,7 @@ PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq) NPY_NO_EXPORT int PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf) { -#if defined(NPY_PY3K) Py_buffer view; -#else - Py_ssize_t buflen; -#endif buf->ptr = NULL; buf->flags = NPY_ARRAY_BEHAVED; @@ -165,7 +161,6 @@ PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf) return NPY_SUCCEED; } -#if defined(NPY_PY3K) if (PyObject_GetBuffer(obj, &view, PyBUF_ANY_CONTIGUOUS|PyBUF_WRITABLE|PyBUF_SIMPLE) != 0) { PyErr_Clear(); @@ -192,22 +187,6 @@ PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf) if (PyMemoryView_Check(obj)) { buf->base = PyMemoryView_GET_BASE(obj); } -#else - if (PyObject_AsWriteBuffer(obj, &(buf->ptr), &buflen) < 0) { - PyErr_Clear(); - buf->flags &= ~NPY_ARRAY_WRITEABLE; - if (PyObject_AsReadBuffer(obj, (const void **)&(buf->ptr), - &buflen) < 0) { - return NPY_FAIL; - } - } - buf->len = (npy_intp) buflen; - - /* Point to the base of the buffer object if present */ - if (PyBuffer_Check(obj)) { - buf->base = ((PyArray_Chunk *)obj)->base; - } -#endif if (buf->base == NULL) { buf->base = obj; } @@ -812,18 +791,6 @@ PyArray_PyIntAsIntp_ErrMsg(PyObject *o, const char * msg) * Since it is the usual case, first check if o is an integer. This is * an exact check, since otherwise __index__ is used. */ -#if !defined(NPY_PY3K) - if (PyInt_CheckExact(o)) { - #if (NPY_SIZEOF_LONG <= NPY_SIZEOF_INTP) - /* No overflow is possible, so we can just return */ - return PyInt_AS_LONG(o); - #else - long_value = PyInt_AS_LONG(o); - goto overflow_check; - #endif - } - else -#endif if (PyLong_CheckExact(o)) { #if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP) long_value = PyLong_AsLongLong(o); @@ -1145,7 +1112,7 @@ PyArray_TypestrConvert(int itemsize, int gentype) PyArray_IntTupleFromIntp */ NPY_NO_EXPORT PyObject * -PyArray_IntTupleFromIntp(int len, npy_intp *vals) +PyArray_IntTupleFromIntp(int len, npy_intp const *vals) { int i; PyObject *intTuple = PyTuple_New(len); diff --git a/numpy/core/src/multiarray/conversion_utils.h b/numpy/core/src/multiarray/conversion_utils.h index cd43f25c3..9bf712c3b 100644 --- a/numpy/core/src/multiarray/conversion_utils.h +++ b/numpy/core/src/multiarray/conversion_utils.h @@ -37,7 +37,7 @@ NPY_NO_EXPORT int PyArray_TypestrConvert(int itemsize, int gentype); NPY_NO_EXPORT PyObject * -PyArray_IntTupleFromIntp(int len, npy_intp *vals); +PyArray_IntTupleFromIntp(int len, npy_intp const *vals); NPY_NO_EXPORT int PyArray_SelectkindConverter(PyObject *obj, NPY_SELECTKIND *selectkind); diff --git a/numpy/core/src/multiarray/convert.c b/numpy/core/src/multiarray/convert.c index aa4e40e66..e7cbeaa77 100644 --- a/numpy/core/src/multiarray/convert.c +++ b/numpy/core/src/multiarray/convert.c @@ -262,18 +262,12 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format) return -1; } } -#if defined(NPY_PY3K) byteobj = PyUnicode_AsASCIIString(strobj); -#else - byteobj = strobj; -#endif NPY_BEGIN_ALLOW_THREADS; n2 = PyBytes_GET_SIZE(byteobj); n = fwrite(PyBytes_AS_STRING(byteobj), 1, n2, fp); NPY_END_ALLOW_THREADS; -#if defined(NPY_PY3K) Py_DECREF(byteobj); -#endif if (n < n2) { PyErr_Format(PyExc_IOError, "problem writing element %" NPY_INTP_FMT diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c index 4326448dc..d4774c2b2 100644 --- a/numpy/core/src/multiarray/convert_datatype.c +++ b/numpy/core/src/multiarray/convert_datatype.c @@ -2121,15 +2121,19 @@ PyArray_ObjectType(PyObject *op, int minimum_type) /* Raises error when len(op) == 0 */ -/*NUMPY_API*/ +/*NUMPY_API + * + * This function is only used in one place within NumPy and should + * generally be avoided. It is provided mainly for backward compatibility. + * + * The user of the function has to free the returned array. + */ NPY_NO_EXPORT PyArrayObject ** PyArray_ConvertToCommonType(PyObject *op, int *retn) { - int i, n, allscalars = 0; + int i, n; + PyArray_Descr *common_descr = NULL; PyArrayObject **mps = NULL; - PyArray_Descr *intype = NULL, *stype = NULL; - PyArray_Descr *newtype = NULL; - NPY_SCALARKIND scalarkind = NPY_NOSCALAR, intypekind = NPY_NOSCALAR; *retn = n = PySequence_Length(op); if (n == 0) { @@ -2165,94 +2169,41 @@ PyArray_ConvertToCommonType(PyObject *op, int *retn) } for (i = 0; i < n; i++) { - PyObject *otmp = PySequence_GetItem(op, i); - if (otmp == NULL) { + /* Convert everything to an array, this could be optimized away */ + PyObject *tmp = PySequence_GetItem(op, i); + if (tmp == NULL) { goto fail; } - if (!PyArray_CheckAnyScalar(otmp)) { - newtype = PyArray_DescrFromObject(otmp, intype); - Py_DECREF(otmp); - Py_XDECREF(intype); - if (newtype == NULL) { - goto fail; - } - intype = newtype; - intypekind = PyArray_ScalarKind(intype->type_num, NULL); - } - else { - newtype = PyArray_DescrFromObject(otmp, stype); - Py_DECREF(otmp); - Py_XDECREF(stype); - if (newtype == NULL) { - goto fail; - } - stype = newtype; - scalarkind = PyArray_ScalarKind(newtype->type_num, NULL); - mps[i] = (PyArrayObject *)Py_None; - Py_INCREF(Py_None); - } - } - if (intype == NULL) { - /* all scalars */ - allscalars = 1; - intype = stype; - Py_INCREF(intype); - for (i = 0; i < n; i++) { - Py_XDECREF(mps[i]); - mps[i] = NULL; - } - } - else if ((stype != NULL) && (intypekind != scalarkind)) { - /* - * we need to upconvert to type that - * handles both intype and stype - * also don't forcecast the scalars. - */ - if (!PyArray_CanCoerceScalar(stype->type_num, - intype->type_num, - scalarkind)) { - newtype = PyArray_PromoteTypes(intype, stype); - Py_XDECREF(intype); - intype = newtype; - if (newtype == NULL) { - goto fail; - } - } - for (i = 0; i < n; i++) { - Py_XDECREF(mps[i]); - mps[i] = NULL; + + mps[i] = (PyArrayObject *)PyArray_FROM_O(tmp); + Py_DECREF(tmp); + if (mps[i] == NULL) { + goto fail; } } + common_descr = PyArray_ResultType(n, mps, 0, NULL); + if (common_descr == NULL) { + goto fail; + } - /* Make sure all arrays are actual array objects. */ + /* Make sure all arrays are contiguous and have the correct dtype. */ for (i = 0; i < n; i++) { int flags = NPY_ARRAY_CARRAY; - PyObject *otmp = PySequence_GetItem(op, i); + PyArrayObject *tmp = mps[i]; - if (otmp == NULL) { - goto fail; - } - if (!allscalars && ((PyObject *)(mps[i]) == Py_None)) { - /* forcecast scalars */ - flags |= NPY_ARRAY_FORCECAST; - Py_DECREF(Py_None); - } - Py_INCREF(intype); - mps[i] = (PyArrayObject*)PyArray_FromAny(otmp, intype, 0, 0, - flags, NULL); - Py_DECREF(otmp); + Py_INCREF(common_descr); + mps[i] = (PyArrayObject *)PyArray_FromArray(tmp, common_descr, flags); + Py_DECREF(tmp); if (mps[i] == NULL) { goto fail; } } - Py_DECREF(intype); - Py_XDECREF(stype); + Py_DECREF(common_descr); return mps; fail: - Py_XDECREF(intype); - Py_XDECREF(stype); + Py_XDECREF(common_descr); *retn = 0; for (i = 0; i < n; i++) { Py_XDECREF(mps[i]); diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 7276add75..07e269b57 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -629,13 +629,7 @@ discover_itemsize(PyObject *s, int nd, int *itemsize, int string_type) } if ((nd == 0) || PyString_Check(s) || -#if defined(NPY_PY3K) - PyMemoryView_Check(s) || -#else - PyBuffer_Check(s) || -#endif - PyUnicode_Check(s)) { - + PyMemoryView_Check(s) || PyUnicode_Check(s)) { /* If an object has no length, leave it be */ if (string_type && s != NULL && !PyString_Check(s) && !PyUnicode_Check(s)) { @@ -644,11 +638,7 @@ discover_itemsize(PyObject *s, int nd, int *itemsize, int string_type) s_string = PyObject_Str(s); } else { -#if defined(NPY_PY3K) s_string = PyObject_Str(s); -#else - s_string = PyObject_Unicode(s); -#endif } if (s_string) { n = PyObject_Length(s_string); @@ -736,10 +726,6 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it, /* obj is a String */ if (PyString_Check(obj) || -#if defined(NPY_PY3K) -#else - PyBuffer_Check(obj) || -#endif PyUnicode_Check(obj)) { if (stop_at_string) { *maxndim = 0; @@ -950,7 +936,7 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it, } static PyObject * -raise_memory_error(int nd, npy_intp *dims, PyArray_Descr *descr) +raise_memory_error(int nd, npy_intp const *dims, PyArray_Descr *descr) { static PyObject *exc_type = NULL; @@ -1909,7 +1895,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, /* If the requested dtype is flexible, adapt it */ if (newtype != NULL) { - newtype = PyArray_AdaptFlexibleDType(op, + newtype = PyArray_AdaptFlexibleDType((arr == NULL) ? op : (PyObject *)arr, (dtype == NULL) ? PyArray_DESCR(arr) : dtype, newtype); if (newtype == NULL) { @@ -2352,9 +2338,7 @@ PyArray_FromStructInterface(PyObject *input) NPY_NO_EXPORT int _is_default_descr(PyObject *descr, PyObject *typestr) { PyObject *tuple, *name, *typestr2; -#if defined(NPY_PY3K) PyObject *tmp = NULL; -#endif int ret = 0; if (!PyList_Check(descr) || PyList_GET_SIZE(descr) != 1) { @@ -2369,7 +2353,6 @@ _is_default_descr(PyObject *descr, PyObject *typestr) { return 0; } typestr2 = PyTuple_GET_ITEM(tuple, 1); -#if defined(NPY_PY3K) /* Allow unicode type strings */ if (PyUnicode_Check(typestr2)) { tmp = PyUnicode_AsASCIIString(typestr2); @@ -2378,14 +2361,11 @@ _is_default_descr(PyObject *descr, PyObject *typestr) { } typestr2 = tmp; } -#endif if (PyBytes_Check(typestr2) && PyObject_RichCompareBool(typestr, typestr2, Py_EQ)) { ret = 1; } -#if defined(NPY_PY3K) Py_XDECREF(tmp); -#endif return ret; } @@ -2402,11 +2382,7 @@ PyArray_FromInterface(PyObject *origin) PyArrayObject *ret; PyArray_Descr *dtype = NULL; char *data = NULL; -#if defined(NPY_PY3K) Py_buffer view; -#else - Py_ssize_t buffer_len; -#endif int res, i, n; npy_intp dims[NPY_MAXDIMS], strides[NPY_MAXDIMS]; int dataflags = NPY_ARRAY_BEHAVED; @@ -2434,7 +2410,7 @@ PyArray_FromInterface(PyObject *origin) "Missing __array_interface__ typestr"); return NULL; } -#if defined(NPY_PY3K) + /* Allow unicode type strings */ if (PyUnicode_Check(attr)) { PyObject *tmp = PyUnicode_AsASCIIString(attr); @@ -2446,7 +2422,7 @@ PyArray_FromInterface(PyObject *origin) else { Py_INCREF(attr); } -#endif + if (!PyBytes_Check(attr)) { PyErr_SetString(PyExc_TypeError, "__array_interface__ typestr must be a string"); @@ -2474,9 +2450,7 @@ PyArray_FromInterface(PyObject *origin) } } -#if defined(NPY_PY3K) Py_DECREF(attr); /* Pairs with the unicode handling above */ -#endif /* Get shape tuple from interface specification */ attr = PyDict_GetItemString(iface, "shape"); @@ -2557,7 +2531,6 @@ PyArray_FromInterface(PyObject *origin) else { base = origin; } -#if defined(NPY_PY3K) if (PyObject_GetBuffer(base, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) { PyErr_Clear(); @@ -2576,18 +2549,7 @@ PyArray_FromInterface(PyObject *origin) */ PyBuffer_Release(&view); _dealloc_cached_buffer_info(base); -#else - res = PyObject_AsWriteBuffer(base, (void **)&data, &buffer_len); - if (res < 0) { - PyErr_Clear(); - res = PyObject_AsReadBuffer( - base, (const void **)&data, &buffer_len); - if (res < 0) { - goto fail; - } - dataflags &= ~NPY_ARRAY_WRITEABLE; - } -#endif + /* Get offset number from interface specification */ attr = PyDict_GetItemString(iface, "offset"); if (attr) { @@ -3590,7 +3552,7 @@ array_fromfile_binary(FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nrea */ #define FROM_BUFFER_SIZE 4096 static PyArrayObject * -array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread, +array_from_text(PyArray_Descr *dtype, npy_intp num, char const *sep, size_t *nread, void *stream, next_element next, skip_separator skip_sep, void *stream_data) { @@ -3780,9 +3742,7 @@ PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type, { PyArrayObject *ret; char *data; -#if defined(NPY_PY3K) Py_buffer view; -#endif Py_ssize_t ts; npy_intp s, n; int itemsize; @@ -3803,7 +3763,6 @@ PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type, return NULL; } -#if defined(NPY_PY3K) if (PyObject_GetBuffer(buf, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) { writeable = 0; PyErr_Clear(); @@ -3822,16 +3781,6 @@ PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type, */ PyBuffer_Release(&view); _dealloc_cached_buffer_info(buf); -#else - if (PyObject_AsWriteBuffer(buf, (void *)&data, &ts) == -1) { - writeable = 0; - PyErr_Clear(); - if (PyObject_AsReadBuffer(buf, (void *)&data, &ts) == -1) { - Py_DECREF(type); - return NULL; - } - } -#endif if ((offset < 0) || (offset > ts)) { PyErr_Format(PyExc_ValueError, diff --git a/numpy/core/src/multiarray/datetime.c b/numpy/core/src/multiarray/datetime.c index 72a3df89c..67ed3ca85 100644 --- a/numpy/core/src/multiarray/datetime.c +++ b/numpy/core/src/multiarray/datetime.c @@ -71,7 +71,7 @@ numpy_pydatetime_import(void) } /* Exported as DATETIMEUNITS in multiarraymodule.c */ -NPY_NO_EXPORT char *_datetime_strings[NPY_DATETIME_NUMUNITS] = { +NPY_NO_EXPORT char const *_datetime_strings[NPY_DATETIME_NUMUNITS] = { "Y", "M", "W", @@ -692,6 +692,14 @@ get_datetime_metadata_from_dtype(PyArray_Descr *dtype) return &(((PyArray_DatetimeDTypeMetaData *)dtype->c_metadata)->meta); } +/* strtol does not know whether to put a const qualifier on endptr, wrap + * it so we can put this cast in one place. + */ +NPY_NO_EXPORT long int +strtol_const(char const *str, char const **endptr, int base) { + return strtol(str, (char**)endptr, base); +} + /* * Converts a substring given by 'str' and 'len' into * a date time unit multiplier + enum value, which are populated @@ -702,15 +710,15 @@ get_datetime_metadata_from_dtype(PyArray_Descr *dtype) * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -parse_datetime_extended_unit_from_string(char *str, Py_ssize_t len, - char *metastr, +parse_datetime_extended_unit_from_string(char const *str, Py_ssize_t len, + char const *metastr, PyArray_DatetimeMetaData *out_meta) { - char *substr = str, *substrend = NULL; + char const *substr = str, *substrend = NULL; int den = 1; /* First comes an optional integer multiplier */ - out_meta->num = (int)strtol(substr, &substrend, 10); + out_meta->num = (int)strtol_const(substr, &substrend, 10); if (substr == substrend) { out_meta->num = 1; } @@ -735,7 +743,7 @@ parse_datetime_extended_unit_from_string(char *str, Py_ssize_t len, /* Next comes an optional integer denominator */ if (substr-str < len && *substr == '/') { substr++; - den = (int)strtol(substr, &substrend, 10); + den = (int)strtol_const(substr, &substrend, 10); /* If the '/' exists, there must be a number followed by ']' */ if (substr == substrend || *substrend != ']') { goto bad_input; @@ -776,10 +784,10 @@ bad_input: * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -parse_datetime_metadata_from_metastr(char *metastr, Py_ssize_t len, +parse_datetime_metadata_from_metastr(char const *metastr, Py_ssize_t len, PyArray_DatetimeMetaData *out_meta) { - char *substr = metastr, *substrend = NULL; + char const *substr = metastr, *substrend = NULL; /* Treat the empty string as generic units */ if (len == 0) { @@ -837,10 +845,10 @@ bad_input: * The "type" string should be NULL-terminated. */ NPY_NO_EXPORT PyArray_Descr * -parse_dtype_from_datetime_typestr(char *typestr, Py_ssize_t len) +parse_dtype_from_datetime_typestr(char const *typestr, Py_ssize_t len) { PyArray_DatetimeMetaData meta; - char *metastr = NULL; + char const *metastr = NULL; int is_timedelta = 0; Py_ssize_t metalen = 0; @@ -923,7 +931,7 @@ static NPY_DATETIMEUNIT _multiples_table[16][4] = { */ NPY_NO_EXPORT int convert_datetime_divisor_to_multiple(PyArray_DatetimeMetaData *meta, - int den, char *metastr) + int den, char const *metastr) { int i, num, ind; NPY_DATETIMEUNIT *totry; @@ -1671,7 +1679,7 @@ datetime_type_promotion(PyArray_Descr *type1, PyArray_Descr *type2) * Returns NPY_DATETIMEUNIT on success, NPY_FR_ERROR on failure. */ NPY_NO_EXPORT NPY_DATETIMEUNIT -parse_datetime_unit_from_string(char *str, Py_ssize_t len, char *metastr) +parse_datetime_unit_from_string(char const *str, Py_ssize_t len, char const *metastr) { /* Use switch statements so the compiler can make it fast */ if (len == 1) { @@ -1956,7 +1964,7 @@ append_metastr_to_string(PyArray_DatetimeMetaData *meta, { PyObject *res; int num; - char *basestr; + char const *basestr; if (ret == NULL) { return NULL; diff --git a/numpy/core/src/multiarray/datetime_busdaycal.c b/numpy/core/src/multiarray/datetime_busdaycal.c index 7a26868e8..eb6ef04be 100644 --- a/numpy/core/src/multiarray/datetime_busdaycal.c +++ b/numpy/core/src/multiarray/datetime_busdaycal.c @@ -493,12 +493,7 @@ static PyGetSetDef busdaycalendar_getsets[] = { }; NPY_NO_EXPORT PyTypeObject NpyBusDayCalendar_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.busdaycalendar", /* tp_name */ sizeof(NpyBusDayCalendar), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -507,11 +502,7 @@ NPY_NO_EXPORT PyTypeObject NpyBusDayCalendar_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ diff --git a/numpy/core/src/multiarray/datetime_strings.c b/numpy/core/src/multiarray/datetime_strings.c index dfc01494f..4574c05d8 100644 --- a/numpy/core/src/multiarray/datetime_strings.c +++ b/numpy/core/src/multiarray/datetime_strings.c @@ -218,7 +218,7 @@ convert_datetimestruct_utc_to_local(npy_datetimestruct *out_dts_local, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -parse_iso_8601_datetime(char *str, Py_ssize_t len, +parse_iso_8601_datetime(char const *str, Py_ssize_t len, NPY_DATETIMEUNIT unit, NPY_CASTING casting, npy_datetimestruct *out, @@ -227,7 +227,7 @@ parse_iso_8601_datetime(char *str, Py_ssize_t len, { int year_leap = 0; int i, numdigits; - char *substr; + char const *substr; Py_ssize_t sublen; NPY_DATETIMEUNIT bestunit; @@ -1487,7 +1487,6 @@ array_datetime_as_string(PyObject *NPY_UNUSED(self), PyObject *args, /* Get a string size long enough for any datetimes we're given */ strsize = get_datetime_iso_8601_strlen(local, unit); -#if defined(NPY_PY3K) /* * For Python3, allocate the output array as a UNICODE array, so * that it will behave as strings properly @@ -1504,7 +1503,6 @@ array_datetime_as_string(PyObject *NPY_UNUSED(self), PyObject *args, op_dtypes[1] = NULL; goto fail; } -#endif /* Create the iteration string data type (always ASCII string) */ op_dtypes[1] = PyArray_DescrNewFromType(NPY_STRING); if (op_dtypes[1] == NULL) { diff --git a/numpy/core/src/multiarray/datetime_strings.h b/numpy/core/src/multiarray/datetime_strings.h index 4e60ce929..148369595 100644 --- a/numpy/core/src/multiarray/datetime_strings.h +++ b/numpy/core/src/multiarray/datetime_strings.h @@ -33,7 +33,7 @@ * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -parse_iso_8601_datetime(char *str, Py_ssize_t len, +parse_iso_8601_datetime(char const *str, Py_ssize_t len, NPY_DATETIMEUNIT unit, NPY_CASTING casting, npy_datetimestruct *out, diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index d4e18e457..da3babc33 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -39,15 +39,37 @@ static PyObject *typeDict = NULL; /* Must be explicitly loaded */ +/* + * Generate a vague error message when a function returned NULL but forgot + * to set an exception. We should aim to remove this eventually. + */ +static void +_report_generic_error(void) { + PyErr_SetString(PyExc_TypeError, "data type not understood"); +} + +static PyArray_Descr * +_try_convert_from_inherit_tuple(PyArray_Descr *type, PyObject *newobj); + static PyArray_Descr * -_use_inherit(PyArray_Descr *type, PyObject *newobj, int *errflag); +_convert_from_any(PyObject *obj, int align); +/* + * This function creates a dtype object when the object is a ctypes subclass. + * + * Returns `Py_NotImplemented` if the type is not a ctypes subclass. + */ static PyArray_Descr * -_arraydescr_from_ctypes_type(PyTypeObject *type) +_try_convert_from_ctypes_type(PyTypeObject *type) { PyObject *_numpy_dtype_ctypes; PyObject *res; + if (!npy_ctypes_check(type)) { + Py_INCREF(Py_NotImplemented); + return (PyArray_Descr *)Py_NotImplemented; + } + /* Call the python function of the same name. */ _numpy_dtype_ctypes = PyImport_ImportModule("numpy.core._dtype_ctypes"); if (_numpy_dtype_ctypes == NULL) { @@ -72,25 +94,21 @@ _arraydescr_from_ctypes_type(PyTypeObject *type) return (PyArray_Descr *)res; } +static PyArray_Descr * +_convert_from_any(PyObject *obj, int align); + /* * This function creates a dtype object when the object has a "dtype" attribute, * and it can be converted to a dtype object. * - * Returns a new reference to a dtype object, or NULL - * if this is not possible. - * When the return value is true, the dtype attribute should have been used - * and parsed. Currently the only failure mode for a 1 return is a - * RecursionError and the descriptor is set to NULL. - * When the return value is false, no error will be set. + * Returns `Py_NotImplemented` if this is not possible. + * Currently the only failure mode for a NULL return is a RecursionError. */ -int -_arraydescr_from_dtype_attr(PyObject *obj, PyArray_Descr **newdescr) +static PyArray_Descr * +_try_convert_from_dtype_attr(PyObject *obj) { - PyObject *dtypedescr; - int ret; - /* For arbitrary objects that have a "dtype" attribute */ - dtypedescr = PyObject_GetAttrString(obj, "dtype"); + PyObject *dtypedescr = PyObject_GetAttrString(obj, "dtype"); if (dtypedescr == NULL) { /* * This can be reached due to recursion limit being hit while fetching @@ -103,26 +121,33 @@ _arraydescr_from_dtype_attr(PyObject *obj, PyArray_Descr **newdescr) " while trying to convert the given data type from its " "`.dtype` attribute.") != 0) { Py_DECREF(dtypedescr); - return 1; + return NULL; } - ret = PyArray_DescrConverter(dtypedescr, newdescr); - + PyArray_Descr *newdescr = _convert_from_any(dtypedescr, 0); Py_DECREF(dtypedescr); Py_LeaveRecursiveCall(); - if (ret != NPY_SUCCEED) { + if (newdescr == NULL) { goto fail; } - return 1; + return newdescr; fail: /* Ignore all but recursion errors, to give ctypes a full try. */ if (!PyErr_ExceptionMatches(PyExc_RecursionError)) { PyErr_Clear(); - return 0; + Py_INCREF(Py_NotImplemented); + return (PyArray_Descr *)Py_NotImplemented; } - return 1; + return NULL; +} + +/* Expose to another file with a prefixed name */ +NPY_NO_EXPORT PyArray_Descr * +_arraydescr_try_convert_from_dtype_attr(PyObject *obj) +{ + return _try_convert_from_dtype_attr(obj); } /* @@ -199,7 +224,7 @@ _check_for_commastring(const char *type, Py_ssize_t len) #undef _chk_byteorder static int -is_datetime_typestr(char *type, Py_ssize_t len) +is_datetime_typestr(char const *type, Py_ssize_t len) { if (len < 2) { return 0; @@ -225,34 +250,24 @@ is_datetime_typestr(char *type, Py_ssize_t len) static PyArray_Descr * _convert_from_tuple(PyObject *obj, int align) { - PyArray_Descr *type, *res; - PyObject *val; - int errflag; - if (PyTuple_GET_SIZE(obj) != 2) { + _report_generic_error(); return NULL; } - if (align) { - if (!PyArray_DescrAlignConverter(PyTuple_GET_ITEM(obj, 0), &type)) { - return NULL; - } + PyArray_Descr *type = _convert_from_any(PyTuple_GET_ITEM(obj, 0), align); + if (type == NULL) { + return NULL; } - else { - if (!PyArray_DescrConverter(PyTuple_GET_ITEM(obj, 0), &type)) { - return NULL; - } - } - val = PyTuple_GET_ITEM(obj,1); + PyObject *val = PyTuple_GET_ITEM(obj,1); /* try to interpret next item as a type */ - res = _use_inherit(type, val, &errflag); - if (res || errflag) { + PyArray_Descr *res = _try_convert_from_inherit_tuple(type, val); + if ((PyObject *)res != Py_NotImplemented) { Py_DECREF(type); return res; } - PyErr_Clear(); + Py_DECREF(res); /* - * We get here if res was NULL but errflag wasn't set - * --- i.e. the conversion to a data-descr failed in _use_inherit + * We get here if _try_convert_from_inherit_tuple failed without crashing */ if (PyDataType_ISUNSIZED(type)) { /* interpret next item as a typesize */ @@ -291,11 +306,6 @@ _convert_from_tuple(PyObject *obj, int align) * a new fields attribute. */ PyArray_Dims shape = {NULL, -1}; - PyArray_Descr *newdescr = NULL; - npy_intp items; - int i, overflowed; - int nbytes; - if (!(PyArray_IntpConverter(val, &shape)) || (shape.len > NPY_MAXDIMS)) { PyErr_SetString(PyExc_ValueError, "invalid shape in fixed-type tuple."); @@ -322,7 +332,7 @@ _convert_from_tuple(PyObject *obj, int align) } /* validate and set shape */ - for (i=0; i < shape.len; i++) { + for (int i=0; i < shape.len; i++) { if (shape.ptr[i] < 0) { PyErr_SetString(PyExc_ValueError, "invalid shape in fixed-type tuple: " @@ -336,7 +346,9 @@ _convert_from_tuple(PyObject *obj, int align) goto fail; } } - items = PyArray_OverflowMultiplyList(shape.ptr, shape.len); + npy_intp items = PyArray_OverflowMultiplyList(shape.ptr, shape.len); + int overflowed; + int nbytes; if (items < 0 || items > NPY_MAX_INT) { overflowed = 1; } @@ -350,13 +362,14 @@ _convert_from_tuple(PyObject *obj, int align) "bytes must fit into a C int."); goto fail; } - newdescr = PyArray_DescrNewFromType(NPY_VOID); + PyArray_Descr *newdescr = PyArray_DescrNewFromType(NPY_VOID); if (newdescr == NULL) { goto fail; } newdescr->elsize = nbytes; newdescr->subarray = PyArray_malloc(sizeof(PyArray_ArrayDescr)); if (newdescr->subarray == NULL) { + Py_DECREF(newdescr); PyErr_NoMemory(); goto fail; } @@ -375,13 +388,15 @@ _convert_from_tuple(PyObject *obj, int align) */ newdescr->subarray->shape = PyTuple_New(shape.len); if (newdescr->subarray->shape == NULL) { + Py_DECREF(newdescr); goto fail; } - for (i=0; i < shape.len; i++) { + for (int i=0; i < shape.len; i++) { PyTuple_SET_ITEM(newdescr->subarray->shape, i, PyInt_FromLong((long)shape.ptr[i])); if (PyTuple_GET_ITEM(newdescr->subarray->shape, i) == NULL) { + Py_DECREF(newdescr); goto fail; } } @@ -391,7 +406,6 @@ _convert_from_tuple(PyObject *obj, int align) fail: Py_XDECREF(type); - Py_XDECREF(newdescr); npy_free_cache_dim_obj(shape); return NULL; } @@ -410,138 +424,112 @@ _convert_from_tuple(PyObject *obj, int align) static PyArray_Descr * _convert_from_array_descr(PyObject *obj, int align) { - int n, i, totalsize; - int ret; - PyObject *fields, *item, *newobj; - PyObject *name, *tup, *title; - PyObject *nameslist; - PyArray_Descr *new; - PyArray_Descr *conv; + int n = PyList_GET_SIZE(obj); + PyObject *nameslist = PyTuple_New(n); + if (!nameslist) { + return NULL; + } + /* Types with fields need the Python C API for field access */ char dtypeflags = NPY_NEEDS_PYAPI; int maxalign = 0; - - n = PyList_GET_SIZE(obj); - nameslist = PyTuple_New(n); - if (!nameslist) { + int totalsize = 0; + PyObject *fields = PyDict_New(); + if (!fields) { return NULL; } - totalsize = 0; - fields = PyDict_New(); - for (i = 0; i < n; i++) { - item = PyList_GET_ITEM(obj, i); + for (int i = 0; i < n; i++) { + PyObject *item = PyList_GET_ITEM(obj, i); if (!PyTuple_Check(item) || (PyTuple_GET_SIZE(item) < 2)) { + _report_generic_error(); goto fail; } - name = PyTuple_GET_ITEM(item, 0); + PyObject *name = PyTuple_GET_ITEM(item, 0); + PyObject *title; if (PyBaseString_Check(name)) { title = NULL; } else if (PyTuple_Check(name)) { if (PyTuple_GET_SIZE(name) != 2) { + _report_generic_error(); goto fail; } title = PyTuple_GET_ITEM(name, 0); name = PyTuple_GET_ITEM(name, 1); if (!PyBaseString_Check(name)) { + _report_generic_error(); goto fail; } } else { + _report_generic_error(); goto fail; } /* Insert name into nameslist */ Py_INCREF(name); -#if !defined(NPY_PY3K) - /* convert unicode name to ascii on Python 2 if possible */ - if (PyUnicode_Check(name)) { - PyObject *tmp = PyUnicode_AsASCIIString(name); - Py_DECREF(name); - if (tmp == NULL) { - goto fail; - } - name = tmp; - } -#endif if (PyUString_GET_SIZE(name) == 0) { Py_DECREF(name); if (title == NULL) { name = PyUString_FromFormat("f%d", i); + if (name == NULL) { + goto fail; + } } -#if defined(NPY_PY3K) /* On Py3, allow only non-empty Unicode strings as field names */ else if (PyUString_Check(title) && PyUString_GET_SIZE(title) > 0) { name = title; Py_INCREF(name); } else { + _report_generic_error(); goto fail; } -#else - else { - name = title; - Py_INCREF(name); - } -#endif } PyTuple_SET_ITEM(nameslist, i, name); /* Process rest */ - + PyArray_Descr *conv; if (PyTuple_GET_SIZE(item) == 2) { - if (align) { - ret = PyArray_DescrAlignConverter(PyTuple_GET_ITEM(item, 1), - &conv); - } - else { - ret = PyArray_DescrConverter(PyTuple_GET_ITEM(item, 1), &conv); + conv = _convert_from_any(PyTuple_GET_ITEM(item, 1), align); + if (conv == NULL) { + goto fail; } } else if (PyTuple_GET_SIZE(item) == 3) { - newobj = PyTuple_GetSlice(item, 1, 3); - if (align) { - ret = PyArray_DescrAlignConverter(newobj, &conv); - } - else { - ret = PyArray_DescrConverter(newobj, &conv); - } + PyObject *newobj = PyTuple_GetSlice(item, 1, 3); + conv = _convert_from_any(newobj, align); Py_DECREF(newobj); + if (conv == NULL) { + goto fail; + } } else { + _report_generic_error(); goto fail; } - if (ret == NPY_FAIL) { - goto fail; - } - if ((PyDict_GetItem(fields, name) != NULL) || (title && PyBaseString_Check(title) && (PyDict_GetItem(fields, title) != NULL))) { -#if defined(NPY_PY3K) - name = PyUnicode_AsUTF8String(name); -#endif PyErr_Format(PyExc_ValueError, - "field '%s' occurs more than once", PyString_AsString(name)); -#if defined(NPY_PY3K) - Py_DECREF(name); -#endif + "field %R occurs more than once", name); Py_DECREF(conv); goto fail; } dtypeflags |= (conv->flags & NPY_FROM_FIELDS); if (align) { - int _align; - - _align = conv->alignment; + int _align = conv->alignment; if (_align > 1) { totalsize = NPY_NEXT_ALIGNED_OFFSET(totalsize, _align); } maxalign = PyArray_MAX(maxalign, _align); } - tup = PyTuple_New((title == NULL ? 2 : 3)); + PyObject *tup = PyTuple_New((title == NULL ? 2 : 3)); + if (tup == NULL) { + goto fail; + } PyTuple_SET_ITEM(tup, 0, (PyObject *)conv); PyTuple_SET_ITEM(tup, 1, PyInt_FromLong((long) totalsize)); @@ -553,7 +541,9 @@ _convert_from_array_descr(PyObject *obj, int align) if (title != NULL) { Py_INCREF(title); PyTuple_SET_ITEM(tup, 2, title); - PyDict_SetItem(fields, name, tup); + if (PyDict_SetItem(fields, name, tup) < 0) { + goto fail; + } if (PyBaseString_Check(title)) { if (PyDict_GetItem(fields, title) != NULL) { PyErr_SetString(PyExc_ValueError, @@ -576,7 +566,7 @@ _convert_from_array_descr(PyObject *obj, int align) totalsize = NPY_NEXT_ALIGNED_OFFSET(totalsize, maxalign); } - new = PyArray_DescrNewFromType(NPY_VOID); + PyArray_Descr *new = PyArray_DescrNewFromType(NPY_VOID); if (new == NULL) { Py_XDECREF(fields); Py_XDECREF(nameslist); @@ -609,69 +599,81 @@ _convert_from_array_descr(PyObject *obj, int align) static PyArray_Descr * _convert_from_list(PyObject *obj, int align) { - int n, i; - int totalsize; - PyObject *fields; - PyArray_Descr *conv = NULL; - PyArray_Descr *new; - PyObject *key, *tup; - PyObject *nameslist = NULL; - int ret; - int maxalign = 0; - /* Types with fields need the Python C API for field access */ - char dtypeflags = NPY_NEEDS_PYAPI; - - n = PyList_GET_SIZE(obj); + int n = PyList_GET_SIZE(obj); /* * Ignore any empty string at end which _internal._commastring * can produce */ - key = PyList_GET_ITEM(obj, n-1); - if (PyBytes_Check(key) && PyBytes_GET_SIZE(key) == 0) { - n = n - 1; + PyObject *last_item = PyList_GET_ITEM(obj, n-1); + if (PyUnicode_Check(last_item)) { + Py_ssize_t s = PySequence_Size(last_item); + if (s < 0) { + return NULL; + } + if (s == 0) { + n = n - 1; + } } - /* End ignore code.*/ - totalsize = 0; if (n == 0) { + PyErr_SetString(PyExc_ValueError, "Expected at least one field name"); return NULL; } - nameslist = PyTuple_New(n); + PyObject *nameslist = PyTuple_New(n); if (!nameslist) { return NULL; } - fields = PyDict_New(); - for (i = 0; i < n; i++) { - tup = PyTuple_New(2); - key = PyUString_FromFormat("f%d", i); - if (align) { - ret = PyArray_DescrAlignConverter(PyList_GET_ITEM(obj, i), &conv); - } - else { - ret = PyArray_DescrConverter(PyList_GET_ITEM(obj, i), &conv); - } - if (ret == NPY_FAIL) { - Py_DECREF(tup); - Py_DECREF(key); + PyObject *fields = PyDict_New(); + if (!fields) { + Py_DECREF(nameslist); + return NULL; + } + + /* Types with fields need the Python C API for field access */ + char dtypeflags = NPY_NEEDS_PYAPI; + int maxalign = 0; + int totalsize = 0; + for (int i = 0; i < n; i++) { + PyArray_Descr *conv = _convert_from_any( + PyList_GET_ITEM(obj, i), align); + if (conv == NULL) { goto fail; } dtypeflags |= (conv->flags & NPY_FROM_FIELDS); - PyTuple_SET_ITEM(tup, 0, (PyObject *)conv); if (align) { - int _align; - - _align = conv->alignment; + int _align = conv->alignment; if (_align > 1) { totalsize = NPY_NEXT_ALIGNED_OFFSET(totalsize, _align); } maxalign = PyArray_MAX(maxalign, _align); } - PyTuple_SET_ITEM(tup, 1, PyInt_FromLong((long) totalsize)); - PyDict_SetItem(fields, key, tup); - Py_DECREF(tup); + PyObject *size_obj = PyInt_FromLong((long) totalsize); + if (!size_obj) { + Py_DECREF(conv); + goto fail; + } + PyObject *tup = PyTuple_New(2); + if (!tup) { + Py_DECREF(size_obj); + Py_DECREF(conv); + goto fail; + } + PyTuple_SET_ITEM(tup, 0, (PyObject *)conv); + PyTuple_SET_ITEM(tup, 1, size_obj); + PyObject *key = PyUString_FromFormat("f%d", i); + if (!key) { + Py_DECREF(tup); + goto fail; + } + /* steals a reference to key */ PyTuple_SET_ITEM(nameslist, i, key); + int ret = PyDict_SetItem(fields, key, tup); + Py_DECREF(tup); + if (ret < 0) { + goto fail; + } totalsize += conv->elsize; } - new = PyArray_DescrNewFromType(NPY_VOID); + PyArray_Descr *new = PyArray_DescrNewFromType(NPY_VOID); new->fields = fields; new->names = nameslist; new->flags = dtypeflags; @@ -711,7 +713,8 @@ _convert_from_commastring(PyObject *obj, int align) PyArray_Descr *res; PyObject *_numpy_internal; - if (!PyBytes_Check(obj)) { + if (!PyUnicode_Check(obj)) { + _report_generic_error(); return NULL; } _numpy_internal = PyImport_ImportModule("numpy.core._internal"); @@ -730,22 +733,12 @@ _convert_from_commastring(PyObject *obj, int align) return NULL; } if (PyList_GET_SIZE(listobj) == 1) { - int retcode; - retcode = PyArray_DescrConverter(PyList_GET_ITEM(listobj, 0), - &res); - if (retcode == NPY_FAIL) { - res = NULL; - } + res = _convert_from_any(PyList_GET_ITEM(listobj, 0), align); } else { res = _convert_from_list(listobj, align); } Py_DECREF(listobj); - if (!res && !PyErr_Occurred()) { - PyErr_SetString(PyExc_ValueError, - "invalid data-type"); - return NULL; - } return res; } @@ -766,7 +759,7 @@ _is_tuple_of_integers(PyObject *obj) } /* - * helper function for _use_inherit to disallow dtypes of the form + * helper function for _try_convert_from_inherit_tuple to disallow dtypes of the form * (old_dtype, new_dtype) where either of the dtypes contains python * objects - these dtypes are not useful and can be a source of segfaults, * when an attempt is made to interpret a python object as a different dtype @@ -775,7 +768,7 @@ _is_tuple_of_integers(PyObject *obj) * people have been using to add a field to an object array without fields */ static int -invalid_union_object_dtype(PyArray_Descr *new, PyArray_Descr *conv) +_validate_union_object_dtype(PyArray_Descr *new, PyArray_Descr *conv) { PyObject *name, *tup; PyArray_Descr *dtype; @@ -827,21 +820,26 @@ fail: * a['real'] and a['imag'] to an int32 array. * * leave type reference alone + * + * Returns `Py_NotImplemented` if the second tuple item is not + * appropriate. */ static PyArray_Descr * -_use_inherit(PyArray_Descr *type, PyObject *newobj, int *errflag) +_try_convert_from_inherit_tuple(PyArray_Descr *type, PyObject *newobj) { - PyArray_Descr *new; - PyArray_Descr *conv; - - *errflag = 0; - if (PyArray_IsScalar(newobj, Integer) - || _is_tuple_of_integers(newobj) - || !PyArray_DescrConverter(newobj, &conv)) { - return NULL; + if (PyArray_IsScalar(newobj, Integer) || _is_tuple_of_integers(newobj)) { + /* It's a subarray or flexible type instead */ + Py_INCREF(Py_NotImplemented); + return (PyArray_Descr *)Py_NotImplemented; + } + PyArray_Descr *conv = _convert_from_any(newobj, 0); + if (conv == NULL) { + /* Let someone else try to convert this */ + PyErr_Clear(); + Py_INCREF(Py_NotImplemented); + return (PyArray_Descr *)Py_NotImplemented; } - *errflag = 1; - new = PyArray_DescrNew(type); + PyArray_Descr *new = PyArray_DescrNew(type); if (new == NULL) { goto fail; } @@ -854,7 +852,7 @@ _use_inherit(PyArray_Descr *type, PyObject *newobj, int *errflag) Py_DECREF(new); goto fail; } - else if (invalid_union_object_dtype(new, conv)) { + else if (_validate_union_object_dtype(new, conv) < 0) { Py_DECREF(new); goto fail; } @@ -875,7 +873,6 @@ _use_inherit(PyArray_Descr *type, PyObject *newobj, int *errflag) } new->flags = conv->flags; Py_DECREF(conv); - *errflag = 0; return new; fail: @@ -895,7 +892,7 @@ _use_inherit(PyArray_Descr *type, PyObject *newobj, int *errflag) * Returns 0 on success, -1 if an exception is raised. */ static int -validate_object_field_overlap(PyArray_Descr *dtype) +_validate_object_field_overlap(PyArray_Descr *dtype) { PyObject *names, *fields, *key, *tup, *title; Py_ssize_t i, j, names_size; @@ -991,7 +988,7 @@ validate_object_field_overlap(PyArray_Descr *dtype) * then it will be checked for conformity and used directly. */ static PyArray_Descr * -_use_fields_dict(PyObject *obj, int align) +_convert_from_field_dict(PyObject *obj, int align) { PyObject *_numpy_internal; PyArray_Descr *res; @@ -1012,48 +1009,34 @@ _use_fields_dict(PyObject *obj, int align) static PyArray_Descr * _convert_from_dict(PyObject *obj, int align) { - PyArray_Descr *new; - PyObject *fields = NULL; - PyObject *names = NULL; - PyObject *offsets= NULL; - PyObject *descrs = NULL; - PyObject *titles = NULL; - PyObject *metadata, *tmp; - int n, i; - int totalsize, itemsize; - int maxalign = 0; - /* Types with fields need the Python C API for field access */ - char dtypeflags = NPY_NEEDS_PYAPI; - int has_out_of_order_fields = 0; - - fields = PyDict_New(); + PyObject *fields = PyDict_New(); if (fields == NULL) { return (PyArray_Descr *)PyErr_NoMemory(); } /* * Use PyMapping_GetItemString to support dictproxy objects as well. */ - names = PyMapping_GetItemString(obj, "names"); + PyObject *names = PyMapping_GetItemString(obj, "names"); if (names == NULL) { Py_DECREF(fields); /* XXX should check this is a KeyError */ PyErr_Clear(); - return _use_fields_dict(obj, align); + return _convert_from_field_dict(obj, align); } - descrs = PyMapping_GetItemString(obj, "formats"); + PyObject *descrs = PyMapping_GetItemString(obj, "formats"); if (descrs == NULL) { Py_DECREF(fields); /* XXX should check this is a KeyError */ PyErr_Clear(); Py_DECREF(names); - return _use_fields_dict(obj, align); + return _convert_from_field_dict(obj, align); } - n = PyObject_Length(names); - offsets = PyMapping_GetItemString(obj, "offsets"); + int n = PyObject_Length(names); + PyObject *offsets = PyMapping_GetItemString(obj, "offsets"); if (!offsets) { PyErr_Clear(); } - titles = PyMapping_GetItemString(obj, "titles"); + PyObject *titles = PyMapping_GetItemString(obj, "titles"); if (!titles) { PyErr_Clear(); } @@ -1071,7 +1054,7 @@ _convert_from_dict(PyObject *obj, int align) * If a property 'aligned' is in the dict, it overrides the align flag * to be True if it not already true. */ - tmp = PyMapping_GetItemString(obj, "aligned"); + PyObject *tmp = PyMapping_GetItemString(obj, "aligned"); if (tmp == NULL) { PyErr_Clear(); } else { @@ -1088,16 +1071,16 @@ _convert_from_dict(PyObject *obj, int align) Py_DECREF(tmp); } - totalsize = 0; - for (i = 0; i < n; i++) { - PyObject *tup, *descr, *ind, *title, *name, *off; - int len, ret, _align = 1; - PyArray_Descr *newdescr; - + /* Types with fields need the Python C API for field access */ + char dtypeflags = NPY_NEEDS_PYAPI; + int totalsize = 0; + int maxalign = 0; + int has_out_of_order_fields = 0; + for (int i = 0; i < n; i++) { /* Build item to insert (descr, offset, [title])*/ - len = 2; - title = NULL; - ind = PyInt_FromLong(i); + int len = 2; + PyObject *title = NULL; + PyObject *ind = PyInt_FromLong(i); if (titles) { title=PyObject_GetItem(titles, ind); if (title && title != Py_None) { @@ -1108,39 +1091,34 @@ _convert_from_dict(PyObject *obj, int align) } PyErr_Clear(); } - tup = PyTuple_New(len); - descr = PyObject_GetItem(descrs, ind); + PyObject *tup = PyTuple_New(len); + PyObject *descr = PyObject_GetItem(descrs, ind); if (!descr) { Py_DECREF(tup); Py_DECREF(ind); goto fail; } - if (align) { - ret = PyArray_DescrAlignConverter(descr, &newdescr); - } - else { - ret = PyArray_DescrConverter(descr, &newdescr); - } + PyArray_Descr *newdescr = _convert_from_any(descr, align); Py_DECREF(descr); - if (ret == NPY_FAIL) { + if (newdescr == NULL) { Py_DECREF(tup); Py_DECREF(ind); goto fail; } PyTuple_SET_ITEM(tup, 0, (PyObject *)newdescr); + int _align = 1; if (align) { _align = newdescr->alignment; maxalign = PyArray_MAX(maxalign,_align); } if (offsets) { - long offset; - off = PyObject_GetItem(offsets, ind); + PyObject *off = PyObject_GetItem(offsets, ind); if (!off) { Py_DECREF(tup); Py_DECREF(ind); goto fail; } - offset = PyArray_PyIntAsInt(off); + long offset = PyArray_PyIntAsInt(off); if (error_converting(offset)) { Py_DECREF(off); Py_DECREF(tup); @@ -1168,7 +1146,9 @@ _convert_from_dict(PyObject *obj, int align) "not divisible by the field alignment %d " "with align=True", offset, newdescr->alignment); - ret = NPY_FAIL; + Py_DECREF(ind); + Py_DECREF(tup); + goto fail; } else if (offset + newdescr->elsize > totalsize) { totalsize = offset + newdescr->elsize; @@ -1181,15 +1161,10 @@ _convert_from_dict(PyObject *obj, int align) PyTuple_SET_ITEM(tup, 1, PyInt_FromLong(totalsize)); totalsize += newdescr->elsize; } - if (ret == NPY_FAIL) { - Py_DECREF(ind); - Py_DECREF(tup); - goto fail; - } if (len == 3) { PyTuple_SET_ITEM(tup, 2, title); } - name = PyObject_GetItem(names, ind); + PyObject *name = PyObject_GetItem(names, ind); Py_DECREF(ind); if (!name) { Py_DECREF(tup); @@ -1223,13 +1198,10 @@ _convert_from_dict(PyObject *obj, int align) } } Py_DECREF(tup); - if (ret == NPY_FAIL) { - goto fail; - } dtypeflags |= (newdescr->flags & NPY_FROM_FIELDS); } - new = PyArray_DescrNewFromType(NPY_VOID); + PyArray_Descr *new = PyArray_DescrNewFromType(NPY_VOID); if (new == NULL) { goto fail; } @@ -1259,7 +1231,7 @@ _convert_from_dict(PyObject *obj, int align) * need to verify that no OBJECT types overlap with something else. */ if (has_out_of_order_fields && PyDataType_REFCHK(new)) { - if (validate_object_field_overlap(new) < 0) { + if (_validate_object_field_overlap(new) < 0) { Py_DECREF(new); goto fail; } @@ -1275,7 +1247,7 @@ _convert_from_dict(PyObject *obj, int align) if (tmp == NULL) { PyErr_Clear(); } else { - itemsize = (int)PyArray_PyIntAsInt(tmp); + int itemsize = (int)PyArray_PyIntAsInt(tmp); Py_DECREF(tmp); if (error_converting(itemsize)) { Py_DECREF(new); @@ -1304,7 +1276,7 @@ _convert_from_dict(PyObject *obj, int align) } /* Add the metadata if provided */ - metadata = PyMapping_GetItemString(obj, "metadata"); + PyObject *metadata = PyMapping_GetItemString(obj, "metadata"); if (metadata == NULL) { PyErr_Clear(); @@ -1366,385 +1338,332 @@ PyArray_DescrConverter2(PyObject *obj, PyArray_Descr **at) } } -/*NUMPY_API - * Get typenum from an object -- None goes to NPY_DEFAULT_TYPE - * This function takes a Python object representing a type and converts it - * to a the correct PyArray_Descr * structure to describe the type. - * - * Many objects can be used to represent a data-type which in NumPy is - * quite a flexible concept. - * - * This is the central code that converts Python objects to - * Type-descriptor objects that are used throughout numpy. - * - * Returns a new reference in *at, but the returned should not be - * modified as it may be one of the canonical immutable objects or - * a reference to the input obj. +/** + * Get a dtype instance from a python type */ -NPY_NO_EXPORT int -PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) -{ - int check_num = NPY_NOTYPE + 10; - int elsize = 0; - char endian = '='; - - *at = NULL; +static PyArray_Descr * +_convert_from_type(PyObject *obj) { + PyTypeObject *typ = (PyTypeObject*)obj; - /* default */ - if (obj == Py_None) { - *at = PyArray_DescrFromType(NPY_DEFAULT_TYPE); - return NPY_SUCCEED; + if (PyType_IsSubtype(typ, &PyGenericArrType_Type)) { + return PyArray_DescrFromTypeObject(obj); } - - if (PyArray_DescrCheck(obj)) { - *at = (PyArray_Descr *)obj; - Py_INCREF(*at); - return NPY_SUCCEED; + else if (typ == &PyLong_Type) { + return PyArray_DescrFromType(NPY_LONG); } - - if (PyType_Check(obj)) { - if (PyType_IsSubtype((PyTypeObject *)obj, &PyGenericArrType_Type)) { - *at = PyArray_DescrFromTypeObject(obj); - return (*at) ? NPY_SUCCEED : NPY_FAIL; - } - check_num = NPY_OBJECT; -#if !defined(NPY_PY3K) - if (obj == (PyObject *)(&PyInt_Type)) { - check_num = NPY_LONG; - } - else if (obj == (PyObject *)(&PyLong_Type)) { - check_num = NPY_LONGLONG; - } -#else - if (obj == (PyObject *)(&PyLong_Type)) { - check_num = NPY_LONG; - } -#endif - else if (obj == (PyObject *)(&PyFloat_Type)) { - check_num = NPY_DOUBLE; - } - else if (obj == (PyObject *)(&PyComplex_Type)) { - check_num = NPY_CDOUBLE; - } - else if (obj == (PyObject *)(&PyBool_Type)) { - check_num = NPY_BOOL; - } - else if (obj == (PyObject *)(&PyBytes_Type)) { - check_num = NPY_STRING; - } - else if (obj == (PyObject *)(&PyUnicode_Type)) { - check_num = NPY_UNICODE; - } -#if defined(NPY_PY3K) - else if (obj == (PyObject *)(&PyMemoryView_Type)) { -#else - else if (obj == (PyObject *)(&PyBuffer_Type)) { -#endif - check_num = NPY_VOID; + else if (typ == &PyFloat_Type) { + return PyArray_DescrFromType(NPY_DOUBLE); + } + else if (typ == &PyComplex_Type) { + return PyArray_DescrFromType(NPY_CDOUBLE); + } + else if (typ == &PyBool_Type) { + return PyArray_DescrFromType(NPY_BOOL); + } + else if (typ == &PyBytes_Type) { + return PyArray_DescrFromType(NPY_STRING); + } + else if (typ == &PyUnicode_Type) { + return PyArray_DescrFromType(NPY_UNICODE); + } + else if (typ == &PyMemoryView_Type) { + return PyArray_DescrFromType(NPY_VOID); + } + else { + PyArray_Descr *ret = _try_convert_from_dtype_attr(obj); + if ((PyObject *)ret != Py_NotImplemented) { + return ret; } - else { - if (_arraydescr_from_dtype_attr(obj, at)) { - /* - * Using dtype attribute, *at may be NULL if a - * RecursionError occurred. - */ - if (*at == NULL) { - goto error; - } - return NPY_SUCCEED; - } - /* - * Note: this comes after _arraydescr_from_dtype_attr because the ctypes - * type might override the dtype if numpy does not otherwise - * support it. - */ - if (npy_ctypes_check((PyTypeObject *)obj)) { - *at = _arraydescr_from_ctypes_type((PyTypeObject *)obj); - return *at ? NPY_SUCCEED : NPY_FAIL; - } + Py_DECREF(ret); + + /* + * Note: this comes after _try_convert_from_dtype_attr because the ctypes + * type might override the dtype if numpy does not otherwise + * support it. + */ + ret = _try_convert_from_ctypes_type(typ); + if ((PyObject *)ret != Py_NotImplemented) { + return ret; } - goto finish; + Py_DECREF(ret); + + /* All other classes are treated as object */ + return PyArray_DescrFromType(NPY_OBJECT); } +} - /* or a typecode string */ - if (PyUnicode_Check(obj)) { - /* Allow unicode format strings: convert to bytes */ - int retval; - PyObject *obj2; - obj2 = PyUnicode_AsASCIIString(obj); +static PyArray_Descr * +_convert_from_str(PyObject *obj, int align); + +static PyArray_Descr * +_convert_from_any(PyObject *obj, int align) +{ + /* default */ + if (obj == Py_None) { + return PyArray_DescrFromType(NPY_DEFAULT_TYPE); + } + else if (PyArray_DescrCheck(obj)) { + PyArray_Descr *ret = (PyArray_Descr *)obj; + Py_INCREF(ret); + return ret; + } + else if (PyType_Check(obj)) { + return _convert_from_type(obj); + } + /* or a typecode string */ + else if (PyBytes_Check(obj)) { + /* Allow bytes format strings: convert to unicode */ + PyObject *obj2 = PyUnicode_FromEncodedObject(obj, NULL, NULL); if (obj2 == NULL) { /* Convert the exception into a TypeError */ - PyObject *err = PyErr_Occurred(); - if (PyErr_GivenExceptionMatches(err, PyExc_UnicodeEncodeError)) { + if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { PyErr_SetString(PyExc_TypeError, "data type not understood"); } - return NPY_FAIL; + return NULL; } - retval = PyArray_DescrConverter(obj2, at); + PyArray_Descr *ret = _convert_from_str(obj2, align); Py_DECREF(obj2); - return retval; + return ret; } - - if (PyBytes_Check(obj)) { - char *type = NULL; - Py_ssize_t len = 0; - - /* Check for a string typecode. */ - if (PyBytes_AsStringAndSize(obj, &type, &len) < 0) { - goto error; - } - - /* Empty string is invalid */ - if (len == 0) { - goto fail; - } - - /* check for commas present or first (or second) element a digit */ - if (_check_for_commastring(type, len)) { - *at = _convert_from_commastring(obj, 0); - return (*at) ? NPY_SUCCEED : NPY_FAIL; - } - - /* Process the endian character. '|' is replaced by '='*/ - switch (type[0]) { - case '>': - case '<': - case '=': - endian = type[0]; - ++type; - --len; - break; - - case '|': - endian = '='; - ++type; - --len; - break; - } - - /* Just an endian character is invalid */ - if (len == 0) { - goto fail; - } - - /* Check for datetime format */ - if (is_datetime_typestr(type, len)) { - *at = parse_dtype_from_datetime_typestr(type, len); - if (*at == NULL) { - return NPY_FAIL; - } - /* *at has byte order '=' at this point */ - if (!PyArray_ISNBO(endian)) { - (*at)->byteorder = endian; - } - return NPY_SUCCEED; - } - - /* A typecode like 'd' */ - if (len == 1) { - /* Python byte string characters are unsigned */ - check_num = (unsigned char) type[0]; - } - /* A kind + size like 'f8' */ - else { - char *typeend = NULL; - int kind; - - /* Parse the integer, make sure it's the rest of the string */ - elsize = (int)strtol(type + 1, &typeend, 10); - if (typeend - type == len) { - - kind = type[0]; - switch (kind) { - case NPY_STRINGLTR: - case NPY_STRINGLTR2: - check_num = NPY_STRING; - break; - - /* - * When specifying length of UNICODE - * the number of characters is given to match - * the STRING interface. Each character can be - * more than one byte and itemsize must be - * the number of bytes. - */ - case NPY_UNICODELTR: - check_num = NPY_UNICODE; - elsize <<= 2; - break; - - case NPY_VOIDLTR: - check_num = NPY_VOID; - break; - - default: - if (elsize == 0) { - check_num = NPY_NOTYPE+10; - } - /* Support for generic processing c8, i4, f8, etc...*/ - else { - check_num = PyArray_TypestrConvert(elsize, kind); - if (check_num == NPY_NOTYPE) { - check_num += 10; - } - elsize = 0; - } - } - } - } + else if (PyUnicode_Check(obj)) { + return _convert_from_str(obj, align); } else if (PyTuple_Check(obj)) { /* or a tuple */ - *at = _convert_from_tuple(obj, 0); - if (*at == NULL){ - if (PyErr_Occurred()) { - return NPY_FAIL; - } - goto fail; - } - return NPY_SUCCEED; + return _convert_from_tuple(obj, align); } else if (PyList_Check(obj)) { /* or a list */ - *at = _convert_from_array_descr(obj,0); - if (*at == NULL) { - if (PyErr_Occurred()) { - return NPY_FAIL; - } - goto fail; - } - return NPY_SUCCEED; + return _convert_from_array_descr(obj, align); } else if (PyDict_Check(obj) || PyDictProxy_Check(obj)) { /* or a dictionary */ - *at = _convert_from_dict(obj,0); - if (*at == NULL) { - if (PyErr_Occurred()) { - return NPY_FAIL; - } - goto fail; - } - return NPY_SUCCEED; + return _convert_from_dict(obj, align); } else if (PyArray_Check(obj)) { - goto fail; + _report_generic_error(); + return NULL; } else { - if (_arraydescr_from_dtype_attr(obj, at)) { - /* - * Using dtype attribute, *at may be NULL if a - * RecursionError occurred. - */ - if (*at == NULL) { - goto error; - } - return NPY_SUCCEED; + PyArray_Descr *ret = _try_convert_from_dtype_attr(obj); + if ((PyObject *)ret != Py_NotImplemented) { + return ret; } + Py_DECREF(ret); /* - * Note: this comes after _arraydescr_from_dtype_attr because the ctypes + * Note: this comes after _try_convert_from_dtype_attr because the ctypes * type might override the dtype if numpy does not otherwise * support it. */ - if (npy_ctypes_check(Py_TYPE(obj))) { - *at = _arraydescr_from_ctypes_type(Py_TYPE(obj)); - return *at ? NPY_SUCCEED : NPY_FAIL; + ret = _try_convert_from_ctypes_type(Py_TYPE(obj)); + if ((PyObject *)ret != Py_NotImplemented) { + return ret; } + Py_DECREF(ret); + _report_generic_error(); + return NULL; + } +} + + +/*NUMPY_API + * Get typenum from an object -- None goes to NPY_DEFAULT_TYPE + * This function takes a Python object representing a type and converts it + * to a the correct PyArray_Descr * structure to describe the type. + * + * Many objects can be used to represent a data-type which in NumPy is + * quite a flexible concept. + * + * This is the central code that converts Python objects to + * Type-descriptor objects that are used throughout numpy. + * + * Returns a new reference in *at, but the returned should not be + * modified as it may be one of the canonical immutable objects or + * a reference to the input obj. + */ +NPY_NO_EXPORT int +PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) +{ + *at = _convert_from_any(obj, 0); + return (*at) ? NPY_SUCCEED : NPY_FAIL; +} + +/** Convert a bytestring specification into a dtype */ +static PyArray_Descr * +_convert_from_str(PyObject *obj, int align) +{ + /* Check for a string typecode. */ + Py_ssize_t len = 0; + char const *type = PyUnicode_AsUTF8AndSize(obj, &len); + if (type == NULL) { + return NULL; + } + + /* Empty string is invalid */ + if (len == 0) { + goto fail; + } + + /* check for commas present or first (or second) element a digit */ + if (_check_for_commastring(type, len)) { + return _convert_from_commastring(obj, align); + } + + /* Process the endian character. '|' is replaced by '='*/ + char endian = '='; + switch (type[0]) { + case '>': + case '<': + case '=': + endian = type[0]; + ++type; + --len; + break; + + case '|': + endian = '='; + ++type; + --len; + break; + } + + /* Just an endian character is invalid */ + if (len == 0) { goto fail; } + + /* Check for datetime format */ + if (is_datetime_typestr(type, len)) { + PyArray_Descr *ret = parse_dtype_from_datetime_typestr(type, len); + if (ret == NULL) { + return NULL; + } + /* ret has byte order '=' at this point */ + if (!PyArray_ISNBO(endian)) { + ret->byteorder = endian; + } + return ret; + } + + int check_num = NPY_NOTYPE + 10; + int elsize = 0; + /* A typecode like 'd' */ + if (len == 1) { + /* Python byte string characters are unsigned */ + check_num = (unsigned char) type[0]; + } + /* A kind + size like 'f8' */ + else { + char *typeend = NULL; + int kind; + + /* Parse the integer, make sure it's the rest of the string */ + elsize = (int)strtol(type + 1, &typeend, 10); + if (typeend - type == len) { + + kind = type[0]; + switch (kind) { + case NPY_STRINGLTR: + case NPY_STRINGLTR2: + check_num = NPY_STRING; + break; + + /* + * When specifying length of UNICODE + * the number of characters is given to match + * the STRING interface. Each character can be + * more than one byte and itemsize must be + * the number of bytes. + */ + case NPY_UNICODELTR: + check_num = NPY_UNICODE; + elsize <<= 2; + break; + + case NPY_VOIDLTR: + check_num = NPY_VOID; + break; + + default: + if (elsize == 0) { + check_num = NPY_NOTYPE+10; + } + /* Support for generic processing c8, i4, f8, etc...*/ + else { + check_num = PyArray_TypestrConvert(elsize, kind); + if (check_num == NPY_NOTYPE) { + check_num += 10; + } + elsize = 0; + } + } + } + } + if (PyErr_Occurred()) { goto fail; } -finish: + PyArray_Descr *ret; if ((check_num == NPY_NOTYPE + 10) || - (*at = PyArray_DescrFromType(check_num)) == NULL) { + (ret = PyArray_DescrFromType(check_num)) == NULL) { PyErr_Clear(); /* Now check to see if the object is registered in typeDict */ - if (typeDict != NULL) { - PyObject *item = NULL; -#if defined(NPY_PY3K) - if (PyBytes_Check(obj)) { - PyObject *tmp; - tmp = PyUnicode_FromEncodedObject(obj, "ascii", "strict"); - if (tmp == NULL) { + if (typeDict == NULL) { + goto fail; + } + PyObject *item = PyDict_GetItem(typeDict, obj); + if (item == NULL) { + goto fail; + } + + /* Check for a deprecated Numeric-style typecode */ + char *dep_tps[] = {"Bool", "Complex", "Float", "Int", + "Object0", "String0", "Timedelta64", + "Unicode0", "UInt", "Void0"}; + int ndep_tps = sizeof(dep_tps) / sizeof(dep_tps[0]); + for (int i = 0; i < ndep_tps; ++i) { + char *dep_tp = dep_tps[i]; + + if (strncmp(type, dep_tp, strlen(dep_tp)) == 0) { + if (DEPRECATE("Numeric-style type codes are " + "deprecated and will result in " + "an error in the future.") < 0) { goto fail; } - item = PyDict_GetItem(typeDict, tmp); - Py_DECREF(tmp); - } - else { - item = PyDict_GetItem(typeDict, obj); - } -#else - item = PyDict_GetItem(typeDict, obj); -#endif - if (item) { - /* Check for a deprecated Numeric-style typecode */ - if (PyBytes_Check(obj)) { - char *type = NULL; - Py_ssize_t len = 0; - char *dep_tps[] = {"Bool", "Complex", "Float", "Int", - "Object0", "String0", "Timedelta64", - "Unicode0", "UInt", "Void0"}; - int ndep_tps = sizeof(dep_tps) / sizeof(dep_tps[0]); - int i; - - if (PyBytes_AsStringAndSize(obj, &type, &len) < 0) { - goto error; - } - for (i = 0; i < ndep_tps; ++i) { - char *dep_tp = dep_tps[i]; - - if (strncmp(type, dep_tp, strlen(dep_tp)) == 0) { - if (DEPRECATE("Numeric-style type codes are " - "deprecated and will result in " - "an error in the future.") < 0) { - goto fail; - } - } - } - } - return PyArray_DescrConverter(item, at); } } - goto fail; + /* + * Probably only ever dispatches to `_convert_from_type`, but who + * knows what users are injecting into `np.typeDict`. + */ + return _convert_from_any(item, align); } - if (PyDataType_ISUNSIZED(*at) && (*at)->elsize != elsize) { - PyArray_DESCR_REPLACE(*at); - if (*at == NULL) { - goto error; + if (PyDataType_ISUNSIZED(ret) && ret->elsize != elsize) { + PyArray_DESCR_REPLACE(ret); + if (ret == NULL) { + return NULL; } - (*at)->elsize = elsize; + ret->elsize = elsize; } if (endian != '=' && PyArray_ISNBO(endian)) { endian = '='; } - if (endian != '=' && (*at)->byteorder != '|' - && (*at)->byteorder != endian) { - PyArray_DESCR_REPLACE(*at); - if (*at == NULL) { - goto error; + if (endian != '=' && ret->byteorder != '|' && ret->byteorder != endian) { + PyArray_DESCR_REPLACE(ret); + if (ret == NULL) { + return NULL; } - (*at)->byteorder = endian; + ret->byteorder = endian; } - return NPY_SUCCEED; + return ret; fail: - if (PyBytes_Check(obj)) { - PyErr_Format(PyExc_TypeError, - "data type \"%s\" not understood", PyBytes_AS_STRING(obj)); - } - else { - PyErr_SetString(PyExc_TypeError, - "data type not understood"); - } - -error: - *at = NULL; - return NPY_FAIL; + PyErr_Format(PyExc_TypeError, "data type %R not understood", obj); + return NULL; } /** Array Descr Objects for dynamic types **/ @@ -2281,12 +2200,8 @@ arraydescr_new(PyTypeObject *NPY_UNUSED(subtype), return NULL; } - if (align) { - if (!PyArray_DescrAlignConverter(odescr, &conv)) { - return NULL; - } - } - else if (!PyArray_DescrConverter(odescr, &conv)) { + conv = _convert_from_any(odescr, align); + if (conv == NULL) { return NULL; } @@ -2733,11 +2648,7 @@ arraydescr_setstate(PyArray_Descr *self, PyObject *args) subarray_shape = PyTuple_GET_ITEM(subarray, 1); if (PyNumber_Check(subarray_shape)) { PyObject *tmp; -#if defined(NPY_PY3K) tmp = PyNumber_Long(subarray_shape); -#else - tmp = PyNumber_Int(subarray_shape); -#endif if (tmp == NULL) { return NULL; } @@ -2792,7 +2703,6 @@ arraydescr_setstate(PyArray_Descr *self, PyObject *args) } } else { -#if defined(NPY_PY3K) /* * To support pickle.load(f, encoding='bytes') for loading Py2 * generated pickles on Py3, we need to be more lenient and convert @@ -2837,11 +2747,6 @@ arraydescr_setstate(PyArray_Descr *self, PyObject *args) return NULL; } } -#else - PyErr_Format(PyExc_ValueError, - "non-string names in Numpy dtype unpickling"); - return NULL; -#endif } } @@ -2930,35 +2835,8 @@ arraydescr_setstate(PyArray_Descr *self, PyObject *args) NPY_NO_EXPORT int PyArray_DescrAlignConverter(PyObject *obj, PyArray_Descr **at) { - if (PyDict_Check(obj) || PyDictProxy_Check(obj)) { - *at = _convert_from_dict(obj, 1); - } - else if (PyBytes_Check(obj)) { - *at = _convert_from_commastring(obj, 1); - } - else if (PyUnicode_Check(obj)) { - PyObject *tmp; - tmp = PyUnicode_AsASCIIString(obj); - *at = _convert_from_commastring(tmp, 1); - Py_DECREF(tmp); - } - else if (PyTuple_Check(obj)) { - *at = _convert_from_tuple(obj, 1); - } - else if (PyList_Check(obj)) { - *at = _convert_from_array_descr(obj, 1); - } - else { - return PyArray_DescrConverter(obj, at); - } - if (*at == NULL) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_ValueError, - "data-type-descriptor not understood"); - } - return NPY_FAIL; - } - return NPY_SUCCEED; + *at = _convert_from_any(obj, 1); + return (*at) ? NPY_SUCCEED : NPY_FAIL; } /*NUMPY_API @@ -2969,32 +2847,13 @@ PyArray_DescrAlignConverter(PyObject *obj, PyArray_Descr **at) NPY_NO_EXPORT int PyArray_DescrAlignConverter2(PyObject *obj, PyArray_Descr **at) { - if (PyDict_Check(obj) || PyDictProxy_Check(obj)) { - *at = _convert_from_dict(obj, 1); - } - else if (PyBytes_Check(obj)) { - *at = _convert_from_commastring(obj, 1); - } - else if (PyUnicode_Check(obj)) { - PyObject *tmp; - tmp = PyUnicode_AsASCIIString(obj); - *at = _convert_from_commastring(tmp, 1); - Py_DECREF(tmp); - } - else if (PyList_Check(obj)) { - *at = _convert_from_array_descr(obj, 1); + if (obj == Py_None) { + *at = NULL; + return NPY_SUCCEED; } else { - return PyArray_DescrConverter2(obj, at); - } - if (*at == NULL) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_ValueError, - "data-type-descriptor not understood"); - } - return NPY_FAIL; + return PyArray_DescrAlignConverter(obj, at); } - return NPY_SUCCEED; } @@ -3217,7 +3076,8 @@ arraydescr_richcompare(PyArray_Descr *self, PyObject *other, int cmp_op) PyArray_Descr *new = NULL; PyObject *result = Py_NotImplemented; if (!PyArray_DescrCheck(other)) { - if (PyArray_DescrConverter(other, &new) == NPY_FAIL) { + new = _convert_from_any(other, 0); + if (new == NULL) { return NULL; } } @@ -3293,10 +3153,6 @@ static PyNumberMethods descr_as_number = { (binaryfunc)0, /* nb_add */ (binaryfunc)0, /* nb_subtract */ (binaryfunc)0, /* nb_multiply */ - #if defined(NPY_PY3K) - #else - (binaryfunc)0, /* nb_divide */ - #endif (binaryfunc)0, /* nb_remainder */ (binaryfunc)0, /* nb_divmod */ (ternaryfunc)0, /* nb_power */ @@ -3336,7 +3192,7 @@ descr_repeat(PyObject *self, Py_ssize_t length) if (tup == NULL) { return NULL; } - PyArray_DescrConverter(tup, &new); + new = _convert_from_any(tup, 0); Py_DECREF(tup); return (PyObject *)new; } @@ -3345,20 +3201,7 @@ static int _check_has_fields(PyArray_Descr *self) { if (!PyDataType_HASFIELDS(self)) { - PyObject *astr = arraydescr_str(self); - if (astr == NULL) { - return -1; - } -#if defined(NPY_PY3K) - { - PyObject *bstr = PyUnicode_AsUnicodeEscapeString(astr); - Py_DECREF(astr); - astr = bstr; - } -#endif - PyErr_Format(PyExc_KeyError, - "There are no fields in dtype %s.", PyBytes_AsString(astr)); - Py_DECREF(astr); + PyErr_Format(PyExc_KeyError, "There are no fields in dtype %S.", self); return -1; } else { @@ -3370,25 +3213,12 @@ static PyObject * _subscript_by_name(PyArray_Descr *self, PyObject *op) { PyObject *obj = PyDict_GetItem(self->fields, op); - PyObject *descr; - PyObject *s; - if (obj == NULL) { - if (PyUnicode_Check(op)) { - s = PyUnicode_AsUnicodeEscapeString(op); - } - else { - s = op; - } - PyErr_Format(PyExc_KeyError, - "Field named \'%s\' not found.", PyBytes_AsString(s)); - if (s != op) { - Py_DECREF(s); - } + "Field named %R not found.", op); return NULL; } - descr = PyTuple_GET_ITEM(obj, 0); + PyObject *descr = PyTuple_GET_ITEM(obj, 0); Py_INCREF(descr); return descr; } @@ -3570,12 +3400,7 @@ static PyMappingMethods descr_as_mapping = { /****************** End of Mapping Protocol ******************************/ NPY_NO_EXPORT PyTypeObject PyArrayDescr_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.dtype", /* tp_name */ sizeof(PyArray_Descr), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -3584,11 +3409,7 @@ NPY_NO_EXPORT PyTypeObject PyArrayDescr_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) (void *)0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif (reprfunc)arraydescr_repr, /* tp_repr */ &descr_as_number, /* tp_as_number */ &descr_as_sequence, /* tp_as_sequence */ diff --git a/numpy/core/src/multiarray/descriptor.h b/numpy/core/src/multiarray/descriptor.h index 6024c5e77..fc9e0895b 100644 --- a/numpy/core/src/multiarray/descriptor.h +++ b/numpy/core/src/multiarray/descriptor.h @@ -7,8 +7,8 @@ NPY_NO_EXPORT PyObject *arraydescr_protocol_descr_get(PyArray_Descr *self); NPY_NO_EXPORT PyObject * array_set_typeDict(PyObject *NPY_UNUSED(ignored), PyObject *args); -int -_arraydescr_from_dtype_attr(PyObject *obj, PyArray_Descr **newdescr); +NPY_NO_EXPORT PyArray_Descr * +_arraydescr_try_convert_from_dtype_attr(PyObject *obj); NPY_NO_EXPORT int @@ -26,6 +26,6 @@ is_dtype_struct_simple_unaligned_layout(PyArray_Descr *dtype); NPY_NO_EXPORT PyArray_Descr * arraydescr_field_subset_view(PyArray_Descr *self, PyObject *ind); -extern NPY_NO_EXPORT char *_datetime_strings[]; +extern NPY_NO_EXPORT char const *_datetime_strings[]; #endif diff --git a/numpy/core/src/multiarray/dtype_transfer.c b/numpy/core/src/multiarray/dtype_transfer.c index ef0dd4a01..b26d5ac89 100644 --- a/numpy/core/src/multiarray/dtype_transfer.c +++ b/numpy/core/src/multiarray/dtype_transfer.c @@ -3832,8 +3832,8 @@ PyArray_CastRawArrays(npy_intp count, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -PyArray_PrepareOneRawArrayIter(int ndim, npy_intp *shape, - char *data, npy_intp *strides, +PyArray_PrepareOneRawArrayIter(int ndim, npy_intp const *shape, + char *data, npy_intp const *strides, int *out_ndim, npy_intp *out_shape, char **out_data, npy_intp *out_strides) { @@ -3953,9 +3953,9 @@ PyArray_PrepareOneRawArrayIter(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -PyArray_PrepareTwoRawArrayIter(int ndim, npy_intp *shape, - char *dataA, npy_intp *stridesA, - char *dataB, npy_intp *stridesB, +PyArray_PrepareTwoRawArrayIter(int ndim, npy_intp const *shape, + char *dataA, npy_intp const *stridesA, + char *dataB, npy_intp const *stridesB, int *out_ndim, npy_intp *out_shape, char **out_dataA, npy_intp *out_stridesA, char **out_dataB, npy_intp *out_stridesB) @@ -4077,10 +4077,10 @@ PyArray_PrepareTwoRawArrayIter(int ndim, npy_intp *shape, * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int -PyArray_PrepareThreeRawArrayIter(int ndim, npy_intp *shape, - char *dataA, npy_intp *stridesA, - char *dataB, npy_intp *stridesB, - char *dataC, npy_intp *stridesC, +PyArray_PrepareThreeRawArrayIter(int ndim, npy_intp const *shape, + char *dataA, npy_intp const *stridesA, + char *dataB, npy_intp const *stridesB, + char *dataC, npy_intp const *stridesC, int *out_ndim, npy_intp *out_shape, char **out_dataA, npy_intp *out_stridesA, char **out_dataB, npy_intp *out_stridesB, diff --git a/numpy/core/src/multiarray/einsum.c.src b/numpy/core/src/multiarray/einsum.c.src index 58af44091..70af3fef9 100644 --- a/numpy/core/src/multiarray/einsum.c.src +++ b/numpy/core/src/multiarray/einsum.c.src @@ -107,7 +107,7 @@ */ static void @name@_sum_of_products_@noplabel@(int nop, char **dataptr, - npy_intp *strides, npy_intp count) + npy_intp const *strides, npy_intp count) { #if (@nop@ == 1) || (@nop@ <= 3 && !@complex@) char *data0 = dataptr[0]; @@ -206,7 +206,7 @@ static void static void @name@_sum_of_products_contig_one(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { @type@ *data0 = (@type@ *)dataptr[0]; @type@ *data_out = (@type@ *)dataptr[1]; @@ -268,7 +268,7 @@ finish_after_unrolled_loop: static void @name@_sum_of_products_contig_two(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { @type@ *data0 = (@type@ *)dataptr[0]; @type@ *data1 = (@type@ *)dataptr[1]; @@ -354,7 +354,7 @@ finish_after_unrolled_loop: /* Some extra specializations for the two operand case */ static void @name@_sum_of_products_stride0_contig_outcontig_two(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { @temptype@ value0 = @from@(*(@type@ *)dataptr[0]); @type@ *data1 = (@type@ *)dataptr[1]; @@ -483,7 +483,7 @@ finish_after_unrolled_loop: static void @name@_sum_of_products_contig_stride0_outcontig_two(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { @type@ *data0 = (@type@ *)dataptr[0]; @temptype@ value1 = @from@(*(@type@ *)dataptr[1]); @@ -567,7 +567,7 @@ finish_after_unrolled_loop: static void @name@_sum_of_products_contig_contig_outstride0_two(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { @type@ *data0 = (@type@ *)dataptr[0]; @type@ *data1 = (@type@ *)dataptr[1]; @@ -727,7 +727,7 @@ finish_after_unrolled_loop: static void @name@_sum_of_products_stride0_contig_outstride0_two(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { @temptype@ value0 = @from@(*(@type@ *)dataptr[0]); @type@ *data1 = (@type@ *)dataptr[1]; @@ -826,7 +826,7 @@ finish_after_unrolled_loop: static void @name@_sum_of_products_contig_stride0_outstride0_two(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { @type@ *data0 = (@type@ *)dataptr[0]; @temptype@ value1 = @from@(*(@type@ *)dataptr[1]); @@ -927,7 +927,7 @@ finish_after_unrolled_loop: static void @name@_sum_of_products_contig_three(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { @type@ *data0 = (@type@ *)dataptr[0]; @type@ *data1 = (@type@ *)dataptr[1]; @@ -971,7 +971,7 @@ static void static void @name@_sum_of_products_contig_@noplabel@(int nop, char **dataptr, - npy_intp *NPY_UNUSED(strides), npy_intp count) + npy_intp const *NPY_UNUSED(strides), npy_intp count) { NPY_EINSUM_DBG_PRINT1("@name@_sum_of_products_contig_@noplabel@ (%d)\n", (int)count); @@ -1024,7 +1024,7 @@ static void static void @name@_sum_of_products_contig_outstride0_one(int nop, char **dataptr, - npy_intp *strides, npy_intp count) + npy_intp const *strides, npy_intp count) { #if @complex@ @temptype@ accum_re = 0, accum_im = 0; @@ -1201,7 +1201,7 @@ finish_after_unrolled_loop: static void @name@_sum_of_products_outstride0_@noplabel@(int nop, char **dataptr, - npy_intp *strides, npy_intp count) + npy_intp const *strides, npy_intp count) { #if @complex@ @temptype@ accum_re = 0, accum_im = 0; @@ -1319,7 +1319,7 @@ static void static void bool_sum_of_products_@noplabel@(int nop, char **dataptr, - npy_intp *strides, npy_intp count) + npy_intp const *strides, npy_intp count) { #if (@nop@ <= 3) char *data0 = dataptr[0]; @@ -1376,7 +1376,7 @@ bool_sum_of_products_@noplabel@(int nop, char **dataptr, static void bool_sum_of_products_contig_@noplabel@(int nop, char **dataptr, - npy_intp *strides, npy_intp count) + npy_intp const *strides, npy_intp count) { #if (@nop@ <= 3) char *data0 = dataptr[0]; @@ -1484,7 +1484,7 @@ finish_after_unrolled_loop: static void bool_sum_of_products_outstride0_@noplabel@(int nop, char **dataptr, - npy_intp *strides, npy_intp count) + npy_intp const *strides, npy_intp count) { npy_bool accum = 0; @@ -1538,7 +1538,7 @@ bool_sum_of_products_outstride0_@noplabel@(int nop, char **dataptr, /**end repeat**/ -typedef void (*sum_of_products_fn)(int, char **, npy_intp *, npy_intp); +typedef void (*sum_of_products_fn)(int, char **, npy_intp const*, npy_intp); /* These tables need to match up with the type enum */ static sum_of_products_fn @@ -1720,7 +1720,7 @@ static sum_of_products_fn _unspecialized_table[NPY_NTYPES][4] = { static sum_of_products_fn get_sum_of_products_function(int nop, int type_num, - npy_intp itemsize, npy_intp *fixed_strides) + npy_intp itemsize, npy_intp const *fixed_strides) { int iop; diff --git a/numpy/core/src/multiarray/flagsobject.c b/numpy/core/src/multiarray/flagsobject.c index a66b9d40d..6fe0eff4a 100644 --- a/numpy/core/src/multiarray/flagsobject.c +++ b/numpy/core/src/multiarray/flagsobject.c @@ -727,47 +727,25 @@ arrayflags_print(PyArrayFlagsObject *self) ); } -static int -arrayflags_compare(PyArrayFlagsObject *self, PyArrayFlagsObject *other) -{ - if (self->flags == other->flags) { - return 0; - } - else if (self->flags < other->flags) { - return -1; - } - else { - return 1; - } -} - - static PyObject* arrayflags_richcompare(PyObject *self, PyObject *other, int cmp_op) { - PyObject *result = Py_NotImplemented; - int cmp; - - if (cmp_op != Py_EQ && cmp_op != Py_NE) { - PyErr_SetString(PyExc_TypeError, - "undefined comparison for flag object"); - return NULL; + if (!PyObject_TypeCheck(other, &PyArrayFlags_Type)) { + Py_RETURN_NOTIMPLEMENTED; } - if (PyObject_TypeCheck(other, &PyArrayFlags_Type)) { - cmp = arrayflags_compare((PyArrayFlagsObject *)self, - (PyArrayFlagsObject *)other); + npy_bool eq = ((PyArrayFlagsObject*) self)->flags == + ((PyArrayFlagsObject*) other)->flags; - if (cmp_op == Py_EQ) { - result = (cmp == 0) ? Py_True : Py_False; - } - else if (cmp_op == Py_NE) { - result = (cmp != 0) ? Py_True : Py_False; - } + if (cmp_op == Py_EQ) { + return PyBool_FromLong(eq); + } + else if (cmp_op == Py_NE) { + return PyBool_FromLong(!eq); + } + else { + Py_RETURN_NOTIMPLEMENTED; } - - Py_INCREF(result); - return result; } static PyMappingMethods arrayflags_as_mapping = { @@ -793,12 +771,7 @@ arrayflags_new(PyTypeObject *NPY_UNUSED(self), PyObject *args, PyObject *NPY_UNU } NPY_NO_EXPORT PyTypeObject PyArrayFlags_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.flagsobj", sizeof(PyArrayFlagsObject), 0, /* tp_itemsize */ @@ -807,11 +780,7 @@ NPY_NO_EXPORT PyTypeObject PyArrayFlags_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - (cmpfunc)arrayflags_compare, /* tp_compare */ -#endif (reprfunc)arrayflags_print, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ diff --git a/numpy/core/src/multiarray/getset.c b/numpy/core/src/multiarray/getset.c index 6e5d480d0..9d39ee7a8 100644 --- a/numpy/core/src/multiarray/getset.c +++ b/numpy/core/src/multiarray/getset.c @@ -110,12 +110,7 @@ array_strides_set(PyArrayObject *self, PyObject *obj) npy_intp offset = 0; npy_intp lower_offset = 0; npy_intp upper_offset = 0; -#if defined(NPY_PY3K) Py_buffer view; -#else - Py_ssize_t buf_len; - char *buf; -#endif if (obj == NULL) { PyErr_SetString(PyExc_AttributeError, @@ -140,7 +135,6 @@ array_strides_set(PyArrayObject *self, PyObject *obj) * Get the available memory through the buffer interface on * PyArray_BASE(new) or if that fails from the current new */ -#if defined(NPY_PY3K) if (PyArray_BASE(new) && PyObject_GetBuffer(PyArray_BASE(new), &view, PyBUF_SIMPLE) >= 0) { offset = PyArray_BYTES(self) - (char *)view.buf; @@ -148,14 +142,6 @@ array_strides_set(PyArrayObject *self, PyObject *obj) PyBuffer_Release(&view); _dealloc_cached_buffer_info((PyObject*)new); } -#else - if (PyArray_BASE(new) && - PyObject_AsReadBuffer(PyArray_BASE(new), (const void **)&buf, - &buf_len) >= 0) { - offset = PyArray_BYTES(self) - buf; - numbytes = buf_len + offset; - } -#endif else { PyErr_Clear(); offset_bounds_from_strides(PyArray_ITEMSIZE(new), PyArray_NDIM(new), @@ -318,23 +304,7 @@ array_interface_get(PyArrayObject *self) static PyObject * array_data_get(PyArrayObject *self) { -#if defined(NPY_PY3K) return PyMemoryView_FromObject((PyObject *)self); -#else - npy_intp nbytes; - if (!(PyArray_ISONESEGMENT(self))) { - PyErr_SetString(PyExc_AttributeError, "cannot get single-"\ - "segment buffer for discontiguous array"); - return NULL; - } - nbytes = PyArray_NBYTES(self); - if (PyArray_ISWRITEABLE(self)) { - return PyBuffer_FromReadWriteObject((PyObject *)self, 0, (Py_ssize_t) nbytes); - } - else { - return PyBuffer_FromObject((PyObject *)self, 0, (Py_ssize_t) nbytes); - } -#endif } static int @@ -343,9 +313,7 @@ array_data_set(PyArrayObject *self, PyObject *op) void *buf; Py_ssize_t buf_len; int writeable=1; -#if defined(NPY_PY3K) Py_buffer view; -#endif /* 2016-19-02, 1.12 */ int ret = DEPRECATE("Assigning the 'data' attribute is an " @@ -360,7 +328,6 @@ array_data_set(PyArrayObject *self, PyObject *op) "Cannot delete array data"); return -1; } -#if defined(NPY_PY3K) if (PyObject_GetBuffer(op, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) { writeable = 0; PyErr_Clear(); @@ -378,18 +345,7 @@ array_data_set(PyArrayObject *self, PyObject *op) */ PyBuffer_Release(&view); _dealloc_cached_buffer_info(op); -#else - if (PyObject_AsWriteBuffer(op, &buf, &buf_len) < 0) { - PyErr_Clear(); - writeable = 0; - if (PyObject_AsReadBuffer(op, (const void **)&buf, &buf_len) < 0) { - PyErr_Clear(); - PyErr_SetString(PyExc_AttributeError, - "object does not have single-segment buffer interface"); - return -1; - } - } -#endif + if (!PyArray_ISONESEGMENT(self)) { PyErr_SetString(PyExc_AttributeError, "cannot set single-segment buffer for discontiguous array"); diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index a6ac902d3..54d9085b7 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -825,7 +825,7 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *out, */ static int _new_sortlike(PyArrayObject *op, int axis, PyArray_SortFunc *sort, - PyArray_PartitionFunc *part, npy_intp *kth, npy_intp nkth) + PyArray_PartitionFunc *part, npy_intp const *kth, npy_intp nkth) { npy_intp N = PyArray_DIM(op, axis); npy_intp elsize = (npy_intp)PyArray_ITEMSIZE(op); @@ -953,7 +953,7 @@ fail: static PyObject* _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, PyArray_ArgPartitionFunc *argpart, - npy_intp *kth, npy_intp nkth) + npy_intp const *kth, npy_intp nkth) { npy_intp N = PyArray_DIM(op, axis); npy_intp elsize = (npy_intp)PyArray_ITEMSIZE(op); @@ -1059,12 +1059,10 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, if (argpart == NULL) { ret = argsort(valptr, idxptr, N, op); -#if defined(NPY_PY3K) /* Object comparisons may raise an exception in Python 3 */ if (hasrefs && PyErr_Occurred()) { ret = -1; } -#endif if (ret < 0) { goto fail; } @@ -1075,12 +1073,10 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, for (i = 0; i < nkth; ++i) { ret = argpart(valptr, idxptr, N, kth[i], pivots, &npiv, op); -#if defined(NPY_PY3K) /* Object comparisons may raise an exception in Python 3 */ if (hasrefs && PyErr_Occurred()) { ret = -1; } -#endif if (ret < 0) { goto fail; } @@ -1566,12 +1562,8 @@ PyArray_LexSort(PyObject *sort_keys, int axis) _strided_byte_swap(valbuffer, (npy_intp) elsize, N, elsize); } rcode = argsort(valbuffer, (npy_intp *)indbuffer, N, mps[j]); -#if defined(NPY_PY3K) if (rcode < 0 || (PyDataType_REFCHK(PyArray_DESCR(mps[j])) && PyErr_Occurred())) { -#else - if (rcode < 0) { -#endif PyDataMem_FREE(valbuffer); PyDataMem_FREE(indbuffer); free(swaps); @@ -1601,12 +1593,8 @@ PyArray_LexSort(PyObject *sort_keys, int axis) } rcode = argsort(its[j]->dataptr, (npy_intp *)rit->dataptr, N, mps[j]); -#if defined(NPY_PY3K) if (rcode < 0 || (PyDataType_REFCHK(PyArray_DESCR(mps[j])) && PyErr_Occurred())) { -#else - if (rcode < 0) { -#endif goto fail; } PyArray_ITER_NEXT(its[j]); @@ -2028,7 +2016,7 @@ count_nonzero_bytes_384(const npy_uint64 * w) * Returns -1 on error. */ NPY_NO_EXPORT npy_intp -count_boolean_trues(int ndim, char *data, npy_intp *ashape, npy_intp *astrides) +count_boolean_trues(int ndim, char *data, npy_intp const *ashape, npy_intp const *astrides) { int idim; npy_intp shape[NPY_MAXDIMS], strides[NPY_MAXDIMS]; @@ -2392,7 +2380,7 @@ PyArray_Nonzero(PyArrayObject *self) Py_DECREF(ret); return NULL; } - + needs_api = NpyIter_IterationNeedsAPI(iter); NPY_BEGIN_THREADS_NDITER(iter); @@ -2436,7 +2424,7 @@ finish: Py_DECREF(ret); return NULL; } - + /* if executed `nonzero()` check for miscount due to side-effect */ if (!is_bool && added_count != nonzero_count) { PyErr_SetString(PyExc_RuntimeError, diff --git a/numpy/core/src/multiarray/item_selection.h b/numpy/core/src/multiarray/item_selection.h index 2276b4db7..c1c8b5567 100644 --- a/numpy/core/src/multiarray/item_selection.h +++ b/numpy/core/src/multiarray/item_selection.h @@ -8,7 +8,7 @@ * Returns -1 on error. */ NPY_NO_EXPORT npy_intp -count_boolean_trues(int ndim, char *data, npy_intp *ashape, npy_intp *astrides); +count_boolean_trues(int ndim, char *data, npy_intp const *ashape, npy_intp const *astrides); /* * Gets a single item from the array, based on a single multi-index diff --git a/numpy/core/src/multiarray/iterators.c b/numpy/core/src/multiarray/iterators.c index e66bb36aa..14e70fe02 100644 --- a/numpy/core/src/multiarray/iterators.c +++ b/numpy/core/src/multiarray/iterators.c @@ -172,7 +172,7 @@ NPY_NO_EXPORT PyObject * PyArray_IterNew(PyObject *obj) { /* - * Note that internall PyArray_RawIterBaseInit may be called directly on a + * Note that internally PyArray_RawIterBaseInit may be called directly on a * statically allocated PyArrayIterObject. */ PyArrayIterObject *it; @@ -1102,12 +1102,7 @@ static PyGetSetDef iter_getsets[] = { }; NPY_NO_EXPORT PyTypeObject PyArrayIter_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.flatiter", /* tp_name */ sizeof(PyArrayIterObject), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -1116,11 +1111,7 @@ NPY_NO_EXPORT PyTypeObject PyArrayIter_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -1560,12 +1551,7 @@ static PyMethodDef arraymultiter_methods[] = { }; NPY_NO_EXPORT PyTypeObject PyArrayMultiIter_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.broadcast", /* tp_name */ sizeof(PyArrayMultiIterObject), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -1574,11 +1560,7 @@ NPY_NO_EXPORT PyTypeObject PyArrayMultiIter_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -1890,12 +1872,7 @@ static void neighiter_dealloc(PyArrayNeighborhoodIterObject* iter) } NPY_NO_EXPORT PyTypeObject PyArrayNeighborhoodIter_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.neigh_internal_iter", /* tp_name*/ sizeof(PyArrayNeighborhoodIterObject), /* tp_basicsize*/ 0, /* tp_itemsize*/ @@ -1903,11 +1880,7 @@ NPY_NO_EXPORT PyTypeObject PyArrayNeighborhoodIter_Type = { 0, /* tp_print*/ 0, /* tp_getattr*/ 0, /* tp_setattr*/ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr*/ 0, /* tp_as_number*/ 0, /* tp_as_sequence*/ diff --git a/numpy/core/src/multiarray/lowlevel_strided_loops.c.src b/numpy/core/src/multiarray/lowlevel_strided_loops.c.src index 63b2a8842..d234c366c 100644 --- a/numpy/core/src/multiarray/lowlevel_strided_loops.c.src +++ b/numpy/core/src/multiarray/lowlevel_strided_loops.c.src @@ -974,9 +974,9 @@ PyArray_GetStridedNumericCastFn(int aligned, npy_intp src_stride, NPY_NO_EXPORT npy_intp PyArray_TransferNDimToStrided(npy_intp ndim, char *dst, npy_intp dst_stride, - char *src, npy_intp *src_strides, npy_intp src_strides_inc, - npy_intp *coords, npy_intp coords_inc, - npy_intp *shape, npy_intp shape_inc, + char *src, npy_intp const *src_strides, npy_intp src_strides_inc, + npy_intp const *coords, npy_intp coords_inc, + npy_intp const *shape, npy_intp shape_inc, npy_intp count, npy_intp src_itemsize, PyArray_StridedUnaryOp *stransfer, NpyAuxData *data) @@ -1092,10 +1092,10 @@ PyArray_TransferNDimToStrided(npy_intp ndim, /* See documentation of arguments in lowlevel_strided_loops.h */ NPY_NO_EXPORT npy_intp PyArray_TransferStridedToNDim(npy_intp ndim, - char *dst, npy_intp *dst_strides, npy_intp dst_strides_inc, + char *dst, npy_intp const *dst_strides, npy_intp dst_strides_inc, char *src, npy_intp src_stride, - npy_intp *coords, npy_intp coords_inc, - npy_intp *shape, npy_intp shape_inc, + npy_intp const *coords, npy_intp coords_inc, + npy_intp const *shape, npy_intp shape_inc, npy_intp count, npy_intp src_itemsize, PyArray_StridedUnaryOp *stransfer, NpyAuxData *data) @@ -1211,11 +1211,11 @@ PyArray_TransferStridedToNDim(npy_intp ndim, /* See documentation of arguments in lowlevel_strided_loops.h */ NPY_NO_EXPORT npy_intp PyArray_TransferMaskedStridedToNDim(npy_intp ndim, - char *dst, npy_intp *dst_strides, npy_intp dst_strides_inc, + char *dst, npy_intp const *dst_strides, npy_intp dst_strides_inc, char *src, npy_intp src_stride, npy_uint8 *mask, npy_intp mask_stride, - npy_intp *coords, npy_intp coords_inc, - npy_intp *shape, npy_intp shape_inc, + npy_intp const *coords, npy_intp coords_inc, + npy_intp const *shape, npy_intp shape_inc, npy_intp count, npy_intp src_itemsize, PyArray_MaskedStridedUnaryOp *stransfer, NpyAuxData *data) diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c index 8dcd28c84..3efb3cb9d 100644 --- a/numpy/core/src/multiarray/mapping.c +++ b/numpy/core/src/multiarray/mapping.c @@ -227,11 +227,7 @@ unpack_indices(PyObject *index, PyObject **result, npy_intp result_n) /* Obvious single-entry cases */ if (0 /* to aid macros below */ -#if !defined(NPY_PY3K) - || PyInt_CheckExact(index) -#else || PyLong_CheckExact(index) -#endif || index == Py_None || PySlice_Check(index) || PyArray_Check(index) @@ -481,11 +477,7 @@ prepare_index(PyArrayObject *self, PyObject *index, * * Check for integers first, purely for performance */ -#if !defined(NPY_PY3K) - if (PyInt_CheckExact(obj) || !PyArray_Check(obj)) { -#else if (PyLong_CheckExact(obj) || !PyArray_Check(obj)) { -#endif npy_intp ind = PyArray_PyIntAsIntp(obj); if (error_converting(ind)) { @@ -3340,12 +3332,7 @@ arraymapiter_dealloc(PyArrayMapIterObject *mit) * to a[indexobj].flat but the latter gets to use slice syntax. */ NPY_NO_EXPORT PyTypeObject PyArrayMapIter_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.mapiter", /* tp_name */ sizeof(PyArrayMapIterObject), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -3354,11 +3341,7 @@ NPY_NO_EXPORT PyTypeObject PyArrayMapIter_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index e5845f2f6..83c993425 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -1469,7 +1469,7 @@ array_argpartition(PyArrayObject *self, PyObject *args, PyObject *kwds) static PyObject * array_searchsorted(PyArrayObject *self, PyObject *args, PyObject *kwds) { - static char *kwlist[] = {"keys", "side", "sorter", NULL}; + static char *kwlist[] = {"v", "side", "sorter", NULL}; PyObject *keys; PyObject *sorter; NPY_SEARCHSIDE side = NPY_SEARCHLEFT; @@ -1954,7 +1954,6 @@ array_setstate(PyArrayObject *self, PyObject *args) else { Py_INCREF(rawdata); -#if defined(NPY_PY3K) /* Backward compatibility with Python 2 NumPy pickles */ if (PyUnicode_Check(rawdata)) { PyObject *tmp; @@ -1969,7 +1968,6 @@ array_setstate(PyArrayObject *self, PyObject *args) return NULL; } } -#endif if (!PyBytes_Check(rawdata)) { PyErr_SetString(PyExc_TypeError, @@ -2030,14 +2028,9 @@ array_setstate(PyArrayObject *self, PyObject *args) if (!PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) { int swap = PyArray_ISBYTESWAPPED(self); fa->data = datastr; -#ifndef NPY_PY3K - /* Check that the string is not interned */ - if (!IsAligned(self) || swap || PyString_CHECK_INTERNED(rawdata)) { -#else /* Bytes should always be considered immutable, but we just grab the * pointer if they are large, to save memory. */ if (!IsAligned(self) || swap || (len <= 1000)) { -#endif npy_intp num = PyArray_NBYTES(self); if (num == 0) { Py_DECREF(rawdata); @@ -2641,51 +2634,6 @@ array_complex(PyArrayObject *self, PyObject *NPY_UNUSED(args)) return c; } -#ifndef NPY_PY3K - -static PyObject * -array_getslice(PyArrayObject *self, PyObject *args) -{ - PyObject *start, *stop, *slice, *result; - if (!PyArg_ParseTuple(args, "OO:__getslice__", &start, &stop)) { - return NULL; - } - - slice = PySlice_New(start, stop, NULL); - if (slice == NULL) { - return NULL; - } - - /* Deliberately delegate to subclasses */ - result = PyObject_GetItem((PyObject *)self, slice); - Py_DECREF(slice); - return result; -} - -static PyObject * -array_setslice(PyArrayObject *self, PyObject *args) -{ - PyObject *start, *stop, *value, *slice; - if (!PyArg_ParseTuple(args, "OOO:__setslice__", &start, &stop, &value)) { - return NULL; - } - - slice = PySlice_New(start, stop, NULL); - if (slice == NULL) { - return NULL; - } - - /* Deliberately delegate to subclasses */ - if (PyObject_SetItem((PyObject *)self, slice, value) < 0) { - Py_DECREF(slice); - return NULL; - } - Py_DECREF(slice); - Py_RETURN_NONE; -} - -#endif - NPY_NO_EXPORT PyMethodDef array_methods[] = { /* for subtypes */ @@ -2705,12 +2653,6 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { (PyCFunction)array_function, METH_VARARGS | METH_KEYWORDS, NULL}, -#ifndef NPY_PY3K - {"__unicode__", - (PyCFunction)array_unicode, - METH_NOARGS, NULL}, -#endif - /* for the sys module */ {"__sizeof__", (PyCFunction) array_sizeof, @@ -2749,23 +2691,6 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { (PyCFunction) array_format, METH_VARARGS, NULL}, -#ifndef NPY_PY3K - /* - * While we could put these in `tp_sequence`, its' easier to define them - * in terms of PyObject* arguments. - * - * We must provide these for compatibility with code that calls them - * directly. They are already deprecated at a language level in python 2.7, - * but are removed outright in python 3. - */ - {"__getslice__", - (PyCFunction) array_getslice, - METH_VARARGS, NULL}, - {"__setslice__", - (PyCFunction) array_setslice, - METH_VARARGS, NULL}, -#endif - /* Original and Extended methods added 2005 */ {"all", (PyCFunction)array_all, diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 33b654729..b1b9c0051 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -161,7 +161,7 @@ PyArray_MultiplyList(npy_intp const *l1, int n) * Multiply a List of Non-negative numbers with over-flow detection. */ NPY_NO_EXPORT npy_intp -PyArray_OverflowMultiplyList(npy_intp *l1, int n) +PyArray_OverflowMultiplyList(npy_intp const *l1, int n) { npy_intp prod = 1; int i; @@ -1920,7 +1920,6 @@ array_scalar(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kwds) alloc = 1; } else { -#if defined(NPY_PY3K) /* Backward compatibility with Python 2 NumPy pickles */ if (PyUnicode_Check(obj)) { tmpobj = PyUnicode_AsLatin1String(obj); @@ -1934,8 +1933,6 @@ array_scalar(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kwds) return NULL; } } -#endif - if (!PyString_Check(obj)) { PyErr_SetString(PyExc_TypeError, "initializing object must be a string"); @@ -2020,11 +2017,7 @@ array_count_nonzero(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) if (count == -1) { return NULL; } -#if defined(NPY_PY3K) return PyLong_FromSsize_t(count); -#else - return PyInt_FromSsize_t(count); -#endif } static PyObject * @@ -2639,13 +2632,11 @@ array_einsum(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) while (PyDict_Next(kwds, &pos, &key, &value)) { char *str = NULL; -#if defined(NPY_PY3K) Py_XDECREF(str_key_obj); str_key_obj = PyUnicode_AsASCIIString(key); if (str_key_obj != NULL) { key = str_key_obj; } -#endif str = PyBytes_AsString(key); @@ -3293,134 +3284,6 @@ array_datetime_data(PyObject *NPY_UNUSED(dummy), PyObject *args) return convert_datetime_metadata_to_tuple(meta); } -#if !defined(NPY_PY3K) -static PyObject * -new_buffer(PyObject *NPY_UNUSED(dummy), PyObject *args) -{ - int size; - - if (!PyArg_ParseTuple(args, "i:buffer", &size)) { - return NULL; - } - return PyBuffer_New(size); -} - -static PyObject * -buffer_buffer(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) -{ - PyObject *obj; - Py_ssize_t offset = 0, n; - Py_ssize_t size = Py_END_OF_BUFFER; - void *unused; - static char *kwlist[] = {"object", "offset", "size", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, - "O|" NPY_SSIZE_T_PYFMT NPY_SSIZE_T_PYFMT ":get_buffer", kwlist, - &obj, &offset, &size)) { - return NULL; - } - if (PyObject_AsWriteBuffer(obj, &unused, &n) < 0) { - PyErr_Clear(); - return PyBuffer_FromObject(obj, offset, size); - } - else { - return PyBuffer_FromReadWriteObject(obj, offset, size); - } -} -#endif - -#ifndef _MSC_VER -#include <setjmp.h> -#include <signal.h> -jmp_buf _NPY_SIGSEGV_BUF; -static void -_SigSegv_Handler(int signum) -{ - longjmp(_NPY_SIGSEGV_BUF, signum); -} -#endif - -#define _test_code() { \ - test = *((char*)memptr); \ - if (!ro) { \ - *((char *)memptr) = '\0'; \ - *((char *)memptr) = test; \ - } \ - test = *((char*)memptr+size-1); \ - if (!ro) { \ - *((char *)memptr+size-1) = '\0'; \ - *((char *)memptr+size-1) = test; \ - } \ - } - -static PyObject * -as_buffer(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) -{ - PyObject *mem; - Py_ssize_t size; - npy_bool ro = NPY_FALSE, check = NPY_TRUE; - void *memptr; - static char *kwlist[] = {"mem", "size", "readonly", "check", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, - "O" NPY_SSIZE_T_PYFMT "|O&O&:int_asbuffer", kwlist, - &mem, &size, PyArray_BoolConverter, &ro, - PyArray_BoolConverter, &check)) { - return NULL; - } - memptr = PyLong_AsVoidPtr(mem); - if (memptr == NULL) { - return NULL; - } - if (check) { - /* - * Try to dereference the start and end of the memory region - * Catch segfault and report error if it occurs - */ - char test; - int err = 0; - -#ifdef _MSC_VER - __try { - _test_code(); - } - __except(1) { - err = 1; - } -#else - PyOS_sighandler_t _npy_sig_save; - _npy_sig_save = PyOS_setsig(SIGSEGV, _SigSegv_Handler); - if (setjmp(_NPY_SIGSEGV_BUF) == 0) { - _test_code(); - } - else { - err = 1; - } - PyOS_setsig(SIGSEGV, _npy_sig_save); -#endif - if (err) { - PyErr_SetString(PyExc_ValueError, - "cannot use memory location as a buffer."); - return NULL; - } - } - - -#if defined(NPY_PY3K) - PyErr_SetString(PyExc_RuntimeError, - "XXX -- not implemented!"); - return NULL; -#else - if (ro) { - return PyBuffer_FromMemory(memptr, size); - } - return PyBuffer_FromReadWriteMemory(memptr, size); -#endif -} - -#undef _test_code - - /* * Prints floating-point scalars using the Dragon4 algorithm, scientific mode. * See docstring of `np.format_float_scientific` for description of arguments. @@ -4001,11 +3864,6 @@ array_shares_memory_impl(PyObject *args, PyObject *kwds, Py_ssize_t default_max_ goto fail; } } -#if !defined(NPY_PY3K) - else if (PyInt_Check(max_work_obj)) { - max_work = PyInt_AsSsize_t(max_work_obj); - } -#endif else { PyErr_SetString(PyExc_ValueError, "max_work must be an integer"); goto fail; @@ -4229,17 +4087,6 @@ static struct PyMethodDef array_module_methods[] = { {"is_busday", (PyCFunction)array_is_busday, METH_VARARGS | METH_KEYWORDS, NULL}, -#if !defined(NPY_PY3K) - {"newbuffer", - (PyCFunction)new_buffer, - METH_VARARGS, NULL}, - {"getbuffer", - (PyCFunction)buffer_buffer, - METH_VARARGS | METH_KEYWORDS, NULL}, -#endif - {"int_asbuffer", - (PyCFunction)as_buffer, - METH_VARARGS | METH_KEYWORDS, NULL}, {"format_longfloat", (PyCFunction)format_longfloat, METH_VARARGS | METH_KEYWORDS, NULL}, @@ -4319,11 +4166,6 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict)) if (PyType_Ready(&PyBool_Type) < 0) { return -1; } -#if !defined(NPY_PY3K) - if (PyType_Ready(&PyInt_Type) < 0) { - return -1; - } -#endif if (PyType_Ready(&PyFloat_Type) < 0) { return -1; } @@ -4374,27 +4216,6 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict)) return -1; \ } -/* - * In Py3K, int is no longer a fixed-width integer type, so don't - * inherit numpy.int_ from it. - */ -#if defined(NPY_PY3K) -#define INHERIT_INT(child, parent2) \ - SINGLE_INHERIT(child, parent2); -#else -#define INHERIT_INT(child, parent2) \ - Py##child##ArrType_Type.tp_flags |= Py_TPFLAGS_INT_SUBCLASS; \ - DUAL_INHERIT(child, Int, parent2); -#endif - -#if defined(NPY_PY3K) -#define DUAL_INHERIT_COMPARE(child, parent1, parent2) -#else -#define DUAL_INHERIT_COMPARE(child, parent1, parent2) \ - Py##child##ArrType_Type.tp_compare = \ - Py##parent1##_Type.tp_compare; -#endif - #define DUAL_INHERIT2(child, parent1, parent2) \ Py##child##ArrType_Type.tp_base = &Py##parent1##_Type; \ Py##child##ArrType_Type.tp_bases = \ @@ -4402,7 +4223,6 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict)) &Py##parent2##ArrType_Type); \ Py##child##ArrType_Type.tp_richcompare = \ Py##parent1##_Type.tp_richcompare; \ - DUAL_INHERIT_COMPARE(child, parent1, parent2) \ Py##child##ArrType_Type.tp_hash = Py##parent1##_Type.tp_hash; \ if (PyType_Ready(&Py##child##ArrType_Type) < 0) { \ PyErr_Print(); \ @@ -4415,20 +4235,9 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict)) SINGLE_INHERIT(Bool, Generic); SINGLE_INHERIT(Byte, SignedInteger); SINGLE_INHERIT(Short, SignedInteger); - -#if NPY_SIZEOF_INT == NPY_SIZEOF_LONG - INHERIT_INT(Int, SignedInteger); -#else SINGLE_INHERIT(Int, SignedInteger); -#endif - - INHERIT_INT(Long, SignedInteger); - -#if NPY_SIZEOF_LONGLONG == NPY_SIZEOF_LONG - INHERIT_INT(LongLong, SignedInteger); -#else + SINGLE_INHERIT(Long, SignedInteger); SINGLE_INHERIT(LongLong, SignedInteger); -#endif /* Datetime doesn't fit in any category */ SINGLE_INHERIT(Datetime, Generic); @@ -4467,9 +4276,7 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict)) #undef SINGLE_INHERIT #undef DUAL_INHERIT -#undef INHERIT_INT #undef DUAL_INHERIT2 -#undef DUAL_INHERIT_COMPARE /* * Clean up string and unicode array types so they act more like @@ -4551,7 +4358,6 @@ intern_strings(void) npy_ma_str_ndmin && npy_ma_str_axis1 && npy_ma_str_axis2; } -#if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_multiarray_umath", @@ -4563,25 +4369,14 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif /* Initialization function for the module */ -#if defined(NPY_PY3K) -#define RETVAL(x) x PyMODINIT_FUNC PyInit__multiarray_umath(void) { -#else -#define RETVAL(x) -PyMODINIT_FUNC init_multiarray_umath(void) { -#endif PyObject *m, *d, *s; PyObject *c_api; /* Create the module and add the functions */ -#if defined(NPY_PY3K) m = PyModule_Create(&moduledef); -#else - m = Py_InitModule("_multiarray_umath", array_module_methods); -#endif if (!m) { goto err; } @@ -4766,12 +4561,12 @@ PyMODINIT_FUNC init_multiarray_umath(void) { if (initumath(m) != 0) { goto err; } - return RETVAL(m); + return m; err: if (!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "cannot load multiarray module."); } - return RETVAL(NULL); + return NULL; } diff --git a/numpy/core/src/multiarray/nditer_constr.c b/numpy/core/src/multiarray/nditer_constr.c index 5e770338d..e40a2d594 100644 --- a/numpy/core/src/multiarray/nditer_constr.c +++ b/numpy/core/src/multiarray/nditer_constr.c @@ -56,7 +56,7 @@ static int npyiter_fill_axisdata(NpyIter *iter, npy_uint32 flags, npyiter_opitflags *op_itflags, char **op_dataptr, const npy_uint32 *op_flags, int **op_axes, - npy_intp *itershape); + npy_intp const *itershape); static void npyiter_replace_axisdata(NpyIter *iter, int iop, PyArrayObject *op, @@ -80,7 +80,7 @@ npyiter_get_common_dtype(int nop, PyArrayObject **op, static PyArrayObject * npyiter_new_temp_array(NpyIter *iter, PyTypeObject *subtype, npy_uint32 flags, npyiter_opitflags *op_itflags, - int op_ndim, npy_intp *shape, + int op_ndim, npy_intp const *shape, PyArray_Descr *op_dtype, const int *op_axes); static int npyiter_allocate_arrays(NpyIter *iter, @@ -1424,7 +1424,7 @@ static int npyiter_fill_axisdata(NpyIter *iter, npy_uint32 flags, npyiter_opitflags *op_itflags, char **op_dataptr, const npy_uint32 *op_flags, int **op_axes, - npy_intp *itershape) + npy_intp const *itershape) { npy_uint32 itflags = NIT_ITFLAGS(iter); int idim, ndim = NIT_NDIM(iter); @@ -2476,7 +2476,7 @@ npyiter_get_common_dtype(int nop, PyArrayObject **op, static PyArrayObject * npyiter_new_temp_array(NpyIter *iter, PyTypeObject *subtype, npy_uint32 flags, npyiter_opitflags *op_itflags, - int op_ndim, npy_intp *shape, + int op_ndim, npy_intp const *shape, PyArray_Descr *op_dtype, const int *op_axes) { npy_uint32 itflags = NIT_ITFLAGS(iter); diff --git a/numpy/core/src/multiarray/nditer_pywrap.c b/numpy/core/src/multiarray/nditer_pywrap.c index 246f9d382..b4b3c6704 100644 --- a/numpy/core/src/multiarray/nditer_pywrap.c +++ b/numpy/core/src/multiarray/nditer_pywrap.c @@ -2490,12 +2490,7 @@ NPY_NO_EXPORT PyMappingMethods npyiter_as_mapping = { }; NPY_NO_EXPORT PyTypeObject NpyIter_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.nditer", /* tp_name */ sizeof(NewNpyArrayIterObject), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -2504,11 +2499,7 @@ NPY_NO_EXPORT PyTypeObject NpyIter_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ &npyiter_as_sequence, /* tp_as_sequence */ diff --git a/numpy/core/src/multiarray/npy_buffer.h b/numpy/core/src/multiarray/npy_buffer.h index fae413c85..2eb97c4b9 100644 --- a/numpy/core/src/multiarray/npy_buffer.h +++ b/numpy/core/src/multiarray/npy_buffer.h @@ -7,7 +7,7 @@ NPY_NO_EXPORT void _dealloc_cached_buffer_info(PyObject *self); NPY_NO_EXPORT PyArray_Descr* -_descriptor_from_pep3118_format(char *s); +_descriptor_from_pep3118_format(char const *s); NPY_NO_EXPORT int gentype_getbuffer(PyObject *obj, Py_buffer *view, int flags); diff --git a/numpy/core/src/multiarray/number.c b/numpy/core/src/multiarray/number.c index dabc866ff..8cdc502d6 100644 --- a/numpy/core/src/multiarray/number.c +++ b/numpy/core/src/multiarray/number.c @@ -32,10 +32,6 @@ static PyObject * array_inplace_subtract(PyArrayObject *m1, PyObject *m2); static PyObject * array_inplace_multiply(PyArrayObject *m1, PyObject *m2); -#if !defined(NPY_PY3K) -static PyObject * -array_inplace_divide(PyArrayObject *m1, PyObject *m2); -#endif static PyObject * array_inplace_true_divide(PyArrayObject *m1, PyObject *m2); static PyObject * @@ -353,20 +349,6 @@ array_multiply(PyArrayObject *m1, PyObject *m2) return PyArray_GenericBinaryFunction(m1, m2, n_ops.multiply); } -#if !defined(NPY_PY3K) -static PyObject * -array_divide(PyArrayObject *m1, PyObject *m2) -{ - PyObject *res; - - BINOP_GIVE_UP_IF_NEEDED(m1, m2, nb_divide, array_divide); - if (try_binary_elide(m1, m2, &array_inplace_divide, &res, 0)) { - return res; - } - return PyArray_GenericBinaryFunction(m1, m2, n_ops.divide); -} -#endif - static PyObject * array_remainder(PyArrayObject *m1, PyObject *m2) { @@ -381,7 +363,6 @@ array_divmod(PyArrayObject *m1, PyObject *m2) return PyArray_GenericBinaryFunction(m1, m2, n_ops.divmod); } -#if PY_VERSION_HEX >= 0x03050000 /* Need this to be version dependent on account of the slot check */ static PyObject * array_matrix_multiply(PyArrayObject *m1, PyObject *m2) @@ -399,7 +380,6 @@ array_inplace_matrix_multiply( "Use 'a = a @ b' instead of 'a @= b'."); return NULL; } -#endif /* * Determine if object is a scalar and if so, convert the object @@ -728,16 +708,6 @@ array_inplace_multiply(PyArrayObject *m1, PyObject *m2) return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.multiply); } -#if !defined(NPY_PY3K) -static PyObject * -array_inplace_divide(PyArrayObject *m1, PyObject *m2) -{ - INPLACE_GIVE_UP_IF_NEEDED( - m1, m2, nb_inplace_divide, array_inplace_divide); - return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.divide); -} -#endif - static PyObject * array_inplace_remainder(PyArrayObject *m1, PyObject *m2) { @@ -931,67 +901,12 @@ array_float(PyArrayObject *v) return array_scalar_forward(v, &PyNumber_Float, " in ndarray.__float__"); } -#if defined(NPY_PY3K) - NPY_NO_EXPORT PyObject * array_int(PyArrayObject *v) { return array_scalar_forward(v, &PyNumber_Long, " in ndarray.__int__"); } -#else - -NPY_NO_EXPORT PyObject * -array_int(PyArrayObject *v) -{ - return array_scalar_forward(v, &PyNumber_Int, " in ndarray.__int__"); -} - -NPY_NO_EXPORT PyObject * -array_long(PyArrayObject *v) -{ - return array_scalar_forward(v, &PyNumber_Long, " in ndarray.__long__"); -} - -/* hex and oct aren't exposed to the C api, but we need a function pointer */ -static PyObject * -_PyNumber_Oct(PyObject *o) { - PyObject *res; - PyObject *mod = PyImport_ImportModule("__builtin__"); - if (mod == NULL) { - return NULL; - } - res = PyObject_CallMethod(mod, "oct", "(O)", o); - Py_DECREF(mod); - return res; -} - -static PyObject * -_PyNumber_Hex(PyObject *o) { - PyObject *res; - PyObject *mod = PyImport_ImportModule("__builtin__"); - if (mod == NULL) { - return NULL; - } - res = PyObject_CallMethod(mod, "hex", "(O)", o); - Py_DECREF(mod); - return res; -} - -NPY_NO_EXPORT PyObject * -array_oct(PyArrayObject *v) -{ - return array_scalar_forward(v, &_PyNumber_Oct, " in ndarray.__oct__"); -} - -NPY_NO_EXPORT PyObject * -array_hex(PyArrayObject *v) -{ - return array_scalar_forward(v, &_PyNumber_Hex, " in ndarray.__hex__"); -} - -#endif - static PyObject * array_index(PyArrayObject *v) { @@ -1008,9 +923,6 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = { (binaryfunc)array_add, /*nb_add*/ (binaryfunc)array_subtract, /*nb_subtract*/ (binaryfunc)array_multiply, /*nb_multiply*/ -#if !defined(NPY_PY3K) - (binaryfunc)array_divide, /*nb_divide*/ -#endif (binaryfunc)array_remainder, /*nb_remainder*/ (binaryfunc)array_divmod, /*nb_divmod*/ (ternaryfunc)array_power, /*nb_power*/ @@ -1024,20 +936,9 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = { (binaryfunc)array_bitwise_and, /*nb_and*/ (binaryfunc)array_bitwise_xor, /*nb_xor*/ (binaryfunc)array_bitwise_or, /*nb_or*/ -#if !defined(NPY_PY3K) - 0, /*nb_coerce*/ -#endif (unaryfunc)array_int, /*nb_int*/ -#if defined(NPY_PY3K) 0, /*nb_reserved*/ -#else - (unaryfunc)array_long, /*nb_long*/ -#endif (unaryfunc)array_float, /*nb_float*/ -#if !defined(NPY_PY3K) - (unaryfunc)array_oct, /*nb_oct*/ - (unaryfunc)array_hex, /*nb_hex*/ -#endif /* * This code adds augmented assignment functionality @@ -1046,9 +947,6 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = { (binaryfunc)array_inplace_add, /*nb_inplace_add*/ (binaryfunc)array_inplace_subtract, /*nb_inplace_subtract*/ (binaryfunc)array_inplace_multiply, /*nb_inplace_multiply*/ -#if !defined(NPY_PY3K) - (binaryfunc)array_inplace_divide, /*nb_inplace_divide*/ -#endif (binaryfunc)array_inplace_remainder, /*nb_inplace_remainder*/ (ternaryfunc)array_inplace_power, /*nb_inplace_power*/ (binaryfunc)array_inplace_left_shift, /*nb_inplace_lshift*/ @@ -1062,8 +960,7 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = { (binaryfunc)array_inplace_floor_divide, /*nb_inplace_floor_divide*/ (binaryfunc)array_inplace_true_divide, /*nb_inplace_true_divide*/ (unaryfunc)array_index, /*nb_index */ -#if PY_VERSION_HEX >= 0x03050000 + (binaryfunc)array_matrix_multiply, /*nb_matrix_multiply*/ (binaryfunc)array_inplace_matrix_multiply, /*nb_inplace_matrix_multiply*/ -#endif }; diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c index b669a3e76..3a66df454 100644 --- a/numpy/core/src/multiarray/scalarapi.c +++ b/numpy/core/src/multiarray/scalarapi.c @@ -425,14 +425,10 @@ PyArray_ScalarFromObject(PyObject *object) NPY_NO_EXPORT PyArray_Descr * PyArray_DescrFromTypeObject(PyObject *type) { - int typenum; - PyArray_Descr *new, *conv = NULL; - /* if it's a builtin type, then use the typenumber */ - typenum = _typenum_fromtypeobj(type,1); + int typenum = _typenum_fromtypeobj(type,1); if (typenum != NPY_NOTYPE) { - new = PyArray_DescrFromType(typenum); - return new; + return PyArray_DescrFromType(typenum); } /* Check the generic types */ @@ -470,11 +466,12 @@ PyArray_DescrFromTypeObject(PyObject *type) /* Do special thing for VOID sub-types */ if (PyType_IsSubtype((PyTypeObject *)type, &PyVoidArrType_Type)) { - new = PyArray_DescrNewFromType(NPY_VOID); + PyArray_Descr *new = PyArray_DescrNewFromType(NPY_VOID); if (new == NULL) { return NULL; } - if (_arraydescr_from_dtype_attr(type, &conv)) { + PyArray_Descr *conv = _arraydescr_try_convert_from_dtype_attr(type); + if ((PyObject *)conv != Py_NotImplemented) { if (conv == NULL) { Py_DECREF(new); return NULL; @@ -486,8 +483,8 @@ PyArray_DescrFromTypeObject(PyObject *type) new->elsize = conv->elsize; new->subarray = conv->subarray; conv->subarray = NULL; - Py_DECREF(conv); } + Py_DECREF(conv); Py_XDECREF(new->typeobj); new->typeobj = (PyTypeObject *)type; Py_INCREF(type); @@ -656,7 +653,6 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base) itemsize = (((itemsize - 1) >> 2) + 1) << 2; } } -#if PY_VERSION_HEX >= 0x03030000 if (type_num == NPY_UNICODE) { PyObject *u, *args; int byteorder; @@ -684,7 +680,6 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base) Py_DECREF(args); return obj; } -#endif if (type->tp_itemsize != 0) { /* String type */ obj = type->tp_alloc(type, itemsize); @@ -710,85 +705,9 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base) if (type_num == NPY_STRING) { destptr = PyString_AS_STRING(obj); ((PyStringObject *)obj)->ob_shash = -1; -#if !defined(NPY_PY3K) - ((PyStringObject *)obj)->ob_sstate = SSTATE_NOT_INTERNED; -#endif memcpy(destptr, data, itemsize); return obj; } -#if PY_VERSION_HEX < 0x03030000 - else if (type_num == NPY_UNICODE) { - /* tp_alloc inherited from Python PyBaseObject_Type */ - PyUnicodeObject *uni = (PyUnicodeObject*)obj; - size_t length = itemsize >> 2; - Py_UNICODE *dst; -#ifndef Py_UNICODE_WIDE - char *buffer; - Py_UNICODE *tmp; - int alloc = 0; - - length *= 2; -#endif - /* Set uni->str so that object can be deallocated on failure */ - uni->str = NULL; - uni->defenc = NULL; - uni->hash = -1; - dst = PyObject_MALLOC(sizeof(Py_UNICODE) * (length + 1)); - if (dst == NULL) { - Py_DECREF(obj); - PyErr_NoMemory(); - return NULL; - } -#ifdef Py_UNICODE_WIDE - memcpy(dst, data, itemsize); - if (swap) { - byte_swap_vector(dst, length, 4); - } - uni->str = dst; - uni->str[length] = 0; - uni->length = length; -#else - /* need aligned data buffer */ - if ((swap) || ((((npy_intp)data) % descr->alignment) != 0)) { - buffer = malloc(itemsize); - if (buffer == NULL) { - PyObject_FREE(dst); - Py_DECREF(obj); - PyErr_NoMemory(); - } - alloc = 1; - memcpy(buffer, data, itemsize); - if (swap) { - byte_swap_vector(buffer, itemsize >> 2, 4); - } - } - else { - buffer = data; - } - - /* - * Allocated enough for 2-characters per itemsize. - * Now convert from the data-buffer - */ - length = PyUCS2Buffer_FromUCS4(dst, - (npy_ucs4 *)buffer, itemsize >> 2); - if (alloc) { - free(buffer); - } - /* Resize the unicode result */ - tmp = PyObject_REALLOC(dst, sizeof(Py_UNICODE)*(length + 1)); - if (tmp == NULL) { - PyObject_FREE(dst); - Py_DECREF(obj); - return NULL; - } - uni->str = tmp; - uni->str[length] = 0; - uni->length = length; -#endif - return obj; - } -#endif /* PY_VERSION_HEX < 0x03030000 */ else { PyVoidScalarObject *vobj = (PyVoidScalarObject *)obj; vobj->base = NULL; diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index 91ee64b5f..cd26d20fa 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -54,12 +54,7 @@ NPY_NO_EXPORT PyTypeObject PyTimeIntegerArrType_Type; * Floating, ComplexFloating, Flexible, Character# */ NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.@name@", /* tp_name*/ sizeof(PyObject), /* tp_basicsize*/ 0, /* tp_itemsize */ @@ -68,11 +63,7 @@ NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -226,20 +217,6 @@ gentype_@name@(PyObject *m1, PyObject *m2) /**end repeat**/ -#if !defined(NPY_PY3K) -/**begin repeat - * - * #name = divide# - */ -static PyObject * -gentype_@name@(PyObject *m1, PyObject *m2) -{ - BINOP_GIVE_UP_IF_NEEDED(m1, m2, nb_@name@, gentype_@name@); - return PyArray_Type.tp_as_number->nb_@name@(m1, m2); -} -/**end repeat**/ -#endif - /* Get a nested slot, or NULL if absent */ #define GET_NESTED_SLOT(type, group, slot) \ ((type)->group == NULL ? NULL : (type)->group->slot) @@ -293,27 +270,6 @@ gentype_@name@(PyObject *m1) } /**end repeat**/ -#if !defined(NPY_PY3K) -/**begin repeat - * - * #name = long, oct, hex# - */ -static PyObject * -gentype_@name@(PyObject *m1) -{ - PyObject *arr, *ret; - - arr = PyArray_FromScalar(m1, NULL); - if (arr == NULL) { - return NULL; - } - ret = Py_TYPE(arr)->tp_as_number->nb_@name@(arr); - Py_DECREF(arr); - return ret; -} -/**end repeat**/ -#endif - static int gentype_nonzero_number(PyObject *m1) { @@ -324,11 +280,7 @@ gentype_nonzero_number(PyObject *m1) if (arr == NULL) { return -1; } -#if defined(NPY_PY3K) ret = Py_TYPE(arr)->tp_as_number->nb_bool(arr); -#else - ret = Py_TYPE(arr)->tp_as_number->nb_nonzero(arr); -#endif Py_DECREF(arr); return ret; } @@ -356,21 +308,9 @@ gentype_format(PyObject *self, PyObject *args) PyObject *format_spec; PyObject *obj, *ret; -#if defined(NPY_PY3K) if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) { return NULL; } -#else - if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) { - return NULL; - } - - if (!PyUnicode_Check(format_spec) && !PyString_Check(format_spec)) { - PyErr_SetString(PyExc_TypeError, - "format must be a string"); - return NULL; - } -#endif /* * Convert to an appropriate Python type and call its format. @@ -381,11 +321,7 @@ gentype_format(PyObject *self, PyObject *args) obj = PyBool_FromLong(((PyBoolScalarObject *)self)->obval); } else if (PyArray_IsScalar(self, Integer)) { -#if defined(NPY_PY3K) obj = Py_TYPE(self)->tp_as_number->nb_int(self); -#else - obj = Py_TYPE(self)->tp_as_number->nb_long(self); -#endif } else if (PyArray_IsScalar(self, Floating)) { obj = Py_TYPE(self)->tp_as_number->nb_float(self); @@ -1077,36 +1013,12 @@ static PyObject * return npy_longdouble_to_PyLong(val); } -#if !defined(NPY_PY3K) - -/**begin repeat1 - * #name = int, hex, oct# - */ -static PyObject * -@char@longdoubletype_@name@(PyObject *self) -{ - PyObject *ret; - PyObject *obj = @char@longdoubletype_long(self); - if (obj == NULL) { - return NULL; - } - ret = Py_TYPE(obj)->tp_as_number->nb_@name@(obj); - Py_DECREF(obj); - return ret; -} -/**end repeat1**/ - -#endif /* !defined(NPY_PY3K) */ - /**end repeat**/ static PyNumberMethods gentype_as_number = { (binaryfunc)gentype_add, /*nb_add*/ (binaryfunc)gentype_subtract, /*nb_subtract*/ (binaryfunc)gentype_multiply, /*nb_multiply*/ -#if !defined(NPY_PY3K) - (binaryfunc)gentype_divide, /*nb_divide*/ -#endif (binaryfunc)gentype_remainder, /*nb_remainder*/ (binaryfunc)gentype_divmod, /*nb_divmod*/ (ternaryfunc)gentype_power, /*nb_power*/ @@ -1120,26 +1032,12 @@ static PyNumberMethods gentype_as_number = { (binaryfunc)gentype_and, /*nb_and*/ (binaryfunc)gentype_xor, /*nb_xor*/ (binaryfunc)gentype_or, /*nb_or*/ -#if !defined(NPY_PY3K) - 0, /*nb_coerce*/ -#endif (unaryfunc)gentype_int, /*nb_int*/ -#if defined(NPY_PY3K) 0, /*nb_reserved*/ -#else - (unaryfunc)gentype_long, /*nb_long*/ -#endif (unaryfunc)gentype_float, /*nb_float*/ -#if !defined(NPY_PY3K) - (unaryfunc)gentype_oct, /*nb_oct*/ - (unaryfunc)gentype_hex, /*nb_hex*/ -#endif 0, /*inplace_add*/ 0, /*inplace_subtract*/ 0, /*inplace_multiply*/ -#if !defined(NPY_PY3K) - 0, /*inplace_divide*/ -#endif 0, /*inplace_remainder*/ 0, /*inplace_power*/ 0, /*inplace_lshift*/ @@ -1152,10 +1050,8 @@ static PyNumberMethods gentype_as_number = { 0, /*nb_inplace_floor_divide*/ 0, /*nb_inplace_true_divide*/ (unaryfunc)NULL, /*nb_index*/ -#if PY_VERSION_HEX >= 0x03050000 0, /*np_matmul*/ 0, /*np_inplace_matmul*/ -#endif }; @@ -1246,11 +1142,7 @@ inttype_denominator_get(PyObject *self) static PyObject * gentype_data_get(PyObject *self) { -#if defined(NPY_PY3K) return PyMemoryView_FromObject(self); -#else - return PyBuffer_FromObject(self, 0, Py_END_OF_BUFFER); -#endif } @@ -1293,7 +1185,6 @@ gentype_sizeof(PyObject *self) return PyLong_FromSsize_t(nbytes); } -#if PY_VERSION_HEX >= 0x03000000 NPY_NO_EXPORT void gentype_struct_free(PyObject *ptr) { @@ -1307,17 +1198,6 @@ gentype_struct_free(PyObject *ptr) PyArray_free(arrif->shape); PyArray_free(arrif); } -#else -NPY_NO_EXPORT void -gentype_struct_free(void *ptr, void *arg) -{ - PyArrayInterface *arrif = (PyArrayInterface *)ptr; - Py_DECREF((PyObject *)arg); - Py_XDECREF(arrif->descr); - PyArray_free(arrif->shape); - PyArray_free(arrif); -} -#endif static PyObject * gentype_struct_get(PyObject *self) @@ -1671,6 +1551,11 @@ gentype_itemset(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) return NULL; } +/* This function matches the Python 2.7 PyBufferProcs.bf_getreadbuffer + * interface, but no longer needs to. In the future we could consider + * rewriting callers to use `gentype_getbuffer`, or inline the function body + * at the caller. + */ static Py_ssize_t gentype_getreadbuf(PyObject *, Py_ssize_t, void **); @@ -1831,9 +1716,7 @@ static PyObject * gentype_reduce(PyObject *self, PyObject *NPY_UNUSED(args)) { PyObject *ret = NULL, *obj = NULL, *mod = NULL; -#if defined(NPY_PY3K) Py_buffer view; -#endif const char *buffer; Py_ssize_t buflen; @@ -1843,7 +1726,6 @@ gentype_reduce(PyObject *self, PyObject *NPY_UNUSED(args)) return NULL; } -#if defined(NPY_PY3K) if (PyArray_IsScalar(self, Unicode)) { /* Unicode on Python 3 does not expose the buffer interface */ buffer = PyUnicode_AS_DATA(self); @@ -1865,12 +1747,6 @@ gentype_reduce(PyObject *self, PyObject *NPY_UNUSED(args)) Py_DECREF(ret); return NULL; } -#else - if (PyObject_AsReadBuffer(self, (const void **)&buffer, &buflen)<0) { - Py_DECREF(ret); - return NULL; - } -#endif mod = PyImport_ImportModule("numpy.core._multiarray_umath"); if (mod == NULL) { @@ -2289,12 +2165,10 @@ static PyMethodDef gentype_methods[] = { {"round", (PyCFunction)gentype_round, METH_VARARGS | METH_KEYWORDS, NULL}, -#if defined(NPY_PY3K) /* Hook for the round() builtin */ {"__round__", (PyCFunction)gentype_round, METH_VARARGS | METH_KEYWORDS, NULL}, -#endif /* For the format function */ {"__format__", gentype_format, @@ -2600,68 +2474,17 @@ gentype_getreadbuf(PyObject *self, Py_ssize_t segment, void **ptrptr) return numbytes; } -#if !defined(NPY_PY3K) -static Py_ssize_t -gentype_getsegcount(PyObject *self, Py_ssize_t *lenp) -{ - PyArray_Descr *outcode; - - outcode = PyArray_DescrFromScalar(self); - if (lenp) { - *lenp = outcode->elsize; -#ifndef Py_UNICODE_WIDE - if (outcode->type_num == NPY_UNICODE) { - *lenp >>= 1; - } -#endif - } - Py_DECREF(outcode); - return 1; -} - -static Py_ssize_t -gentype_getcharbuf(PyObject *self, Py_ssize_t segment, constchar **ptrptr) -{ - if (PyArray_IsScalar(self, String) || - PyArray_IsScalar(self, Unicode)) { - return gentype_getreadbuf(self, segment, (void **)ptrptr); - } - else { - PyErr_SetString(PyExc_TypeError, - "Non-character array cannot be interpreted "\ - "as character buffer."); - return -1; - } -} -#endif /* !defined(NPY_PY3K) */ - static PyBufferProcs gentype_as_buffer = { -#if !defined(NPY_PY3K) - gentype_getreadbuf, /* bf_getreadbuffer*/ - NULL, /* bf_getwritebuffer*/ - gentype_getsegcount, /* bf_getsegcount*/ - gentype_getcharbuf, /* bf_getcharbuffer*/ -#endif gentype_getbuffer, /* bf_getbuffer */ NULL, /* bf_releasebuffer */ }; -#if defined(NPY_PY3K) #define BASEFLAGS Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE #define LEAFFLAGS Py_TPFLAGS_DEFAULT -#else -#define BASEFLAGS Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES -#define LEAFFLAGS Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES -#endif NPY_NO_EXPORT PyTypeObject PyGenericArrType_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.generic", /* tp_name*/ sizeof(PyObject), /* tp_basicsize*/ 0, /* tp_itemsize */ @@ -2670,11 +2493,7 @@ NPY_NO_EXPORT PyTypeObject PyGenericArrType_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -2889,11 +2708,7 @@ finish: *((npy_@name@ *)dest) = *((npy_@name@ *)src); #elif @default@ == 1 /* unicode and strings */ if (itemsize == 0) { /* unicode */ -#if PY_VERSION_HEX >= 0x03030000 itemsize = PyUnicode_GetLength(robj) * PyUnicode_KIND(robj); -#else - itemsize = ((PyUnicodeObject *)robj)->length * sizeof(Py_UNICODE); -#endif } memcpy(dest, src, itemsize); /* @default@ == 2 won't get here */ @@ -3082,10 +2897,6 @@ NPY_NO_EXPORT PyNumberMethods bool_arrtype_as_number = { 0, /* nb_add */ 0, /* nb_subtract */ 0, /* nb_multiply */ -#if defined(NPY_PY3K) -#else - 0, /* nb_divide */ -#endif 0, /* nb_remainder */ 0, /* nb_divmod */ 0, /* nb_power */ @@ -3099,30 +2910,13 @@ NPY_NO_EXPORT PyNumberMethods bool_arrtype_as_number = { (binaryfunc)bool_arrtype_and, /* nb_and */ (binaryfunc)bool_arrtype_xor, /* nb_xor */ (binaryfunc)bool_arrtype_or, /* nb_or */ -#if defined(NPY_PY3K) -#else - 0, /* nb_coerce */ -#endif 0, /* nb_int */ -#if defined(NPY_PY3K) 0, /* nb_reserved */ -#else - 0, /* nb_long */ -#endif 0, /* nb_float */ -#if defined(NPY_PY3K) -#else - 0, /* nb_oct */ - 0, /* nb_hex */ -#endif /* Added in release 2.0 */ 0, /* nb_inplace_add */ 0, /* nb_inplace_subtract */ 0, /* nb_inplace_multiply */ -#if defined(NPY_PY3K) -#else - 0, /* nb_inplace_divide */ -#endif 0, /* nb_inplace_remainder */ 0, /* nb_inplace_power */ 0, /* nb_inplace_lshift */ @@ -3158,11 +2952,7 @@ void_arrtype_new(PyTypeObject *type, PyObject *args, PyObject *NPY_UNUSED(kwds)) (PyArray_Check(obj) && PyArray_NDIM((PyArrayObject *)obj)==0 && PyArray_ISINTEGER((PyArrayObject *)obj))) { -#if defined(NPY_PY3K) new = Py_TYPE(obj)->tp_as_number->nb_int(obj); -#else - new = Py_TYPE(obj)->tp_as_number->nb_long(obj); -#endif } if (new && PyLong_Check(new)) { PyObject *ret; @@ -3238,7 +3028,6 @@ ulong_arrtype_hash(PyObject *obj) return x; } -#if (NPY_SIZEOF_INT != NPY_SIZEOF_LONG) || defined(NPY_PY3K) static npy_hash_t int_arrtype_hash(PyObject *obj) { @@ -3248,9 +3037,7 @@ int_arrtype_hash(PyObject *obj) } return x; } -#endif -#if defined(NPY_PY3K) static npy_hash_t long_arrtype_hash(PyObject *obj) { @@ -3259,7 +3046,6 @@ long_arrtype_hash(PyObject *obj) Py_DECREF(l); return x; } -#endif /**begin repeat * #char = ,u# @@ -3499,72 +3285,6 @@ static PyMappingMethods object_arrtype_as_mapping = { (objobjargproc)object_arrtype_ass_subscript, }; -#if !defined(NPY_PY3K) -static Py_ssize_t -object_arrtype_getsegcount(PyObjectScalarObject *self, Py_ssize_t *lenp) -{ - Py_ssize_t newlen; - int cnt; - PyBufferProcs *pb = Py_TYPE(self->obval)->tp_as_buffer; - - if (pb == NULL || - pb->bf_getsegcount == NULL || - (cnt = (*pb->bf_getsegcount)(self->obval, &newlen)) != 1) { - return 0; - } - if (lenp) { - *lenp = newlen; - } - return cnt; -} - -static Py_ssize_t -object_arrtype_getreadbuf(PyObjectScalarObject *self, Py_ssize_t segment, void **ptrptr) -{ - PyBufferProcs *pb = Py_TYPE(self->obval)->tp_as_buffer; - - if (pb == NULL || - pb->bf_getreadbuffer == NULL || - pb->bf_getsegcount == NULL) { - PyErr_SetString(PyExc_TypeError, - "expected a readable buffer object"); - return -1; - } - return (*pb->bf_getreadbuffer)(self->obval, segment, ptrptr); -} - -static Py_ssize_t -object_arrtype_getwritebuf(PyObjectScalarObject *self, Py_ssize_t segment, void **ptrptr) -{ - PyBufferProcs *pb = Py_TYPE(self->obval)->tp_as_buffer; - - if (pb == NULL || - pb->bf_getwritebuffer == NULL || - pb->bf_getsegcount == NULL) { - PyErr_SetString(PyExc_TypeError, - "expected a writeable buffer object"); - return -1; - } - return (*pb->bf_getwritebuffer)(self->obval, segment, ptrptr); -} - -static Py_ssize_t -object_arrtype_getcharbuf(PyObjectScalarObject *self, Py_ssize_t segment, - constchar **ptrptr) -{ - PyBufferProcs *pb = Py_TYPE(self->obval)->tp_as_buffer; - - if (pb == NULL || - pb->bf_getcharbuffer == NULL || - pb->bf_getsegcount == NULL) { - PyErr_SetString(PyExc_TypeError, - "expected a character buffer object"); - return -1; - } - return (*pb->bf_getcharbuffer)(self->obval, segment, ptrptr); -} -#endif - static int object_arrtype_getbuffer(PyObjectScalarObject *self, Py_buffer *view, int flags) { @@ -3592,12 +3312,6 @@ object_arrtype_releasebuffer(PyObjectScalarObject *self, Py_buffer *view) } static PyBufferProcs object_arrtype_as_buffer = { -#if !defined(NPY_PY3K) - (readbufferproc)object_arrtype_getreadbuf, - (writebufferproc)object_arrtype_getwritebuf, - (segcountproc)object_arrtype_getsegcount, - (charbufferproc)object_arrtype_getcharbuf, -#endif (getbufferproc)object_arrtype_getbuffer, (releasebufferproc)object_arrtype_releasebuffer, }; @@ -3609,12 +3323,7 @@ object_arrtype_call(PyObjectScalarObject *obj, PyObject *args, PyObject *kwds) } NPY_NO_EXPORT PyTypeObject PyObjectArrType_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.object_", /* tp_name*/ sizeof(PyObjectScalarObject), /* tp_basicsize*/ 0, /* tp_itemsize */ @@ -3622,11 +3331,7 @@ NPY_NO_EXPORT PyTypeObject PyObjectArrType_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ &object_arrtype_as_sequence, /* tp_as_sequence */ @@ -3693,13 +3398,8 @@ gen_arrtype_subscript(PyObject *self, PyObject *key) #define NAME_bool "bool" #define NAME_void "void" -#if defined(NPY_PY3K) #define NAME_string "bytes" #define NAME_unicode "str" -#else -#define NAME_string "string" -#define NAME_unicode "unicode" -#endif /**begin repeat * #name = bool, string, unicode, void# @@ -3707,12 +3407,7 @@ gen_arrtype_subscript(PyObject *self, PyObject *key) * #ex = _,_,_,# */ NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy." NAME_@name@ "@ex@", /* tp_name*/ sizeof(Py@NAME@ScalarObject), /* tp_basicsize*/ 0, /* tp_itemsize */ @@ -3720,11 +3415,7 @@ NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -3796,12 +3487,7 @@ NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { #define _THIS_SIZE "256" #endif NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.@name@" _THIS_SIZE, /* tp_name*/ sizeof(Py@NAME@ScalarObject), /* tp_basicsize*/ 0, /* tp_itemsize */ @@ -3809,11 +3495,7 @@ NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -3887,12 +3569,7 @@ static PyMappingMethods gentype_as_mapping = { #endif NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(0, 0) -#else - PyObject_HEAD_INIT(0) - 0, /* ob_size */ -#endif "numpy.@name@" _THIS_SIZE, /* tp_name*/ sizeof(Py@NAME@ScalarObject), /* tp_basicsize*/ 0, /* tp_itemsize*/ @@ -3900,11 +3577,7 @@ NPY_NO_EXPORT PyTypeObject Py@NAME@ArrType_Type = { 0, /* tp_print*/ 0, /* tp_getattr*/ 0, /* tp_setattr*/ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif 0, /* tp_repr*/ 0, /* tp_as_number*/ 0, /* tp_as_sequence*/ @@ -4275,38 +3948,6 @@ initialize_casting_tables(void) } } -#ifndef NPY_PY3K -/* - * In python2, the `float` and `complex` types still implement the obsolete - * "tp_print" method, which uses CPython's float-printing routines to print the - * float. Numpy's float_/cfloat inherit from Python float/complex, but - * override its tp_repr and tp_str methods. In order to avoid an inconsistency - * with the inherited tp_print, we need to override it too. - * - * In python3 the tp_print method is reserved/unused. - */ -static int -doubletype_print(PyObject *o, FILE *fp, int flags) -{ - int ret; - PyObject *to_print; - if (flags & Py_PRINT_RAW) { - to_print = PyObject_Str(o); - } - else { - to_print = PyObject_Repr(o); - } - - if (to_print == NULL) { - return -1; - } - - ret = PyObject_Print(to_print, fp, Py_PRINT_RAW); - Py_DECREF(to_print); - return ret; -} -#endif - static PyNumberMethods longdoubletype_as_number; static PyNumberMethods clongdoubletype_as_number; static void init_basetypes(void); @@ -4358,12 +3999,6 @@ initialize_numeric_types(void) /**end repeat**/ -#ifndef NPY_PY3K - PyDoubleArrType_Type.tp_print = &doubletype_print; - PyCDoubleArrType_Type.tp_print = &doubletype_print; -#endif - - PyBoolArrType_Type.tp_as_number->nb_index = (unaryfunc)bool_index; PyStringArrType_Type.tp_alloc = NULL; @@ -4441,20 +4076,14 @@ initialize_numeric_types(void) /**end repeat**/ -#if (NPY_SIZEOF_INT != NPY_SIZEOF_LONG) || defined(NPY_PY3K) /* We won't be inheriting from Python Int type. */ PyIntArrType_Type.tp_hash = int_arrtype_hash; -#endif -#if defined(NPY_PY3K) /* We won't be inheriting from Python Int type. */ PyLongArrType_Type.tp_hash = long_arrtype_hash; -#endif -#if (NPY_SIZEOF_LONG != NPY_SIZEOF_LONGLONG) || defined(NPY_PY3K) /* We won't be inheriting from Python Int type. */ PyLongLongArrType_Type.tp_hash = longlong_arrtype_hash; -#endif /**begin repeat * #name = repr, str# @@ -4497,14 +4126,7 @@ initialize_numeric_types(void) * does not return a normal Python type */ @char@longdoubletype_as_number.nb_float = @char@longdoubletype_float; -#if defined(NPY_PY3K) @char@longdoubletype_as_number.nb_int = @char@longdoubletype_long; -#else - @char@longdoubletype_as_number.nb_int = @char@longdoubletype_int; - @char@longdoubletype_as_number.nb_long = @char@longdoubletype_long; - @char@longdoubletype_as_number.nb_hex = @char@longdoubletype_hex; - @char@longdoubletype_as_number.nb_oct = @char@longdoubletype_oct; -#endif Py@CHAR@LongDoubleArrType_Type.tp_as_number = &@char@longdoubletype_as_number; Py@CHAR@LongDoubleArrType_Type.tp_repr = @char@longdoubletype_repr; diff --git a/numpy/core/src/multiarray/scalartypes.h b/numpy/core/src/multiarray/scalartypes.h index 83b188128..861f2c943 100644 --- a/numpy/core/src/multiarray/scalartypes.h +++ b/numpy/core/src/multiarray/scalartypes.h @@ -19,13 +19,8 @@ initialize_casting_tables(void); NPY_NO_EXPORT void initialize_numeric_types(void); -#if PY_VERSION_HEX >= 0x03000000 NPY_NO_EXPORT void gentype_struct_free(PyObject *ptr); -#else -NPY_NO_EXPORT void -gentype_struct_free(void *ptr, void *arg); -#endif NPY_NO_EXPORT int is_anyscalar_exact(PyObject *obj); diff --git a/numpy/core/src/multiarray/shape.c b/numpy/core/src/multiarray/shape.c index 4e31f003b..127ac5134 100644 --- a/numpy/core/src/multiarray/shape.c +++ b/numpy/core/src/multiarray/shape.c @@ -981,7 +981,7 @@ PyArray_Flatten(PyArrayObject *a, NPY_ORDER order) /* See shape.h for parameters documentation */ NPY_NO_EXPORT PyObject * -build_shape_string(npy_intp n, npy_intp *vals) +build_shape_string(npy_intp n, npy_intp const *vals) { npy_intp i; PyObject *ret, *tmp; diff --git a/numpy/core/src/multiarray/shape.h b/numpy/core/src/multiarray/shape.h index 0451a463e..d25292556 100644 --- a/numpy/core/src/multiarray/shape.h +++ b/numpy/core/src/multiarray/shape.h @@ -6,7 +6,7 @@ * A negative value in 'vals' gets interpreted as newaxis. */ NPY_NO_EXPORT PyObject * -build_shape_string(npy_intp n, npy_intp *vals); +build_shape_string(npy_intp n, npy_intp const *vals); /* * Creates a sorted stride perm matching the KEEPORDER behavior diff --git a/numpy/core/src/multiarray/strfuncs.c b/numpy/core/src/multiarray/strfuncs.c index 495d897b2..b570aec08 100644 --- a/numpy/core/src/multiarray/strfuncs.c +++ b/numpy/core/src/multiarray/strfuncs.c @@ -64,7 +64,7 @@ extend_str(char **strp, Py_ssize_t n, Py_ssize_t *maxp) static int dump_data(char **string, Py_ssize_t *n, Py_ssize_t *max_n, char *data, int nd, - npy_intp *dimensions, npy_intp *strides, PyArrayObject* self) + npy_intp const *dimensions, npy_intp const *strides, PyArrayObject* self) { PyObject *op = NULL, *sp = NULL; char *ostring; @@ -226,34 +226,3 @@ array_format(PyArrayObject *self, PyObject *args) } } -#ifndef NPY_PY3K - -NPY_NO_EXPORT PyObject * -array_unicode(PyArrayObject *self) -{ - PyObject *uni; - - if (PyArray_NDIM(self) == 0) { - PyObject *item = PyArray_ToScalar(PyArray_DATA(self), self); - if (item == NULL){ - return NULL; - } - - /* defer to invoking `unicode` on the scalar */ - uni = PyObject_CallFunctionObjArgs( - (PyObject *)&PyUnicode_Type, item, NULL); - Py_DECREF(item); - } - else { - /* Do what unicode(self) would normally do */ - PyObject *str = PyObject_Str((PyObject *)self); - if (str == NULL){ - return NULL; - } - uni = PyUnicode_FromObject(str); - Py_DECREF(str); - } - return uni; -} - -#endif diff --git a/numpy/core/src/multiarray/strfuncs.h b/numpy/core/src/multiarray/strfuncs.h index 7e869d926..5dd661a20 100644 --- a/numpy/core/src/multiarray/strfuncs.h +++ b/numpy/core/src/multiarray/strfuncs.h @@ -13,9 +13,4 @@ array_str(PyArrayObject *self); NPY_NO_EXPORT PyObject * array_format(PyArrayObject *self, PyObject *args); -#ifndef NPY_PY3K - NPY_NO_EXPORT PyObject * - array_unicode(PyArrayObject *self); -#endif - #endif diff --git a/numpy/core/src/multiarray/typeinfo.c b/numpy/core/src/multiarray/typeinfo.c index 14c4f27cb..30053887b 100644 --- a/numpy/core/src/multiarray/typeinfo.c +++ b/numpy/core/src/multiarray/typeinfo.c @@ -58,11 +58,7 @@ PyArray_typeinfo( PyObject *entry = PyStructSequence_New(&PyArray_typeinfoType); if (entry == NULL) return NULL; -#if defined(NPY_PY3K) PyStructSequence_SET_ITEM(entry, 0, Py_BuildValue("C", typechar)); -#else - PyStructSequence_SET_ITEM(entry, 0, Py_BuildValue("c", typechar)); -#endif PyStructSequence_SET_ITEM(entry, 1, Py_BuildValue("i", typenum)); PyStructSequence_SET_ITEM(entry, 2, Py_BuildValue("i", nbits)); PyStructSequence_SET_ITEM(entry, 3, Py_BuildValue("i", align)); @@ -84,11 +80,7 @@ PyArray_typeinforanged( PyObject *entry = PyStructSequence_New(&PyArray_typeinforangedType); if (entry == NULL) return NULL; -#if defined(NPY_PY3K) PyStructSequence_SET_ITEM(entry, 0, Py_BuildValue("C", typechar)); -#else - PyStructSequence_SET_ITEM(entry, 0, Py_BuildValue("c", typechar)); -#endif PyStructSequence_SET_ITEM(entry, 1, Py_BuildValue("i", typenum)); PyStructSequence_SET_ITEM(entry, 2, Py_BuildValue("i", nbits)); PyStructSequence_SET_ITEM(entry, 3, Py_BuildValue("i", align)); @@ -104,10 +96,8 @@ PyArray_typeinforanged( return entry; } -/* Python version only needed for backport to 2.7 */ -#if (PY_VERSION_HEX < 0x03040000) \ - || (defined(PYPY_VERSION_NUM) && (PYPY_VERSION_NUM < 0x07020000)) - +/* Python version needed for older PyPy */ +#if (defined(PYPY_VERSION_NUM) && (PYPY_VERSION_NUM < 0x07020000)) static int PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) { PyStructSequence_InitType(type, desc); diff --git a/numpy/core/src/umath/_operand_flag_tests.c.src b/numpy/core/src/umath/_operand_flag_tests.c.src index 551a9c632..15fd7be6a 100644 --- a/numpy/core/src/umath/_operand_flag_tests.c.src +++ b/numpy/core/src/umath/_operand_flag_tests.c.src @@ -39,7 +39,6 @@ static char types[2] = {NPY_LONG, NPY_LONG}; static void *data[1] = {NULL}; -#if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_operand_flag_tests", @@ -52,22 +51,12 @@ static struct PyModuleDef moduledef = { NULL }; -#define RETVAL m PyMODINIT_FUNC PyInit__operand_flag_tests(void) { -#else -#define RETVAL -PyMODINIT_FUNC init_operand_flag_tests(void) -{ -#endif PyObject *m = NULL; PyObject *ufunc; -#if defined(NPY_PY3K) m = PyModule_Create(&moduledef); -#else - m = Py_InitModule("_operand_flag_tests", TestMethods); -#endif if (m == NULL) { goto fail; } @@ -87,19 +76,16 @@ PyMODINIT_FUNC init_operand_flag_tests(void) ((PyUFuncObject*)ufunc)->iter_flags = NPY_ITER_REDUCE_OK; PyModule_AddObject(m, "inplace_add", (PyObject*)ufunc); - return RETVAL; + return m; fail: if (!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "cannot load _operand_flag_tests module."); } -#if defined(NPY_PY3K) if (m) { Py_DECREF(m); m = NULL; } -#endif - return RETVAL; - + return m; } diff --git a/numpy/core/src/umath/_rational_tests.c.src b/numpy/core/src/umath/_rational_tests.c.src index 615e395c7..9ab3c651d 100644 --- a/numpy/core/src/umath/_rational_tests.c.src +++ b/numpy/core/src/umath/_rational_tests.c.src @@ -609,9 +609,6 @@ static PyNumberMethods pyrational_as_number = { pyrational_add, /* nb_add */ pyrational_subtract, /* nb_subtract */ pyrational_multiply, /* nb_multiply */ -#if PY_MAJOR_VERSION < 3 - pyrational_divide, /* nb_divide */ -#endif pyrational_remainder, /* nb_remainder */ 0, /* nb_divmod */ 0, /* nb_power */ @@ -625,27 +622,13 @@ static PyNumberMethods pyrational_as_number = { 0, /* nb_and */ 0, /* nb_xor */ 0, /* nb_or */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_coerce */ -#endif pyrational_int, /* nb_int */ -#if PY_MAJOR_VERSION < 3 - pyrational_int, /* nb_long */ -#else 0, /* reserved */ -#endif pyrational_float, /* nb_float */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_oct */ - 0, /* nb_hex */ -#endif 0, /* nb_inplace_add */ 0, /* nb_inplace_subtract */ 0, /* nb_inplace_multiply */ -#if PY_MAJOR_VERSION < 3 - 0, /* nb_inplace_divide */ -#endif 0, /* nb_inplace_remainder */ 0, /* nb_inplace_power */ 0, /* nb_inplace_lshift */ @@ -678,12 +661,7 @@ static PyGetSetDef pyrational_getset[] = { }; static PyTypeObject PyRational_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "rational", /* tp_name */ sizeof(PyRational), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -691,11 +669,7 @@ static PyTypeObject PyRational_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif pyrational_repr, /* tp_repr */ &pyrational_as_number, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -1126,7 +1100,6 @@ PyMethodDef module_methods[] = { {0} /* sentinel */ }; -#if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_rational_tests", @@ -1138,16 +1111,8 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif -#if defined(NPY_PY3K) -#define RETVAL m PyMODINIT_FUNC PyInit__rational_tests(void) { -#else -#define RETVAL -PyMODINIT_FUNC init_rational_tests(void) { -#endif - PyObject *m = NULL; PyObject* numpy_str; PyObject* numpy; @@ -1292,11 +1257,7 @@ PyMODINIT_FUNC init_rational_tests(void) { REGISTER_UFUNC_UNARY(sign) /* Create module */ -#if defined(NPY_PY3K) m = PyModule_Create(&moduledef); -#else - m = Py_InitModule("_rational_tests", module_methods); -#endif if (!m) { goto fail; @@ -1392,18 +1353,16 @@ PyMODINIT_FUNC init_rational_tests(void) { GCD_LCM_UFUNC(gcd,NPY_INT64,"greatest common denominator of two integers"); GCD_LCM_UFUNC(lcm,NPY_INT64,"least common multiple of two integers"); - return RETVAL; + return m; fail: if (!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "cannot load _rational_tests module."); } -#if defined(NPY_PY3K) if (m) { Py_DECREF(m); m = NULL; } -#endif - return RETVAL; + return m; } diff --git a/numpy/core/src/umath/_struct_ufunc_tests.c.src b/numpy/core/src/umath/_struct_ufunc_tests.c.src index 3eaac73e1..1706dc829 100644 --- a/numpy/core/src/umath/_struct_ufunc_tests.c.src +++ b/numpy/core/src/umath/_struct_ufunc_tests.c.src @@ -100,7 +100,6 @@ static PyMethodDef StructUfuncTestMethods[] = { {NULL, NULL, 0, NULL} }; -#if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_struct_ufunc_tests", @@ -112,31 +111,18 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif -#if defined(NPY_PY3K) PyMODINIT_FUNC PyInit__struct_ufunc_tests(void) -#else -PyMODINIT_FUNC init_struct_ufunc_tests(void) -#endif { PyObject *m, *add_triplet, *d; PyObject *dtype_dict; PyArray_Descr *dtype; PyArray_Descr *dtypes[3]; -#if defined(NPY_PY3K) m = PyModule_Create(&moduledef); -#else - m = Py_InitModule("_struct_ufunc_tests", StructUfuncTestMethods); -#endif if (m == NULL) { -#if defined(NPY_PY3K) return NULL; -#else - return; -#endif } import_array(); @@ -166,7 +152,5 @@ PyMODINIT_FUNC init_struct_ufunc_tests(void) PyDict_SetItemString(d, "add_triplet", add_triplet); Py_DECREF(add_triplet); -#if defined(NPY_PY3K) return m; -#endif } diff --git a/numpy/core/src/umath/_umath_tests.c.src b/numpy/core/src/umath/_umath_tests.c.src index 6c3bcce71..bd3fe80b6 100644 --- a/numpy/core/src/umath/_umath_tests.c.src +++ b/numpy/core/src/umath/_umath_tests.c.src @@ -586,7 +586,6 @@ static PyMethodDef UMath_TestsMethods[] = { {NULL, NULL, 0, NULL} /* Sentinel */ }; -#if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_umath_tests", @@ -598,27 +597,16 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif /* Initialization function for the module */ -#if defined(NPY_PY3K) -#define RETVAL(x) x PyMODINIT_FUNC PyInit__umath_tests(void) { -#else -#define RETVAL(x) -PyMODINIT_FUNC init_umath_tests(void) { -#endif PyObject *m; PyObject *d; PyObject *version; -#if defined(NPY_PY3K) m = PyModule_Create(&moduledef); -#else - m = Py_InitModule("_umath_tests", UMath_TestsMethods); -#endif if (m == NULL) { - return RETVAL(NULL); + return NULL; } import_array(); @@ -636,8 +624,8 @@ PyMODINIT_FUNC init_umath_tests(void) { PyErr_Print(); PyErr_SetString(PyExc_RuntimeError, "cannot load _umath_tests module."); - return RETVAL(NULL); + return NULL; } - return RETVAL(m); + return m; } diff --git a/numpy/core/src/umath/funcs.inc.src b/numpy/core/src/umath/funcs.inc.src index 9c59cc8fb..273779ee8 100644 --- a/numpy/core/src/umath/funcs.inc.src +++ b/numpy/core/src/umath/funcs.inc.src @@ -38,11 +38,7 @@ Py_reciprocal(PyObject *o) if (!one) { return NULL; } -#if defined(NPY_PY3K) result = PyNumber_TrueDivide(one, o); -#else - result = PyNumber_Divide(one, o); -#endif Py_DECREF(one); return result; } @@ -197,8 +193,7 @@ npy_ObjectGCD(PyObject *i1, PyObject *i2) { PyObject *gcd = NULL; - /* use math.gcd if available, and valid on the provided types */ -#if PY_VERSION_HEX >= 0x03050000 + /* use math.gcd if valid on the provided types */ { static PyObject *math_gcd_func = NULL; @@ -213,7 +208,6 @@ npy_ObjectGCD(PyObject *i1, PyObject *i2) /* silence errors, and fall back on pure-python gcd */ PyErr_Clear(); } -#endif /* otherwise, use our internal one, written in python */ { diff --git a/numpy/core/src/umath/loops.c.src b/numpy/core/src/umath/loops.c.src index 8a2e5bc40..33d10da49 100644 --- a/numpy/core/src/umath/loops.c.src +++ b/numpy/core/src/umath/loops.c.src @@ -1824,6 +1824,7 @@ NPY_NO_EXPORT void *((npy_bool *)op1) = in1 @OP@ in2; } } + npy_clear_floatstatus_barrier((char*)dimensions); } /**end repeat1**/ @@ -2068,6 +2069,7 @@ NPY_NO_EXPORT void const @type@ in1 = *(@type@ *)ip1; *((@type@ *)op1) = in1 > 0 ? 1 : (in1 < 0 ? -1 : (in1 == 0 ? 0 : in1)); } + npy_clear_floatstatus_barrier((char*)dimensions); } NPY_NO_EXPORT void diff --git a/numpy/core/src/umath/reduction.c b/numpy/core/src/umath/reduction.c index 4ce8d8ab7..79c302755 100644 --- a/numpy/core/src/umath/reduction.c +++ b/numpy/core/src/umath/reduction.c @@ -84,10 +84,12 @@ allocate_reduce_result(PyArrayObject *arr, const npy_bool *axis_flags, * The return value is a view into 'out'. */ static PyArrayObject * -conform_reduce_result(int ndim, const npy_bool *axis_flags, +conform_reduce_result(PyArrayObject *in, const npy_bool *axis_flags, PyArrayObject *out, int keepdims, const char *funcname, int need_copy) { + int ndim = PyArray_NDIM(in); + npy_intp *shape_in = PyArray_DIMS(in); npy_intp strides[NPY_MAXDIMS], shape[NPY_MAXDIMS]; npy_intp *strides_out = PyArray_STRIDES(out); npy_intp *shape_out = PyArray_DIMS(out); @@ -118,6 +120,16 @@ conform_reduce_result(int ndim, const npy_bool *axis_flags, return NULL; } } + else { + if (shape_out[idim] != shape_in[idim]) { + PyErr_Format(PyExc_ValueError, + "output parameter for reduction operation %s " + "has a non-reduction dimension not equal to " + "the input one.", funcname); + return NULL; + } + } + } Py_INCREF(out); @@ -138,6 +150,13 @@ conform_reduce_result(int ndim, const npy_bool *axis_flags, "does not have enough dimensions", funcname); return NULL; } + if (shape_out[idim_out] != shape_in[idim]) { + PyErr_Format(PyExc_ValueError, + "output parameter for reduction operation %s " + "has a non-reduction dimension not equal to " + "the input one.", funcname); + return NULL; + } strides[idim] = strides_out[idim_out]; shape[idim] = shape_out[idim_out]; ++idim_out; @@ -240,7 +259,7 @@ PyArray_CreateReduceResult(PyArrayObject *operand, PyArrayObject *out, /* Steal the dtype reference */ Py_XDECREF(dtype); - result = conform_reduce_result(PyArray_NDIM(operand), axis_flags, + result = conform_reduce_result(operand, axis_flags, out, keepdims, funcname, need_copy); } diff --git a/numpy/core/src/umath/reduction.h b/numpy/core/src/umath/reduction.h index dfaeabcbb..0c2183ed6 100644 --- a/numpy/core/src/umath/reduction.h +++ b/numpy/core/src/umath/reduction.h @@ -100,8 +100,8 @@ typedef int (PyArray_AssignReduceIdentityFunc)(PyArrayObject *result, */ typedef int (PyArray_ReduceLoopFunc)(NpyIter *iter, char **dataptr, - npy_intp *strideptr, - npy_intp *countptr, + npy_intp const *strideptr, + npy_intp const *countptr, NpyIter_IterNextFunc *iternext, int needs_api, npy_intp skip_first_count, diff --git a/numpy/core/src/umath/scalarmath.c.src b/numpy/core/src/umath/scalarmath.c.src index d5d8d659b..b3826eef4 100644 --- a/numpy/core/src/umath/scalarmath.c.src +++ b/numpy/core/src/umath/scalarmath.c.src @@ -744,56 +744,50 @@ _@name@_convert2_to_ctypes(PyObject *a, @type@ *arg1, /**end repeat**/ -#if defined(NPY_PY3K) -#define CODEGEN_SKIP_divide_FLAG -#endif - /**begin repeat * * #name = (byte, ubyte, short, ushort, int, uint, - * long, ulong, longlong, ulonglong)*13, + * long, ulong, longlong, ulonglong)*12, * (half, float, double, longdouble, - * cfloat, cdouble, clongdouble)*6, + * cfloat, cdouble, clongdouble)*5, * (half, float, double, longdouble)*2# * #Name = (Byte, UByte, Short, UShort, Int, UInt, - * Long, ULong,LongLong,ULongLong)*13, + * Long, ULong,LongLong,ULongLong)*12, * (Half, Float, Double, LongDouble, - * CFloat, CDouble, CLongDouble)*6, + * CFloat, CDouble, CLongDouble)*5, * (Half, Float, Double, LongDouble)*2# * #type = (npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint, - * npy_long, npy_ulong, npy_longlong, npy_ulonglong)*13, + * npy_long, npy_ulong, npy_longlong, npy_ulonglong)*12, * (npy_half, npy_float, npy_double, npy_longdouble, - * npy_cfloat, npy_cdouble, npy_clongdouble)*6, + * npy_cfloat, npy_cdouble, npy_clongdouble)*5, * (npy_half, npy_float, npy_double, npy_longdouble)*2# * - * #oper = add*10, subtract*10, multiply*10, divide*10, remainder*10, + * #oper = add*10, subtract*10, multiply*10, remainder*10, * divmod*10, floor_divide*10, lshift*10, rshift*10, and*10, * or*10, xor*10, true_divide*10, - * add*7, subtract*7, multiply*7, divide*7, floor_divide*7, true_divide*7, + * add*7, subtract*7, multiply*7, floor_divide*7, true_divide*7, * divmod*4, remainder*4# * - * #fperr = 1*70,0*50,1*10, - * 1*42, + * #fperr = 1*60,0*50,1*10, + * 1*35, * 1*8# - * #twoout = 0*50,1*10,0*70, - * 0*42, + * #twoout = 0*40,1*10,0*70, + * 0*35, * 1*4,0*4# * #otype = (npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint, - * npy_long, npy_ulong, npy_longlong, npy_ulonglong)*12, + * npy_long, npy_ulong, npy_longlong, npy_ulonglong)*11, * npy_float*4, npy_double*6, * (npy_half, npy_float, npy_double, npy_longdouble, - * npy_cfloat, npy_cdouble, npy_clongdouble)*6, + * npy_cfloat, npy_cdouble, npy_clongdouble)*5, * (npy_half, npy_float, npy_double, npy_longdouble)*2# * #OName = (Byte, UByte, Short, UShort, Int, UInt, - * Long, ULong, LongLong, ULongLong)*12, + * Long, ULong, LongLong, ULongLong)*11, * Float*4, Double*6, * (Half, Float, Double, LongDouble, - * CFloat, CDouble, CLongDouble)*6, + * CFloat, CDouble, CLongDouble)*5, * (Half, Float, Double, LongDouble)*2# */ -#if !defined(CODEGEN_SKIP_@oper@_FLAG) - static PyObject * @name@_@oper@(PyObject *a, PyObject *b) { @@ -904,12 +898,9 @@ static PyObject * #endif return ret; } -#endif /**end repeat**/ -#undef CODEGEN_SKIP_divide_FLAG - #define _IS_ZERO(x) (x == 0) /**begin repeat @@ -1300,12 +1291,6 @@ static PyObject * /**end repeat**/ -#if defined(NPY_PY3K) -#define NONZERO_NAME(prefix) prefix##bool -#else -#define NONZERO_NAME(prefix) prefix##nonzero -#endif - #define _IS_NONZERO(x) (x != 0) /**begin repeat * @@ -1321,7 +1306,7 @@ static PyObject * * #nonzero = _IS_NONZERO*10, !npy_half_iszero, _IS_NONZERO*6# */ static int -NONZERO_NAME(@name@_)(PyObject *a) +@name@_bool(PyObject *a) { int ret; @type@ arg1; @@ -1330,7 +1315,7 @@ NONZERO_NAME(@name@_)(PyObject *a) if (PyErr_Occurred()) { return -1; } - return PyGenericArrType_Type.tp_as_number->NONZERO_NAME(nb_)(a); + return PyGenericArrType_Type.tp_as_number->nb_bool(a); } /* @@ -1410,15 +1395,6 @@ static PyObject * return NULL; } -#ifndef NPY_PY3K - /* Invoke long.__int__ to try to downcast */ - { - PyObject *before_downcast = long_result; - long_result = Py_TYPE(long_result)->tp_as_number->nb_int(long_result); - Py_DECREF(before_downcast); - } -#endif - return long_result; } /**end repeat**/ @@ -1451,63 +1427,6 @@ static NPY_INLINE PyObject * } /**end repeat**/ - -#if !defined(NPY_PY3K) - -/**begin repeat - * - * #name = (byte, ubyte, short, ushort, int, uint, - * long, ulong, longlong, ulonglong, - * half, float, double, longdouble, - * cfloat, cdouble, clongdouble)# - * #Name = (Byte, UByte, Short, UShort, Int, UInt, - * Long, ULong, LongLong, ULongLong, - * Half, Float, Double, LongDouble, - * CFloat, CDouble, CLongDouble)# - * #cmplx = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1)# - * #to_ctype = (, , , , , , , , , , npy_half_to_double, , , , , , )# - * #func = (PyLong_FromLongLong, PyLong_FromUnsignedLongLong)*5, - * PyLong_FromDouble*3, npy_longdouble_to_PyLong, - * PyLong_FromDouble*2, npy_longdouble_to_PyLong# - */ -static NPY_INLINE PyObject * -@name@_long(PyObject *obj) -{ -#if @cmplx@ - if (emit_complexwarning() < 0) { - return NULL; - } - return @func@(@to_ctype@(PyArrayScalar_VAL(obj, @Name@).real)); -#else - return @func@(@to_ctype@(PyArrayScalar_VAL(obj, @Name@))); -#endif -} -/**end repeat**/ - -/**begin repeat - * - * #name = (byte, ubyte, short, ushort, int, uint, - * long, ulong, longlong, ulonglong, - * half, float, double, longdouble, - * cfloat, cdouble, clongdouble)*2# - * #oper = oct*17, hex*17# - * #kind = (int*5, long*5, int*2, long*2, int, long*2)*2# - * #cap = (Int*5, Long*5, Int*2, Long*2, Int, Long*2)*2# - */ -static PyObject * -@name@_@oper@(PyObject *obj) -{ - PyObject *pyint; - pyint = @name@_@kind@(obj); - if (pyint == NULL) { - return NULL; - } - return Py@cap@_Type.tp_as_number->nb_@oper@(pyint); -} -/**end repeat**/ - -#endif - /**begin repeat * #oper = le, ge, lt, gt, eq, ne# * #op = <=, >=, <, >, ==, !=# @@ -1597,46 +1516,25 @@ static PyNumberMethods @name@_as_number = { (binaryfunc)@name@_add, /*nb_add*/ (binaryfunc)@name@_subtract, /*nb_subtract*/ (binaryfunc)@name@_multiply, /*nb_multiply*/ -#if !defined(NPY_PY3K) - (binaryfunc)@name@_divide, /*nb_divide*/ -#endif (binaryfunc)@name@_remainder, /*nb_remainder*/ (binaryfunc)@name@_divmod, /*nb_divmod*/ (ternaryfunc)@name@_power, /*nb_power*/ (unaryfunc)@name@_negative, (unaryfunc)@name@_positive, /*nb_pos*/ (unaryfunc)@name@_absolute, /*nb_abs*/ -#if defined(NPY_PY3K) (inquiry)@name@_bool, /*nb_bool*/ -#else - (inquiry)@name@_nonzero, /*nb_nonzero*/ -#endif (unaryfunc)@name@_invert, /*nb_invert*/ (binaryfunc)@name@_lshift, /*nb_lshift*/ (binaryfunc)@name@_rshift, /*nb_rshift*/ (binaryfunc)@name@_and, /*nb_and*/ (binaryfunc)@name@_xor, /*nb_xor*/ (binaryfunc)@name@_or, /*nb_or*/ -#if !defined(NPY_PY3K) - 0, /*nb_coerce*/ -#endif (unaryfunc)@name@_int, /*nb_int*/ -#if defined(NPY_PY3K) (unaryfunc)0, /*nb_reserved*/ -#else - (unaryfunc)@name@_long, /*nb_long*/ -#endif (unaryfunc)@name@_float, /*nb_float*/ -#if !defined(NPY_PY3K) - (unaryfunc)@name@_oct, /*nb_oct*/ - (unaryfunc)@name@_hex, /*nb_hex*/ -#endif 0, /*inplace_add*/ 0, /*inplace_subtract*/ 0, /*inplace_multiply*/ -#if !defined(NPY_PY3K) - 0, /*inplace_divide*/ -#endif 0, /*inplace_remainder*/ 0, /*inplace_power*/ 0, /*inplace_lshift*/ diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 1dc581977..fdbe8f2ad 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -929,22 +929,9 @@ parse_ufunc_keywords(PyUFuncObject *ufunc, PyObject *kwds, PyObject **kwnames, . } } else { -#if PY_VERSION_HEX >= 0x03000000 PyErr_Format(PyExc_TypeError, "'%S' is an invalid keyword to ufunc '%s'", key, ufunc_get_name_cstr(ufunc)); -#else - char *str = PyString_AsString(key); - if (str == NULL) { - PyErr_Clear(); - PyErr_SetString(PyExc_TypeError, "invalid keyword argument"); - } - else { - PyErr_Format(PyExc_TypeError, - "'%s' is an invalid keyword to ufunc '%s'", - str, ufunc_get_name_cstr(ufunc)); - } -#endif return -1; } } @@ -3437,8 +3424,8 @@ reduce_type_resolver(PyUFuncObject *ufunc, PyArrayObject *arr, } static int -reduce_loop(NpyIter *iter, char **dataptrs, npy_intp *strides, - npy_intp *countptr, NpyIter_IterNextFunc *iternext, +reduce_loop(NpyIter *iter, char **dataptrs, npy_intp const *strides, + npy_intp const *countptr, NpyIter_IterNextFunc *iternext, int needs_api, npy_intp skip_first_count, void *data) { PyArray_Descr *dtypes[3], **iter_dtypes; @@ -3515,7 +3502,11 @@ reduce_loop(NpyIter *iter, char **dataptrs, npy_intp *strides, strides_copy[2] = strides[0]; if (!masked) { - innerloop(dataptrs_copy, countptr, + /* gh-15252: The signature of the inner loop considers `countptr` + * mutable. Inner loops aren't actually allowed to modify this + * though, so it's fine to cast it. + */ + innerloop(dataptrs_copy, (npy_intp *)countptr, strides_copy, innerloopdata); } else { @@ -5068,21 +5059,12 @@ _free_loop1d_list(PyUFunc_Loop1d *data) } } -#if PY_VERSION_HEX >= 0x03000000 static void _loop1d_list_free(PyObject *ptr) { PyUFunc_Loop1d *data = (PyUFunc_Loop1d *)PyCapsule_GetPointer(ptr, NULL); _free_loop1d_list(data); } -#else -static void -_loop1d_list_free(void *ptr) -{ - PyUFunc_Loop1d *data = (PyUFunc_Loop1d *)ptr; - _free_loop1d_list(data); -} -#endif /* @@ -6056,12 +6038,7 @@ static PyGetSetDef ufunc_getset[] = { *****************************************************************************/ NPY_NO_EXPORT PyTypeObject PyUFunc_Type = { -#if defined(NPY_PY3K) PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ -#endif "numpy.ufunc", /* tp_name */ sizeof(PyUFuncObject), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -6070,11 +6047,7 @@ NPY_NO_EXPORT PyTypeObject PyUFunc_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ -#if defined(NPY_PY3K) 0, /* tp_reserved */ -#else - 0, /* tp_compare */ -#endif (reprfunc)ufunc_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ diff --git a/numpy/core/src/umath/ufunc_type_resolution.c b/numpy/core/src/umath/ufunc_type_resolution.c index f93d8229e..0e71305b6 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.c +++ b/numpy/core/src/umath/ufunc_type_resolution.c @@ -1347,37 +1347,6 @@ PyUFunc_TrueDivisionTypeResolver(PyUFuncObject *ufunc, return PyUFunc_DivisionTypeResolver(ufunc, casting, operands, type_tup, out_dtypes); } -/* - * Function to check and report floor division warning when python2.x is - * invoked with -3 switch - * See PEP238 and #7949 for numpy - * This function will not be hit for py3 or when __future__ imports division. - * See generate_umath.py for reason -*/ -NPY_NO_EXPORT int -PyUFunc_MixedDivisionTypeResolver(PyUFuncObject *ufunc, - NPY_CASTING casting, - PyArrayObject **operands, - PyObject *type_tup, - PyArray_Descr **out_dtypes) -{ - /* Deprecation checks needed only on python 2 */ -#if !defined(NPY_PY3K) - int type_num1, type_num2; - - type_num1 = PyArray_DESCR(operands[0])->type_num; - type_num2 = PyArray_DESCR(operands[1])->type_num; - - /* If both types are integer, warn the user, same as python does */ - if (Py_DivisionWarningFlag && - (PyTypeNum_ISINTEGER(type_num1) || PyTypeNum_ISBOOL(type_num1)) && - (PyTypeNum_ISINTEGER(type_num2) || PyTypeNum_ISBOOL(type_num2))) { - PyErr_Warn(PyExc_DeprecationWarning, "numpy: classic int division"); - } -#endif - return PyUFunc_DivisionTypeResolver(ufunc, casting, operands, - type_tup, out_dtypes); -} static int find_userloop(PyUFuncObject *ufunc, diff --git a/numpy/core/src/umath/ufunc_type_resolution.h b/numpy/core/src/umath/ufunc_type_resolution.h index a4e670a8e..1d6ad3358 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.h +++ b/numpy/core/src/umath/ufunc_type_resolution.h @@ -72,13 +72,6 @@ PyUFunc_MultiplicationTypeResolver(PyUFuncObject *ufunc, PyArray_Descr **out_dtypes); NPY_NO_EXPORT int -PyUFunc_MixedDivisionTypeResolver(PyUFuncObject *ufunc, - NPY_CASTING casting, - PyArrayObject **operands, - PyObject *type_tup, - PyArray_Descr **out_dtypes); - -NPY_NO_EXPORT int PyUFunc_TrueDivisionTypeResolver(PyUFuncObject *ufunc, NPY_CASTING casting, PyArrayObject **operands, diff --git a/numpy/core/src/umath/umathmodule.c b/numpy/core/src/umath/umathmodule.c index 6ec474376..e14006985 100644 --- a/numpy/core/src/umath/umathmodule.c +++ b/numpy/core/src/umath/umathmodule.c @@ -174,7 +174,6 @@ add_newdoc_ufunc(PyObject *NPY_UNUSED(dummy), PyObject *args) PyObject *str, *tmp; char *docstr, *newdocstr; -#if defined(NPY_PY3K) if (!PyArg_ParseTuple(args, "O!O!:_add_newdoc_ufunc", &PyUFunc_Type, &ufunc, &PyUnicode_Type, &str)) { return NULL; @@ -184,20 +183,11 @@ add_newdoc_ufunc(PyObject *NPY_UNUSED(dummy), PyObject *args) return NULL; } docstr = PyBytes_AS_STRING(tmp); -#else - if (!PyArg_ParseTuple(args, "O!O!:_add_newdoc_ufunc", &PyUFunc_Type, &ufunc, - &PyString_Type, &str)) { - return NULL; - } - docstr = PyString_AS_STRING(str); -#endif if (NULL != ufunc->doc) { PyErr_SetString(PyExc_ValueError, "Cannot change docstring of ufunc with non-NULL docstring"); -#if defined(NPY_PY3K) Py_DECREF(tmp); -#endif return NULL; } @@ -211,9 +201,7 @@ add_newdoc_ufunc(PyObject *NPY_UNUSED(dummy), PyObject *args) strcpy(newdocstr, docstr); ufunc->doc = newdocstr; -#if defined(NPY_PY3K) Py_DECREF(tmp); -#endif Py_RETURN_NONE; } @@ -324,10 +312,8 @@ int initumath(PyObject *m) PyModule_AddObject(m, "NZERO", PyFloat_FromDouble(NPY_NZERO)); PyModule_AddObject(m, "NAN", PyFloat_FromDouble(NPY_NAN)); -#if defined(NPY_PY3K) s = PyDict_GetItemString(d, "true_divide"); PyDict_SetItemString(d, "divide", s); -#endif s = PyDict_GetItemString(d, "conjugate"); s2 = PyDict_GetItemString(d, "remainder"); diff --git a/numpy/core/tests/_locales.py b/numpy/core/tests/_locales.py index 52e4ff36d..ce7b81f00 100644 --- a/numpy/core/tests/_locales.py +++ b/numpy/core/tests/_locales.py @@ -1,8 +1,6 @@ """Provide class for testing in French locale """ -from __future__ import division, absolute_import, print_function - import sys import locale @@ -45,7 +43,7 @@ def find_comma_decimal_point_locale(): return old_locale, new_locale -class CommaDecimalPointLocale(object): +class CommaDecimalPointLocale: """Sets LC_NUMERIC to a locale with comma as decimal point. Classes derived from this class have setup and teardown methods that run diff --git a/numpy/core/tests/test_abc.py b/numpy/core/tests/test_abc.py index d9c61b0c6..30e5748af 100644 --- a/numpy/core/tests/test_abc.py +++ b/numpy/core/tests/test_abc.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.testing import assert_ import numbers @@ -7,7 +5,7 @@ import numbers import numpy as np from numpy.core.numerictypes import sctypes -class TestABC(object): +class TestABC: def test_abstract(self): assert_(issubclass(np.number, numbers.Number)) diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index 89fc2b0b9..3f0a59eec 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import numpy as np diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 702e68e76..eac4647c9 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import division, absolute_import, print_function - import sys import gc import pytest @@ -12,7 +10,7 @@ from numpy.testing import ( ) import textwrap -class TestArrayRepr(object): +class TestArrayRepr: def test_nan_inf(self): x = np.array([np.nan, np.inf]) assert_equal(repr(x), 'array([nan, inf])') @@ -160,7 +158,7 @@ class TestArrayRepr(object): assert_equal(repr(arr_no_fields), 'array([(), (), (), ()], dtype=[])') -class TestComplexArray(object): +class TestComplexArray: def test_str(self): rvals = [0, 1, -1, np.inf, -np.inf, np.nan] cvals = [complex(rp, ip) for rp in rvals for ip in rvals] @@ -207,7 +205,7 @@ class TestComplexArray(object): for res, val in zip(actual, wanted): assert_equal(res, val) -class TestArray2String(object): +class TestArray2String: def test_basic(self): """Basic test of array2string.""" a = np.arange(3) @@ -413,7 +411,7 @@ class TestArray2String(object): gc.enable() assert_(r1 == r2) -class TestPrintOptions(object): +class TestPrintOptions: """Test getting and setting global print options.""" def setup(self): @@ -856,7 +854,7 @@ def test_unicode_object_array(): assert_equal(repr(x), expected) -class TestContextManager(object): +class TestContextManager: def test_ctx_mgr(self): # test that context manager actuall works with np.printoptions(precision=2): diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py index d38444ef7..438d52f97 100644 --- a/numpy/core/tests/test_datetime.py +++ b/numpy/core/tests/test_datetime.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy import numpy as np @@ -24,7 +22,7 @@ except NameError: RecursionError = RuntimeError # python < 3.5 -class TestDateTime(object): +class TestDateTime: def test_datetime_dtype_creation(self): for unit in ['Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', @@ -2368,7 +2366,7 @@ class TestDateTime(object): assert limit_via_str == limit -class TestDateTimeData(object): +class TestDateTimeData: def test_basic(self): a = np.array(['1980-03-23'], dtype=np.datetime64) diff --git a/numpy/core/tests/test_defchararray.py b/numpy/core/tests/test_defchararray.py index 7b0e6f8a4..d2a1e86d2 100644 --- a/numpy/core/tests/test_defchararray.py +++ b/numpy/core/tests/test_defchararray.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import numpy as np @@ -12,7 +10,7 @@ from numpy.testing import ( kw_unicode_true = {'unicode': True} # make 2to3 work properly kw_unicode_false = {'unicode': False} -class TestBasic(object): +class TestBasic: def test_from_object_array(self): A = np.array([['abc', 2], ['long ', '0123456789']], dtype='O') @@ -83,7 +81,7 @@ class TestBasic(object): assert_equal(A.itemsize, 4) assert_(issubclass(A.dtype.type, np.unicode_)) -class TestVecString(object): +class TestVecString: def test_non_existent_method(self): def fail(): @@ -134,7 +132,7 @@ class TestVecString(object): assert_raises(ValueError, fail) -class TestWhitespace(object): +class TestWhitespace: def setup(self): self.A = np.array([['abc ', '123 '], ['789 ', 'xyz ']]).view(np.chararray) @@ -149,7 +147,7 @@ class TestWhitespace(object): assert_(not np.any(self.A < self.B)) assert_(not np.any(self.A != self.B)) -class TestChar(object): +class TestChar: def setup(self): self.A = np.array('abc1', dtype='c').view(np.chararray) @@ -157,7 +155,7 @@ class TestChar(object): assert_equal(self.A.shape, (4,)) assert_equal(self.A.upper()[:2].tobytes(), b'AB') -class TestComparisons(object): +class TestComparisons: def setup(self): self.A = np.array([['abc', '123'], ['789', 'xyz']]).view(np.chararray) @@ -198,7 +196,7 @@ class TestComparisonsMixed2(TestComparisons): self.A = np.array([['abc', '123'], ['789', 'xyz']], np.unicode_).view(np.chararray) -class TestInformation(object): +class TestInformation: def setup(self): self.A = np.array([[' abc ', ''], ['12345', 'MixedCase'], @@ -304,7 +302,7 @@ class TestInformation(object): assert_raises(TypeError, fail) -class TestMethods(object): +class TestMethods: def setup(self): self.A = np.array([[' abc ', ''], ['12345', 'MixedCase'], @@ -599,7 +597,7 @@ class TestMethods(object): [False, False], [True, False], [False, False]]) -class TestOperations(object): +class TestOperations: def setup(self): self.A = np.array([['abc', '123'], ['789', 'xyz']]).view(np.chararray) diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 363ff26db..7232b5949 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -3,8 +3,6 @@ Tests related to deprecation warnings. Also a convenient place to document how deprecations should eventually be turned into errors. """ -from __future__ import division, absolute_import, print_function - import datetime import sys import operator @@ -27,7 +25,7 @@ except ImportError: _has_pytz = False -class _DeprecationTestCase(object): +class _DeprecationTestCase: # Just as warning: warnings uses re.match, so the start of this message # must match. message = '' @@ -137,7 +135,7 @@ class _VisibleDeprecationTestCase(_DeprecationTestCase): warning_cls = np.VisibleDeprecationWarning -class TestNonTupleNDIndexDeprecation(object): +class TestNonTupleNDIndexDeprecation: def test_basic(self): a = np.zeros((5, 5)) with warnings.catch_warnings(): @@ -189,7 +187,7 @@ class TestComparisonDeprecations(_DeprecationTestCase): assert_warns(FutureWarning, lambda: a == []) def test_void_dtype_equality_failures(self): - class NotArray(object): + class NotArray: def __array__(self): raise TypeError @@ -342,7 +340,7 @@ class TestNumericStyleTypecodes(_DeprecationTestCase): args=(dt,)) -class TestTestDeprecated(object): +class TestTestDeprecated: def test_assert_deprecated(self): test_case_instance = _DeprecationTestCase() test_case_instance.setup() diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index e18e66c64..1d24d8a3d 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import operator import pytest @@ -23,7 +21,7 @@ def assert_dtype_not_equal(a, b): assert_(hash(a) != hash(b), "two different types hash to the same value !") -class TestBuiltin(object): +class TestBuiltin: @pytest.mark.parametrize('t', [int, float, complex, np.int32, str, object, np.compat.unicode]) def test_run(self, t): @@ -146,7 +144,7 @@ class TestBuiltin(object): 'offsets': [4, 0]}) assert_equal(x == y, False) -class TestRecord(object): +class TestRecord: def test_equivalent_record(self): """Test whether equivalent record dtypes hash the same.""" a = np.dtype([('yo', int)]) @@ -445,7 +443,7 @@ class TestRecord(object): np.ones((1, 2), dtype=bool)) -class TestSubarray(object): +class TestSubarray: def test_single_subarray(self): a = np.dtype((int, (2))) b = np.dtype((int, (2,))) @@ -521,7 +519,7 @@ class TestSubarray(object): assert_(isinstance(dt['a'].shape, tuple)) # - class IntLike(object): + class IntLike: def __index__(self): return 3 @@ -711,7 +709,7 @@ class TestStructuredObjectRefcounting: assert after_repeat - after == count * 2 * 10 -class TestStructuredDtypeSparseFields(object): +class TestStructuredDtypeSparseFields: """Tests subarray fields which contain sparse dtypes so that not all memory is used by the dtype work. Such dtype's should leave the underlying memory unchanged. @@ -732,7 +730,7 @@ class TestStructuredDtypeSparseFields(object): assert_array_equal(arr["a"]["aa"], np.zeros((3, 2, 3))) def test_sparse_field_assignment_fancy(self): - # Fancy assignment goes to the copyswap function for comlex types: + # Fancy assignment goes to the copyswap function for complex types: arr = np.zeros(3, self.dtype) sparse_arr = arr.view(self.sparse_dtype) @@ -741,7 +739,7 @@ class TestStructuredDtypeSparseFields(object): assert_array_equal(arr["a"]["aa"], np.zeros((3, 2, 3))) -class TestMonsterType(object): +class TestMonsterType: """Test deeply nested subtypes.""" def test1(self): @@ -759,7 +757,7 @@ class TestMonsterType(object): ('yi', np.dtype((a, (3, 2))))]) assert_dtype_equal(c, d) -class TestMetadata(object): +class TestMetadata: def test_no_metadata(self): d = np.dtype(int) assert_(d.metadata is None) @@ -781,7 +779,7 @@ class TestMetadata(object): d = np.dtype((np.void, np.dtype('i4,i4', metadata={'datum': 1}))) assert_(d.metadata == {'datum': 1}) -class TestString(object): +class TestString: def test_complex_dtype_str(self): dt = np.dtype([('top', [('tiles', ('>f4', (64, 64)), (1,)), ('rtile', '>f4', (64, 36))], (3,)), @@ -934,7 +932,7 @@ class TestString(object): assert_equal(dt.name, 'record16') -class TestDtypeAttributeDeletion(object): +class TestDtypeAttributeDeletion: def test_dtype_non_writable_attributes_deletion(self): dt = np.dtype(np.double) @@ -952,7 +950,7 @@ class TestDtypeAttributeDeletion(object): assert_raises(AttributeError, delattr, dt, s) -class TestDtypeAttributes(object): +class TestDtypeAttributes: def test_descr_has_trailing_void(self): # see gh-6359 dtype = np.dtype({ @@ -970,7 +968,7 @@ class TestDtypeAttributes(object): assert_equal(np.dtype(user_def_subcls).name, 'user_def_subcls') -class TestPickling(object): +class TestPickling: def check_pickling(self, dtype): for proto in range(pickle.HIGHEST_PROTOCOL + 1): @@ -1054,7 +1052,7 @@ def test_invalid_dtype_string(): assert_raises(TypeError, np.dtype, u'Fl\xfcgel') -class TestFromDTypeAttribute(object): +class TestFromDTypeAttribute: def test_simple(self): class dt: dtype = "f8" @@ -1098,7 +1096,7 @@ class TestFromDTypeAttribute(object): with pytest.raises(RecursionError): np.dtype(dt(1)) -class TestFromCTypes(object): +class TestFromCTypes: @staticmethod def check(ctype, dtype): diff --git a/numpy/core/tests/test_einsum.py b/numpy/core/tests/test_einsum.py index 1b5b4cb26..6ec61fb1d 100644 --- a/numpy/core/tests/test_einsum.py +++ b/numpy/core/tests/test_einsum.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import itertools import numpy as np @@ -14,7 +12,7 @@ sizes = np.array([2, 3, 4, 5, 4, 3, 2, 6, 5, 4, 3]) global_size_dict = dict(zip(chars, sizes)) -class TestEinsum(object): +class TestEinsum: def test_einsum_errors(self): for do_opt in [True, False]: # Need enough arguments @@ -868,7 +866,7 @@ class TestEinsum(object): self.optimize_compare('obk,ijk->ioj', operands=[g, g]) -class TestEinsumPath(object): +class TestEinsumPath: def build_operands(self, string, size_dict=global_size_dict): # Builds views based off initial operands diff --git a/numpy/core/tests/test_errstate.py b/numpy/core/tests/test_errstate.py index 0008c4cc8..7c1780607 100644 --- a/numpy/core/tests/test_errstate.py +++ b/numpy/core/tests/test_errstate.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import platform import pytest @@ -7,7 +5,7 @@ import numpy as np from numpy.testing import assert_, assert_raises -class TestErrstate(object): +class TestErrstate: @pytest.mark.skipif(platform.machine() == "armv5tel", reason="See gh-413.") def test_invalid(self): with np.errstate(all='raise', under='ignore'): diff --git a/numpy/core/tests/test_extint128.py b/numpy/core/tests/test_extint128.py index 7c454a603..3b64915f3 100644 --- a/numpy/core/tests/test_extint128.py +++ b/numpy/core/tests/test_extint128.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import itertools import contextlib import operator diff --git a/numpy/core/tests/test_function_base.py b/numpy/core/tests/test_function_base.py index c8a7cb6ce..7e5ea1cc6 100644 --- a/numpy/core/tests/test_function_base.py +++ b/numpy/core/tests/test_function_base.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy import ( logspace, linspace, geomspace, dtype, array, sctypes, arange, isnan, ndarray, sqrt, nextafter, stack @@ -42,7 +40,7 @@ class PhysicalQuantity2(ndarray): __array_priority__ = 10 -class TestLogspace(object): +class TestLogspace: def test_basic(self): y = logspace(0, 6) @@ -92,7 +90,7 @@ class TestLogspace(object): assert_equal(ls, logspace(1.0, 7.0, 1)) -class TestGeomspace(object): +class TestGeomspace: def test_basic(self): y = geomspace(1, 1e6) @@ -222,7 +220,7 @@ class TestGeomspace(object): assert_raises(ValueError, geomspace, 0, 0) -class TestLinspace(object): +class TestLinspace: def test_basic(self): y = linspace(0, 10) @@ -309,7 +307,7 @@ class TestLinspace(object): # Ensure that start/stop can be objects that implement # __array_interface__ and are convertible to numeric scalars - class Arrayish(object): + class Arrayish: """ A generic object that supports the __array_interface__ and hence can in principle be converted to a numeric scalar, but is not diff --git a/numpy/core/tests/test_getlimits.py b/numpy/core/tests/test_getlimits.py index 2f6648183..bcf8cf659 100644 --- a/numpy/core/tests/test_getlimits.py +++ b/numpy/core/tests/test_getlimits.py @@ -1,8 +1,6 @@ """ Test functions for limits module. """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.core import finfo, iinfo from numpy import half, single, double, longdouble @@ -11,37 +9,37 @@ from numpy.core.getlimits import _discovered_machar, _float_ma ################################################## -class TestPythonFloat(object): +class TestPythonFloat: def test_singleton(self): ftype = finfo(float) ftype2 = finfo(float) assert_equal(id(ftype), id(ftype2)) -class TestHalf(object): +class TestHalf: def test_singleton(self): ftype = finfo(half) ftype2 = finfo(half) assert_equal(id(ftype), id(ftype2)) -class TestSingle(object): +class TestSingle: def test_singleton(self): ftype = finfo(single) ftype2 = finfo(single) assert_equal(id(ftype), id(ftype2)) -class TestDouble(object): +class TestDouble: def test_singleton(self): ftype = finfo(double) ftype2 = finfo(double) assert_equal(id(ftype), id(ftype2)) -class TestLongdouble(object): +class TestLongdouble: def test_singleton(self): ftype = finfo(longdouble) ftype2 = finfo(longdouble) assert_equal(id(ftype), id(ftype2)) -class TestFinfo(object): +class TestFinfo: def test_basic(self): dts = list(zip(['f2', 'f4', 'f8', 'c8', 'c16'], [np.float16, np.float32, np.float64, np.complex64, @@ -54,7 +52,7 @@ class TestFinfo(object): getattr(finfo(dt2), attr), attr) assert_raises(ValueError, finfo, 'i4') -class TestIinfo(object): +class TestIinfo: def test_basic(self): dts = list(zip(['i1', 'i2', 'i4', 'i8', 'u1', 'u2', 'u4', 'u8'], @@ -71,7 +69,7 @@ class TestIinfo(object): for T in types: assert_equal(iinfo(T).max, T(-1)) -class TestRepr(object): +class TestRepr: def test_iinfo_repr(self): expected = "iinfo(min=-32768, max=32767, dtype=int16)" assert_equal(repr(np.iinfo(np.int16)), expected) diff --git a/numpy/core/tests/test_half.py b/numpy/core/tests/test_half.py index 1e1e6d7d9..c6d046be1 100644 --- a/numpy/core/tests/test_half.py +++ b/numpy/core/tests/test_half.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import platform import pytest @@ -18,7 +16,7 @@ def assert_raises_fpe(strmatch, callable, *args, **kwargs): assert_(False, "Did not raise floating point %s error" % strmatch) -class TestHalf(object): +class TestHalf: def setup(self): # An array of all possible float16 values self.all_f16 = np.arange(0x10000, dtype=uint16) diff --git a/numpy/core/tests/test_indexerrors.py b/numpy/core/tests/test_indexerrors.py index 63b43c473..9d2433fc5 100644 --- a/numpy/core/tests/test_indexerrors.py +++ b/numpy/core/tests/test_indexerrors.py @@ -1,9 +1,7 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_raises -class TestIndexErrors(object): +class TestIndexErrors: '''Tests to exercise indexerrors not covered by other tests.''' def test_arraytypes_fasttake(self): diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py index 70a5a246f..237e381a7 100644 --- a/numpy/core/tests/test_indexing.py +++ b/numpy/core/tests/test_indexing.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import warnings import functools @@ -11,11 +9,11 @@ from numpy.core._multiarray_tests import array_indexing from itertools import product from numpy.testing import ( assert_, assert_equal, assert_raises, assert_array_equal, assert_warns, - HAS_REFCOUNT, suppress_warnings, + HAS_REFCOUNT, ) -class TestIndexing(object): +class TestIndexing: def test_index_no_floats(self): a = np.array([[[5]]]) @@ -397,14 +395,14 @@ class TestIndexing(object): def test_scalar_return_type(self): # Full scalar indices should return scalars and object # arrays should not call PyArray_Return on their items - class Zero(object): + class Zero: # The most basic valid indexing def __index__(self): return 0 z = Zero() - class ArrayLike(object): + class ArrayLike: # Simple array, should behave like the array def __array__(self): return np.array(0) @@ -484,7 +482,7 @@ class TestIndexing(object): # on item getting, this should not be converted to an nd-index (tuple) # If this object happens to be a valid index otherwise, it should work # This object here is very dubious and probably bad though: - class SequenceLike(object): + class SequenceLike: def __index__(self): return 0 @@ -527,7 +525,7 @@ class TestIndexing(object): arr[slices] = 10 assert_array_equal(arr, 10.) -class TestFieldIndexing(object): +class TestFieldIndexing: def test_scalar_return_type(self): # Field access on an array should return an array, even if it # is 0-d. @@ -536,7 +534,7 @@ class TestFieldIndexing(object): assert_(isinstance(a[['a']], np.ndarray)) -class TestBroadcastedAssignments(object): +class TestBroadcastedAssignments: def assign(self, a, ind, val): a[ind] = val return a @@ -587,7 +585,7 @@ class TestBroadcastedAssignments(object): assert_((a[::-1] == v).all()) -class TestSubclasses(object): +class TestSubclasses: def test_basic(self): # Test that indexing in various ways produces SubClass instances, # and that the base is set up correctly: the original subclass @@ -650,56 +648,8 @@ class TestSubclasses(object): assert_array_equal(new_s.finalize_status, new_s) assert_array_equal(new_s.old, s) - @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") - def test_slice_decref_getsetslice(self): - # See gh-10066, a temporary slice object should be discarted. - # This test is only really interesting on Python 2 since - # it goes through `__set/getslice__` here and can probably be - # removed. Use 0:7 to make sure it is never None:7. - class KeepIndexObject(np.ndarray): - def __getitem__(self, indx): - self.indx = indx - if indx == slice(0, 7): - raise ValueError - - def __setitem__(self, indx, val): - self.indx = indx - if indx == slice(0, 4): - raise ValueError - k = np.array([1]).view(KeepIndexObject) - k[0:5] - assert_equal(k.indx, slice(0, 5)) - assert_equal(sys.getrefcount(k.indx), 2) - try: - k[0:7] - raise AssertionError - except ValueError: - # The exception holds a reference to the slice so clear on Py2 - if hasattr(sys, 'exc_clear'): - with suppress_warnings() as sup: - sup.filter(DeprecationWarning) - sys.exc_clear() - assert_equal(k.indx, slice(0, 7)) - assert_equal(sys.getrefcount(k.indx), 2) - - k[0:3] = 6 - assert_equal(k.indx, slice(0, 3)) - assert_equal(sys.getrefcount(k.indx), 2) - try: - k[0:4] = 2 - raise AssertionError - except ValueError: - # The exception holds a reference to the slice so clear on Py2 - if hasattr(sys, 'exc_clear'): - with suppress_warnings() as sup: - sup.filter(DeprecationWarning) - sys.exc_clear() - assert_equal(k.indx, slice(0, 4)) - assert_equal(sys.getrefcount(k.indx), 2) - - -class TestFancyIndexingCast(object): +class TestFancyIndexingCast: def test_boolean_index_cast_assign(self): # Setup the boolean index and float arrays. shape = (8, 63) @@ -721,7 +671,7 @@ class TestFancyIndexingCast(object): zero_array.__setitem__, bool_index, np.array([1j])) assert_equal(zero_array[0, 1], 0) -class TestFancyIndexingEquivalence(object): +class TestFancyIndexingEquivalence: def test_object_assign(self): # Check that the field and object special case using copyto is active. # The right hand side cannot be converted to an array here. @@ -769,7 +719,7 @@ class TestFancyIndexingEquivalence(object): assert_array_equal(a, b[0]) -class TestMultiIndexingAutomated(object): +class TestMultiIndexingAutomated: """ These tests use code to mimic the C-Code indexing for selection. @@ -1191,7 +1141,7 @@ class TestMultiIndexingAutomated(object): for index in self.complex_indices: self._check_single_index(a, index) -class TestFloatNonIntegerArgument(object): +class TestFloatNonIntegerArgument: """ These test that ``TypeError`` is raised when you try to use non-integers as arguments to for indexing and slicing e.g. ``a[0.0:5]`` @@ -1246,7 +1196,7 @@ class TestFloatNonIntegerArgument(object): assert_raises(TypeError, np.min, d, (.2, 1.2)) -class TestBooleanIndexing(object): +class TestBooleanIndexing: # Using a boolean as integer argument/indexing is an error. def test_bool_as_int_argument_errors(self): a = np.array([[[1]]]) @@ -1267,7 +1217,7 @@ class TestBooleanIndexing(object): assert_raises(IndexError, lambda: a[False, [0, 1], ...]) -class TestArrayToIndexDeprecation(object): +class TestArrayToIndexDeprecation: """Creating an an index from array not 0-D is an error. """ @@ -1280,7 +1230,7 @@ class TestArrayToIndexDeprecation(object): assert_raises(TypeError, np.take, a, [0], a) -class TestNonIntegerArrayLike(object): +class TestNonIntegerArrayLike: """Tests that array_likes only valid if can safely cast to integer. For instance, lists give IndexError when they cannot be safely cast to @@ -1297,7 +1247,7 @@ class TestNonIntegerArrayLike(object): a.__getitem__([]) -class TestMultipleEllipsisError(object): +class TestMultipleEllipsisError: """An index can only have a single ellipsis. """ @@ -1308,7 +1258,7 @@ class TestMultipleEllipsisError(object): assert_raises(IndexError, a.__getitem__, ((Ellipsis,) * 3,)) -class TestCApiAccess(object): +class TestCApiAccess: def test_getitem(self): subscript = functools.partial(array_indexing, 0) diff --git a/numpy/core/tests/test_issue14735.py b/numpy/core/tests/test_issue14735.py deleted file mode 100644 index 6105c8e6a..000000000 --- a/numpy/core/tests/test_issue14735.py +++ /dev/null @@ -1,29 +0,0 @@ -import pytest -import warnings -import numpy as np - - -class Wrapper: - def __init__(self, array): - self.array = array - - def __len__(self): - return len(self.array) - - def __getitem__(self, item): - return type(self)(self.array[item]) - - def __getattr__(self, name): - if name.startswith("__array_"): - warnings.warn("object got converted", UserWarning, stacklevel=1) - - return getattr(self.array, name) - - def __repr__(self): - return "<Wrapper({self.array})>".format(self=self) - -@pytest.mark.filterwarnings("error") -def test_getattr_warning(): - array = Wrapper(np.arange(10)) - with pytest.raises(UserWarning, match="object got converted"): - np.asarray(array) diff --git a/numpy/core/tests/test_item_selection.py b/numpy/core/tests/test_item_selection.py index 9bd246866..cadd0d513 100644 --- a/numpy/core/tests/test_item_selection.py +++ b/numpy/core/tests/test_item_selection.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import numpy as np @@ -8,7 +6,7 @@ from numpy.testing import ( ) -class TestTake(object): +class TestTake: def test_simple(self): a = [[1, 2], [3, 4]] a_str = [[b'1', b'2'], [b'3', b'4']] diff --git a/numpy/core/tests/test_longdouble.py b/numpy/core/tests/test_longdouble.py index 2b6e1c5a2..bf12f0e1b 100644 --- a/numpy/core/tests/test_longdouble.py +++ b/numpy/core/tests/test_longdouble.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import warnings import pytest @@ -121,7 +119,7 @@ def test_fromstring_missing(): np.array([1])) -class TestFileBased(object): +class TestFileBased: ldbl = 1 + LD_INFO.eps tgt = np.array([ldbl]*5) diff --git a/numpy/core/tests/test_machar.py b/numpy/core/tests/test_machar.py index ab8800c09..673f309f1 100644 --- a/numpy/core/tests/test_machar.py +++ b/numpy/core/tests/test_machar.py @@ -3,20 +3,18 @@ Test machar. Given recent changes to hardcode type data, we might want to get rid of both MachAr and this test at some point. """ -from __future__ import division, absolute_import, print_function - from numpy.core.machar import MachAr import numpy.core.numerictypes as ntypes from numpy import errstate, array -class TestMachAr(object): +class TestMachAr: def _run_machar_highprec(self): # Instantiate MachAr instance with high enough precision to cause # underflow try: hiprec = ntypes.float96 - MachAr(lambda v:array([v], hiprec)) + MachAr(lambda v: array(v, hiprec)) except AttributeError: # Fixme, this needs to raise a 'skip' exception. "Skipping test: no ntypes.float96 available on this platform." diff --git a/numpy/core/tests/test_mem_overlap.py b/numpy/core/tests/test_mem_overlap.py index 3c8e0e722..c5115fa7e 100644 --- a/numpy/core/tests/test_mem_overlap.py +++ b/numpy/core/tests/test_mem_overlap.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import itertools import pytest @@ -564,7 +562,7 @@ def test_internal_overlap_fuzz(): def test_non_ndarray_inputs(): # Regression check for gh-5604 - class MyArray(object): + class MyArray: def __init__(self, data): self.data = data @@ -572,7 +570,7 @@ def test_non_ndarray_inputs(): def __array_interface__(self): return self.data.__array_interface__ - class MyArray2(object): + class MyArray2: def __init__(self, data): self.data = data @@ -619,7 +617,7 @@ def assert_copy_equivalent(operation, args, out, **kwargs): assert_equal(got, expected) -class TestUFunc(object): +class TestUFunc: """ Test ufunc call memory overlap handling """ diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py index d2ae564b2..bae7a318a 100644 --- a/numpy/core/tests/test_memmap.py +++ b/numpy/core/tests/test_memmap.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import os import shutil @@ -16,7 +14,7 @@ from numpy.testing import ( assert_, assert_equal, assert_array_equal, suppress_warnings ) -class TestMemmap(object): +class TestMemmap: def setup(self): self.tmpfp = NamedTemporaryFile(prefix='mmap') self.tempdir = mkdtemp() diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 22d550ecc..85910886a 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - try: # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 @@ -20,7 +18,6 @@ import gc import weakref import pytest from contextlib import contextmanager -from test.support import no_tracing from numpy.compat import pickle @@ -97,8 +94,28 @@ def _aligned_zeros(shape, dtype=float, order="C", align=None): data.fill(0) return data +def _no_tracing(func): + """ + Decorator to temporarily turn off tracing for the duration of a test. + Needed in tests that check refcounting, otherwise the tracing itself + influences the refcounts + """ + if not hasattr(sys, 'gettrace'): + return func + else: + @functools.wraps(func) + def wrapper(*args, **kwargs): + original_trace = sys.gettrace() + try: + sys.settrace(None) + return func(*args, **kwargs) + finally: + sys.settrace(original_trace) + return wrapper + + -class TestFlags(object): +class TestFlags: def setup(self): self.a = np.arange(10) @@ -266,7 +283,7 @@ class TestFlags(object): assert_(a.flags.aligned) -class TestHash(object): +class TestHash: # see #3793 def test_int(self): for st, ut, s in [(np.int8, np.uint8, 8), @@ -288,7 +305,7 @@ class TestHash(object): err_msg="%r: 2**%d - 1" % (ut, i)) -class TestAttributes(object): +class TestAttributes: def setup(self): self.one = np.arange(10) self.two = np.arange(20).reshape(4, 5) @@ -329,17 +346,8 @@ class TestAttributes(object): numpy_int = np.int_(0) - if sys.version_info[0] >= 3: - # On Py3k int_ should not inherit from int, because it's not - # fixed-width anymore - assert_equal(isinstance(numpy_int, int), False) - else: - # Otherwise, it should inherit from int... - assert_equal(isinstance(numpy_int, int), True) - - # ... and fast-path checks on C-API level should also work - from numpy.core._multiarray_tests import test_int_subclass - assert_equal(test_int_subclass(numpy_int), True) + # int_ doesn't inherit from Python int, because it's not fixed-width + assert_(not isinstance(numpy_int, int)) def test_stridesattr(self): x = self.one @@ -421,7 +429,7 @@ class TestAttributes(object): assert_array_equal(x['b'], [-2, -2]) -class TestArrayConstruction(object): +class TestArrayConstruction: def test_array(self): d = np.ones(6) r = np.array([d, d]) @@ -499,7 +507,7 @@ class TestArrayConstruction(object): assert_(np.asfortranarray(d).flags.f_contiguous) -class TestAssignment(object): +class TestAssignment: def test_assignment_broadcasting(self): a = np.arange(6).reshape(2, 3) @@ -565,7 +573,7 @@ class TestAssignment(object): u = np.array([u'done']) b = np.array([b'done']) - class bad_sequence(object): + class bad_sequence: def __getitem__(self): pass def __len__(self): raise RuntimeError @@ -616,7 +624,7 @@ class TestAssignment(object): assert_equal(a[0], b"1.1234567890123457") -class TestDtypedescr(object): +class TestDtypedescr: def test_construction(self): d1 = np.dtype('i4') assert_equal(d1, np.dtype(np.int32)) @@ -638,7 +646,7 @@ class TestDtypedescr(object): "array([0, 0, 0, 0], dtype=(numpy.int32, [('a', '<i2'), ('b', '<i2')]))") -class TestZeroRank(object): +class TestZeroRank: def setup(self): self.d = np.array(0), np.array('x', object) @@ -736,7 +744,7 @@ class TestZeroRank(object): assert_equal(xi.flags.f_contiguous, True) -class TestScalarIndexing(object): +class TestScalarIndexing: def setup(self): self.d = np.array([0, 1])[0] @@ -832,12 +840,12 @@ class TestScalarIndexing(object): assert_equal(a, [0, 1, 0, 1, 2]) -class TestCreation(object): +class TestCreation: """ Test the np.array constructor """ def test_from_attribute(self): - class x(object): + class x: def __array__(self, dtype=None): pass @@ -979,14 +987,14 @@ class TestCreation(object): of an error in the Fail case. """ - class Fail(object): + class Fail: def __len__(self): return 1 def __getitem__(self, index): raise ValueError() - class Map(object): + class Map: def __len__(self): return 1 @@ -1024,7 +1032,7 @@ class TestCreation(object): def test_failed_len_sequence(self): # gh-7393 - class A(object): + class A: def __init__(self, data): self._data = data def __getitem__(self, item): @@ -1080,7 +1088,7 @@ class TestCreation(object): assert_equal(a.dtype, object) -class TestStructured(object): +class TestStructured: def test_subarray_field_access(self): a = np.zeros((3, 5), dtype=[('a', ('i4', (2, 2)))]) a['a'] = np.arange(60).reshape(3, 5, 2, 2) @@ -1368,7 +1376,7 @@ class TestStructured(object): a[['b','c']] # no exception -class TestBool(object): +class TestBool: def test_test_interning(self): a0 = np.bool_(0) b0 = np.bool_(False) @@ -1458,7 +1466,7 @@ class TestBool(object): self._test_cast_from_flexible(np.bytes_) -class TestZeroSizeFlexible(object): +class TestZeroSizeFlexible: @staticmethod def _zeros(shape, dtype=str): dtype = np.dtype(dtype) @@ -1545,7 +1553,7 @@ class TestZeroSizeFlexible(object): assert array_from_buffer[0] == -1, array_from_buffer[0] -class TestMethods(object): +class TestMethods: sort_kinds = ['quicksort', 'heapsort', 'stable'] @@ -1872,7 +1880,7 @@ class TestMethods(object): # test generic class with bogus ordering, # should not segfault. - class Boom(object): + class Boom: def __lt__(self, other): return True @@ -1900,7 +1908,7 @@ class TestMethods(object): for kind in self.sort_kinds: assert_raises(TypeError, arr.sort, kind=kind) #gh-3879 - class Raiser(object): + class Raiser: def raises_anything(*args, **kwargs): raise TypeError("SOMETHING ERRORED") __eq__ = __ne__ = __lt__ = __gt__ = __ge__ = __le__ = raises_anything @@ -2144,6 +2152,8 @@ class TestMethods(object): msg = "Test real searchsorted with nans, side='r'" b = a.searchsorted(a, side='r') assert_equal(b, np.arange(1, 4), msg) + # check keyword arguments + a.searchsorted(v=1) # check double complex a = np.zeros(9, dtype=np.complex128) a.real += [0, 0, 1, 1, 0, 1, np.nan, np.nan, np.nan] @@ -2942,7 +2952,7 @@ class TestMethods(object): def test_dot_matmul_inner_array_casting_fails(self): - class A(object): + class A: def __array__(self, *args, **kwargs): raise NotImplementedError @@ -3303,12 +3313,12 @@ class TestMethods(object): e = np.array(['1+1j'], 'U') assert_raises(TypeError, complex, e) -class TestCequenceMethods(object): +class TestCequenceMethods: def test_array_contains(self): assert_(4.0 in np.arange(16.).reshape(4,4)) assert_(20.0 not in np.arange(16.).reshape(4,4)) -class TestBinop(object): +class TestBinop: def test_inplace(self): # test refcount 1 inplace conversion assert_array_almost_equal(np.array([0.5]) * np.array([1.0, 2.0]), @@ -3533,7 +3543,7 @@ class TestBinop(object): def test_ufunc_override_normalize_signature(self): # gh-5674 - class SomeClass(object): + class SomeClass: def __array_ufunc__(self, ufunc, method, *inputs, **kw): return kw @@ -3551,7 +3561,7 @@ class TestBinop(object): # Check that index is set appropriately, also if only an output # is passed on (latter is another regression tests for github bug 4753) # This also checks implicitly that 'out' is always a tuple. - class CheckIndex(object): + class CheckIndex: def __array_ufunc__(self, ufunc, method, *inputs, **kw): for i, a in enumerate(inputs): if a is self: @@ -3639,7 +3649,7 @@ class TestBinop(object): def test_pow_array_object_dtype(self): # test pow on arrays of object dtype - class SomeClass(object): + class SomeClass: def __init__(self, num=None): self.num = num @@ -3680,7 +3690,7 @@ class TestBinop(object): +tst -class TestTemporaryElide(object): +class TestTemporaryElide: # elision is only triggered on relatively large arrays def test_extension_incref_elide(self): @@ -3782,7 +3792,7 @@ class TestTemporaryElide(object): assert_equal(a, 1) -class TestCAPI(object): +class TestCAPI: def test_IsPythonScalar(self): from numpy.core._multiarray_tests import IsPythonScalar assert_(IsPythonScalar(b'foobar')) @@ -3792,7 +3802,7 @@ class TestCAPI(object): assert_(IsPythonScalar("a")) -class TestSubscripting(object): +class TestSubscripting: def test_test_zero_rank(self): x = np.array([1, 2, 3]) assert_(isinstance(x[0], np.int_)) @@ -3801,7 +3811,7 @@ class TestSubscripting(object): assert_(type(x[0, ...]) is np.ndarray) -class TestPickling(object): +class TestPickling: @pytest.mark.skipif(pickle.HIGHEST_PROTOCOL >= 5, reason=('this tests the error messages when trying to' 'protocol 5 although it is not available')) @@ -3958,7 +3968,7 @@ class TestPickling(object): assert_equal(original.dtype, new.dtype) -class TestFancyIndexing(object): +class TestFancyIndexing: def test_list(self): x = np.ones((1, 1)) x[:, [0]] = 2.0 @@ -4012,7 +4022,7 @@ class TestFancyIndexing(object): assert_array_equal(x, np.array([[1, 10, 3, 4], [5, 6, 7, 8]])) -class TestStringCompare(object): +class TestStringCompare: def test_string(self): g1 = np.array(["This", "is", "example"]) g2 = np.array(["This", "was", "example"]) @@ -4044,7 +4054,7 @@ class TestStringCompare(object): assert_array_equal(g1 > g2, [g1[i] > g2[i] for i in [0, 1, 2]]) -class TestArgmax(object): +class TestArgmax: nan_arr = [ ([0, 1, 2, 3, np.nan], 4), @@ -4179,7 +4189,7 @@ class TestArgmax(object): assert_equal(a.argmax(), 1) -class TestArgmin(object): +class TestArgmin: nan_arr = [ ([0, 1, 2, 3, np.nan], 4), @@ -4328,7 +4338,7 @@ class TestArgmin(object): assert_equal(a.argmin(), 1) -class TestMinMax(object): +class TestMinMax: def test_scalar(self): assert_raises(np.AxisError, np.amax, 1, 1) @@ -4354,14 +4364,14 @@ class TestMinMax(object): assert_equal(np.amax(a), a[3]) -class TestNewaxis(object): +class TestNewaxis: def test_basic(self): sk = np.array([0, -0.1, 0.1]) res = 250*sk[:, np.newaxis] assert_almost_equal(res.ravel(), 250*sk) -class TestClip(object): +class TestClip: def _check_range(self, x, cmin, cmax): assert_(np.all(x >= cmin)) assert_(np.all(x <= cmax)) @@ -4439,7 +4449,7 @@ class TestClip(object): assert_array_equal(result, expected) -class TestCompress(object): +class TestCompress: def test_axis(self): tgt = [[5, 6, 7, 8, 9]] arr = np.arange(10).reshape(2, 5) @@ -4462,7 +4472,7 @@ class TestCompress(object): assert_equal(out, 1) -class TestPutmask(object): +class TestPutmask: def tst_basic(self, x, T, mask, val): np.putmask(x, mask, val) assert_equal(x[mask], T(val)) @@ -4513,7 +4523,7 @@ class TestPutmask(object): assert_equal(x, np.array([True, True, True, True])) -class TestTake(object): +class TestTake: def tst_basic(self, x): ind = list(range(x.shape[0])) assert_array_equal(x.take(ind, axis=0), x) @@ -4566,7 +4576,7 @@ class TestTake(object): y = np.take(x, [1, 2, 3], out=x[2:5], mode='wrap') assert_equal(y, np.array([1, 2, 3])) -class TestLexsort(object): +class TestLexsort: @pytest.mark.parametrize('dtype',[ np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64, @@ -4621,7 +4631,7 @@ class TestLexsort(object): x = np.linspace(0., 1., 42*3).reshape(42, 3) assert_raises(np.AxisError, np.lexsort, x, axis=2) -class TestIO(object): +class TestIO: """Test tofile, fromfile, tobytes, and fromstring""" def setup(self): @@ -5011,7 +5021,7 @@ class TestIO(object): assert_array_equal(x, res) -class TestFromBuffer(object): +class TestFromBuffer: @pytest.mark.parametrize('byteorder', ['<', '>']) @pytest.mark.parametrize('dtype', [float, int, complex]) def test_basic(self, byteorder, dtype): @@ -5024,7 +5034,7 @@ class TestFromBuffer(object): assert_array_equal(np.frombuffer(b''), np.array([])) -class TestFlat(object): +class TestFlat: def setup(self): a0 = np.arange(20.0) a = a0.reshape(4, 5) @@ -5096,9 +5106,9 @@ class TestFlat(object): assert_(abs(sys.getrefcount(indtype) - rc_indtype) < 50) -class TestResize(object): +class TestResize: - @no_tracing + @_no_tracing def test_basic(self): x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) if IS_PYPY: @@ -5115,7 +5125,7 @@ class TestResize(object): assert_raises(ValueError, x.resize, (5, 1)) del y # avoid pyflakes unused variable warning. - @no_tracing + @_no_tracing def test_int_shape(self): x = np.eye(3) if IS_PYPY: @@ -5149,7 +5159,7 @@ class TestResize(object): assert_raises(TypeError, np.eye(3).resize, order=1) assert_raises(TypeError, np.eye(3).resize, refcheck='hi') - @no_tracing + @_no_tracing def test_freeform_shape(self): x = np.eye(3) if IS_PYPY: @@ -5158,7 +5168,7 @@ class TestResize(object): x.resize(3, 2, 1) assert_(x.shape == (3, 2, 1)) - @no_tracing + @_no_tracing def test_zeros_appended(self): x = np.eye(3) if IS_PYPY: @@ -5168,7 +5178,7 @@ class TestResize(object): assert_array_equal(x[0], np.eye(3)) assert_array_equal(x[1], np.zeros((3, 3))) - @no_tracing + @_no_tracing def test_obj_obj(self): # check memory is initialized on resize, gh-4857 a = np.ones(10, dtype=[('k', object, 2)]) @@ -5195,7 +5205,7 @@ class TestResize(object): del xref # avoid pyflakes unused variable warning. -class TestRecord(object): +class TestRecord: def test_field_rename(self): dt = np.dtype([('f', float), ('i', int)]) dt.names = ['p', 'q'] @@ -5357,7 +5367,7 @@ class TestRecord(object): v[:] = (4,5) assert_equal(a[0].item(), (4, 1, 5)) -class TestView(object): +class TestView: def test_basic(self): x = np.array([(1, 2, 3, 4), (5, 6, 7, 8)], dtype=[('r', np.int8), ('g', np.int8), @@ -5382,7 +5392,7 @@ def _std(a, **args): return a.std(**args) -class TestStats(object): +class TestStats: funcs = [_mean, _var, _std] @@ -5589,7 +5599,7 @@ class TestStats(object): res = dat.var(1) assert_(res.info == dat.info) -class TestVdot(object): +class TestVdot: def test_basic(self): dt_numeric = np.typecodes['AllFloat'] + np.typecodes['AllInteger'] dt_complex = np.typecodes['Complex'] @@ -5649,7 +5659,7 @@ class TestVdot(object): np.vdot(a.flatten(), b.flatten())) -class TestDot(object): +class TestDot: def setup(self): np.random.seed(128) self.A = np.random.rand(4, 2) @@ -5751,7 +5761,7 @@ class TestDot(object): assert_almost_equal(res, tgt, decimal=self.N) def test_vecobject(self): - class Vec(object): + class Vec: def __init__(self, sequence=None): if sequence is None: sequence = [] @@ -5920,7 +5930,7 @@ class TestDot(object): assert_dot_close(A_f_12, X_f_2, desired) -class MatmulCommon(object): +class MatmulCommon: """Common tests for '@' operator and numpy.matmul. """ @@ -6302,7 +6312,7 @@ if sys.version_info[:2] >= (3, 5): def test_array_priority_override(self): - class A(object): + class A: __array_priority__ = 1000 def __matmul__(self, other): @@ -6347,7 +6357,7 @@ if sys.version_info[:2] >= (3, 5): assert f.shape == (4, 5) -class TestInner(object): +class TestInner: def test_inner_type_mismatch(self): c = 1. @@ -6425,7 +6435,7 @@ class TestInner(object): assert_equal(np.inner(b, a).transpose(2,3,0,1), desired) -class TestAlen(object): +class TestAlen: def test_basic(self): with pytest.warns(DeprecationWarning): m = np.array([1, 2, 3]) @@ -6445,7 +6455,7 @@ class TestAlen(object): assert_equal(np.alen(5), 1) -class TestChoose(object): +class TestChoose: def setup(self): self.x = 2*np.ones((3,), dtype=int) self.y = 3*np.ones((3,), dtype=int) @@ -6465,8 +6475,17 @@ class TestChoose(object): A = np.choose(self.ind, (self.x, self.y2)) assert_equal(A, [[2, 2, 3], [2, 2, 3]]) + @pytest.mark.parametrize("ops", + [(1000, np.array([1], dtype=np.uint8)), + (-1, np.array([1], dtype=np.uint8)), + (1., np.float32(3)), + (1., np.array([3], dtype=np.float32))],) + def test_output_dtype(self, ops): + expected_dt = np.result_type(*ops) + assert(np.choose([0], ops).dtype == expected_dt) + -class TestRepeat(object): +class TestRepeat: def setup(self): self.m = np.array([1, 2, 3, 4, 5, 6]) self.m_rect = self.m.reshape((2, 3)) @@ -6508,7 +6527,7 @@ NEIGH_MODE = {'zero': 0, 'one': 1, 'constant': 2, 'circular': 3, 'mirror': 4} @pytest.mark.parametrize('dt', [float, Decimal], ids=['float', 'object']) -class TestNeighborhoodIter(object): +class TestNeighborhoodIter: # Simple, 2d tests def test_simple2d(self, dt): # Test zero and one padding for simple data type @@ -6587,7 +6606,7 @@ class TestNeighborhoodIter(object): # Test stacking neighborhood iterators -class TestStackedNeighborhoodIter(object): +class TestStackedNeighborhoodIter: # Simple, 1d test: stacking 2 constant-padded neigh iterators def test_simple_const(self): dt = np.float64 @@ -6737,7 +6756,7 @@ class TestStackedNeighborhoodIter(object): x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['circular']) assert_array_equal(l, r) -class TestWarnings(object): +class TestWarnings: def test_complex_warning(self): x = np.array([1, 2]) @@ -6749,7 +6768,7 @@ class TestWarnings(object): assert_equal(x, [1, 2]) -class TestMinScalarType(object): +class TestMinScalarType: def test_usigned_shortshort(self): dt = np.min_scalar_type(2**8-1) @@ -6780,7 +6799,7 @@ class TestMinScalarType(object): from numpy.core._internal import _dtype_from_pep3118 -class TestPEP3118Dtype(object): +class TestPEP3118Dtype: def _check(self, spec, wanted): dt = np.dtype(wanted) actual = _dtype_from_pep3118(spec) @@ -6887,7 +6906,7 @@ class TestPEP3118Dtype(object): self._check('i:f0:', [('f0', 'i')]) -class TestNewBufferProtocol(object): +class TestNewBufferProtocol: """ Test PEP3118 buffers """ def _check_roundtrip(self, obj): @@ -7284,7 +7303,7 @@ class TestNewBufferProtocol(object): assert_equal(arr['a'], 3) -class TestArrayAttributeDeletion(object): +class TestArrayAttributeDeletion: def test_multiarray_writable_attributes_deletion(self): # ticket #2046, should not seqfault, raise AttributeError @@ -7319,7 +7338,7 @@ class TestArrayAttributeDeletion(object): class TestArrayInterface(): - class Foo(object): + class Foo: def __init__(self, value): self.value = value self.iface = {'typestr': 'f8'} @@ -7364,7 +7383,7 @@ class TestArrayInterface(): assert_equal(pre_cnt, post_cnt) def test_interface_no_shape(): - class ArrayLike(object): + class ArrayLike: array = np.array(1) __array_interface__ = array.__array_interface__ assert_equal(np.array(ArrayLike()), 1) @@ -7386,7 +7405,7 @@ def test_array_interface_empty_shape(): interface1 = dict(arr.__array_interface__) interface1['shape'] = () - class DummyArray1(object): + class DummyArray1: __array_interface__ = interface1 # NOTE: Because Py2 str/Py3 bytes supports the buffer interface, setting @@ -7396,7 +7415,7 @@ def test_array_interface_empty_shape(): interface2 = dict(interface1) interface2['data'] = arr[0].tobytes() - class DummyArray2(object): + class DummyArray2: __array_interface__ = interface2 arr1 = np.asarray(DummyArray1()) @@ -7413,7 +7432,7 @@ def test_array_interface_offset(): interface['offset'] = 4 - class DummyArray(object): + class DummyArray: __array_interface__ = interface arr1 = np.asarray(DummyArray()) @@ -7435,7 +7454,7 @@ def test_scalar_element_deletion(): assert_raises(ValueError, a[0].__delitem__, 'x') -class TestMemEventHook(object): +class TestMemEventHook: def test_mem_seteventhook(self): # The actual tests are within the C code in # multiarray/_multiarray_tests.c.src @@ -7447,7 +7466,7 @@ class TestMemEventHook(object): break_cycles() _multiarray_tests.test_pydatamem_seteventhook_end() -class TestMapIter(object): +class TestMapIter: def test_mapiter(self): # The actual tests are within the C code in # multiarray/_multiarray_tests.c.src @@ -7469,7 +7488,7 @@ class TestMapIter(object): assert_equal(b, [100.1, 51., 6., 3., 4., 5.]) -class TestAsCArray(object): +class TestAsCArray: def test_1darray(self): array = np.arange(24, dtype=np.double) from_c = _multiarray_tests.test_as_c_array(array, 3) @@ -7486,7 +7505,7 @@ class TestAsCArray(object): assert_equal(array[1, 2, 3], from_c) -class TestConversion(object): +class TestConversion: def test_array_scalar_relational_operation(self): # All integer for dt1 in np.typecodes['AllInteger']: @@ -7534,7 +7553,7 @@ class TestConversion(object): assert_equal(bool(np.array([[42]])), True) assert_raises(ValueError, bool, np.array([1, 2])) - class NotConvertible(object): + class NotConvertible: def __bool__(self): raise NotImplementedError __nonzero__ = __bool__ # python 2 @@ -7555,8 +7574,8 @@ class TestConversion(object): # gh-9972 means that these aren't always the same int_funcs = (int, lambda x: x.__int__()) for int_func in int_funcs: + assert_equal(int_func(np.array(0)), 0) assert_equal(int_func(np.array([1])), 1) - assert_equal(int_func(np.array([0])), 0) assert_equal(int_func(np.array([[42]])), 42) assert_raises(TypeError, int_func, np.array([1, 2])) @@ -7571,7 +7590,7 @@ class TestConversion(object): assert_equal(3, int_func(np.array(HasTrunc()))) assert_equal(3, int_func(np.array([HasTrunc()]))) - class NotConvertible(object): + class NotConvertible: def __int__(self): raise NotImplementedError assert_raises(NotImplementedError, @@ -7580,7 +7599,7 @@ class TestConversion(object): int_func, np.array([NotConvertible()])) -class TestWhere(object): +class TestWhere: def test_basic(self): dts = [bool, np.int16, np.int32, np.int64, np.double, np.complex128, np.longdouble, np.clongdouble] @@ -7747,7 +7766,7 @@ class TestWhere(object): if not IS_PYPY: # sys.getsizeof() is not valid on PyPy - class TestSizeOf(object): + class TestSizeOf: def test_empty_array(self): x = np.array([]) @@ -7780,7 +7799,7 @@ if not IS_PYPY: d = np.ones(100) assert_(sys.getsizeof(d) < sys.getsizeof(d.reshape(100, 1, 1).copy())) - @no_tracing + @_no_tracing def test_resize(self): d = np.ones(100) old = sys.getsizeof(d) @@ -7794,7 +7813,7 @@ if not IS_PYPY: assert_raises(TypeError, d.__sizeof__, "a") -class TestHashing(object): +class TestHashing: def test_arrays_not_hashable(self): x = np.ones(3) @@ -7805,7 +7824,7 @@ class TestHashing(object): assert_(not isinstance(x, collections_abc.Hashable)) -class TestArrayPriority(object): +class TestArrayPriority: # This will go away when __array_priority__ is settled, meanwhile # it serves to check unintended changes. op = operator @@ -7832,7 +7851,7 @@ class TestArrayPriority(object): def __new__(cls, *args, **kwargs): return np.array(*args, **kwargs).view(cls) - class Other(object): + class Other: __array_priority__ = 1000. def _all(self, other): @@ -7891,7 +7910,7 @@ class TestArrayPriority(object): assert_(isinstance(f(b, a), self.Other), msg) -class TestBytestringArrayNonzero(object): +class TestBytestringArrayNonzero: def test_empty_bstring_array_is_falsey(self): assert_(not np.array([''], dtype=str)) @@ -7912,7 +7931,7 @@ class TestBytestringArrayNonzero(object): assert_(a) -class TestUnicodeArrayNonzero(object): +class TestUnicodeArrayNonzero: def test_empty_ustring_array_is_falsey(self): assert_(not np.array([''], dtype=np.unicode_)) @@ -7933,7 +7952,7 @@ class TestUnicodeArrayNonzero(object): assert_(a) -class TestFormat(object): +class TestFormat: def test_0d(self): a = np.array(np.pi) @@ -7958,7 +7977,7 @@ class TestFormat(object): from numpy.testing import IS_PYPY -class TestCTypes(object): +class TestCTypes: def test_ctypes_is_available(self): test_arr = np.array([[1, 2, 3], [4, 5, 6]]) @@ -8050,7 +8069,7 @@ class TestCTypes(object): assert_(arr_ref() is None, "unknowable whether ctypes pointer holds a reference") -class TestWritebackIfCopy(object): +class TestWritebackIfCopy: # all these tests use the WRITEBACKIFCOPY mechanism def test_argmax_with_out(self): mat = np.eye(5) @@ -8163,7 +8182,7 @@ class TestWritebackIfCopy(object): assert_equal(arr, orig) -class TestArange(object): +class TestArange: def test_infinite(self): assert_raises_regex( ValueError, "size exceeded", @@ -8185,7 +8204,7 @@ class TestArange(object): assert_raises(ZeroDivisionError, np.arange, 0.0, 0.0, 0.0) -class TestArrayFinalize(object): +class TestArrayFinalize: """ Tests __array_finalize__ """ def test_receives_base(self): @@ -8205,7 +8224,7 @@ class TestArrayFinalize(object): raise Exception(self) # a plain object can't be weakref'd - class Dummy(object): pass + class Dummy: pass # get a weak reference to an object within an array obj_arr = np.array(Dummy()) @@ -8214,9 +8233,6 @@ class TestArrayFinalize(object): # get an array that crashed in __array_finalize__ with assert_raises(Exception) as e: obj_arr.view(RaisesInFinalize) - if sys.version_info.major == 2: - # prevent an extra reference being kept - sys.exc_clear() obj_subarray = e.exception.args[0] del e @@ -8245,7 +8261,7 @@ def test_equal_override(): # gh-9153: ndarray.__eq__ uses special logic for structured arrays, which # did not respect overrides with __array_priority__ or __array_ufunc__. # The PR fixed this for __array_priority__ and __array_ufunc__ = None. - class MyAlwaysEqual(object): + class MyAlwaysEqual: def __eq__(self, other): return "eq" @@ -8343,7 +8359,7 @@ def test_uintalignment_and_alignment(): dst = np.zeros((2,2), dtype='c8') dst[:,1] = src[:,1] # assert in lowlevel_strided_loops fails? -class TestAlignment(object): +class TestAlignment: # adapted from scipy._lib.tests.test__util.test__aligned_zeros # Checks that unusual memory alignments don't trip up numpy. # In particular, check RELAXED_STRIDES don't trip alignment assertions in diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index daec9ce6d..24272bb0d 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import pytest @@ -2188,7 +2186,7 @@ def test_iter_no_broadcast(): [['readonly'], ['readonly'], ['readonly', 'no_broadcast']]) -class TestIterNested(object): +class TestIterNested: def test_basic(self): # Test nested iteration basic usage diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index ffebdf648..934f0a2fd 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import warnings import itertools @@ -17,7 +15,7 @@ from numpy.testing import ( ) -class TestResize(object): +class TestResize: def test_copies(self): A = np.array([[1, 2], [3, 4]]) Ar1 = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) @@ -49,7 +47,7 @@ class TestResize(object): assert_equal(A.dtype, Ar.dtype) -class TestNonarrayArgs(object): +class TestNonarrayArgs: # check that non-array arguments to functions wrap them in arrays def test_choose(self): choices = [[0, 1, 2], @@ -220,7 +218,7 @@ class TestNonarrayArgs(object): B[0] = 1j assert_almost_equal(np.var(B), 0.25) -class TestIsscalar(object): +class TestIsscalar: def test_isscalar(self): assert_(np.isscalar(3.1)) assert_(np.isscalar(np.int16(12345))) @@ -236,7 +234,7 @@ class TestIsscalar(object): assert_(np.isscalar(Number())) -class TestBoolScalar(object): +class TestBoolScalar: def test_logical(self): f = np.False_ t = np.True_ @@ -269,7 +267,7 @@ class TestBoolScalar(object): assert_((f ^ f) is f) -class TestBoolArray(object): +class TestBoolArray: def setup(self): # offset for simd tests self.t = np.array([True] * 41, dtype=bool)[1::] @@ -356,7 +354,7 @@ class TestBoolArray(object): assert_array_equal(self.im ^ False, self.im) -class TestBoolCmp(object): +class TestBoolCmp: def setup(self): self.f = np.ones(256, dtype=np.float32) self.ef = np.ones(self.f.size, dtype=bool) @@ -456,7 +454,7 @@ class TestBoolCmp(object): assert_array_equal(np.signbit(self.signd[i:]), self.ed[i:]) -class TestSeterr(object): +class TestSeterr: def test_default(self): err = np.geterr() assert_equal(err, @@ -537,7 +535,7 @@ class TestSeterr(object): np.seterrobj(olderrobj) -class TestFloatExceptions(object): +class TestFloatExceptions: def assert_raises_fpe(self, fpeerr, flop, x, y): ftype = type(x) try: @@ -633,7 +631,7 @@ class TestFloatExceptions(object): assert_("underflow" in str(w[-1].message)) -class TestTypes(object): +class TestTypes: def check_promotion_cases(self, promote_func): # tests that the scalars get coerced correctly. b = np.bool_(0) @@ -954,7 +952,7 @@ class NIterError(Exception): pass -class TestFromiter(object): +class TestFromiter: def makegen(self): for x in range(24): yield x**2 @@ -1005,7 +1003,7 @@ class TestFromiter(object): self.load_data(count, eindex), dtype=int, count=count) -class TestNonzero(object): +class TestNonzero: def test_nonzero_trivial(self): assert_equal(np.count_nonzero(np.array([])), 0) assert_equal(np.count_nonzero(np.array([], dtype='?')), 0) @@ -1288,7 +1286,7 @@ class TestNonzero(object): assert_raises(ValueError, np.nonzero, a) -class TestIndex(object): +class TestIndex: def test_boolean(self): a = rand(3, 5, 8) V = rand(5, 8) @@ -1305,7 +1303,7 @@ class TestIndex(object): assert_equal(c.dtype, np.dtype('int32')) -class TestBinaryRepr(object): +class TestBinaryRepr: def test_zero(self): assert_equal(np.binary_repr(0), '0') @@ -1347,7 +1345,7 @@ class TestBinaryRepr(object): '11' + '0'*62) -class TestBaseRepr(object): +class TestBaseRepr: def test_base3(self): assert_equal(np.base_repr(3**5, 3), '100000') @@ -1369,7 +1367,7 @@ class TestBaseRepr(object): np.base_repr(1, 37) -class TestArrayComparisons(object): +class TestArrayComparisons: def test_array_equal(self): res = np.array_equal(np.array([1, 2]), np.array([1, 2])) assert_(res) @@ -1448,7 +1446,7 @@ def assert_array_strict_equal(x, y): assert_(x.dtype.isnative == y.dtype.isnative) -class TestClip(object): +class TestClip: def setup(self): self.nr = 5 self.nc = 3 @@ -2022,7 +2020,7 @@ class TestClip(object): assert_equal(actual, expected) -class TestAllclose(object): +class TestAllclose: rtol = 1e-5 atol = 1e-8 @@ -2107,7 +2105,7 @@ class TestAllclose(object): assert_(type(np.allclose(a, a)) is bool) -class TestIsclose(object): +class TestIsclose: rtol = 1e-5 atol = 1e-8 @@ -2245,7 +2243,7 @@ class TestIsclose(object): assert_(type(np.isclose(0, np.inf)) is np.bool_) -class TestStdVar(object): +class TestStdVar: def setup(self): self.A = np.array([1, -1, 1, -1]) self.real_var = 1 @@ -2284,7 +2282,7 @@ class TestStdVar(object): assert_array_equal(r, out) -class TestStdVarComplex(object): +class TestStdVarComplex: def test_basic(self): A = np.array([1, 1.j, -1, -1.j]) real_var = 1 @@ -2296,7 +2294,7 @@ class TestStdVarComplex(object): assert_equal(np.std(1j), 0) -class TestCreationFuncs(object): +class TestCreationFuncs: # Test ones, zeros, empty and full. def setup(self): @@ -2367,7 +2365,7 @@ class TestCreationFuncs(object): assert_(sys.getrefcount(dim) == beg) -class TestLikeFuncs(object): +class TestLikeFuncs: '''Test ones_like, zeros_like, empty_like and full_like''' def setup(self): @@ -2517,7 +2515,7 @@ class TestLikeFuncs(object): self.check_like_function(np.full_like, np.inf, True) -class TestCorrelate(object): +class TestCorrelate: def _setup(self, dt): self.x = np.array([1, 2, 3, 4, 5], dtype=dt) self.xs = np.arange(1, 20)[::3] @@ -2573,7 +2571,7 @@ class TestCorrelate(object): with pytest.raises(ValueError): np.correlate(np.ones(1000), np.array([]), mode='full') -class TestConvolve(object): +class TestConvolve: def test_object(self): d = [1.] * 100 k = [1.] * 3 @@ -2587,7 +2585,7 @@ class TestConvolve(object): assert_array_equal(k, np.ones(3)) -class TestArgwhere(object): +class TestArgwhere: @pytest.mark.parametrize('nd', [0, 1, 2]) def test_nd(self, nd): @@ -2624,7 +2622,7 @@ class TestArgwhere(object): assert_equal(np.argwhere([4, 0, 2, 1, 3]), [[0], [2], [3], [4]]) -class TestStringFunction(object): +class TestStringFunction: def test_set_string_function(self): a = np.array([1]) @@ -2639,7 +2637,7 @@ class TestStringFunction(object): assert_equal(str(a), "[1]") -class TestRoll(object): +class TestRoll: def test_roll1d(self): x = np.arange(10) xr = np.roll(x, 2) @@ -2697,7 +2695,7 @@ class TestRoll(object): assert_equal(np.roll(x, 1), np.array([])) -class TestRollaxis(object): +class TestRollaxis: # expected shape indexed by (axis, start) for array of # shape (1, 2, 3, 4) @@ -2759,7 +2757,7 @@ class TestRollaxis(object): assert_(not res.flags['OWNDATA']) -class TestMoveaxis(object): +class TestMoveaxis: def test_move_to_end(self): x = np.random.randn(5, 6, 7) for source, expected in [(0, (6, 7, 5)), @@ -2833,7 +2831,7 @@ class TestMoveaxis(object): assert_(isinstance(result, np.ndarray)) -class TestCross(object): +class TestCross: def test_2x2(self): u = [1, 2] v = [3, 4] @@ -2922,7 +2920,7 @@ def test_outer_out_param(): assert_equal(np.outer(arr2, arr3, out2), out2) -class TestIndices(object): +class TestIndices: def test_simple(self): [x, y] = np.indices((4, 3)) @@ -2963,7 +2961,7 @@ class TestIndices(object): assert_(arr.dtype == dtype) -class TestRequire(object): +class TestRequire: flag_names = ['C', 'C_CONTIGUOUS', 'CONTIGUOUS', 'F', 'F_CONTIGUOUS', 'FORTRAN', 'A', 'ALIGNED', @@ -3037,7 +3035,7 @@ class TestRequire(object): self.set_and_check_flag(flag, None, a) -class TestBroadcast(object): +class TestBroadcast: def test_broadcast_in_args(self): # gh-5881 arrs = [np.empty((6, 7)), np.empty((5, 6, 1)), np.empty((7,)), @@ -3088,7 +3086,7 @@ class TestBroadcast(object): assert_raises(ValueError, np.broadcast, 1, **{'x': 1}) -class TestKeepdims(object): +class TestKeepdims: class sub_array(np.ndarray): def sum(self, axis=None, dtype=None, out=None): @@ -3100,7 +3098,7 @@ class TestKeepdims(object): assert_raises(TypeError, np.sum, x, keepdims=True) -class TestTensordot(object): +class TestTensordot: def test_zero_dimension(self): # Test resolution to issue #5663 diff --git a/numpy/core/tests/test_numerictypes.py b/numpy/core/tests/test_numerictypes.py index 387740e35..7105154ed 100644 --- a/numpy/core/tests/test_numerictypes.py +++ b/numpy/core/tests/test_numerictypes.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import itertools @@ -100,7 +98,7 @@ def normalize_descr(descr): # Creation tests ############################################################ -class CreateZeros(object): +class CreateZeros: """Check the creation of heterogeneous arrays zero-valued""" def test_zeros0D(self): @@ -143,7 +141,7 @@ class TestCreateZerosNested(CreateZeros): _descr = Ndescr -class CreateValues(object): +class CreateValues: """Check the creation of heterogeneous arrays with values""" def test_tuple(self): @@ -203,7 +201,7 @@ class TestCreateValuesNestedMultiple(CreateValues): # Reading tests ############################################################ -class ReadValuesPlain(object): +class ReadValuesPlain: """Check the reading of values in heterogeneous arrays (plain)""" def test_access_fields(self): @@ -235,7 +233,7 @@ class TestReadValuesPlainMultiple(ReadValuesPlain): multiple_rows = 1 _buffer = PbufferT -class ReadValuesNested(object): +class ReadValuesNested: """Check the reading of values in heterogeneous arrays (nested)""" def test_access_top_fields(self): @@ -333,14 +331,14 @@ class TestReadValuesNestedMultiple(ReadValuesNested): multiple_rows = True _buffer = NbufferT -class TestEmptyField(object): +class TestEmptyField: def test_assign(self): a = np.arange(10, dtype=np.float32) a.dtype = [("int", "<0i4"), ("float", "<2f4")] assert_(a['int'].shape == (5, 0)) assert_(a['float'].shape == (5, 2)) -class TestCommonType(object): +class TestCommonType: def test_scalar_loses1(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['f8']) assert_(res == 'f4') @@ -361,7 +359,7 @@ class TestCommonType(object): res = np.find_common_type(['u8', 'i8', 'i8'], ['f8']) assert_(res == 'f8') -class TestMultipleFields(object): +class TestMultipleFields: def setup(self): self.ary = np.array([(1, 2, 3, 4), (5, 6, 7, 8)], dtype='i4,f4,i2,c8') @@ -376,7 +374,7 @@ class TestMultipleFields(object): assert_(res == [(1, 3), (5, 7)]) -class TestIsSubDType(object): +class TestIsSubDType: # scalar types can be promoted into dtypes wrappers = [np.dtype, lambda x: x] @@ -407,18 +405,18 @@ class TestIsSubDType(object): assert_(not np.issubdtype(w1(np.float64), w2(np.float32))) -class TestSctypeDict(object): +class TestSctypeDict: def test_longdouble(self): assert_(np.sctypeDict['f8'] is not np.longdouble) assert_(np.sctypeDict['c16'] is not np.clongdouble) -class TestBitName(object): +class TestBitName: def test_abstract(self): assert_raises(ValueError, np.core.numerictypes.bitname, np.floating) -class TestMaximumSctype(object): +class TestMaximumSctype: # note that parametrizing with sctype['int'] and similar would skip types # with the same size (gh-11923) @@ -444,7 +442,7 @@ class TestMaximumSctype(object): assert_equal(np.maximum_sctype(t), t) -class Test_sctype2char(object): +class Test_sctype2char: # This function is old enough that we're really just documenting the quirks # at this point. @@ -492,7 +490,7 @@ def test_issctype(rep, expected): @pytest.mark.skipif(sys.flags.optimize > 1, reason="no docstrings present to inspect when PYTHONOPTIMIZE/Py_OptimizeFlag > 1") @pytest.mark.xfail(IS_PYPY, reason="PyPy does not modify tp_doc") -class TestDocStrings(object): +class TestDocStrings: def test_platform_dependent_aliases(self): if np.int64 is np.int_: assert_('int64' in np.int_.__doc__) diff --git a/numpy/core/tests/test_overrides.py b/numpy/core/tests/test_overrides.py index 63b0e4539..7e73d8c03 100644 --- a/numpy/core/tests/test_overrides.py +++ b/numpy/core/tests/test_overrides.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import inspect import sys from unittest import mock @@ -36,7 +34,7 @@ def dispatched_two_arg(array1, array2): return 'original' -class TestGetImplementingArgs(object): +class TestGetImplementingArgs: def test_ndarray(self): array = np.array(1) @@ -77,7 +75,7 @@ class TestGetImplementingArgs(object): def test_ndarray_and_duck_array(self): - class Other(object): + class Other: __array_function__ = _return_not_implemented array = np.array(1) @@ -94,7 +92,7 @@ class TestGetImplementingArgs(object): class OverrideSub(np.ndarray): __array_function__ = _return_not_implemented - class Other(object): + class Other: __array_function__ = _return_not_implemented array = np.array(1) @@ -108,7 +106,7 @@ class TestGetImplementingArgs(object): def test_many_duck_arrays(self): - class A(object): + class A: __array_function__ = _return_not_implemented class B(A): @@ -117,7 +115,7 @@ class TestGetImplementingArgs(object): class C(A): __array_function__ = _return_not_implemented - class D(object): + class D: __array_function__ = _return_not_implemented a = A() @@ -147,12 +145,12 @@ class TestGetImplementingArgs(object): _get_implementing_args(relevant_args) -class TestNDArrayArrayFunction(object): +class TestNDArrayArrayFunction: @requires_array_function def test_method(self): - class Other(object): + class Other: __array_function__ = _return_not_implemented class NoOverrideSub(np.ndarray): @@ -209,7 +207,7 @@ class TestNDArrayArrayFunction(object): @requires_array_function -class TestArrayFunctionDispatch(object): +class TestArrayFunctionDispatch: def test_pickle(self): for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): @@ -224,7 +222,7 @@ class TestArrayFunctionDispatch(object): def test_interface(self): - class MyArray(object): + class MyArray: def __array_function__(self, func, types, args, kwargs): return (self, func, types, args, kwargs) @@ -239,7 +237,7 @@ class TestArrayFunctionDispatch(object): def test_not_implemented(self): - class MyArray(object): + class MyArray: def __array_function__(self, func, types, args, kwargs): return NotImplemented @@ -249,7 +247,7 @@ class TestArrayFunctionDispatch(object): @requires_array_function -class TestVerifyMatchingSignatures(object): +class TestVerifyMatchingSignatures: def test_verify_matching_signatures(self): @@ -283,7 +281,7 @@ def _new_duck_type_and_implements(): """Create a duck array type and implements functions.""" HANDLED_FUNCTIONS = {} - class MyArray(object): + class MyArray: def __array_function__(self, func, types, args, kwargs): if func not in HANDLED_FUNCTIONS: return NotImplemented @@ -302,7 +300,7 @@ def _new_duck_type_and_implements(): @requires_array_function -class TestArrayFunctionImplementation(object): +class TestArrayFunctionImplementation: def test_one_arg(self): MyArray, implements = _new_duck_type_and_implements() @@ -355,7 +353,7 @@ class TestArrayFunctionImplementation(object): func(MyArray()) -class TestNDArrayMethods(object): +class TestNDArrayMethods: def test_repr(self): # gh-12162: should still be defined even if __array_function__ doesn't @@ -370,7 +368,7 @@ class TestNDArrayMethods(object): assert_equal(str(array), '1') -class TestNumPyFunctions(object): +class TestNumPyFunctions: def test_set_module(self): assert_equal(np.sum.__module__, 'numpy') diff --git a/numpy/core/tests/test_print.py b/numpy/core/tests/test_print.py index c5c091e13..36d652a41 100644 --- a/numpy/core/tests/test_print.py +++ b/numpy/core/tests/test_print.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import pytest diff --git a/numpy/core/tests/test_protocols.py b/numpy/core/tests/test_protocols.py new file mode 100644 index 000000000..55a2bcf72 --- /dev/null +++ b/numpy/core/tests/test_protocols.py @@ -0,0 +1,44 @@ +import pytest +import warnings +import numpy as np + + +@pytest.mark.filterwarnings("error") +def test_getattr_warning(): + # issue gh-14735: make sure we clear only getattr errors, and let warnings + # through + class Wrapper: + def __init__(self, array): + self.array = array + + def __len__(self): + return len(self.array) + + def __getitem__(self, item): + return type(self)(self.array[item]) + + def __getattr__(self, name): + if name.startswith("__array_"): + warnings.warn("object got converted", UserWarning, stacklevel=1) + + return getattr(self.array, name) + + def __repr__(self): + return "<Wrapper({self.array})>".format(self=self) + + array = Wrapper(np.arange(10)) + with pytest.raises(UserWarning, match="object got converted"): + np.asarray(array) + + +def test_array_called(): + class Wrapper: + val = '0' * 100 + def __array__(self, result=None): + return np.array([self.val], dtype=object) + + + wrapped = Wrapper() + arr = np.array(wrapped, dtype=str) + assert arr.dtype == 'U100' + assert arr[0] == Wrapper.val diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index 0f88f99e0..4b4bd2729 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys try: # Accessing collections abstract classes from collections @@ -20,7 +18,7 @@ from numpy.testing import ( from numpy.compat import pickle -class TestFromrecords(object): +class TestFromrecords: def test_fromrecords(self): r = np.rec.fromrecords([[456, 'dbe', 1.2], [2, 'de', 1.3]], names='col1,col2,col3') @@ -326,7 +324,7 @@ class TestFromrecords(object): @pytest.mark.skipif(Path is None, reason="No pathlib.Path") -class TestPathUsage(object): +class TestPathUsage: # Test that pathlib.Path can be used def test_tofile_fromfile(self): with temppath(suffix='.bin') as path: @@ -342,7 +340,7 @@ class TestPathUsage(object): assert_array_equal(x, a) -class TestRecord(object): +class TestRecord: def setup(self): self.data = np.rec.fromrecords([(1, 2, 3), (4, 5, 6)], dtype=[("col1", "<i4"), diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index f2f9c1457..c77c11d41 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import copy import sys import gc @@ -24,7 +22,7 @@ try: except NameError: RecursionError = RuntimeError # python < 3.5 -class TestRegression(object): +class TestRegression: def test_invalid_round(self): # Ticket #3 v = 4.7599999999999998 @@ -428,7 +426,7 @@ class TestRegression(object): def test_lexsort_invalid_sequence(self): # Issue gh-4123 - class BuggySequence(object): + class BuggySequence: def __len__(self): return 4 @@ -1041,7 +1039,7 @@ class TestRegression(object): def test_mem_custom_float_to_array(self): # Ticket 702 - class MyFloat(object): + class MyFloat: def __float__(self): return 1.0 @@ -1050,7 +1048,7 @@ class TestRegression(object): def test_object_array_refcount_self_assign(self): # Ticket #711 - class VictimObject(object): + class VictimObject: deleted = False def __del__(self): @@ -2233,7 +2231,7 @@ class TestRegression(object): import operator as op # dummy class where __array__ throws exception - class Foo(object): + class Foo: __array_priority__ = 1002 def __array__(self, *args, **kwargs): @@ -2482,7 +2480,7 @@ class TestRegression(object): assert_equal(pickle.loads(dumped), arr) def test_bad_array_interface(self): - class T(object): + class T: __array_interface__ = {} np.array([T()]) diff --git a/numpy/core/tests/test_scalar_ctors.py b/numpy/core/tests/test_scalar_ctors.py index b21bc9dad..ae29d0605 100644 --- a/numpy/core/tests/test_scalar_ctors.py +++ b/numpy/core/tests/test_scalar_ctors.py @@ -1,8 +1,6 @@ """ Test the scalar constructors, which also do type-coercion """ -from __future__ import division, absolute_import, print_function - import sys import platform import pytest @@ -12,7 +10,7 @@ from numpy.testing import ( assert_equal, assert_almost_equal, assert_raises, assert_warns, ) -class TestFromString(object): +class TestFromString: def test_floating(self): # Ticket #640, floats from string fsingle = np.single('1.234') @@ -56,7 +54,7 @@ class TestFromString(object): assert_equal(255, np.intp('0xFF', 16)) -class TestFromInt(object): +class TestFromInt: def test_intp(self): # Ticket #99 assert_equal(1024, np.intp(1024)) diff --git a/numpy/core/tests/test_scalar_methods.py b/numpy/core/tests/test_scalar_methods.py index 93434dd1b..c9de3e402 100644 --- a/numpy/core/tests/test_scalar_methods.py +++ b/numpy/core/tests/test_scalar_methods.py @@ -1,8 +1,6 @@ """ Test the scalar constructors, which also do type-coercion """ -from __future__ import division, absolute_import, print_function - import os import fractions import platform @@ -16,7 +14,7 @@ from numpy.testing import ( dec ) -class TestAsIntegerRatio(object): +class TestAsIntegerRatio: # derived in part from the cpython test "test_floatasratio" @pytest.mark.parametrize("ftype", [ diff --git a/numpy/core/tests/test_scalarbuffer.py b/numpy/core/tests/test_scalarbuffer.py index 3ded7eecd..85673e3ab 100644 --- a/numpy/core/tests/test_scalarbuffer.py +++ b/numpy/core/tests/test_scalarbuffer.py @@ -33,7 +33,7 @@ scalars_only, codes_only = zip(*scalars_and_codes) @pytest.mark.skipif(sys.version_info.major < 3, reason="Python 2 scalars lack a buffer interface") -class TestScalarPEP3118(object): +class TestScalarPEP3118: @pytest.mark.parametrize('scalar', scalars_only, ids=codes_only) def test_scalar_match_array(self, scalar): diff --git a/numpy/core/tests/test_scalarinherit.py b/numpy/core/tests/test_scalarinherit.py index 6a5c4fde9..af3669d73 100644 --- a/numpy/core/tests/test_scalarinherit.py +++ b/numpy/core/tests/test_scalarinherit.py @@ -2,13 +2,11 @@ """ Test printing of scalar types. """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_ -class A(object): +class A: pass class B(A, np.float64): pass @@ -23,7 +21,7 @@ class B0(np.float64, A): class C0(B0): pass -class TestInherit(object): +class TestInherit: def test_init(self): x = B(1.0) assert_(str(x) == '1.0') @@ -39,7 +37,7 @@ class TestInherit(object): assert_(str(y) == '2.0') -class TestCharacter(object): +class TestCharacter: def test_char_radd(self): # GH issue 9620, reached gentype_add and raise TypeError np_s = np.string_('abc') diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index c84380cd9..789ef4acd 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import warnings import itertools @@ -25,7 +23,7 @@ complex_floating_types = np.complexfloating.__subclasses__() # This compares scalarmath against ufuncs. -class TestTypes(object): +class TestTypes: def test_types(self): for atype in types: a = atype(1) @@ -64,7 +62,7 @@ class TestTypes(object): np.add(1, 1) -class TestBaseMath(object): +class TestBaseMath: def test_blocked(self): # test alignments offsets for simd instructions # alignments for vz + 2 * (vs - 1) + 1 @@ -110,7 +108,7 @@ class TestBaseMath(object): np.add(d, np.ones_like(d)) -class TestPower(object): +class TestPower: def test_small_types(self): for t in [np.int8, np.int16, np.float16]: a = t(3) @@ -202,7 +200,7 @@ def _signs(dt): return (+1, -1) -class TestModulus(object): +class TestModulus: def test_modulus_basic(self): dt = np.typecodes['AllInteger'] + np.typecodes['Float'] @@ -304,7 +302,7 @@ class TestModulus(object): a //= b -class TestComplexDivision(object): +class TestComplexDivision: def test_zero_division(self): with np.errstate(all="ignore"): for t in [np.complex64, np.complex128]: @@ -376,7 +374,7 @@ class TestComplexDivision(object): assert_equal(result.imag, ex[1]) -class TestConversion(object): +class TestConversion: def test_int_from_long(self): l = [1e6, 1e12, 1e18, -1e6, -1e12, -1e18] li = [10**6, 10**12, 10**18, -10**6, -10**12, -10**18] @@ -512,7 +510,7 @@ class TestConversion(object): assert_(np.equal(np.datetime64('NaT'), None)) -#class TestRepr(object): +#class TestRepr: # def test_repr(self): # for t in types: # val = t(1197346475.0137341) @@ -521,7 +519,7 @@ class TestConversion(object): # assert_equal( val, val2 ) -class TestRepr(object): +class TestRepr: def _test_type_repr(self, t): finfo = np.finfo(t) last_fraction_bit_idx = finfo.nexp + finfo.nmant @@ -556,7 +554,7 @@ class TestRepr(object): if not IS_PYPY: # sys.getsizeof() is not valid on PyPy - class TestSizeOf(object): + class TestSizeOf: def test_equal_nbytes(self): for type in types: @@ -568,7 +566,7 @@ if not IS_PYPY: assert_raises(TypeError, d.__sizeof__, "a") -class TestMultiply(object): +class TestMultiply: def test_seq_repeat(self): # Test that basic sequences get repeated when multiplied with # numpy integers. And errors are raised when multiplied with others. @@ -605,7 +603,7 @@ class TestMultiply(object): # Test that an array-like which does not know how to be multiplied # does not attempt sequence repeat (raise TypeError). # See also gh-7428. - class ArrayLike(object): + class ArrayLike: def __init__(self, arr): self.arr = arr def __array__(self): @@ -619,7 +617,7 @@ class TestMultiply(object): assert_array_equal(np.int_(3) * arr_like, np.full(3, 3)) -class TestNegative(object): +class TestNegative: def test_exceptions(self): a = np.ones((), dtype=np.bool_)[()] assert_raises(TypeError, operator.neg, a) @@ -633,7 +631,7 @@ class TestNegative(object): assert_equal(operator.neg(a) + a, 0) -class TestSubtract(object): +class TestSubtract: def test_exceptions(self): a = np.ones((), dtype=np.bool_)[()] assert_raises(TypeError, operator.sub, a, a) @@ -647,7 +645,7 @@ class TestSubtract(object): assert_equal(operator.sub(a, a), 0) -class TestAbs(object): +class TestAbs: def _test_abs_func(self, absfunc): for tp in floating_types + complex_floating_types: x = tp(-1.5) @@ -676,7 +674,7 @@ class TestAbs(object): self._test_abs_func(np.abs) -class TestBitShifts(object): +class TestBitShifts: @pytest.mark.parametrize('type_code', np.typecodes['AllInteger']) @pytest.mark.parametrize('op', diff --git a/numpy/core/tests/test_scalarprint.py b/numpy/core/tests/test_scalarprint.py index 86b0ca199..225b8295f 100644 --- a/numpy/core/tests/test_scalarprint.py +++ b/numpy/core/tests/test_scalarprint.py @@ -2,8 +2,6 @@ """ Test printing of scalar types. """ -from __future__ import division, absolute_import, print_function - import code, sys import platform import pytest @@ -12,7 +10,7 @@ from tempfile import TemporaryFile import numpy as np from numpy.testing import assert_, assert_equal, suppress_warnings -class TestRealScalars(object): +class TestRealScalars: def test_str(self): svals = [0.0, -0.0, 1, -1, np.inf, -np.inf, np.nan] styps = [np.float16, np.float32, np.float64, np.longdouble] diff --git a/numpy/core/tests/test_shape_base.py b/numpy/core/tests/test_shape_base.py index 53d272fc5..d2f5287a6 100644 --- a/numpy/core/tests/test_shape_base.py +++ b/numpy/core/tests/test_shape_base.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest import sys import numpy as np @@ -16,7 +14,7 @@ from numpy.testing import ( from numpy.compat import long -class TestAtleast1d(object): +class TestAtleast1d: def test_0D_array(self): a = array(1) b = array(2) @@ -57,7 +55,7 @@ class TestAtleast1d(object): assert_(atleast_1d([[2, 3], [4, 5]]).shape == (2, 2)) -class TestAtleast2d(object): +class TestAtleast2d: def test_0D_array(self): a = array(1) b = array(2) @@ -96,7 +94,7 @@ class TestAtleast2d(object): assert_(atleast_2d([[[3, 1], [4, 5]], [[3, 5], [1, 2]]]).shape == (2, 2, 2)) -class TestAtleast3d(object): +class TestAtleast3d: def test_0D_array(self): a = array(1) b = array(2) @@ -128,7 +126,7 @@ class TestAtleast3d(object): assert_array_equal(res, desired) -class TestHstack(object): +class TestHstack: def test_non_iterable(self): assert_raises(TypeError, hstack, 1) @@ -165,7 +163,7 @@ class TestHstack(object): hstack(map(lambda x: x, np.ones((3, 2)))) -class TestVstack(object): +class TestVstack: def test_non_iterable(self): assert_raises(TypeError, vstack, 1) @@ -205,7 +203,7 @@ class TestVstack(object): vstack((np.arange(3) for _ in range(2))) -class TestConcatenate(object): +class TestConcatenate: def test_returns_copy(self): a = np.eye(3) b = np.concatenate([a]) @@ -409,7 +407,7 @@ def test_stack(): assert_array_equal(result, np.array([0, 1, 2])) -class TestBlock(object): +class TestBlock: @pytest.fixture(params=['block', 'force_concatenate', 'force_slicing']) def block(self, request): # blocking small arrays and large arrays go through different paths. @@ -707,7 +705,7 @@ class TestBlock(object): def test_block_dispatcher(): - class ArrayLike(object): + class ArrayLike: pass a = ArrayLike() b = ArrayLike() diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 7109de776..01e3c5087 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import warnings import itertools @@ -23,7 +21,7 @@ UNARY_UFUNCS = [obj for obj in np.core.umath.__dict__.values() UNARY_OBJECT_UFUNCS = [uf for uf in UNARY_UFUNCS if "O->O" in uf.types] -class TestUfuncKwargs(object): +class TestUfuncKwargs: def test_kwarg_exact(self): assert_raises(TypeError, np.add, 1, 2, castingx='safe') assert_raises(TypeError, np.add, 1, 2, dtypex=int) @@ -49,7 +47,7 @@ class TestUfuncKwargs(object): assert_raises(TypeError, np.add, 1, 2, extobj=[4096], parrot=True) -class TestUfuncGenericLoops(object): +class TestUfuncGenericLoops: """Test generic loops. The loops to be tested are: @@ -118,7 +116,7 @@ class TestUfuncGenericLoops(object): assert_equal(ys.dtype, output_dtype) # class to use in testing object method loops - class foo(object): + class foo: def conjugate(self): return np.bool_(1) @@ -179,7 +177,7 @@ class TestUfuncGenericLoops(object): assert_array_equal(res_num.astype("O"), res_obj) -class TestUfunc(object): +class TestUfunc: def test_pickle(self): for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): assert_(pickle.loads(pickle.dumps(np.sin, @@ -1123,7 +1121,7 @@ class TestUfunc(object): assert_equal(np.logical_and.reduce(a), None) def test_object_comparison(self): - class HasComparisons(object): + class HasComparisons: def __eq__(self, other): return '==' @@ -1592,7 +1590,7 @@ class TestUfunc(object): def test_custom_array_like(self): - class MyThing(object): + class MyThing: __array_priority__ = 1000 rmul_count = 0 @@ -1888,6 +1886,23 @@ class TestUfunc(object): assert_equal(y_base[1,:], y_base_copy[1,:]) assert_equal(y_base[3,:], y_base_copy[3,:]) + @pytest.mark.parametrize('output_shape', + [(), (1,), (1, 1), (1, 3), (4, 3)]) + @pytest.mark.parametrize('f_reduce', [np.add.reduce, np.minimum.reduce]) + def test_reduce_wrong_dimension_output(self, f_reduce, output_shape): + # Test that we're not incorrectly broadcasting dimensions. + # See gh-15144 (failed for np.add.reduce previously). + a = np.arange(12.).reshape(4, 3) + out = np.empty(output_shape, a.dtype) + assert_raises(ValueError, f_reduce, a, axis=0, out=out) + if output_shape != (1, 3): + assert_raises(ValueError, f_reduce, a, axis=0, out=out, + keepdims=True) + else: + check = f_reduce(a, axis=0, out=out, keepdims=True) + assert_(check is out) + assert_array_equal(check, f_reduce(a, axis=0, keepdims=True)) + def test_no_doc_string(self): # gh-9337 assert_('\n' not in umt.inner1d_no_doc.__doc__) @@ -1984,3 +1999,21 @@ def test_ufunc_noncontiguous(ufunc): assert_allclose(res_c, res_n, atol=tol, rtol=tol) else: assert_equal(c_ar, n_ar) + + +@pytest.mark.parametrize('ufunc', [np.sign, np.equal]) +def test_ufunc_warn_with_nan(ufunc): + # issue gh-15127 + # test that calling certain ufuncs with a non-standard `nan` value does not + # emit a warning + # `b` holds a 64 bit signaling nan: the most significant bit of the + # significand is zero. + b = np.array([0x7ff0000000000001], 'i8').view('f8') + assert np.isnan(b) + if ufunc.nin == 1: + ufunc(b) + elif ufunc.nin == 2: + ufunc(b, b.copy()) + else: + raise ValueError('ufunc with more than 2 inputs') + diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index ae1090c23..e966eebf0 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import platform import warnings import fnmatch @@ -23,7 +21,7 @@ def on_powerpc(): platform.machine().startswith('ppc') -class _FilterInvalids(object): +class _FilterInvalids: def setup(self): self.olderr = np.seterr(invalid='ignore') @@ -31,7 +29,7 @@ class _FilterInvalids(object): np.seterr(**self.olderr) -class TestConstants(object): +class TestConstants: def test_pi(self): assert_allclose(ncu.pi, 3.141592653589793, 1e-15) @@ -42,7 +40,7 @@ class TestConstants(object): assert_allclose(ncu.euler_gamma, 0.5772156649015329, 1e-15) -class TestOut(object): +class TestOut: def test_out_subok(self): for subok in (True, False): a = np.array(0.5) @@ -168,7 +166,7 @@ class TestOut(object): r1, r2 = np.frexp(d, out=o1, subok=subok) -class TestComparisons(object): +class TestComparisons: def test_ignore_object_identity_in_equal(self): # Check comparing identical objects whose comparison # is not a simple boolean, e.g., arrays that are compared elementwise. @@ -176,7 +174,7 @@ class TestComparisons(object): assert_raises(ValueError, np.equal, a, a) # Check error raised when comparing identical non-comparable objects. - class FunkyType(object): + class FunkyType: def __eq__(self, other): raise TypeError("I won't compare") @@ -194,7 +192,7 @@ class TestComparisons(object): assert_raises(ValueError, np.not_equal, a, a) # Check error raised when comparing identical non-comparable objects. - class FunkyType(object): + class FunkyType: def __ne__(self, other): raise TypeError("I won't compare") @@ -206,7 +204,7 @@ class TestComparisons(object): assert_equal(np.not_equal(a, a), [True]) -class TestAdd(object): +class TestAdd: def test_reduce_alignment(self): # gh-9876 # make sure arrays with weird strides work with the optimizations in @@ -217,7 +215,7 @@ class TestAdd(object): assert_equal(a['b'].sum(), 0) -class TestDivision(object): +class TestDivision: def test_division_int(self): # int division should follow Python x = np.array([5, 10, 90, 100, -5, -10, -90, -100, -120]) @@ -284,7 +282,7 @@ def _signs(dt): return (+1, -1) -class TestRemainder(object): +class TestRemainder: def test_remainder_basic(self): dt = np.typecodes['AllInteger'] + np.typecodes['Float'] @@ -375,7 +373,7 @@ class TestRemainder(object): assert_(np.isnan(rem), 'dt: %s, rem: %s' % (dt, rem)) -class TestCbrt(object): +class TestCbrt: def test_cbrt_scalar(self): assert_almost_equal((np.cbrt(np.float32(-2.5)**3)), -2.5) @@ -388,7 +386,7 @@ class TestCbrt(object): assert_equal(np.cbrt(-np.inf), -np.inf) -class TestPower(object): +class TestPower: def test_power_float(self): x = np.array([1., 2., 3.]) assert_equal(x**0, [1., 1., 1.]) @@ -527,7 +525,7 @@ class TestPower(object): assert_raises(ValueError, np.power, one, minusone) -class TestFloat_power(object): +class TestFloat_power: def test_type_conversion(self): arg_type = '?bhilBHILefdgFDG' res_type = 'ddddddddddddgDDG' @@ -538,7 +536,7 @@ class TestFloat_power(object): assert_(res.dtype.name == np.dtype(dtout).name, msg) -class TestLog2(object): +class TestLog2: def test_log2_values(self): x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] @@ -569,7 +567,7 @@ class TestLog2(object): assert_(w[2].category is RuntimeWarning) -class TestExp2(object): +class TestExp2: def test_exp2_values(self): x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] @@ -621,7 +619,7 @@ class TestLogAddExp2(_FilterInvalids): assert_(np.isnan(np.logaddexp2(np.nan, np.nan))) -class TestLog(object): +class TestLog: def test_log_values(self): x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] @@ -632,7 +630,7 @@ class TestLog(object): assert_almost_equal(np.log(xf), yf) -class TestExp(object): +class TestExp: def test_exp_values(self): x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] @@ -642,7 +640,7 @@ class TestExp(object): yf = np.array(y, dtype=dt)*log2_ assert_almost_equal(np.exp(yf), xf) -class TestSpecialFloats(object): +class TestSpecialFloats: def test_exp_values(self): x = [np.nan, np.nan, np.inf, 0.] y = [np.nan, -np.nan, np.inf, -np.inf] @@ -744,7 +742,7 @@ avx_ufuncs = {'sqrt' :[1, 0., 100.], 'ceil' :[0, -100., 100.], 'trunc' :[0, -100., 100.]} -class TestAVXUfuncs(object): +class TestAVXUfuncs: def test_avx_based_ufunc(self): strides = np.array([-4,-3,-2,-1,1,2,3,4]) np.random.seed(42) @@ -776,7 +774,7 @@ class TestAVXUfuncs(object): assert_equal(myfunc(x_f64[::jj]), y_true64[::jj]) assert_equal(myfunc(x_f32[::jj]), y_true32[::jj]) -class TestAVXFloat32Transcendental(object): +class TestAVXFloat32Transcendental: def test_exp_float32(self): np.random.seed(42) x_f32 = np.float32(np.random.uniform(low=0.0,high=88.1,size=1000000)) @@ -862,7 +860,7 @@ class TestLogAddExp(_FilterInvalids): assert_equal(np.logaddexp.reduce([]), -np.inf) -class TestLog1p(object): +class TestLog1p: def test_log1p(self): assert_almost_equal(ncu.log1p(0.2), ncu.log(1.2)) assert_almost_equal(ncu.log1p(1e-6), ncu.log(1+1e-6)) @@ -876,7 +874,7 @@ class TestLog1p(object): assert_equal(ncu.log1p(-np.inf), np.nan) -class TestExpm1(object): +class TestExpm1: def test_expm1(self): assert_almost_equal(ncu.expm1(0.2), ncu.exp(0.2)-1) assert_almost_equal(ncu.expm1(1e-6), ncu.exp(1e-6)-1) @@ -895,7 +893,7 @@ class TestExpm1(object): assert_allclose(x, ncu.expm1(x)) -class TestHypot(object): +class TestHypot: def test_simple(self): assert_almost_equal(ncu.hypot(1, 1), ncu.sqrt(2)) assert_almost_equal(ncu.hypot(0, 0), 0) @@ -919,7 +917,7 @@ def assert_hypot_isinf(x, y): "hypot(%s, %s) is %s, not inf" % (x, y, ncu.hypot(x, y))) -class TestHypotSpecialValues(object): +class TestHypotSpecialValues: def test_nan_outputs(self): assert_hypot_isnan(np.nan, np.nan) assert_hypot_isnan(np.nan, 1) @@ -956,7 +954,7 @@ def assert_arctan2_isnzero(x, y): assert_((ncu.arctan2(x, y) == 0 and np.signbit(ncu.arctan2(x, y))), "arctan(%s, %s) is %s, not -0" % (x, y, ncu.arctan2(x, y))) -class TestArctan2SpecialValues(object): +class TestArctan2SpecialValues: def test_one_one(self): # atan2(1, 1) returns pi/4. assert_almost_equal(ncu.arctan2(1, 1), 0.25 * np.pi) @@ -1025,7 +1023,7 @@ class TestArctan2SpecialValues(object): assert_arctan2_isnan(np.nan, np.nan) -class TestLdexp(object): +class TestLdexp: def _check_ldexp(self, tp): assert_almost_equal(ncu.ldexp(np.array(2., np.float32), np.array(3, tp)), 16.) @@ -1253,7 +1251,7 @@ class TestFmin(_FilterInvalids): assert_equal(np.fmin(arg1, arg2), out) -class TestBool(object): +class TestBool: def test_exceptions(self): a = np.ones(1, dtype=np.bool_) assert_raises(TypeError, np.negative, a) @@ -1316,7 +1314,7 @@ class TestBool(object): assert_equal(np.logical_xor.reduce(arr), arr.sum() % 2 == 1) -class TestBitwiseUFuncs(object): +class TestBitwiseUFuncs: bitwise_types = [np.dtype(c) for c in '?' + 'bBhHiIlLqQ' + 'O'] @@ -1400,7 +1398,7 @@ class TestBitwiseUFuncs(object): assert_(type(f.reduce(btype)) is bool, msg) -class TestInt(object): +class TestInt: def test_logical_not(self): x = np.ones(10, dtype=np.int16) o = np.ones(10 * 2, dtype=bool) @@ -1411,24 +1409,24 @@ class TestInt(object): assert_array_equal(o, tgt) -class TestFloatingPoint(object): +class TestFloatingPoint: def test_floating_point(self): assert_equal(ncu.FLOATING_POINT_SUPPORT, 1) -class TestDegrees(object): +class TestDegrees: def test_degrees(self): assert_almost_equal(ncu.degrees(np.pi), 180.0) assert_almost_equal(ncu.degrees(-0.5*np.pi), -90.0) -class TestRadians(object): +class TestRadians: def test_radians(self): assert_almost_equal(ncu.radians(180.0), np.pi) assert_almost_equal(ncu.radians(-90.0), -0.5*np.pi) -class TestHeavside(object): +class TestHeavside: def test_heaviside(self): x = np.array([[-30.0, -0.1, 0.0, 0.2], [7.5, np.nan, np.inf, -np.inf]]) expectedhalf = np.array([[0.0, 0.0, 0.5, 1.0], [1.0, np.nan, 1.0, 0.0]]) @@ -1450,7 +1448,7 @@ class TestHeavside(object): assert_equal(h, expected1.astype(np.float32)) -class TestSign(object): +class TestSign: def test_sign(self): a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0]) out = np.zeros(a.shape) @@ -1481,7 +1479,7 @@ class TestSign(object): assert_raises(TypeError, test_nan) -class TestMinMax(object): +class TestMinMax: def test_minmax_blocked(self): # simd tests on max/min, test all alignments, slow but important # for 2 * vz + 2 * (vs - 1) + 1 (unrolled once) @@ -1524,7 +1522,7 @@ class TestMinMax(object): assert_equal(a, np.nan) -class TestAbsoluteNegative(object): +class TestAbsoluteNegative: def test_abs_neg_blocked(self): # simd tests on abs, test all alignments for vz + 2 * (vs - 1) + 1 for dt, sz in [(np.float32, 11), (np.float64, 5)]: @@ -1566,7 +1564,7 @@ class TestAbsoluteNegative(object): np.abs(np.ones_like(d), out=d) -class TestPositive(object): +class TestPositive: def test_valid(self): valid_dtypes = [int, float, complex, object] for dtype in valid_dtypes: @@ -1585,10 +1583,10 @@ class TestPositive(object): np.positive(np.array(['bar'], dtype=object)) -class TestSpecialMethods(object): +class TestSpecialMethods: def test_wrap(self): - class with_wrap(object): + class with_wrap: def __array__(self): return np.zeros(1) @@ -1692,7 +1690,7 @@ class TestSpecialMethods(object): def test_old_wrap(self): - class with_wrap(object): + class with_wrap: def __array__(self): return np.zeros(1) @@ -1707,7 +1705,7 @@ class TestSpecialMethods(object): def test_priority(self): - class A(object): + class A: def __array__(self): return np.zeros(1) @@ -1750,7 +1748,7 @@ class TestSpecialMethods(object): def test_failing_wrap(self): - class A(object): + class A: def __array__(self): return np.zeros(2) @@ -1782,7 +1780,7 @@ class TestSpecialMethods(object): def test_none_wrap(self): # Tests that issue #8507 is resolved. Previously, this would segfault - class A(object): + class A: def __array__(self): return np.zeros(1) @@ -1794,7 +1792,7 @@ class TestSpecialMethods(object): def test_default_prepare(self): - class with_wrap(object): + class with_wrap: __array_priority__ = 10 def __array__(self): @@ -1840,7 +1838,7 @@ class TestSpecialMethods(object): def test_failing_prepare(self): - class A(object): + class A: def __array__(self): return np.zeros(1) @@ -1852,7 +1850,7 @@ class TestSpecialMethods(object): def test_array_with_context(self): - class A(object): + class A: def __array__(self, dtype=None, context=None): func, args, i = context self.func = func @@ -1860,11 +1858,11 @@ class TestSpecialMethods(object): self.i = i return np.zeros(1) - class B(object): + class B: def __array__(self, dtype=None): return np.zeros(1, dtype) - class C(object): + class C: def __array__(self): return np.zeros(1) @@ -1879,7 +1877,7 @@ class TestSpecialMethods(object): def test_ufunc_override(self): # check override works even with instance with high priority. - class A(object): + class A: def __array_ufunc__(self, func, method, *inputs, **kwargs): return self, func, method, inputs, kwargs @@ -1916,7 +1914,7 @@ class TestSpecialMethods(object): three_mul_ufunc = np.frompyfunc(tres_mul, 3, 1) four_mul_ufunc = np.frompyfunc(quatro_mul, 4, 1) - class A(object): + class A: def __array_ufunc__(self, func, method, *inputs, **kwargs): return "A" @@ -1924,11 +1922,11 @@ class TestSpecialMethods(object): def __array_ufunc__(self, func, method, *inputs, **kwargs): return "ASub" - class B(object): + class B: def __array_ufunc__(self, func, method, *inputs, **kwargs): return "B" - class C(object): + class C: def __init__(self): self.count = 0 @@ -2040,7 +2038,7 @@ class TestSpecialMethods(object): def test_ufunc_override_methods(self): - class A(object): + class A: def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): return self, ufunc, method, inputs, kwargs @@ -2209,11 +2207,11 @@ class TestSpecialMethods(object): def test_ufunc_override_out(self): - class A(object): + class A: def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): return kwargs - class B(object): + class B: def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): return kwargs @@ -2265,7 +2263,7 @@ class TestSpecialMethods(object): def test_ufunc_override_exception(self): - class A(object): + class A: def __array_ufunc__(self, *a, **kwargs): raise ValueError("oops") @@ -2276,7 +2274,7 @@ class TestSpecialMethods(object): def test_ufunc_override_not_implemented(self): - class A(object): + class A: def __array_ufunc__(self, *args, **kwargs): return NotImplemented @@ -2293,7 +2291,7 @@ class TestSpecialMethods(object): def test_ufunc_override_disabled(self): - class OptOut(object): + class OptOut: __array_ufunc__ = None opt_out = OptOut() @@ -2310,7 +2308,7 @@ class TestSpecialMethods(object): # opt-outs still hold even when other arguments have pathological # __array_ufunc__ implementations - class GreedyArray(object): + class GreedyArray: def __array_ufunc__(self, *args, **kwargs): return self @@ -2324,7 +2322,7 @@ class TestSpecialMethods(object): def test_gufunc_override(self): # gufunc are just ufunc instances, but follow a different path, # so check __array_ufunc__ overrides them properly. - class A(object): + class A: def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): return self, ufunc, method, inputs, kwargs @@ -2355,7 +2353,7 @@ class TestSpecialMethods(object): # NOTE: this class is given as an example in doc/subclassing.py; # if you make any changes here, do update it there too. class A(np.ndarray): - def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + def __array_ufunc__(self, ufunc, method, *inputs, out=None, **kwargs): args = [] in_no = [] for i, input_ in enumerate(inputs): @@ -2365,7 +2363,7 @@ class TestSpecialMethods(object): else: args.append(input_) - outputs = kwargs.pop('out', None) + outputs = out out_no = [] if outputs: out_args = [] @@ -2406,7 +2404,7 @@ class TestSpecialMethods(object): return results[0] if len(results) == 1 else results - class B(object): + class B: def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): if any(isinstance(input_, A) for input_ in inputs): return "A!" @@ -2525,14 +2523,14 @@ class TestSpecialMethods(object): assert_(a.info, {'inputs': [0, 2]}) -class TestChoose(object): +class TestChoose: def test_mixed(self): c = np.array([True, True]) a = np.array([True, True]) assert_equal(np.choose(c, (a, 1)), np.array([1, 1])) -class TestRationalFunctions(object): +class TestRationalFunctions: def test_lcm(self): self._test_lcm_inner(np.int16) self._test_lcm_inner(np.uint16) @@ -2631,7 +2629,7 @@ class TestRationalFunctions(object): assert_equal(np.gcd(2**100, 3**100), 1) -class TestRoundingFunctions(object): +class TestRoundingFunctions: def test_object_direct(self): """ test direct implementation of these magic methods """ @@ -2667,7 +2665,7 @@ class TestRoundingFunctions(object): assert_equal(np.trunc(f), -1) -class TestComplexFunctions(object): +class TestComplexFunctions: funcs = [np.arcsin, np.arccos, np.arctan, np.arcsinh, np.arccosh, np.arctanh, np.sin, np.cos, np.tan, np.exp, np.exp2, np.log, np.sqrt, np.log10, np.log2, @@ -2850,7 +2848,7 @@ class TestComplexFunctions(object): check(func, pts, 1+1j) -class TestAttributes(object): +class TestAttributes: def test_attributes(self): add = ncu.add assert_equal(add.__name__, 'add') @@ -2869,7 +2867,7 @@ class TestAttributes(object): "frexp(x[, out1, out2], / [, out=(None, None)], *, where=True")) -class TestSubclass(object): +class TestSubclass: def test_subclass_op(self): diff --git a/numpy/core/tests/test_umath_accuracy.py b/numpy/core/tests/test_umath_accuracy.py index fec180786..677d9af60 100644 --- a/numpy/core/tests/test_umath_accuracy.py +++ b/numpy/core/tests/test_umath_accuracy.py @@ -28,7 +28,7 @@ files = ['umath-validation-set-exp', 'umath-validation-set-sin', 'umath-validation-set-cos'] -class TestAccuracy(object): +class TestAccuracy: @pytest.mark.xfail(reason="Fails for MacPython/numpy-wheels builds") def test_validate_transcendentals(self): with np.errstate(all='ignore'): diff --git a/numpy/core/tests/test_umath_complex.py b/numpy/core/tests/test_umath_complex.py index 1f5b4077f..5e5ced85c 100644 --- a/numpy/core/tests/test_umath_complex.py +++ b/numpy/core/tests/test_umath_complex.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import platform import pytest @@ -31,7 +29,7 @@ platform_skip = pytest.mark.skipif(xfail_complex_tests, -class TestCexp(object): +class TestCexp: def test_simple(self): check = check_complex_value f = np.exp @@ -131,7 +129,7 @@ class TestCexp(object): check(f, np.nan, 0, np.nan, 0) -class TestClog(object): +class TestClog: def test_simple(self): x = np.array([1+0j, 1+2j]) y_r = np.log(np.abs(x)) + 1j * np.angle(x) @@ -276,7 +274,7 @@ class TestClog(object): assert_almost_equal(np.log(xa[i].conj()), ya[i].conj()) -class TestCsqrt(object): +class TestCsqrt: def test_simple(self): # sqrt(1) @@ -356,7 +354,7 @@ class TestCsqrt(object): # XXX: check for conj(csqrt(z)) == csqrt(conj(z)) (need to fix branch # cuts first) -class TestCpow(object): +class TestCpow: def setup(self): self.olderr = np.seterr(invalid='ignore') @@ -396,7 +394,7 @@ class TestCpow(object): for i in lx: assert_almost_equal(n_r[i], p_r[i], err_msg='Loop %d\n' % i) -class TestCabs(object): +class TestCabs: def setup(self): self.olderr = np.seterr(invalid='ignore') @@ -458,7 +456,7 @@ class TestCabs(object): ref = g(x[i], y[i]) check_real_value(f, x[i], y[i], ref) -class TestCarg(object): +class TestCarg: def test_simple(self): check_real_value(ncu._arg, 1, 0, 0, False) check_real_value(ncu._arg, 0, 1, 0.5*np.pi, False) diff --git a/numpy/core/tests/test_unicode.py b/numpy/core/tests/test_unicode.py index 2ffd8801b..b8ec56116 100644 --- a/numpy/core/tests/test_unicode.py +++ b/numpy/core/tests/test_unicode.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import numpy as np @@ -67,7 +65,7 @@ def test_string_cast(): # Creation tests ############################################################ -class CreateZeros(object): +class CreateZeros: """Check the creation of zero-valued arrays""" def content_check(self, ua, ua_scalar, nbytes): @@ -119,7 +117,7 @@ class TestCreateZeros_1009(CreateZeros): ulen = 1009 -class CreateValues(object): +class CreateValues: """Check the creation of unicode arrays with values""" def content_check(self, ua, ua_scalar, nbytes): @@ -204,7 +202,7 @@ class TestCreateValues_1009_UCS4(CreateValues): # Assignment tests ############################################################ -class AssignValues(object): +class AssignValues: """Check the assignment of unicode arrays with values""" def content_check(self, ua, ua_scalar, nbytes): @@ -294,7 +292,7 @@ class TestAssignValues_1009_UCS4(AssignValues): # Byteorder tests ############################################################ -class ByteorderValues(object): +class ByteorderValues: """Check the byteorder of unicode arrays in round-trip conversions""" def test_values0D(self): diff --git a/numpy/core/umath_tests.py b/numpy/core/umath_tests.py index 28e325b98..90ab17e67 100644 --- a/numpy/core/umath_tests.py +++ b/numpy/core/umath_tests.py @@ -2,8 +2,6 @@ Shim for _umath_tests to allow a deprecation period for the new name. """ -from __future__ import division, absolute_import, print_function - import warnings # 2018-04-04, numpy 1.15.0 diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py index 58f3ef9d3..ec3cdc33d 100644 --- a/numpy/ctypeslib.py +++ b/numpy/ctypeslib.py @@ -49,8 +49,6 @@ Then, we're ready to call ``foo_func``: >>> _lib.foo_func(out, len(out)) #doctest: +SKIP """ -from __future__ import division, absolute_import, print_function - __all__ = ['load_library', 'ndpointer', 'ctypes_load_library', 'c_intp', 'as_ctypes', 'as_array'] diff --git a/numpy/distutils/__init__.py b/numpy/distutils/__init__.py index 8dbb63b28..79974d1c2 100644 --- a/numpy/distutils/__init__.py +++ b/numpy/distutils/__init__.py @@ -19,8 +19,6 @@ LAPACK, and for setting include paths and similar build options, please see """ -from __future__ import division, absolute_import, print_function - # Must import local ccompiler ASAP in order to get # customized CCompiler.spawn effective. from . import ccompiler diff --git a/numpy/distutils/ccompiler.py b/numpy/distutils/ccompiler.py index 684c7535b..5e3cd0e74 100644 --- a/numpy/distutils/ccompiler.py +++ b/numpy/distutils/ccompiler.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import re import sys @@ -16,7 +14,6 @@ from distutils.sysconfig import customize_compiler from distutils.version import LooseVersion from numpy.distutils import log -from numpy.distutils.compat import get_exception from numpy.distutils.exec_command import ( filepath_from_subprocess_output, forward_bytes_to_stdout ) @@ -532,11 +529,6 @@ def CCompiler_customize(self, dist, need_cxx=0): 'g++' in self.compiler[0] or 'clang' in self.compiler[0]): self._auto_depends = True - if 'gcc' in self.compiler[0]: - # add std=c99 flag for gcc - # TODO: does this need to be more specific? - self.compiler.append('-std=c99') - self.compiler_so.append('-std=c99') elif os.name == 'posix': import tempfile import shutil @@ -756,15 +748,15 @@ def new_compiler (plat=None, module_name = "numpy.distutils." + module_name try: __import__ (module_name) - except ImportError: - msg = str(get_exception()) + except ImportError as e: + msg = str(e) log.info('%s in numpy.distutils; trying from distutils', str(msg)) module_name = module_name[6:] try: __import__(module_name) - except ImportError: - msg = str(get_exception()) + except ImportError as e: + msg = str(e) raise DistutilsModuleError("can't compile C/C++ code: unable to load module '%s'" % \ module_name) try: diff --git a/numpy/distutils/command/__init__.py b/numpy/distutils/command/__init__.py index 76a260072..3ba501de0 100644 --- a/numpy/distutils/command/__init__.py +++ b/numpy/distutils/command/__init__.py @@ -4,8 +4,6 @@ Package containing implementation of all the standard Distutils commands. """ -from __future__ import division, absolute_import, print_function - def test_na_writable_attributes_deletion(): a = np.NA(2) attr = ['payload', 'dtype'] diff --git a/numpy/distutils/command/autodist.py b/numpy/distutils/command/autodist.py index 9c98b84d8..1475a5e24 100644 --- a/numpy/distutils/command/autodist.py +++ b/numpy/distutils/command/autodist.py @@ -1,8 +1,6 @@ """This module implements additional tests ala autoconf which can be useful. """ -from __future__ import division, absolute_import, print_function - import textwrap # We put them here since they could be easily reused outside numpy.distutils diff --git a/numpy/distutils/command/bdist_rpm.py b/numpy/distutils/command/bdist_rpm.py index 3e52a503b..682e7a8eb 100644 --- a/numpy/distutils/command/bdist_rpm.py +++ b/numpy/distutils/command/bdist_rpm.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys if 'setuptools' in sys.modules: diff --git a/numpy/distutils/command/build.py b/numpy/distutils/command/build.py index 5a9da1217..a156a7c6e 100644 --- a/numpy/distutils/command/build.py +++ b/numpy/distutils/command/build.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys from distutils.command.build import build as old_build diff --git a/numpy/distutils/command/build_clib.py b/numpy/distutils/command/build_clib.py index 13edf0717..1b4666888 100644 --- a/numpy/distutils/command/build_clib.py +++ b/numpy/distutils/command/build_clib.py @@ -1,7 +1,5 @@ """ Modified version of build_clib that handles fortran source files. """ -from __future__ import division, absolute_import, print_function - import os from glob import glob import shutil diff --git a/numpy/distutils/command/build_ext.py b/numpy/distutils/command/build_ext.py index cd9b1c6f1..a0da83ff4 100644 --- a/numpy/distutils/command/build_ext.py +++ b/numpy/distutils/command/build_ext.py @@ -1,8 +1,6 @@ """ Modified version of build_ext that handles fortran source files. """ -from __future__ import division, absolute_import, print_function - import os import subprocess from glob import glob diff --git a/numpy/distutils/command/build_py.py b/numpy/distutils/command/build_py.py index 54dcde435..d30dc5bf4 100644 --- a/numpy/distutils/command/build_py.py +++ b/numpy/distutils/command/build_py.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from distutils.command.build_py import build_py as old_build_py from numpy.distutils.misc_util import is_string diff --git a/numpy/distutils/command/build_scripts.py b/numpy/distutils/command/build_scripts.py index c8b25fc71..d5cadb274 100644 --- a/numpy/distutils/command/build_scripts.py +++ b/numpy/distutils/command/build_scripts.py @@ -1,8 +1,6 @@ """ Modified version of build_scripts that handles building scripts from functions. """ -from __future__ import division, absolute_import, print_function - from distutils.command.build_scripts import build_scripts as old_build_scripts from numpy.distutils import log from numpy.distutils.misc_util import is_string diff --git a/numpy/distutils/command/build_src.py b/numpy/distutils/command/build_src.py index 3e0522c5f..303d6197c 100644 --- a/numpy/distutils/command/build_src.py +++ b/numpy/distutils/command/build_src.py @@ -1,7 +1,5 @@ """ Build swig and f2py sources. """ -from __future__ import division, absolute_import, print_function - import os import re import sys diff --git a/numpy/distutils/command/config.py b/numpy/distutils/command/config.py index b9f2fa76e..2c833aad7 100644 --- a/numpy/distutils/command/config.py +++ b/numpy/distutils/command/config.py @@ -2,8 +2,6 @@ # try_compile call. try_run works but is untested for most of Fortran # compilers (they must define linker_exe first). # Pearu Peterson -from __future__ import division, absolute_import, print_function - import os, signal import warnings import sys @@ -24,7 +22,6 @@ from numpy.distutils.command.autodist import (check_gcc_function_attribute, check_inline, check_restrict, check_compiler_gcc4) -from numpy.distutils.compat import get_exception LANG_EXT['f77'] = '.f' LANG_EXT['f90'] = '.f90' @@ -52,8 +49,7 @@ class config(old_config): if not self.compiler.initialized: try: self.compiler.initialize() - except IOError: - e = get_exception() + except IOError as e: msg = textwrap.dedent("""\ Could not initialize compiler instance: do you have Visual Studio installed? If you are trying to build with MinGW, please use "python setup.py @@ -96,8 +92,8 @@ class config(old_config): self.compiler = self.fcompiler try: ret = mth(*((self,)+args)) - except (DistutilsExecError, CompileError): - str(get_exception()) + except (DistutilsExecError, CompileError) as e: + str(e) self.compiler = save_compiler raise CompileError self.compiler = save_compiler @@ -495,7 +491,7 @@ class config(old_config): self._clean() return exitcode, output -class GrabStdout(object): +class GrabStdout: def __init__(self): self.sys_stdout = sys.stdout diff --git a/numpy/distutils/command/config_compiler.py b/numpy/distutils/command/config_compiler.py index bf170063e..44265bfcc 100644 --- a/numpy/distutils/command/config_compiler.py +++ b/numpy/distutils/command/config_compiler.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from distutils.core import Command from numpy.distutils import log diff --git a/numpy/distutils/command/develop.py b/numpy/distutils/command/develop.py index 1410ab2a0..af24baf2e 100644 --- a/numpy/distutils/command/develop.py +++ b/numpy/distutils/command/develop.py @@ -3,8 +3,6 @@ generated files (from build_src or build_scripts) are properly converted to real files with filenames. """ -from __future__ import division, absolute_import, print_function - from setuptools.command.develop import develop as old_develop class develop(old_develop): diff --git a/numpy/distutils/command/egg_info.py b/numpy/distutils/command/egg_info.py index 18673ece7..14c62b4d1 100644 --- a/numpy/distutils/command/egg_info.py +++ b/numpy/distutils/command/egg_info.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys from setuptools.command.egg_info import egg_info as _egg_info diff --git a/numpy/distutils/command/install.py b/numpy/distutils/command/install.py index c74ae9446..2eff2d145 100644 --- a/numpy/distutils/command/install.py +++ b/numpy/distutils/command/install.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys if 'setuptools' in sys.modules: import setuptools.command.install as old_install_mod diff --git a/numpy/distutils/command/install_clib.py b/numpy/distutils/command/install_clib.py index 6a73f7e33..aa2e5594c 100644 --- a/numpy/distutils/command/install_clib.py +++ b/numpy/distutils/command/install_clib.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os from distutils.core import Command from distutils.ccompiler import new_compiler diff --git a/numpy/distutils/command/install_data.py b/numpy/distutils/command/install_data.py index 996cf7e40..0a2e68ae1 100644 --- a/numpy/distutils/command/install_data.py +++ b/numpy/distutils/command/install_data.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys have_setuptools = ('setuptools' in sys.modules) diff --git a/numpy/distutils/command/install_headers.py b/numpy/distutils/command/install_headers.py index f3f58aa28..bb4ad563b 100644 --- a/numpy/distutils/command/install_headers.py +++ b/numpy/distutils/command/install_headers.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os from distutils.command.install_headers import install_headers as old_install_headers diff --git a/numpy/distutils/command/sdist.py b/numpy/distutils/command/sdist.py index bfaab1c8f..e34193883 100644 --- a/numpy/distutils/command/sdist.py +++ b/numpy/distutils/command/sdist.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys if 'setuptools' in sys.modules: from setuptools.command.sdist import sdist as old_sdist diff --git a/numpy/distutils/compat.py b/numpy/distutils/compat.py deleted file mode 100644 index 9a81cd392..000000000 --- a/numpy/distutils/compat.py +++ /dev/null @@ -1,10 +0,0 @@ -"""Small modules to cope with python 2 vs 3 incompatibilities inside -numpy.distutils - -""" -from __future__ import division, absolute_import, print_function - -import sys - -def get_exception(): - return sys.exc_info()[1] diff --git a/numpy/distutils/conv_template.py b/numpy/distutils/conv_template.py index 3bcb7b884..ec5a84a68 100644 --- a/numpy/distutils/conv_template.py +++ b/numpy/distutils/conv_template.py @@ -78,8 +78,6 @@ Example: 3, 3, jim """ -from __future__ import division, absolute_import, print_function - __all__ = ['process_str', 'process_file'] @@ -87,8 +85,6 @@ import os import sys import re -from numpy.distutils.compat import get_exception - # names for replacement that are already global. global_names = {} @@ -240,8 +236,7 @@ def parse_string(astr, env, level, line) : code.append(replace_re.sub(replace, pref)) try : envlist = parse_loop_header(head) - except ValueError: - e = get_exception() + except ValueError as e: msg = "line %d: %s" % (newline, e) raise ValueError(msg) for newenv in envlist : @@ -289,8 +284,7 @@ def process_file(source): sourcefile = os.path.normcase(source).replace("\\", "\\\\") try: code = process_str(''.join(lines)) - except ValueError: - e = get_exception() + except ValueError as e: raise ValueError('In "%s" loop at %s' % (sourcefile, e)) return '#line 1 "%s"\n%s' % (sourcefile, code) @@ -327,8 +321,7 @@ def main(): allstr = fid.read() try: writestr = process_str(allstr) - except ValueError: - e = get_exception() + except ValueError as e: raise ValueError("In %s loop at %s" % (file, e)) outfile.write(writestr) diff --git a/numpy/distutils/core.py b/numpy/distutils/core.py index 70cc37caa..a78bbf484 100644 --- a/numpy/distutils/core.py +++ b/numpy/distutils/core.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys from distutils.core import * diff --git a/numpy/distutils/cpuinfo.py b/numpy/distutils/cpuinfo.py index bc9728335..efea90113 100644 --- a/numpy/distutils/cpuinfo.py +++ b/numpy/distutils/cpuinfo.py @@ -12,8 +12,6 @@ NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. Pearu Peterson """ -from __future__ import division, absolute_import, print_function - __all__ = ['cpu'] import sys, re, types @@ -27,13 +25,10 @@ else: import warnings import platform -from numpy.distutils.compat import get_exception - def getoutput(cmd, successful_status=(0,), stacklevel=1): try: status, output = getstatusoutput(cmd) - except EnvironmentError: - e = get_exception() + except EnvironmentError as e: warnings.warn(str(e), UserWarning, stacklevel=stacklevel) return False, "" if os.WIFEXITED(status) and os.WEXITSTATUS(status) in successful_status: @@ -67,7 +62,7 @@ def key_value_from_command(cmd, sep, successful_status=(0,), d[l[0]] = l[1] return d -class CPUInfoBase(object): +class CPUInfoBase: """Holds CPU information and provides methods for requiring the availability of various CPU features. """ @@ -115,8 +110,7 @@ class LinuxCPUInfo(CPUInfoBase): info[0]['uname_m'] = output.strip() try: fo = open('/proc/cpuinfo') - except EnvironmentError: - e = get_exception() + except EnvironmentError as e: warnings.warn(str(e), UserWarning, stacklevel=2) else: for line in fo: @@ -523,8 +517,8 @@ class Win32CPUInfo(CPUInfoBase): info[-1]["Family"]=int(srch.group("FML")) info[-1]["Model"]=int(srch.group("MDL")) info[-1]["Stepping"]=int(srch.group("STP")) - except Exception: - print(sys.exc_info()[1], '(ignoring)') + except Exception as e: + print(e, '(ignoring)') self.__class__.info = info def _not_impl(self): pass diff --git a/numpy/distutils/exec_command.py b/numpy/distutils/exec_command.py index 712f22666..d35b4f898 100644 --- a/numpy/distutils/exec_command.py +++ b/numpy/distutils/exec_command.py @@ -49,8 +49,6 @@ Known bugs: because the messages are lost at some point. """ -from __future__ import division, absolute_import, print_function - __all__ = ['exec_command', 'find_executable'] import os diff --git a/numpy/distutils/extension.py b/numpy/distutils/extension.py index 872bd5362..3b5c3db35 100644 --- a/numpy/distutils/extension.py +++ b/numpy/distutils/extension.py @@ -6,8 +6,6 @@ modules in setup scripts. Overridden to support f2py. """ -from __future__ import division, absolute_import, print_function - import sys import re from distutils.extension import Extension as old_Extension diff --git a/numpy/distutils/fcompiler/__init__.py b/numpy/distutils/fcompiler/__init__.py index 3723470f3..a88b0d713 100644 --- a/numpy/distutils/fcompiler/__init__.py +++ b/numpy/distutils/fcompiler/__init__.py @@ -13,8 +13,6 @@ should be a list. But note that FCompiler.executables is actually a dictionary of commands. """ -from __future__ import division, absolute_import, print_function - __all__ = ['FCompiler', 'new_fcompiler', 'show_fcompilers', 'dummy_fortran_file'] @@ -36,7 +34,6 @@ from numpy.distutils import log from numpy.distutils.misc_util import is_string, all_strings, is_sequence, \ make_temp_file, get_shared_lib_extension from numpy.distutils.exec_command import find_executable -from numpy.distutils.compat import get_exception from numpy.distutils import _shell_utils from .environment import EnvironmentConfig @@ -614,8 +611,8 @@ class FCompiler(CCompiler): src) try: self.spawn(command, display=display) - except DistutilsExecError: - msg = str(get_exception()) + except DistutilsExecError as e: + msg = str(e) raise CompileError(msg) def module_options(self, module_dirs, module_build_dir): @@ -682,8 +679,8 @@ class FCompiler(CCompiler): command = linker + ld_args try: self.spawn(command) - except DistutilsExecError: - msg = str(get_exception()) + except DistutilsExecError as e: + msg = str(e) raise LinkError(msg) else: log.debug("skipping %s (up-to-date)", output_filename) @@ -931,8 +928,7 @@ def show_fcompilers(dist=None): c = new_fcompiler(compiler=compiler, verbose=dist.verbose) c.customize(dist) v = c.get_version() - except (DistutilsModuleError, CompilerNotFound): - e = get_exception() + except (DistutilsModuleError, CompilerNotFound) as e: log.debug("show_fcompilers: %s not found" % (compiler,)) log.debug(repr(e)) diff --git a/numpy/distutils/fcompiler/absoft.py b/numpy/distutils/fcompiler/absoft.py index d14fee0e1..efe3a4cb5 100644 --- a/numpy/distutils/fcompiler/absoft.py +++ b/numpy/distutils/fcompiler/absoft.py @@ -5,8 +5,6 @@ # Notes: # - when using -g77 then use -DUNDERSCORE_G77 to compile f2py # generated extension modules (works for f2py v2.45.241_1936 and up) -from __future__ import division, absolute_import, print_function - import os from numpy.distutils.cpuinfo import cpu diff --git a/numpy/distutils/fcompiler/compaq.py b/numpy/distutils/fcompiler/compaq.py index 671b3a55f..6ce590c7c 100644 --- a/numpy/distutils/fcompiler/compaq.py +++ b/numpy/distutils/fcompiler/compaq.py @@ -1,12 +1,9 @@ #http://www.compaq.com/fortran/docs/ -from __future__ import division, absolute_import, print_function - import os import sys from numpy.distutils.fcompiler import FCompiler -from numpy.distutils.compat import get_exception from distutils.errors import DistutilsPlatformError compilers = ['CompaqFCompiler'] @@ -82,19 +79,16 @@ class CompaqVisualFCompiler(FCompiler): ar_exe = m.lib except DistutilsPlatformError: pass - except AttributeError: - msg = get_exception() + except AttributeError as e: if '_MSVCCompiler__root' in str(msg): print('Ignoring "%s" (I think it is msvccompiler.py bug)' % (msg)) else: raise - except IOError: - e = get_exception() + except IOError as e: if not "vcvarsall.bat" in str(e): print("Unexpected IOError in", __file__) raise e - except ValueError: - e = get_exception() + except ValueError as e: if not "'path'" in str(e): print("Unexpected ValueError in", __file__) raise e diff --git a/numpy/distutils/fcompiler/environment.py b/numpy/distutils/fcompiler/environment.py index bb362d483..ae5fc404a 100644 --- a/numpy/distutils/fcompiler/environment.py +++ b/numpy/distutils/fcompiler/environment.py @@ -1,12 +1,10 @@ -from __future__ import division, absolute_import, print_function - import os import warnings from distutils.dist import Distribution __metaclass__ = type -class EnvironmentConfig(object): +class EnvironmentConfig: def __init__(self, distutils_section='ALL', **kw): self._distutils_section = distutils_section self._conf_keys = kw diff --git a/numpy/distutils/fcompiler/g95.py b/numpy/distutils/fcompiler/g95.py index e7c659b33..e109a972a 100644 --- a/numpy/distutils/fcompiler/g95.py +++ b/numpy/distutils/fcompiler/g95.py @@ -1,6 +1,4 @@ # http://g95.sourceforge.net/ -from __future__ import division, absolute_import, print_function - from numpy.distutils.fcompiler import FCompiler compilers = ['G95FCompiler'] diff --git a/numpy/distutils/fcompiler/gnu.py b/numpy/distutils/fcompiler/gnu.py index 965c67041..4fc9f33ff 100644 --- a/numpy/distutils/fcompiler/gnu.py +++ b/numpy/distutils/fcompiler/gnu.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import re import os import sys @@ -12,7 +10,6 @@ import subprocess from subprocess import Popen, PIPE, STDOUT from numpy.distutils.exec_command import filepath_from_subprocess_output from numpy.distutils.fcompiler import FCompiler -from numpy.distutils.compat import get_exception from numpy.distutils.system_info import system_info compilers = ['GnuFCompiler', 'Gnu95FCompiler'] @@ -560,5 +557,5 @@ if __name__ == '__main__': print(customized_fcompiler('gnu').get_version()) try: print(customized_fcompiler('g95').get_version()) - except Exception: - print(get_exception()) + except Exception as e: + print(e) diff --git a/numpy/distutils/fcompiler/hpux.py b/numpy/distutils/fcompiler/hpux.py index 51bad548a..09e6483bf 100644 --- a/numpy/distutils/fcompiler/hpux.py +++ b/numpy/distutils/fcompiler/hpux.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.distutils.fcompiler import FCompiler compilers = ['HPUXFCompiler'] diff --git a/numpy/distutils/fcompiler/ibm.py b/numpy/distutils/fcompiler/ibm.py index 70d2132e1..4a83682e5 100644 --- a/numpy/distutils/fcompiler/ibm.py +++ b/numpy/distutils/fcompiler/ibm.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import re import sys diff --git a/numpy/distutils/fcompiler/intel.py b/numpy/distutils/fcompiler/intel.py index 51f681274..d84f38c76 100644 --- a/numpy/distutils/fcompiler/intel.py +++ b/numpy/distutils/fcompiler/intel.py @@ -1,6 +1,4 @@ # http://developer.intel.com/software/products/compilers/flin/ -from __future__ import division, absolute_import, print_function - import sys from numpy.distutils.ccompiler import simple_version_match diff --git a/numpy/distutils/fcompiler/lahey.py b/numpy/distutils/fcompiler/lahey.py index 1beb662f4..e92583826 100644 --- a/numpy/distutils/fcompiler/lahey.py +++ b/numpy/distutils/fcompiler/lahey.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os from numpy.distutils.fcompiler import FCompiler diff --git a/numpy/distutils/fcompiler/mips.py b/numpy/distutils/fcompiler/mips.py index da337b24a..a09738045 100644 --- a/numpy/distutils/fcompiler/mips.py +++ b/numpy/distutils/fcompiler/mips.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.distutils.cpuinfo import cpu from numpy.distutils.fcompiler import FCompiler diff --git a/numpy/distutils/fcompiler/nag.py b/numpy/distutils/fcompiler/nag.py index cb71d548c..908e724e6 100644 --- a/numpy/distutils/fcompiler/nag.py +++ b/numpy/distutils/fcompiler/nag.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import re from numpy.distutils.fcompiler import FCompiler diff --git a/numpy/distutils/fcompiler/none.py b/numpy/distutils/fcompiler/none.py index bdeea1560..ef411fffc 100644 --- a/numpy/distutils/fcompiler/none.py +++ b/numpy/distutils/fcompiler/none.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.distutils.fcompiler import FCompiler from numpy.distutils import customized_fcompiler diff --git a/numpy/distutils/fcompiler/pathf95.py b/numpy/distutils/fcompiler/pathf95.py index 5de86f63a..0768cb12e 100644 --- a/numpy/distutils/fcompiler/pathf95.py +++ b/numpy/distutils/fcompiler/pathf95.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.distutils.fcompiler import FCompiler compilers = ['PathScaleFCompiler'] diff --git a/numpy/distutils/fcompiler/pg.py b/numpy/distutils/fcompiler/pg.py index 9c51947fd..77bc4f08e 100644 --- a/numpy/distutils/fcompiler/pg.py +++ b/numpy/distutils/fcompiler/pg.py @@ -1,6 +1,4 @@ # http://www.pgroup.com -from __future__ import division, absolute_import, print_function - import sys from numpy.distutils.fcompiler import FCompiler, dummy_fortran_file diff --git a/numpy/distutils/fcompiler/sun.py b/numpy/distutils/fcompiler/sun.py index 561ea854f..d039f0b25 100644 --- a/numpy/distutils/fcompiler/sun.py +++ b/numpy/distutils/fcompiler/sun.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.distutils.ccompiler import simple_version_match from numpy.distutils.fcompiler import FCompiler diff --git a/numpy/distutils/fcompiler/vast.py b/numpy/distutils/fcompiler/vast.py index adc1591bd..92a1647ba 100644 --- a/numpy/distutils/fcompiler/vast.py +++ b/numpy/distutils/fcompiler/vast.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os from numpy.distutils.fcompiler.gnu import GnuFCompiler diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index c5c1163c6..b4dd05b5e 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -45,8 +45,6 @@ process_file(filename) <ctypereal=float,double,\\0,\\1> """ -from __future__ import division, absolute_import, print_function - __all__ = ['process_str', 'process_file'] import os diff --git a/numpy/distutils/intelccompiler.py b/numpy/distutils/intelccompiler.py index 3386775ee..0388ad577 100644 --- a/numpy/distutils/intelccompiler.py +++ b/numpy/distutils/intelccompiler.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import platform from distutils.unixccompiler import UnixCCompiler diff --git a/numpy/distutils/lib2def.py b/numpy/distutils/lib2def.py index 2d013a1e3..c6d445d09 100644 --- a/numpy/distutils/lib2def.py +++ b/numpy/distutils/lib2def.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import re import sys import subprocess diff --git a/numpy/distutils/line_endings.py b/numpy/distutils/line_endings.py index fe8fd1b0f..92a84e56a 100644 --- a/numpy/distutils/line_endings.py +++ b/numpy/distutils/line_endings.py @@ -1,8 +1,6 @@ """ Functions for converting from DOS to UNIX line endings """ -from __future__ import division, absolute_import, print_function - import sys, re, os def dos2unix(file): diff --git a/numpy/distutils/log.py b/numpy/distutils/log.py index ff7de86b1..ec1100b1b 100644 --- a/numpy/distutils/log.py +++ b/numpy/distutils/log.py @@ -1,6 +1,4 @@ # Colored log, requires Python 2.3 or up. -from __future__ import division, absolute_import, print_function - import sys from distutils.log import * from distutils.log import Log as old_Log diff --git a/numpy/distutils/mingw32ccompiler.py b/numpy/distutils/mingw32ccompiler.py index 075858cfe..e2cd1c19b 100644 --- a/numpy/distutils/mingw32ccompiler.py +++ b/numpy/distutils/mingw32ccompiler.py @@ -7,8 +7,6 @@ Support code for building Python extensions on Windows. # 3. Force windows to use g77 """ -from __future__ import division, absolute_import, print_function - import os import sys import subprocess diff --git a/numpy/distutils/misc_util.py b/numpy/distutils/misc_util.py index d46ff8981..eec8d56a3 100644 --- a/numpy/distutils/misc_util.py +++ b/numpy/distutils/misc_util.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import re import sys @@ -34,7 +32,6 @@ def clean_up_temporary_directory(): atexit.register(clean_up_temporary_directory) -from numpy.distutils.compat import get_exception from numpy.compat import basestring from numpy.compat import npy_load_module @@ -51,7 +48,7 @@ __all__ = ['Configuration', 'get_numpy_include_dirs', 'default_config_dict', 'quote_args', 'get_build_architecture', 'get_info', 'get_pkg_info', 'get_num_build_jobs'] -class InstallableLib(object): +class InstallableLib: """ Container to hold information on an installable library. @@ -728,7 +725,7 @@ def get_frame(level=0): ###################### -class Configuration(object): +class Configuration: _list_keys = ['packages', 'ext_modules', 'data_files', 'include_dirs', 'libraries', 'headers', 'scripts', 'py_modules', @@ -1972,9 +1969,8 @@ class Configuration(object): try: version_module = npy_load_module('_'.join(n.split('.')), fn, info) - except ImportError: - msg = get_exception() - self.warn(str(msg)) + except ImportError as e: + self.warn(str(e)) version_module = None if version_module is None: continue diff --git a/numpy/distutils/msvc9compiler.py b/numpy/distutils/msvc9compiler.py index e9cc334a5..68239495d 100644 --- a/numpy/distutils/msvc9compiler.py +++ b/numpy/distutils/msvc9compiler.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os from distutils.msvc9compiler import MSVCCompiler as _MSVCCompiler diff --git a/numpy/distutils/msvccompiler.py b/numpy/distutils/msvccompiler.py index 0cb4bf979..681a254b8 100644 --- a/numpy/distutils/msvccompiler.py +++ b/numpy/distutils/msvccompiler.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os from distutils.msvccompiler import MSVCCompiler as _MSVCCompiler diff --git a/numpy/distutils/npy_pkg_config.py b/numpy/distutils/npy_pkg_config.py index 48584b4c4..47965b4ae 100644 --- a/numpy/distutils/npy_pkg_config.py +++ b/numpy/distutils/npy_pkg_config.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import re import os @@ -78,7 +76,7 @@ def parse_flags(line): def _escape_backslash(val): return val.replace('\\', '\\\\') -class LibraryInfo(object): +class LibraryInfo: """ Object containing build information about a library. @@ -150,7 +148,7 @@ class LibraryInfo(object): return "\n".join(m) -class VariableSet(object): +class VariableSet: """ Container object for the variables defined in a config file. diff --git a/numpy/distutils/numpy_distribution.py b/numpy/distutils/numpy_distribution.py index 6ae19d16b..ea8182659 100644 --- a/numpy/distutils/numpy_distribution.py +++ b/numpy/distutils/numpy_distribution.py @@ -1,6 +1,4 @@ # XXX: Handle setuptools ? -from __future__ import division, absolute_import, print_function - from distutils.core import Distribution # This class is used because we add new files (sconscripts, and so on) with the diff --git a/numpy/distutils/pathccompiler.py b/numpy/distutils/pathccompiler.py index fc9872db3..48051810e 100644 --- a/numpy/distutils/pathccompiler.py +++ b/numpy/distutils/pathccompiler.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from distutils.unixccompiler import UnixCCompiler class PathScaleCCompiler(UnixCCompiler): diff --git a/numpy/distutils/setup.py b/numpy/distutils/setup.py index 82a53bd08..415d2adeb 100644 --- a/numpy/distutils/setup.py +++ b/numpy/distutils/setup.py @@ -1,6 +1,4 @@ #!/usr/bin/env python -from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('distutils', parent_package, top_path) diff --git a/numpy/distutils/system_info.py b/numpy/distutils/system_info.py index 4786b3a0c..508aeefc5 100644 --- a/numpy/distutils/system_info.py +++ b/numpy/distutils/system_info.py @@ -128,8 +128,6 @@ this distribution for specifics. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. """ -from __future__ import division, absolute_import, print_function - import sys import os import re @@ -163,7 +161,6 @@ from numpy.distutils.exec_command import ( from numpy.distutils.misc_util import (is_sequence, is_string, get_shared_lib_extension) from numpy.distutils.command.config import config as cmd_config -from numpy.distutils.compat import get_exception from numpy.distutils import customized_ccompiler as _customized_ccompiler from numpy.distutils import _shell_utils import distutils.ccompiler @@ -582,7 +579,7 @@ class UmfpackNotFoundError(NotFoundError): the UMFPACK environment variable.""" -class system_info(object): +class system_info: """ get_info() is the only public method. Don't use others. """ @@ -2102,16 +2099,17 @@ class openblas_info(blas_info): return None # Generate numpy.distutils virtual static library file - tmpdir = os.path.join(os.getcwd(), 'build', 'openblas') + basename = self.__class__.__name__ + tmpdir = os.path.join(os.getcwd(), 'build', basename) if not os.path.isdir(tmpdir): os.makedirs(tmpdir) info = {'library_dirs': [tmpdir], - 'libraries': ['openblas'], + 'libraries': [basename], 'language': 'f77'} - fake_lib_file = os.path.join(tmpdir, 'openblas.fobjects') - fake_clib_file = os.path.join(tmpdir, 'openblas.cobjects') + fake_lib_file = os.path.join(tmpdir, basename + '.fobjects') + fake_clib_file = os.path.join(tmpdir, basename + '.cobjects') with open(fake_lib_file, 'w') as f: f.write("\n".join(library_paths)) with open(fake_clib_file, 'w') as f: @@ -2540,18 +2538,18 @@ class numerix_info(system_info): try: import numpy # noqa: F401 which = "numpy", "defaulted" - except ImportError: - msg1 = str(get_exception()) + except ImportError as e: + msg1 = str(e) try: import Numeric # noqa: F401 which = "numeric", "defaulted" - except ImportError: - msg2 = str(get_exception()) + except ImportError as e: + msg2 = str(e) try: import numarray # noqa: F401 which = "numarray", "defaulted" - except ImportError: - msg3 = str(get_exception()) + except ImportError as e: + msg3 = str(e) log.info(msg1) log.info(msg2) log.info(msg3) diff --git a/numpy/distutils/tests/test_exec_command.py b/numpy/distutils/tests/test_exec_command.py index 37912f5ba..2ac0a6308 100644 --- a/numpy/distutils/tests/test_exec_command.py +++ b/numpy/distutils/tests/test_exec_command.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys from tempfile import TemporaryFile @@ -15,7 +13,7 @@ if sys.version_info[0] >= 3: else: from StringIO import StringIO -class redirect_stdout(object): +class redirect_stdout: """Context manager to redirect stdout for exec_command test.""" def __init__(self, stdout=None): self._stdout = stdout or sys.stdout @@ -30,7 +28,7 @@ class redirect_stdout(object): # note: closing sys.stdout won't close it. self._stdout.close() -class redirect_stderr(object): +class redirect_stderr: """Context manager to redirect stderr for exec_command test.""" def __init__(self, stderr=None): self._stderr = stderr or sys.stderr @@ -45,7 +43,7 @@ class redirect_stderr(object): # note: closing sys.stderr won't close it. self._stderr.close() -class emulate_nonposix(object): +class emulate_nonposix: """Context manager to emulate os.name != 'posix' """ def __init__(self, osname='non-posix'): self._new_name = osname @@ -98,7 +96,7 @@ def test_exec_command_stderr(): exec_command.exec_command("cd '.'") -class TestExecCommand(object): +class TestExecCommand: def setup(self): self.pyexe = get_pythonexe() diff --git a/numpy/distutils/tests/test_fcompiler.py b/numpy/distutils/tests/test_fcompiler.py index 6d245fbd4..c559becf8 100644 --- a/numpy/distutils/tests/test_fcompiler.py +++ b/numpy/distutils/tests/test_fcompiler.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest from numpy.testing import assert_, suppress_warnings diff --git a/numpy/distutils/tests/test_fcompiler_gnu.py b/numpy/distutils/tests/test_fcompiler_gnu.py index 49208aace..0817ae58c 100644 --- a/numpy/distutils/tests/test_fcompiler_gnu.py +++ b/numpy/distutils/tests/test_fcompiler_gnu.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.testing import assert_ import numpy.distutils.fcompiler @@ -30,7 +28,7 @@ gfortran_version_strings = [ ('GNU Fortran (crosstool-NG 8a21ab48) 7.2.0', '7.2.0') ] -class TestG77Versions(object): +class TestG77Versions: def test_g77_version(self): fc = numpy.distutils.fcompiler.new_fcompiler(compiler='gnu') for vs, version in g77_version_strings: @@ -43,7 +41,7 @@ class TestG77Versions(object): v = fc.version_match(vs) assert_(v is None, (vs, v)) -class TestGFortranVersions(object): +class TestGFortranVersions: def test_gfortran_version(self): fc = numpy.distutils.fcompiler.new_fcompiler(compiler='gnu95') for vs, version in gfortran_version_strings: diff --git a/numpy/distutils/tests/test_fcompiler_intel.py b/numpy/distutils/tests/test_fcompiler_intel.py index 5e014bada..45c9cdac1 100644 --- a/numpy/distutils/tests/test_fcompiler_intel.py +++ b/numpy/distutils/tests/test_fcompiler_intel.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy.distutils.fcompiler from numpy.testing import assert_ @@ -16,7 +14,7 @@ intel_64bit_version_strings = [ "running on Intel(R) 64, Version 11.1", '11.1') ] -class TestIntelFCompilerVersions(object): +class TestIntelFCompilerVersions: def test_32bit_version(self): fc = numpy.distutils.fcompiler.new_fcompiler(compiler='intel') for vs, version in intel_32bit_version_strings: @@ -24,7 +22,7 @@ class TestIntelFCompilerVersions(object): assert_(v == version) -class TestIntelEM64TFCompilerVersions(object): +class TestIntelEM64TFCompilerVersions: def test_64bit_version(self): fc = numpy.distutils.fcompiler.new_fcompiler(compiler='intelem') for vs, version in intel_64bit_version_strings: diff --git a/numpy/distutils/tests/test_fcompiler_nagfor.py b/numpy/distutils/tests/test_fcompiler_nagfor.py index 1c936056a..2e04f5266 100644 --- a/numpy/distutils/tests/test_fcompiler_nagfor.py +++ b/numpy/distutils/tests/test_fcompiler_nagfor.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from numpy.testing import assert_ import numpy.distutils.fcompiler @@ -16,7 +14,7 @@ nag_version_strings = [('nagfor', 'NAG Fortran Compiler Release ' '431,435,437,446,459-460,463,472,494,496,503,508,' '511,517,529,555,557,565)', '5.1')] -class TestNagFCompilerVersions(object): +class TestNagFCompilerVersions: def test_version_match(self): for comp, vs, version in nag_version_strings: fc = numpy.distutils.fcompiler.new_fcompiler(compiler=comp) diff --git a/numpy/distutils/tests/test_misc_util.py b/numpy/distutils/tests/test_misc_util.py index 3e239cf48..605c80483 100644 --- a/numpy/distutils/tests/test_misc_util.py +++ b/numpy/distutils/tests/test_misc_util.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from os.path import join, sep, dirname from numpy.distutils.misc_util import ( @@ -11,7 +9,7 @@ from numpy.testing import ( ajoin = lambda *paths: join(*((sep,)+paths)) -class TestAppendpath(object): +class TestAppendpath: def test_1(self): assert_equal(appendpath('prefix', 'name'), join('prefix', 'name')) @@ -35,7 +33,7 @@ class TestAppendpath(object): assert_equal(appendpath('/prefix/sub/sub2', '/prefix/sub/sup/name'), ajoin('prefix', 'sub', 'sub2', 'sup', 'name')) -class TestMinrelpath(object): +class TestMinrelpath: def test_1(self): n = lambda path: path.replace('/', sep) @@ -49,7 +47,7 @@ class TestMinrelpath(object): assert_equal(minrelpath(n('.././..')), n('../..')) assert_equal(minrelpath(n('aa/bb/.././../dd')), n('dd')) -class TestGpaths(object): +class TestGpaths: def test_gpaths(self): local_path = minrelpath(join(dirname(__file__), '..')) @@ -58,7 +56,7 @@ class TestGpaths(object): f = gpaths('system_info.py', local_path) assert_(join(local_path, 'system_info.py') == f[0], repr(f)) -class TestSharedExtension(object): +class TestSharedExtension: def test_get_shared_lib_extension(self): import sys diff --git a/numpy/distutils/tests/test_npy_pkg_config.py b/numpy/distutils/tests/test_npy_pkg_config.py index 537e16e90..b287ebe2e 100644 --- a/numpy/distutils/tests/test_npy_pkg_config.py +++ b/numpy/distutils/tests/test_npy_pkg_config.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os from numpy.distutils.npy_pkg_config import read_config, parse_flags @@ -36,7 +34,7 @@ libs = -L${libdir} simple_variable_d = {'cflags': '-I/foo/bar/include', 'libflags': '-L/foo/bar/lib', 'version': '0.1', 'name': 'foo'} -class TestLibraryInfo(object): +class TestLibraryInfo: def test_simple(self): with temppath('foo.ini') as path: with open(path, 'w') as f: @@ -63,7 +61,7 @@ class TestLibraryInfo(object): out.vars['prefix'] = '/Users/david' assert_(out.cflags() == '-I/Users/david/include') -class TestParseFlags(object): +class TestParseFlags: def test_simple_cflags(self): d = parse_flags("-I/usr/include") assert_(d['include_dirs'] == ['/usr/include']) diff --git a/numpy/distutils/tests/test_shell_utils.py b/numpy/distutils/tests/test_shell_utils.py index a0344244f..754609a5d 100644 --- a/numpy/distutils/tests/test_shell_utils.py +++ b/numpy/distutils/tests/test_shell_utils.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest import subprocess import os diff --git a/numpy/distutils/tests/test_system_info.py b/numpy/distutils/tests/test_system_info.py index 3c7638960..c40cdb7db 100644 --- a/numpy/distutils/tests/test_system_info.py +++ b/numpy/distutils/tests/test_system_info.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - import os import shutil import pytest @@ -130,7 +128,7 @@ class DuplicateOptionInfo(_system_info): section = 'duplicate_options' -class TestSystemInfoReading(object): +class TestSystemInfoReading: def setup(self): """ Create the libraries """ diff --git a/numpy/distutils/unixccompiler.py b/numpy/distutils/unixccompiler.py index 11b2cce52..9a4d3ba52 100644 --- a/numpy/distutils/unixccompiler.py +++ b/numpy/distutils/unixccompiler.py @@ -2,14 +2,11 @@ unixccompiler - can handle very long argument lists for ar. """ -from __future__ import division, absolute_import, print_function - import os from distutils.errors import DistutilsExecError, CompileError from distutils.unixccompiler import * from numpy.distutils.ccompiler import replace_method -from numpy.distutils.compat import get_exception from numpy.distutils.misc_util import _commandline_dep_string if sys.version_info[0] < 3: @@ -56,8 +53,8 @@ def UnixCCompiler__compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts try: self.spawn(self.compiler_so + cc_args + [src, '-o', obj] + deps + extra_postargs, display = display) - except DistutilsExecError: - msg = str(get_exception()) + except DistutilsExecError as e: + msg = str(e) raise CompileError(msg) # add commandline flags to dependency file @@ -128,8 +125,8 @@ def UnixCCompiler_create_static_lib(self, objects, output_libname, try: self.spawn(self.ranlib + [output_filename], display = display) - except DistutilsExecError: - msg = str(get_exception()) + except DistutilsExecError as e: + msg = str(e) raise LibError(msg) else: log.debug("skipping %s (up-to-date)", output_filename) diff --git a/numpy/doc/__init__.py b/numpy/doc/__init__.py index b6f1fa71c..8a944fecd 100644 --- a/numpy/doc/__init__.py +++ b/numpy/doc/__init__.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os ref_dir = os.path.join(os.path.dirname(__file__)) diff --git a/numpy/doc/basics.py b/numpy/doc/basics.py index c05f347a1..635c1b1b8 100644 --- a/numpy/doc/basics.py +++ b/numpy/doc/basics.py @@ -339,4 +339,3 @@ be useful to test your code with the value ``1 + np.finfo(np.longdouble).eps``. """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/broadcasting.py b/numpy/doc/broadcasting.py index cb548a0d0..63975e6a9 100644 --- a/numpy/doc/broadcasting.py +++ b/numpy/doc/broadcasting.py @@ -178,4 +178,3 @@ making it a two-dimensional ``4x1`` array. Combining the ``4x1`` array with ``b``, which has shape ``(3,)``, yields a ``4x3`` array. """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/byteswapping.py b/numpy/doc/byteswapping.py index 7a749c8d5..fe9461977 100644 --- a/numpy/doc/byteswapping.py +++ b/numpy/doc/byteswapping.py @@ -153,4 +153,3 @@ can be achieved with the ndarray astype method: False """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/constants.py b/numpy/doc/constants.py index 72793e44d..96813b66c 100644 --- a/numpy/doc/constants.py +++ b/numpy/doc/constants.py @@ -13,8 +13,6 @@ NumPy includes several constants: # # Note: the docstring is autogenerated. # -from __future__ import division, absolute_import, print_function - import textwrap, re # Maintain same format as in numpy.add_newdocs diff --git a/numpy/doc/creation.py b/numpy/doc/creation.py index 9ebe938be..067f8bb33 100644 --- a/numpy/doc/creation.py +++ b/numpy/doc/creation.py @@ -141,4 +141,3 @@ random values, and some utility functions to generate special matrices (e.g. diagonal). """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/dispatch.py b/numpy/doc/dispatch.py index c9029941b..ba76a43ae 100644 --- a/numpy/doc/dispatch.py +++ b/numpy/doc/dispatch.py @@ -58,7 +58,7 @@ numpy.ndarray How can we pass our custom array type through this function? Numpy allows a class to indicate that it would like to handle computations in a custom-defined -way through the interaces ``__array_ufunc__`` and ``__array_function__``. Let's +way through the interfaces ``__array_ufunc__`` and ``__array_function__``. Let's take one at a time, starting with ``_array_ufunc__``. This method covers :ref:`ufuncs`, a class of functions that includes, for example, :func:`numpy.multiply` and :func:`numpy.sin`. diff --git a/numpy/doc/glossary.py b/numpy/doc/glossary.py index 7d1c9a1d5..6d2e0010f 100644 --- a/numpy/doc/glossary.py +++ b/numpy/doc/glossary.py @@ -182,7 +182,7 @@ Glossary instance A class definition gives the blueprint for constructing an object:: - >>> class House(object): + >>> class House: ... wall_colour = 'white' Yet, we have to *build* a house before it exists:: @@ -336,7 +336,7 @@ Glossary Often seen in method signatures, ``self`` refers to the instance of the associated class. For example: - >>> class Paintbrush(object): + >>> class Paintbrush: ... color = 'blue' ... ... def paint(self): @@ -473,4 +473,3 @@ Glossary and f2py (which wraps Fortran). """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/indexing.py b/numpy/doc/indexing.py index 676015668..aa84e2b11 100644 --- a/numpy/doc/indexing.py +++ b/numpy/doc/indexing.py @@ -446,4 +446,3 @@ converted to an array as a list would be. As an example: :: 40 """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/internals.py b/numpy/doc/internals.py index a14fee7c2..6718f1108 100644 --- a/numpy/doc/internals.py +++ b/numpy/doc/internals.py @@ -160,4 +160,3 @@ when accessing elements of an array. Granted, it goes against the grain, but it is more in line with Python semantics and the natural order of the data. """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/misc.py b/numpy/doc/misc.py index a76abe164..fc1c4cd01 100644 --- a/numpy/doc/misc.py +++ b/numpy/doc/misc.py @@ -224,4 +224,3 @@ Interfacing to C++: 5) SIP (used mainly in PyQT) """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/structured_arrays.py b/numpy/doc/structured_arrays.py index 1343d2adc..72990cf89 100644 --- a/numpy/doc/structured_arrays.py +++ b/numpy/doc/structured_arrays.py @@ -644,4 +644,3 @@ attribute takes precedence. Such fields will be inaccessible by attribute but will still be accessible by index. """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/subclassing.py b/numpy/doc/subclassing.py index d0685328e..5a54ddd90 100644 --- a/numpy/doc/subclassing.py +++ b/numpy/doc/subclassing.py @@ -114,7 +114,7 @@ For example, consider the following Python code: .. testcode:: - class C(object): + class C: def __new__(cls, *args): print('Cls in __new__:', cls) print('Args in __new__:', args) @@ -454,7 +454,7 @@ following. input numpy as np class A(np.ndarray): - def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + def __array_ufunc__(self, ufunc, method, *inputs, out=None, **kwargs): args = [] in_no = [] for i, input_ in enumerate(inputs): @@ -464,7 +464,7 @@ following. else: args.append(input_) - outputs = kwargs.pop('out', None) + outputs = out out_no = [] if outputs: out_args = [] @@ -750,4 +750,3 @@ This object is now compatible with ``np.sum`` again because any extraneous argum ``**unused_kwargs`` parameter. """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/doc/ufuncs.py b/numpy/doc/ufuncs.py index df2c455ec..eecc15083 100644 --- a/numpy/doc/ufuncs.py +++ b/numpy/doc/ufuncs.py @@ -135,4 +135,3 @@ results in an error. There are two alternatives: a convenient way to apply these operators. """ -from __future__ import division, absolute_import, print_function diff --git a/numpy/dual.py b/numpy/dual.py index 651e845bb..92afec52d 100644 --- a/numpy/dual.py +++ b/numpy/dual.py @@ -10,8 +10,6 @@ NumPy. .. _Scipy : https://www.scipy.org """ -from __future__ import division, absolute_import, print_function - # This module should be used for functions both in numpy and scipy if # you want to use the numpy version if available but the scipy version # otherwise. diff --git a/numpy/f2py/__init__.py b/numpy/f2py/__init__.py index 42e3632fd..13ffef6fc 100644 --- a/numpy/f2py/__init__.py +++ b/numpy/f2py/__init__.py @@ -2,8 +2,6 @@ """Fortran to Python Interface Generator. """ -from __future__ import division, absolute_import, print_function - __all__ = ['run_main', 'compile', 'f2py_testing'] import sys diff --git a/numpy/f2py/__main__.py b/numpy/f2py/__main__.py index 708f7f362..c6115070e 100644 --- a/numpy/f2py/__main__.py +++ b/numpy/f2py/__main__.py @@ -1,6 +1,4 @@ # See http://cens.ioc.ee/projects/f2py2e/ -from __future__ import division, print_function - from numpy.f2py.f2py2e import main main() diff --git a/numpy/f2py/__version__.py b/numpy/f2py/__version__.py index 49a2199bf..104c2e1a8 100644 --- a/numpy/f2py/__version__.py +++ b/numpy/f2py/__version__.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - major = 2 try: diff --git a/numpy/f2py/auxfuncs.py b/numpy/f2py/auxfuncs.py index 404bdbd2d..31802621e 100644 --- a/numpy/f2py/auxfuncs.py +++ b/numpy/f2py/auxfuncs.py @@ -14,8 +14,6 @@ $Date: 2005/07/24 19:01:55 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - import pprint import sys import types @@ -552,7 +550,7 @@ class F2PYError(Exception): pass -class throw_error(object): +class throw_error: def __init__(self, mess): self.mess = mess diff --git a/numpy/f2py/capi_maps.py b/numpy/f2py/capi_maps.py index ce79f680f..6f9ff7bc6 100644 --- a/numpy/f2py/capi_maps.py +++ b/numpy/f2py/capi_maps.py @@ -11,8 +11,6 @@ $Date: 2005/05/06 10:57:33 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - __version__ = "$Revision: 1.60 $"[10:-1] from . import __version__ @@ -79,7 +77,7 @@ c2capi_map = {'double': 'NPY_DOUBLE', 'complex_long_double': 'NPY_CDOUBLE', # forced casting 'string': 'NPY_STRING'} -# These new maps aren't used anyhere yet, but should be by default +# These new maps aren't used anywhere yet, but should be by default # unless building numeric or numarray extensions. if using_newcore: c2capi_map = {'double': 'NPY_DOUBLE', diff --git a/numpy/f2py/cb_rules.py b/numpy/f2py/cb_rules.py index 183d7c2f9..dc178078d 100644 --- a/numpy/f2py/cb_rules.py +++ b/numpy/f2py/cb_rules.py @@ -13,8 +13,6 @@ $Date: 2005/07/20 11:27:58 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - from . import __version__ from .auxfuncs import ( applyrules, debugcapi, dictappend, errmess, getargs, hasnote, isarray, diff --git a/numpy/f2py/cfuncs.py b/numpy/f2py/cfuncs.py index cede06119..28b2c0670 100644 --- a/numpy/f2py/cfuncs.py +++ b/numpy/f2py/cfuncs.py @@ -14,8 +14,6 @@ $Date: 2005/05/06 11:42:34 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - import sys import copy @@ -646,7 +644,6 @@ fprintf(stderr,\"string_from_pyobj(str='%s',len=%d,inistr='%s',obj=%p)\\n\",(cha tmp = obj; Py_INCREF(tmp); } -#if PY_VERSION_HEX >= 0x03000000 else if (PyUnicode_Check(obj)) { tmp = PyUnicode_AsASCIIString(obj); } @@ -661,11 +658,6 @@ fprintf(stderr,\"string_from_pyobj(str='%s',len=%d,inistr='%s',obj=%p)\\n\",(cha tmp = NULL; } } -#else - else { - tmp = PyObject_Str(obj); - } -#endif if (tmp == NULL) goto capi_fail; if (*len == -1) *len = PyString_GET_SIZE(tmp); @@ -1094,13 +1086,8 @@ if (tmp_fun==NULL) { fprintf(stderr,\"Call-back argument must be function|instance|instance.__call__|f2py-function but got %s.\\n\",(fun==NULL?\"NULL\":Py_TYPE(fun)->tp_name)); goto capi_fail; } -#if PY_VERSION_HEX >= 0x03000000 if (PyObject_HasAttrString(tmp_fun,\"__code__\")) { if (PyObject_HasAttrString(tmp = PyObject_GetAttrString(tmp_fun,\"__code__\"),\"co_argcount\")) { -#else - if (PyObject_HasAttrString(tmp_fun,\"func_code\")) { - if (PyObject_HasAttrString(tmp = PyObject_GetAttrString(tmp_fun,\"func_code\"),\"co_argcount\")) { -#endif PyObject *tmp_argcount = PyObject_GetAttrString(tmp,\"co_argcount\"); Py_DECREF(tmp); if (tmp_argcount == NULL) { @@ -1111,13 +1098,8 @@ goto capi_fail; } } /* Get the number of optional arguments */ -#if PY_VERSION_HEX >= 0x03000000 if (PyObject_HasAttrString(tmp_fun,\"__defaults__\")) { if (PyTuple_Check(tmp = PyObject_GetAttrString(tmp_fun,\"__defaults__\"))) -#else - if (PyObject_HasAttrString(tmp_fun,\"func_defaults\")) { - if (PyTuple_Check(tmp = PyObject_GetAttrString(tmp_fun,\"func_defaults\"))) -#endif opt = PyTuple_Size(tmp); Py_XDECREF(tmp); } diff --git a/numpy/f2py/common_rules.py b/numpy/f2py/common_rules.py index f61d8810a..31aefcda9 100644 --- a/numpy/f2py/common_rules.py +++ b/numpy/f2py/common_rules.py @@ -13,8 +13,6 @@ $Date: 2005/05/06 10:57:33 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - __version__ = "$Revision: 1.19 $"[10:-1] from . import __version__ diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py index 2db4a47e8..09bab11bd 100755 --- a/numpy/f2py/crackfortran.py +++ b/numpy/f2py/crackfortran.py @@ -138,8 +138,6 @@ TODO: The above may be solved by creating appropriate preprocessor program, for example. """ -from __future__ import division, absolute_import, print_function - import sys import string import fileinput @@ -3115,11 +3113,12 @@ def true_intent_list(var): ret = [] for intent in lst: try: - c = eval('isintent_%s(var)' % intent) - except NameError: - c = 0 - if c: - ret.append(intent) + f = globals()['isintent_%s' % intent] + except KeyError: + pass + else: + if f(var): + ret.append(intent) return ret diff --git a/numpy/f2py/diagnose.py b/numpy/f2py/diagnose.py index 0241fed12..092368c82 100644 --- a/numpy/f2py/diagnose.py +++ b/numpy/f2py/diagnose.py @@ -1,6 +1,4 @@ #!/usr/bin/env python -from __future__ import division, absolute_import, print_function - import os import sys import tempfile diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py index d03eff9e3..a6751154b 100755 --- a/numpy/f2py/f2py2e.py +++ b/numpy/f2py/f2py2e.py @@ -14,8 +14,6 @@ $Date: 2005/05/06 08:31:19 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - import sys import os import pprint diff --git a/numpy/f2py/f2py_testing.py b/numpy/f2py/f2py_testing.py index f5d5fa63d..1f109e67a 100644 --- a/numpy/f2py/f2py_testing.py +++ b/numpy/f2py/f2py_testing.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import re diff --git a/numpy/f2py/f90mod_rules.py b/numpy/f2py/f90mod_rules.py index 85eae8047..f4f1bf1a9 100644 --- a/numpy/f2py/f90mod_rules.py +++ b/numpy/f2py/f90mod_rules.py @@ -13,8 +13,6 @@ $Date: 2005/02/03 19:30:23 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - __version__ = "$Revision: 1.27 $"[10:-1] f2py_version = 'See `f2py -v`' @@ -180,7 +178,7 @@ def buildhooks(pymod): (m['name'], undo_rmbadname1(n))) fadd('integer flag\n') fhooks[0] = fhooks[0] + fgetdims1 - dms = eval('range(1,%s+1)' % (dm['rank'])) + dms = range(1, int(dm['rank']) + 1) fadd(' allocate(d(%s))\n' % (','.join(['s(%s)' % i for i in dms]))) fhooks[0] = fhooks[0] + use_fgetdims2 diff --git a/numpy/f2py/func2subr.py b/numpy/f2py/func2subr.py index 6010d5a23..8e18a3236 100644 --- a/numpy/f2py/func2subr.py +++ b/numpy/f2py/func2subr.py @@ -13,8 +13,6 @@ $Date: 2004/11/26 11:13:06 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - __version__ = "$Revision: 1.16 $"[10:-1] f2py_version = 'See `f2py -v`' diff --git a/numpy/f2py/rules.py b/numpy/f2py/rules.py index 28eb9da30..459c87aa3 100755 --- a/numpy/f2py/rules.py +++ b/numpy/f2py/rules.py @@ -50,8 +50,6 @@ $Date: 2005/08/30 08:58:42 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - __version__ = "$Revision: 1.129 $"[10:-1] from . import __version__ @@ -180,7 +178,6 @@ static PyMethodDef f2py_module_methods[] = { \t{NULL,NULL} }; -#if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef moduledef = { \tPyModuleDef_HEAD_INIT, \t"#modulename#", @@ -192,35 +189,20 @@ static struct PyModuleDef moduledef = { \tNULL, \tNULL }; -#endif -#if PY_VERSION_HEX >= 0x03000000 -#define RETVAL m PyMODINIT_FUNC PyInit_#modulename#(void) { -#else -#define RETVAL -PyMODINIT_FUNC init#modulename#(void) { -#endif \tint i; \tPyObject *m,*d, *s, *tmp; -#if PY_VERSION_HEX >= 0x03000000 \tm = #modulename#_module = PyModule_Create(&moduledef); -#else -\tm = #modulename#_module = Py_InitModule(\"#modulename#\", f2py_module_methods); -#endif \tPy_TYPE(&PyFortran_Type) = &PyType_Type; \timport_array(); \tif (PyErr_Occurred()) -\t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return RETVAL;} +\t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return m;} \td = PyModule_GetDict(m); \ts = PyString_FromString(\"$R""" + """evision: $\"); \tPyDict_SetItemString(d, \"__version__\", s); \tPy_DECREF(s); -#if PY_VERSION_HEX >= 0x03000000 \ts = PyUnicode_FromString( -#else -\ts = PyString_FromString( -#endif \t\t\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\"); \tPyDict_SetItemString(d, \"__doc__\", s); \tPy_DECREF(s); @@ -245,7 +227,7 @@ PyMODINIT_FUNC init#modulename#(void) { \tif (! PyErr_Occurred()) \t\ton_exit(f2py_report_on_exit,(void*)\"#modulename#\"); #endif -\treturn RETVAL; +\treturn m; } #ifdef __cplusplus } @@ -448,11 +430,7 @@ rout_rules = [ tmp = F2PyCapsule_FromVoidPtr((void*)#F_FUNC#(#name_lower#,#NAME#),NULL); PyObject_SetAttrString(o,"_cpointer", tmp); Py_DECREF(tmp); -#if PY_VERSION_HEX >= 0x03000000 s = PyUnicode_FromString("#name#"); -#else - s = PyString_FromString("#name#"); -#endif PyObject_SetAttrString(o,"__name__", s); Py_DECREF(s); } @@ -490,11 +468,7 @@ rout_rules = [ tmp = F2PyCapsule_FromVoidPtr((void*)#F_FUNC#(#name_lower#,#NAME#),NULL); PyObject_SetAttrString(o,"_cpointer", tmp); Py_DECREF(tmp); -#if PY_VERSION_HEX >= 0x03000000 s = PyUnicode_FromString("#name#"); -#else - s = PyString_FromString("#name#"); -#endif PyObject_SetAttrString(o,"__name__", s); Py_DECREF(s); } diff --git a/numpy/f2py/setup.py b/numpy/f2py/setup.py index a8c1401aa..5e4d7cd56 100644 --- a/numpy/f2py/setup.py +++ b/numpy/f2py/setup.py @@ -16,8 +16,6 @@ $Date: 2005/01/30 17:22:14 $ Pearu Peterson """ -from __future__ import division, print_function - from numpy.distutils.core import setup from numpy.distutils.misc_util import Configuration diff --git a/numpy/f2py/src/fortranobject.c b/numpy/f2py/src/fortranobject.c index eb1050dd7..8ec5b510f 100644 --- a/numpy/f2py/src/fortranobject.c +++ b/numpy/f2py/src/fortranobject.c @@ -115,14 +115,6 @@ fortran_dealloc(PyFortranObject *fp) { } -#if PY_VERSION_HEX >= 0x03000000 -#else -static PyMethodDef fortran_methods[] = { - {NULL, NULL} /* sentinel */ -}; -#endif - - /* Returns number of bytes consumed from buf, or -1 on error. */ static Py_ssize_t format_def(char *buf, Py_ssize_t size, FortranDataDef def) @@ -242,11 +234,7 @@ fortran_doc(FortranDataDef def) size--; /* p now points one beyond the last character of the string in buf */ -#if PY_VERSION_HEX >= 0x03000000 s = PyUnicode_FromStringAndSize(buf, p - buf); -#else - s = PyString_FromStringAndSize(buf, p - buf); -#endif PyMem_Free(buf); return s; @@ -306,7 +294,6 @@ fortran_getattr(PyFortranObject *fp, char *name) { return fp->dict; } if (strcmp(name,"__doc__")==0) { -#if PY_VERSION_HEX >= 0x03000000 PyObject *s = PyUnicode_FromString(""), *s2, *s3; for (i=0;i<fp->len;i++) { s2 = fortran_doc(fp->defs[i]); @@ -315,11 +302,6 @@ fortran_getattr(PyFortranObject *fp, char *name) { Py_DECREF(s); s = s3; } -#else - PyObject *s = PyString_FromString(""); - for (i=0;i<fp->len;i++) - PyString_ConcatAndDel(&s,fortran_doc(fp->defs[i])); -#endif if (PyDict_SetItemString(fp->dict, name, s)) return NULL; return s; @@ -330,17 +312,11 @@ fortran_getattr(PyFortranObject *fp, char *name) { return NULL; return cobj; } -#if PY_VERSION_HEX >= 0x03000000 - if (1) { - PyObject *str, *ret; - str = PyUnicode_FromString(name); - ret = PyObject_GenericGetAttr((PyObject *)fp, str); - Py_DECREF(str); - return ret; - } -#else - return Py_FindMethod(fortran_methods, (PyObject *)fp, name); -#endif + PyObject *str, *ret; + str = PyUnicode_FromString(name); + ret = PyObject_GenericGetAttr((PyObject *)fp, str); + Py_DECREF(str); + return ret; } static int @@ -434,33 +410,19 @@ fortran_repr(PyFortranObject *fp) PyObject *name = NULL, *repr = NULL; name = PyObject_GetAttrString((PyObject *)fp, "__name__"); PyErr_Clear(); -#if PY_VERSION_HEX >= 0x03000000 if (name != NULL && PyUnicode_Check(name)) { repr = PyUnicode_FromFormat("<fortran %U>", name); } else { repr = PyUnicode_FromString("<fortran object>"); } -#else - if (name != NULL && PyString_Check(name)) { - repr = PyString_FromFormat("<fortran %s>", PyString_AsString(name)); - } - else { - repr = PyString_FromString("<fortran object>"); - } -#endif Py_XDECREF(name); return repr; } PyTypeObject PyFortran_Type = { -#if PY_VERSION_HEX >= 0x03000000 PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(0) - 0, /*ob_size*/ -#endif "fortran", /*tp_name*/ sizeof(PyFortranObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -626,7 +588,7 @@ count_negative_dimensions(const int rank, } #ifdef DEBUG_COPY_ND_ARRAY -void dump_dims(int rank, npy_intp* dims) { +void dump_dims(int rank, npy_intp const* dims) { int i; printf("["); for(i=0;i<rank;++i) { @@ -1053,8 +1015,6 @@ int copy_ND_array(const PyArrayObject *arr, PyArrayObject *out) /* Compatibility functions for Python >= 3.0 */ /*********************************************/ -#if PY_VERSION_HEX >= 0x03000000 - PyObject * F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *)) { @@ -1081,29 +1041,6 @@ F2PyCapsule_Check(PyObject *ptr) return PyCapsule_CheckExact(ptr); } -#else - -PyObject * -F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *)) -{ - return PyCObject_FromVoidPtr(ptr, dtor); -} - -void * -F2PyCapsule_AsVoidPtr(PyObject *ptr) -{ - return PyCObject_AsVoidPtr(ptr); -} - -int -F2PyCapsule_Check(PyObject *ptr) -{ - return PyCObject_Check(ptr); -} - -#endif - - #ifdef __cplusplus } #endif diff --git a/numpy/f2py/src/fortranobject.h b/numpy/f2py/src/fortranobject.h index 21f1977eb..5c382ab7b 100644 --- a/numpy/f2py/src/fortranobject.h +++ b/numpy/f2py/src/fortranobject.h @@ -82,20 +82,10 @@ typedef struct { extern PyObject * PyFortranObject_New(FortranDataDef* defs, f2py_void_func init); extern PyObject * PyFortranObject_NewAsAttr(FortranDataDef* defs); -#if PY_VERSION_HEX >= 0x03000000 - PyObject * F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *)); void * F2PyCapsule_AsVoidPtr(PyObject *obj); int F2PyCapsule_Check(PyObject *ptr); -#else - -PyObject * F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *)); -void * F2PyCapsule_AsVoidPtr(PyObject *ptr); -int F2PyCapsule_Check(PyObject *ptr); - -#endif - #define ISCONTIGUOUS(m) (PyArray_FLAGS(m) & NPY_ARRAY_C_CONTIGUOUS) #define F2PY_INTENT_IN 1 #define F2PY_INTENT_INOUT 2 diff --git a/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c b/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c index 978db4e69..83c0da2cf 100644 --- a/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c +++ b/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c @@ -129,7 +129,6 @@ static PyMethodDef f2py_module_methods[] = { {NULL,NULL} }; -#if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "test_array_from_pyobj_ext", @@ -141,21 +140,10 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif -#if PY_VERSION_HEX >= 0x03000000 -#define RETVAL m PyMODINIT_FUNC PyInit_test_array_from_pyobj_ext(void) { -#else -#define RETVAL -PyMODINIT_FUNC inittest_array_from_pyobj_ext(void) { -#endif PyObject *m,*d, *s; -#if PY_VERSION_HEX >= 0x03000000 m = wrap_module = PyModule_Create(&moduledef); -#else - m = wrap_module = Py_InitModule("test_array_from_pyobj_ext", f2py_module_methods); -#endif Py_TYPE(&PyFortran_Type) = &PyType_Type; import_array(); if (PyErr_Occurred()) @@ -238,7 +226,7 @@ PyMODINIT_FUNC inittest_array_from_pyobj_ext(void) { on_exit(f2py_report_on_exit,(void*)"array_from_pyobj.wrap.call"); #endif - return RETVAL; + return m; } #ifdef __cplusplus } diff --git a/numpy/f2py/tests/test_array_from_pyobj.py b/numpy/f2py/tests/test_array_from_pyobj.py index a80090185..b719f2495 100644 --- a/numpy/f2py/tests/test_array_from_pyobj.py +++ b/numpy/f2py/tests/test_array_from_pyobj.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys import copy @@ -57,7 +55,7 @@ def flags2names(flags): return info -class Intent(object): +class Intent: def __init__(self, intent_list=[]): self.intent_list = intent_list[:] @@ -131,7 +129,7 @@ if ((intp().dtype.itemsize != 4 or clongdouble().dtype.alignment <= 8) and _cast_dict['CDOUBLE'] = _cast_dict['DOUBLE'] + ['CFLOAT', 'CDOUBLE'] -class Type(object): +class Type: _type_cache = {} def __new__(cls, name): @@ -192,7 +190,7 @@ class Type(object): return types -class Array(object): +class Array: def __init__(self, typ, dims, intent, obj): self.type = typ @@ -293,7 +291,7 @@ class Array(object): return obj_attr[0] == self.arr_attr[0] -class TestIntent(object): +class TestIntent: def test_in_out(self): assert_equal(str(intent.in_.out), 'intent(in,out)') @@ -304,7 +302,7 @@ class TestIntent(object): assert_(not intent.in_.is_intent('c')) -class TestSharedMemory(object): +class TestSharedMemory: num2seq = [1, 2] num23seq = [[1, 2, 3], [4, 5, 6]] diff --git a/numpy/f2py/tests/test_assumed_shape.py b/numpy/f2py/tests/test_assumed_shape.py index e5695a61c..dfc252660 100644 --- a/numpy/f2py/tests/test_assumed_shape.py +++ b/numpy/f2py/tests/test_assumed_shape.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import pytest import tempfile diff --git a/numpy/f2py/tests/test_block_docstring.py b/numpy/f2py/tests/test_block_docstring.py index 4f1678980..03660f021 100644 --- a/numpy/f2py/tests/test_block_docstring.py +++ b/numpy/f2py/tests/test_block_docstring.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import pytest from . import util diff --git a/numpy/f2py/tests/test_callback.py b/numpy/f2py/tests/test_callback.py index 21c29ba5f..7629df605 100644 --- a/numpy/f2py/tests/test_callback.py +++ b/numpy/f2py/tests/test_callback.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import math import textwrap import sys @@ -118,7 +116,7 @@ cf2py intent(out) a r = t(self.module.func0._cpointer) assert_(r == 11, repr(r)) - class A(object): + class A: def __call__(self): return 7 diff --git a/numpy/f2py/tests/test_common.py b/numpy/f2py/tests/test_common.py index dcb01b0ec..e4bf35504 100644 --- a/numpy/f2py/tests/test_common.py +++ b/numpy/f2py/tests/test_common.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys import pytest diff --git a/numpy/f2py/tests/test_compile_function.py b/numpy/f2py/tests/test_compile_function.py index 40ea7997f..d40ed63cf 100644 --- a/numpy/f2py/tests/test_compile_function.py +++ b/numpy/f2py/tests/test_compile_function.py @@ -1,8 +1,6 @@ """See https://github.com/numpy/numpy/pull/11937. """ -from __future__ import division, absolute_import, print_function - import sys import os import uuid diff --git a/numpy/f2py/tests/test_crackfortran.py b/numpy/f2py/tests/test_crackfortran.py index 941696be3..796965e6f 100644 --- a/numpy/f2py/tests/test_crackfortran.py +++ b/numpy/f2py/tests/test_crackfortran.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest import numpy as np @@ -37,4 +35,3 @@ class TestNoSpace(util.F2PyTest): self.module.subc([w, k]) assert_array_equal(k, w + 1) assert self.module.t0(23) == b'2' - diff --git a/numpy/f2py/tests/test_kind.py b/numpy/f2py/tests/test_kind.py index 1f7762a80..a7e2b28ed 100644 --- a/numpy/f2py/tests/test_kind.py +++ b/numpy/f2py/tests/test_kind.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import pytest diff --git a/numpy/f2py/tests/test_mixed.py b/numpy/f2py/tests/test_mixed.py index 0337538ff..fc00ccc43 100644 --- a/numpy/f2py/tests/test_mixed.py +++ b/numpy/f2py/tests/test_mixed.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import textwrap import pytest diff --git a/numpy/f2py/tests/test_parameter.py b/numpy/f2py/tests/test_parameter.py index 6a378687a..b61827169 100644 --- a/numpy/f2py/tests/test_parameter.py +++ b/numpy/f2py/tests/test_parameter.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import pytest diff --git a/numpy/f2py/tests/test_quoted_character.py b/numpy/f2py/tests/test_quoted_character.py index c9a1c36f5..d89ef1385 100644 --- a/numpy/f2py/tests/test_quoted_character.py +++ b/numpy/f2py/tests/test_quoted_character.py @@ -1,8 +1,6 @@ """See https://github.com/numpy/numpy/pull/10676. """ -from __future__ import division, absolute_import, print_function - import sys from importlib import import_module import pytest diff --git a/numpy/f2py/tests/test_regression.py b/numpy/f2py/tests/test_regression.py index 3adae635d..67e00f1f7 100644 --- a/numpy/f2py/tests/test_regression.py +++ b/numpy/f2py/tests/test_regression.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import pytest diff --git a/numpy/f2py/tests/test_return_character.py b/numpy/f2py/tests/test_return_character.py index fc3a58d36..6cb95a8b6 100644 --- a/numpy/f2py/tests/test_return_character.py +++ b/numpy/f2py/tests/test_return_character.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest from numpy import array diff --git a/numpy/f2py/tests/test_return_complex.py b/numpy/f2py/tests/test_return_complex.py index 43c884dfb..9063695bc 100644 --- a/numpy/f2py/tests/test_return_complex.py +++ b/numpy/f2py/tests/test_return_complex.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest from numpy import array diff --git a/numpy/f2py/tests/test_return_integer.py b/numpy/f2py/tests/test_return_integer.py index 22f4acfdf..35f32e37d 100644 --- a/numpy/f2py/tests/test_return_integer.py +++ b/numpy/f2py/tests/test_return_integer.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest from numpy import array diff --git a/numpy/f2py/tests/test_return_logical.py b/numpy/f2py/tests/test_return_logical.py index 96f215a91..3139e0df7 100644 --- a/numpy/f2py/tests/test_return_logical.py +++ b/numpy/f2py/tests/test_return_logical.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest from numpy import array diff --git a/numpy/f2py/tests/test_return_real.py b/numpy/f2py/tests/test_return_real.py index 315cfe49b..1707aab45 100644 --- a/numpy/f2py/tests/test_return_real.py +++ b/numpy/f2py/tests/test_return_real.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import platform import pytest diff --git a/numpy/f2py/tests/test_semicolon_split.py b/numpy/f2py/tests/test_semicolon_split.py index bcd18c893..d8b4bf222 100644 --- a/numpy/f2py/tests/test_semicolon_split.py +++ b/numpy/f2py/tests/test_semicolon_split.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import platform import pytest diff --git a/numpy/f2py/tests/test_size.py b/numpy/f2py/tests/test_size.py index e2af61804..b609fa77f 100644 --- a/numpy/f2py/tests/test_size.py +++ b/numpy/f2py/tests/test_size.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import pytest diff --git a/numpy/f2py/tests/test_string.py b/numpy/f2py/tests/test_string.py index 0493c99cf..e3ec96af9 100644 --- a/numpy/f2py/tests/test_string.py +++ b/numpy/f2py/tests/test_string.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import pytest diff --git a/numpy/f2py/tests/util.py b/numpy/f2py/tests/util.py index bf005df88..c430a6608 100644 --- a/numpy/f2py/tests/util.py +++ b/numpy/f2py/tests/util.py @@ -5,8 +5,6 @@ Utility functions for - detecting if compilers are present """ -from __future__ import division, absolute_import, print_function - import os import sys import subprocess @@ -315,7 +313,7 @@ def build_module_distutils(source_files, config_code, module_name, **kw): # -class F2PyTest(object): +class F2PyTest: code = None sources = None options = [] diff --git a/numpy/f2py/use_rules.py b/numpy/f2py/use_rules.py index 6f44f1634..268c7e81b 100644 --- a/numpy/f2py/use_rules.py +++ b/numpy/f2py/use_rules.py @@ -15,8 +15,6 @@ $Date: 2000/09/10 12:35:43 $ Pearu Peterson """ -from __future__ import division, absolute_import, print_function - __version__ = "$Revision: 1.3 $"[10:-1] f2py_version = 'See `f2py -v`' diff --git a/numpy/fft/__init__.py b/numpy/fft/__init__.py index 37b3f0da6..36cfe81b3 100644 --- a/numpy/fft/__init__.py +++ b/numpy/fft/__init__.py @@ -191,8 +191,6 @@ For examples, see the various functions. """ -from __future__ import division, absolute_import, print_function - from ._pocketfft import * from .helper import * diff --git a/numpy/fft/_pocketfft.c b/numpy/fft/_pocketfft.c index d75b9983c..de86e36d3 100644 --- a/numpy/fft/_pocketfft.c +++ b/numpy/fft/_pocketfft.c @@ -2359,7 +2359,6 @@ static struct PyMethodDef methods[] = { {NULL, NULL, 0, NULL} /* sentinel */ }; -#if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_pocketfft_internal", @@ -2371,30 +2370,14 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif /* Initialization function for the module */ -#if PY_MAJOR_VERSION >= 3 -#define RETVAL(x) x PyMODINIT_FUNC PyInit__pocketfft_internal(void) -#else -#define RETVAL(x) -PyMODINIT_FUNC -init_pocketfft_internal(void) -#endif { PyObject *m; -#if PY_MAJOR_VERSION >= 3 m = PyModule_Create(&moduledef); -#else - static const char module_documentation[] = ""; - - m = Py_InitModule4("_pocketfft_internal", methods, - module_documentation, - (PyObject*)NULL,PYTHON_API_VERSION); -#endif if (m == NULL) { - return RETVAL(NULL); + return NULL; } /* Import the array object */ @@ -2402,5 +2385,5 @@ init_pocketfft_internal(void) /* XXXX Add constants here */ - return RETVAL(m); + return m; } diff --git a/numpy/fft/_pocketfft.py b/numpy/fft/_pocketfft.py index 50720cda4..f2510a6c2 100644 --- a/numpy/fft/_pocketfft.py +++ b/numpy/fft/_pocketfft.py @@ -27,8 +27,6 @@ n = n-dimensional transform behavior.) """ -from __future__ import division, absolute_import, print_function - __all__ = ['fft', 'ifft', 'rfft', 'irfft', 'hfft', 'ihfft', 'rfftn', 'irfftn', 'rfft2', 'irfft2', 'fft2', 'ifft2', 'fftn', 'ifftn'] diff --git a/numpy/fft/helper.py b/numpy/fft/helper.py index a920a4ac0..3dacd9ee1 100644 --- a/numpy/fft/helper.py +++ b/numpy/fft/helper.py @@ -2,8 +2,6 @@ Discrete Fourier Transforms - helper.py """ -from __future__ import division, absolute_import, print_function - from numpy.compat import integer_types from numpy.core import integer, empty, arange, asarray, roll from numpy.core.overrides import array_function_dispatch, set_module diff --git a/numpy/fft/setup.py b/numpy/fft/setup.py index 8c3a31557..40d632ec5 100644 --- a/numpy/fft/setup.py +++ b/numpy/fft/setup.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration diff --git a/numpy/fft/tests/test_helper.py b/numpy/fft/tests/test_helper.py index 6613c8002..2b457271b 100644 --- a/numpy/fft/tests/test_helper.py +++ b/numpy/fft/tests/test_helper.py @@ -3,13 +3,12 @@ Copied from fftpack.helper by Pearu Peterson, October 2005 """ -from __future__ import division, absolute_import, print_function import numpy as np from numpy.testing import assert_array_almost_equal, assert_equal from numpy import fft, pi -class TestFFTShift(object): +class TestFFTShift: def test_definition(self): x = [0, 1, 2, 3, 4, -4, -3, -2, -1] @@ -135,7 +134,7 @@ class TestFFTShift(object): original_ifftshift(inp, axes_keyword)) -class TestFFTFreq(object): +class TestFFTFreq: def test_definition(self): x = [0, 1, 2, 3, 4, -4, -3, -2, -1] @@ -146,7 +145,7 @@ class TestFFTFreq(object): assert_array_almost_equal(10*pi*fft.fftfreq(10, pi), x) -class TestRFFTFreq(object): +class TestRFFTFreq: def test_definition(self): x = [0, 1, 2, 3, 4] @@ -157,7 +156,7 @@ class TestRFFTFreq(object): assert_array_almost_equal(10*pi*fft.rfftfreq(10, pi), x) -class TestIRFFTN(object): +class TestIRFFTN: def test_not_last_axis_success(self): ar, ai = np.random.random((2, 16, 8, 32)) diff --git a/numpy/fft/tests/test_pocketfft.py b/numpy/fft/tests/test_pocketfft.py index 453e964fa..da31ec09e 100644 --- a/numpy/fft/tests/test_pocketfft.py +++ b/numpy/fft/tests/test_pocketfft.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np import pytest from numpy.random import random @@ -21,13 +19,13 @@ def fft1(x): return np.sum(x*np.exp(phase), axis=1) -class TestFFTShift(object): +class TestFFTShift: def test_fft_n(self): assert_raises(ValueError, np.fft.fft, [1, 2, 3], 0) -class TestFFT1D(object): +class TestFFT1D: def test_identity(self): maxlen = 512 @@ -222,7 +220,7 @@ def test_fft_with_order(dtype, order, fft): raise ValueError() -class TestFFTThreadSafe(object): +class TestFFTThreadSafe: threads = 16 input_shape = (800, 200) diff --git a/numpy/lib/__init__.py b/numpy/lib/__init__.py index 2db12d9a4..cb0de0d15 100644 --- a/numpy/lib/__init__.py +++ b/numpy/lib/__init__.py @@ -11,8 +11,6 @@ Most contains basic functions that are used by several submodules and are useful to have in the main name-space. """ -from __future__ import division, absolute_import, print_function - import math from numpy.version import version as __version__ diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py index 0d71375c2..aa793958e 100644 --- a/numpy/lib/_datasource.py +++ b/numpy/lib/_datasource.py @@ -34,8 +34,6 @@ Example:: >>> fp.close() # doctest: +SKIP """ -from __future__ import division, absolute_import, print_function - import os import sys import warnings @@ -141,7 +139,7 @@ def _python2_gzipopen(fn, mode, encoding, newline): # deferring the import of lzma, bz2 and gzip until needed # TODO: .zip support, .tar support? -class _FileOpeners(object): +class _FileOpeners: """ Container for different methods to open (un-)compressed files. @@ -270,7 +268,7 @@ def open(path, mode='r', destpath=os.curdir, encoding=None, newline=None): @set_module('numpy') -class DataSource(object): +class DataSource: """ DataSource(destpath='.') diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py index c392929fd..dd6e9ec66 100644 --- a/numpy/lib/_iotools.py +++ b/numpy/lib/_iotools.py @@ -1,21 +1,12 @@ """A collection of functions designed to help I/O with ascii files. """ -from __future__ import division, absolute_import, print_function - __docformat__ = "restructuredtext en" -import sys import numpy as np import numpy.core.numeric as nx from numpy.compat import asbytes, asunicode, bytes, basestring -if sys.version_info[0] >= 3: - from builtins import bool, int, float, complex, object, str - unicode = str -else: - from __builtin__ import bool, int, float, complex, object, unicode, str - def _decode_line(line, encoding=None): """Decode bytes from binary input streams. @@ -65,40 +56,6 @@ def _is_bytes_like(obj): return True -def _to_filehandle(fname, flag='r', return_opened=False): - """ - Returns the filehandle corresponding to a string or a file. - If the string ends in '.gz', the file is automatically unzipped. - - Parameters - ---------- - fname : string, filehandle - Name of the file whose filehandle must be returned. - flag : string, optional - Flag indicating the status of the file ('r' for read, 'w' for write). - return_opened : boolean, optional - Whether to return the opening status of the file. - """ - if _is_string_like(fname): - if fname.endswith('.gz'): - import gzip - fhd = gzip.open(fname, flag) - elif fname.endswith('.bz2'): - import bz2 - fhd = bz2.BZ2File(fname) - else: - fhd = file(fname, flag) - opened = True - elif hasattr(fname, 'seek'): - fhd = fname - opened = False - else: - raise ValueError('fname must be a string or file handle') - if return_opened: - return fhd, opened - return fhd - - def has_nested_fields(ndtype): """ Returns whether one or several fields of a dtype are nested. @@ -173,7 +130,7 @@ def flatten_dtype(ndtype, flatten_base=False): return types -class LineSplitter(object): +class LineSplitter: """ Object to split a string at a given delimiter or at given places. @@ -210,7 +167,8 @@ class LineSplitter(object): return lambda input: [_.strip() for _ in method(input)] # - def __init__(self, delimiter=None, comments='#', autostrip=True, encoding=None): + def __init__(self, delimiter=None, comments='#', autostrip=True, + encoding=None): delimiter = _decode_line(delimiter) comments = _decode_line(comments) @@ -273,7 +231,7 @@ class LineSplitter(object): return self._handyman(_decode_line(line, self.encoding)) -class NameValidator(object): +class NameValidator: """ Object to validate a list of strings to use as field names. @@ -496,7 +454,7 @@ class ConversionWarning(UserWarning): pass -class StringConverter(object): +class StringConverter: """ Factory class for function transforming a string into another object (int, float). @@ -949,9 +907,10 @@ def easy_dtype(ndtype, names=None, defaultfmt="f%i", **validationargs): elif ndtype.names is not None: validate = NameValidator(**validationargs) # Default initial names : should we change the format ? - if ((ndtype.names == tuple("f%i" % i for i in range(len(ndtype.names)))) and - (defaultfmt != "f%i")): - ndtype.names = validate([''] * len(ndtype.names), defaultfmt=defaultfmt) + numbered_names = tuple("f%i" % i for i in range(len(ndtype.names))) + if ((ndtype.names == numbered_names) and (defaultfmt != "f%i")): + ndtype.names = validate([''] * len(ndtype.names), + defaultfmt=defaultfmt) # Explicit initial names : just validate else: ndtype.names = validate(ndtype.names, defaultfmt=defaultfmt) diff --git a/numpy/lib/_version.py b/numpy/lib/_version.py index 8aa999fc9..6a7c5cba1 100644 --- a/numpy/lib/_version.py +++ b/numpy/lib/_version.py @@ -5,8 +5,6 @@ The LooseVersion and StrictVersion classes that distutils provides don't work; they don't recognize anything like alpha/beta/rc/dev versions. """ -from __future__ import division, absolute_import, print_function - import re from numpy.compat import basestring diff --git a/numpy/lib/arraypad.py b/numpy/lib/arraypad.py index 33e64708d..247eed07c 100644 --- a/numpy/lib/arraypad.py +++ b/numpy/lib/arraypad.py @@ -3,8 +3,6 @@ The arraypad module contains a group of functions to pad values onto the edges of an n-dimensional array. """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.core.overrides import array_function_dispatch from numpy.lib.index_tricks import ndindex diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index d65316598..ad508e85d 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -25,8 +25,6 @@ To do: Optionally return indices analogously to unique for all functions. :Author: Robert Cimrman """ -from __future__ import division, absolute_import, print_function - import functools import numpy as np diff --git a/numpy/lib/arrayterator.py b/numpy/lib/arrayterator.py index c16668582..15b0f8aa9 100644 --- a/numpy/lib/arrayterator.py +++ b/numpy/lib/arrayterator.py @@ -7,8 +7,6 @@ an array object, and when iterated it will return sub-arrays with at most a user-specified number of elements. """ -from __future__ import division, absolute_import, print_function - from operator import mul from functools import reduce @@ -17,7 +15,7 @@ from numpy.compat import long __all__ = ['Arrayterator'] -class Arrayterator(object): +class Arrayterator: """ Buffered iterator for big arrays. diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py index a011e52a9..b055bb1ec 100644 --- a/numpy/lib/financial.py +++ b/numpy/lib/financial.py @@ -10,8 +10,6 @@ or arrays (or other sequences). Functions support the :class:`decimal.Decimal` type unless otherwise stated. """ -from __future__ import division, absolute_import, print_function - import warnings from decimal import Decimal import functools diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 20e2e9c72..2ee43637c 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -161,8 +161,6 @@ alternatives, is described in the `"npy-format" NEP evolved with time and this document is more current. """ -from __future__ import division, absolute_import, print_function - import numpy import sys import io diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 499120630..ef8a26fe3 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - try: # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 @@ -13,10 +11,10 @@ import warnings import numpy as np import numpy.core.numeric as _nx -from numpy.core import atleast_1d, transpose +from numpy.core import transpose from numpy.core.numeric import ( ones, zeros, arange, concatenate, array, asarray, asanyarray, empty, - empty_like, ndarray, around, floor, ceil, take, dot, where, intp, + ndarray, around, floor, ceil, take, dot, where, intp, integer, isscalar, absolute ) from numpy.core.umath import ( @@ -38,21 +36,16 @@ from numpy.core.multiarray import ( from numpy.core.umath import _add_newdoc_ufunc as add_newdoc_ufunc from numpy.compat import long -if sys.version_info[0] < 3: - # Force range to be a generator, for np.delete's usage. - range = xrange - import __builtin__ as builtins -else: - import builtins +import builtins + +# needed in this module for compatibility +from numpy.lib.histograms import histogram, histogramdd array_function_dispatch = functools.partial( overrides.array_function_dispatch, module='numpy') -# needed in this module for compatibility -from numpy.lib.histograms import histogram, histogramdd - __all__ = [ 'select', 'piecewise', 'trim_zeros', 'copy', 'iterable', 'percentile', 'diff', 'gradient', 'angle', 'unwrap', 'sort_complex', 'disp', 'flip', @@ -70,7 +63,7 @@ def _rot90_dispatcher(m, k=None, axes=None): @array_function_dispatch(_rot90_dispatcher) -def rot90(m, k=1, axes=(0,1)): +def rot90(m, k=1, axes=(0, 1)): """ Rotate an array by 90 degrees in the plane specified by axes. @@ -150,7 +143,7 @@ def rot90(m, k=1, axes=(0,1)): axes_list[axes[0]]) if k == 1: - return transpose(flip(m,axes[1]), axes_list) + return transpose(flip(m, axes[1]), axes_list) else: # k == 3 return flip(transpose(m, axes_list), axes[1]) @@ -777,14 +770,14 @@ def copy(a, order='K'): # Basic operations -def _gradient_dispatcher(f, *varargs, **kwargs): +def _gradient_dispatcher(f, *varargs, axis=None, edge_order=None): yield f for v in varargs: yield v @array_function_dispatch(_gradient_dispatcher) -def gradient(f, *varargs, **kwargs): +def gradient(f, *varargs, axis=None, edge_order=1): """ Return the gradient of an N-dimensional array. @@ -961,11 +954,10 @@ def gradient(f, *varargs, **kwargs): f = np.asanyarray(f) N = f.ndim # number of dimensions - axes = kwargs.pop('axis', None) - if axes is None: + if axis is None: axes = tuple(range(N)) else: - axes = _nx.normalize_axis_tuple(axes, N) + axes = _nx.normalize_axis_tuple(axis, N) len_axes = len(axes) n = len(varargs) @@ -979,13 +971,18 @@ def gradient(f, *varargs, **kwargs): # scalar or 1d array for each axis dx = list(varargs) for i, distances in enumerate(dx): - if np.ndim(distances) == 0: + distances = np.asanyarray(distances) + if distances.ndim == 0: continue - elif np.ndim(distances) != 1: + elif distances.ndim != 1: raise ValueError("distances must be either scalars or 1d") if len(distances) != f.shape[axes[i]]: raise ValueError("when 1d, distances must match " "the length of the corresponding dimension") + if np.issubdtype(distances.dtype, np.integer): + # Convert numpy integer types to float64 to avoid modular + # arithmetic in np.diff(distances). + distances = distances.astype(np.float64) diffx = np.diff(distances) # if distances are constant reduce to the scalar case # since it brings a consistent speedup @@ -995,10 +992,6 @@ def gradient(f, *varargs, **kwargs): else: raise TypeError("invalid number of arguments") - edge_order = kwargs.pop('edge_order', 1) - if kwargs: - raise TypeError('"{}" are not valid keyword arguments.'.format( - '", "'.join(kwargs.keys()))) if edge_order > 2: raise ValueError("'edge_order' greater than 2 not supported") @@ -1024,8 +1017,12 @@ def gradient(f, *varargs, **kwargs): elif np.issubdtype(otype, np.inexact): pass else: - # all other types convert to floating point - otype = np.double + # All other types convert to floating point. + # First check if f is a numpy integer type; if so, convert f to float64 + # to avoid modular arithmetic when computing the changes in f. + if np.issubdtype(otype, np.integer): + f = f.astype(np.float64) + otype = np.float64 for axis, ax_dx in zip(axes, dx): if f.shape[axis] < edge_order + 1: @@ -1612,6 +1609,7 @@ def trim_zeros(filt, trim='fb'): last = last - 1 return filt[first:last] + def _extract_dispatcher(condition, arr): return (condition, arr) @@ -1867,7 +1865,7 @@ def _create_arrays(broadcast_shape, dim_sizes, list_of_core_dims, dtypes): @set_module('numpy') -class vectorize(object): +class vectorize: """ vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False, signature=None) @@ -2947,6 +2945,7 @@ def hamming(M): n = arange(0, M) return 0.54 - 0.46*cos(2.0*pi*n/(M-1)) + ## Code from cephes for i0 _i0A = [ @@ -3489,6 +3488,7 @@ def median(a, axis=None, out=None, overwrite_input=False, keepdims=False): else: return r + def _median(a, axis=None, out=None, overwrite_input=False): # can't be reasonably be implemented in terms of percentile as we have to # call mean to not break astropy @@ -3707,7 +3707,7 @@ def quantile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False): """ Compute the q-th quantile of the data along the specified axis. - + .. versionadded:: 1.15.0 Parameters @@ -3878,7 +3878,7 @@ def _quantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, "interpolation can only be 'linear', 'lower' 'higher', " "'midpoint', or 'nearest'") - n = np.array(False, dtype=bool) # check for nan's flag + n = np.array(False, dtype=bool) # check for nan's flag if indices.dtype == intp: # take the points along axis # Check if the array contains any nan's if np.issubdtype(a.dtype, np.inexact): @@ -3898,7 +3898,6 @@ def _quantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, indices = indices[0] r = take(ap, indices, axis=axis, out=out) - else: # weight the points above and below the indices indices_below = floor(indices).astype(intp) indices_above = indices_below + 1 @@ -4059,13 +4058,13 @@ def trapz(y, x=None, dx=1.0, axis=-1): return ret -def _meshgrid_dispatcher(*xi, **kwargs): +def _meshgrid_dispatcher(*xi, copy=None, sparse=None, indexing=None): return xi # Based on scitools meshgrid @array_function_dispatch(_meshgrid_dispatcher) -def meshgrid(*xi, **kwargs): +def meshgrid(*xi, copy=True, sparse=False, indexing='xy'): """ Return coordinate matrices from coordinate vectors. @@ -4171,14 +4170,6 @@ def meshgrid(*xi, **kwargs): """ ndim = len(xi) - copy_ = kwargs.pop('copy', True) - sparse = kwargs.pop('sparse', False) - indexing = kwargs.pop('indexing', 'xy') - - if kwargs: - raise TypeError("meshgrid() got an unexpected keyword argument '%s'" - % (list(kwargs)[0],)) - if indexing not in ['xy', 'ij']: raise ValueError( "Valid values for `indexing` are 'xy' and 'ij'.") @@ -4196,7 +4187,7 @@ def meshgrid(*xi, **kwargs): # Return the full N-D matrix (not only the 1-D vector) output = np.broadcast_arrays(*output, subok=True) - if copy_: + if copy: output = [x.copy() for x in output] return output diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py index 03c365ab6..0eff73b39 100644 --- a/numpy/lib/histograms.py +++ b/numpy/lib/histograms.py @@ -1,8 +1,6 @@ """ Histogram-related functions """ -from __future__ import division, absolute_import, print_function - import contextlib import functools import operator diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index 04384854c..0560bd36d 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import functools import sys import math @@ -107,7 +105,7 @@ def ix_(*args): out.append(new) return tuple(out) -class nd_grid(object): +class nd_grid: """ Construct a multi-dimensional "meshgrid". @@ -299,7 +297,7 @@ class OGridClass(nd_grid): ogrid = OGridClass() -class AxisConcatenator(object): +class AxisConcatenator: """ Translates slice objects to concatenation along an axis. @@ -552,7 +550,7 @@ c_ = CClass() @set_module('numpy') -class ndenumerate(object): +class ndenumerate: """ Multidimensional index iterator. @@ -603,7 +601,7 @@ class ndenumerate(object): @set_module('numpy') -class ndindex(object): +class ndindex: """ An N-dimensional iterator object to index arrays. @@ -681,7 +679,7 @@ class ndindex(object): # # -class IndexExpression(object): +class IndexExpression: """ A nicer way to build up index tuples for arrays. diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index f974a7724..d4811b94d 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -1,6 +1,4 @@ """Mixin classes for custom array types that don't inherit from ndarray.""" -from __future__ import division, absolute_import, print_function - import sys from numpy.core import umath as um @@ -60,7 +58,7 @@ def _unary_method(ufunc, name): return func -class NDArrayOperatorsMixin(object): +class NDArrayOperatorsMixin: """Mixin defining all operator special methods using __array_ufunc__. This class implements the special methods for almost all of Python's diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 8e2a34e70..003550432 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -20,8 +20,6 @@ Functions - `nanpercentile` -- qth percentile of non-NaN values """ -from __future__ import division, absolute_import, print_function - import functools import warnings import numpy as np diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 3e54ff10c..c47e388c0 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import os import re @@ -55,7 +53,7 @@ array_function_dispatch = functools.partial( overrides.array_function_dispatch, module='numpy') -class BagObj(object): +class BagObj: """ BagObj(obj) @@ -69,7 +67,7 @@ class BagObj(object): Examples -------- >>> from numpy.lib.npyio import BagObj as BO - >>> class BagDemo(object): + >>> class BagDemo: ... def __getitem__(self, key): # An instance of BagObj(BagDemo) ... # will call this method when any ... # attribute look-up is required @@ -1336,7 +1334,7 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', fmt = asstr(fmt) delimiter = asstr(delimiter) - class WriteWrap(object): + class WriteWrap: """Convert to unicode in py2 or to bytes on bytestream inputs. """ diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index 3d07a0de4..5a0fa5431 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -2,8 +2,6 @@ Functions to operate on polynomials. """ -from __future__ import division, absolute_import, print_function - __all__ = ['poly', 'roots', 'polyint', 'polyder', 'polyadd', 'polysub', 'polymul', 'polydiv', 'polyval', 'poly1d', 'polyfit', 'RankWarning'] @@ -1007,7 +1005,7 @@ def _raise_power(astr, wrap=70): @set_module('numpy') -class poly1d(object): +class poly1d: """ A one-dimensional polynomial class. diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index 927161ddb..93aa67a3b 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -5,8 +5,6 @@ Most of these functions were initially implemented by John Hunter for matplotlib. They have been rewritten and extended for convenience. """ -from __future__ import division, absolute_import, print_function - import sys import itertools import numpy as np diff --git a/numpy/lib/scimath.py b/numpy/lib/scimath.py index 5ac790ce9..555a3d5a8 100644 --- a/numpy/lib/scimath.py +++ b/numpy/lib/scimath.py @@ -15,8 +15,6 @@ Similarly, `sqrt`, other base logarithms, `power` and trig functions are correctly handled. See their respective docstrings for specific examples. """ -from __future__ import division, absolute_import, print_function - import numpy.core.numeric as nx import numpy.core.numerictypes as nt from numpy.core.numeric import asarray, any diff --git a/numpy/lib/setup.py b/numpy/lib/setup.py index d342410b8..5d0341d86 100644 --- a/numpy/lib/setup.py +++ b/numpy/lib/setup.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index dbb61c225..7634af010 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import functools import numpy.core.numeric as _nx diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py index 8aafd094b..502235bdf 100644 --- a/numpy/lib/stride_tricks.py +++ b/numpy/lib/stride_tricks.py @@ -5,15 +5,13 @@ An explanation of strides can be found in the "ndarray.rst" file in the NumPy reference guide. """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.core.overrides import array_function_dispatch __all__ = ['broadcast_to', 'broadcast_arrays'] -class DummyArray(object): +class DummyArray: """Dummy object that just exists to hang __array_interface__ dictionaries and possibly keep alive a reference to a base array. """ @@ -199,12 +197,12 @@ def _broadcast_shape(*args): return b.shape -def _broadcast_arrays_dispatcher(*args, **kwargs): +def _broadcast_arrays_dispatcher(*args, subok=None): return args @array_function_dispatch(_broadcast_arrays_dispatcher, module='numpy') -def broadcast_arrays(*args, **kwargs): +def broadcast_arrays(*args, subok=False): """ Broadcast any number of arrays against each other. @@ -255,10 +253,6 @@ def broadcast_arrays(*args, **kwargs): # return np.nditer(args, flags=['multi_index', 'zerosize_ok'], # order='C').itviews - subok = kwargs.pop('subok', False) - if kwargs: - raise TypeError('broadcast_arrays() got an unexpected keyword ' - 'argument {!r}'.format(list(kwargs.keys())[0])) args = [np.array(_m, copy=False, subok=subok) for _m in args] shape = _broadcast_shape(*args) diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py index 8eac16b58..fdd22347d 100644 --- a/numpy/lib/tests/test__datasource.py +++ b/numpy/lib/tests/test__datasource.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys import pytest @@ -96,7 +94,7 @@ def invalid_httpfile(): return http_fakefile -class TestDataSourceOpen(object): +class TestDataSourceOpen: def setup(self): self.tmpdir = mkdtemp() self.ds = datasource.DataSource(self.tmpdir) @@ -183,7 +181,7 @@ class TestDataSourceOpen(object): assert_equal(magic_line, result) -class TestDataSourceExists(object): +class TestDataSourceExists: def setup(self): self.tmpdir = mkdtemp() self.ds = datasource.DataSource(self.tmpdir) @@ -213,7 +211,7 @@ class TestDataSourceExists(object): assert_equal(self.ds.exists(tmpfile), False) -class TestDataSourceAbspath(object): +class TestDataSourceAbspath: def setup(self): self.tmpdir = os.path.abspath(mkdtemp()) self.ds = datasource.DataSource(self.tmpdir) @@ -278,7 +276,7 @@ class TestDataSourceAbspath(object): os.sep = orig_os_sep -class TestRepositoryAbspath(object): +class TestRepositoryAbspath: def setup(self): self.tmpdir = os.path.abspath(mkdtemp()) self.repos = datasource.Repository(valid_baseurl(), self.tmpdir) @@ -311,7 +309,7 @@ class TestRepositoryAbspath(object): os.sep = orig_os_sep -class TestRepositoryExists(object): +class TestRepositoryExists: def setup(self): self.tmpdir = mkdtemp() self.repos = datasource.Repository(valid_baseurl(), self.tmpdir) @@ -344,7 +342,7 @@ class TestRepositoryExists(object): assert_(self.repos.exists(tmpfile)) -class TestOpenFunc(object): +class TestOpenFunc: def setup(self): self.tmpdir = mkdtemp() diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py index 15cd3ad9d..1d69d869e 100644 --- a/numpy/lib/tests/test__iotools.py +++ b/numpy/lib/tests/test__iotools.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import time from datetime import date @@ -14,7 +12,7 @@ from numpy.lib._iotools import ( from numpy.compat import unicode -class TestLineSplitter(object): +class TestLineSplitter: "Tests the LineSplitter class." def test_no_delimiter(self): @@ -83,7 +81,7 @@ class TestLineSplitter(object): # ----------------------------------------------------------------------------- -class TestNameValidator(object): +class TestNameValidator: def test_case_sensitivity(self): "Test case sensitivity" @@ -141,7 +139,7 @@ def _bytes_to_date(s): return date(*time.strptime(s, "%Y-%m-%d")[:3]) -class TestStringConverter(object): +class TestStringConverter: "Test StringConverter" def test_creation(self): @@ -266,7 +264,7 @@ class TestStringConverter(object): assert_(converter(val) == 9223372043271415339) -class TestMiscFunctions(object): +class TestMiscFunctions: def test_has_nested_dtype(self): "Test has_nested_dtype" diff --git a/numpy/lib/tests/test__version.py b/numpy/lib/tests/test__version.py index 8e66a0c03..182504631 100644 --- a/numpy/lib/tests/test__version.py +++ b/numpy/lib/tests/test__version.py @@ -1,8 +1,6 @@ """Tests for the NumpyVersion class. """ -from __future__ import division, absolute_import, print_function - from numpy.testing import assert_, assert_raises from numpy.lib import NumpyVersion diff --git a/numpy/lib/tests/test_arraypad.py b/numpy/lib/tests/test_arraypad.py index 65593dd29..cd75b4ac4 100644 --- a/numpy/lib/tests/test_arraypad.py +++ b/numpy/lib/tests/test_arraypad.py @@ -1,8 +1,6 @@ """Tests for the array padding functions. """ -from __future__ import division, absolute_import, print_function - import pytest import numpy as np @@ -31,7 +29,7 @@ _all_modes = { } -class TestAsPairs(object): +class TestAsPairs: def test_single_value(self): """Test casting for a single value.""" expected = np.array([[3, 3]] * 10) @@ -114,7 +112,7 @@ class TestAsPairs(object): _as_pairs(np.ones((2, 3)), 3) -class TestConditionalShortcuts(object): +class TestConditionalShortcuts: @pytest.mark.parametrize("mode", _all_modes.keys()) def test_zero_padding_shortcuts(self, mode): test = np.arange(120).reshape(4, 5, 6) @@ -136,7 +134,7 @@ class TestConditionalShortcuts(object): np.pad(test, pad_amt, mode=mode, stat_length=30)) -class TestStatistic(object): +class TestStatistic: def test_check_mean_stat_length(self): a = np.arange(100).astype('f') a = np.pad(a, ((25, 20), ), 'mean', stat_length=((2, 3), )) @@ -498,7 +496,7 @@ class TestStatistic(object): np.pad([1., 2.], 1, mode, stat_length=(1, 0)) -class TestConstant(object): +class TestConstant: def test_check_constant(self): a = np.arange(100) a = np.pad(a, (25, 20), 'constant', constant_values=(10, 20)) @@ -677,7 +675,7 @@ class TestConstant(object): assert result.shape == (3, 4, 4) -class TestLinearRamp(object): +class TestLinearRamp: def test_check_simple(self): a = np.arange(100).astype('f') a = np.pad(a, (25, 20), 'linear_ramp', end_values=(4, 5)) @@ -762,7 +760,7 @@ class TestLinearRamp(object): assert_equal(result, expected) -class TestReflect(object): +class TestReflect: def test_check_simple(self): a = np.arange(100) a = np.pad(a, (25, 20), 'reflect') @@ -872,7 +870,7 @@ class TestReflect(object): assert_array_equal(a, b) -class TestEmptyArray(object): +class TestEmptyArray: """Check how padding behaves on arrays with an empty dimension.""" @pytest.mark.parametrize( @@ -896,7 +894,7 @@ class TestEmptyArray(object): assert result.shape == (8, 0, 4) -class TestSymmetric(object): +class TestSymmetric: def test_check_simple(self): a = np.arange(100) a = np.pad(a, (25, 20), 'symmetric') @@ -1030,7 +1028,7 @@ class TestSymmetric(object): assert_array_equal(a, b) -class TestWrap(object): +class TestWrap: def test_check_simple(self): a = np.arange(100) a = np.pad(a, (25, 20), 'wrap') @@ -1144,7 +1142,7 @@ class TestWrap(object): assert_array_equal(np.r_[a, a, a, a][:-3], b) -class TestEdge(object): +class TestEdge: def test_check_simple(self): a = np.arange(12) a = np.reshape(a, (4, 3)) @@ -1183,7 +1181,7 @@ class TestEdge(object): assert_array_equal(padded, expected) -class TestEmpty(object): +class TestEmpty: def test_simple(self): arr = np.arange(24).reshape(4, 6) result = np.pad(arr, [(2, 3), (3, 1)], mode="empty") @@ -1231,7 +1229,7 @@ def test_object_input(mode): assert_array_equal(np.pad(a, pad_amt, mode=mode), b) -class TestPadWidth(object): +class TestPadWidth: @pytest.mark.parametrize("pad_width", [ (4, 5, 6, 7), ((1,), (2,), (3,)), diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 1d38d8d27..851fd31ea 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -1,8 +1,6 @@ """Test functions for 1D array set operations. """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import (assert_array_equal, assert_equal, @@ -14,7 +12,7 @@ import pytest -class TestSetOps(object): +class TestSetOps: def test_intersect1d(self): # unique inputs @@ -36,7 +34,7 @@ class TestSetOps(object): def test_intersect1d_array_like(self): # See gh-11772 - class Test(object): + class Test: def __array__(self): return np.arange(3) @@ -415,7 +413,7 @@ class TestSetOps(object): assert_array_equal(c1, c2) -class TestUnique(object): +class TestUnique: def test_unique_1d(self): diff --git a/numpy/lib/tests/test_arrayterator.py b/numpy/lib/tests/test_arrayterator.py index 2ce4456a5..c00ed13d7 100644 --- a/numpy/lib/tests/test_arrayterator.py +++ b/numpy/lib/tests/test_arrayterator.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from operator import mul from functools import reduce diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index cb67f7c0f..26e79bc06 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import warnings from decimal import Decimal @@ -18,7 +16,7 @@ def filter_deprecation(func): return newfunc -class TestFinancial(object): +class TestFinancial: @filter_deprecation def test_npv_irr_congruence(self): # IRR is defined as the rate required for the present value of a diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 0592e0b12..28ce038ae 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - # doctest r''' Test the .npy file format. diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 9075ff538..dfce2d55d 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1,10 +1,7 @@ -from __future__ import division, absolute_import, print_function - import operator import warnings import sys import decimal -import types from fractions import Fraction import pytest @@ -45,7 +42,7 @@ def _make_complex(real, imag): return ret -class TestRot90(object): +class TestRot90: def test_basic(self): assert_raises(ValueError, rot90, np.ones(4)) assert_raises(ValueError, rot90, np.ones((2,2,2)), axes=(0,1,2)) @@ -113,7 +110,7 @@ class TestRot90(object): rot90(a_rot90_20, k=k-1, axes=(2, 0))) -class TestFlip(object): +class TestFlip: def test_axes(self): assert_raises(np.AxisError, np.flip, np.ones(4), axis=1) @@ -216,7 +213,7 @@ class TestFlip(object): assert_equal(np.flip(a, axis=(1, 2)), c) -class TestAny(object): +class TestAny: def test_basic(self): y1 = [0, 0, 1, 0] @@ -233,7 +230,7 @@ class TestAny(object): assert_array_equal(np.sometrue(y1, axis=1), [0, 1, 1]) -class TestAll(object): +class TestAll: def test_basic(self): y1 = [0, 1, 1, 0] @@ -251,7 +248,7 @@ class TestAll(object): assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1]) -class TestCopy(object): +class TestCopy: def test_basic(self): a = np.array([[1, 2], [3, 4]]) @@ -279,7 +276,7 @@ class TestCopy(object): assert_(a_fort_copy.flags.f_contiguous) -class TestAverage(object): +class TestAverage: def test_basic(self): y1 = np.array([1, 2, 3]) @@ -380,7 +377,7 @@ class TestAverage(object): w /= w.sum() assert_almost_equal(a.mean(0), average(a, weights=w)) -class TestSelect(object): +class TestSelect: choices = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])] @@ -442,7 +439,7 @@ class TestSelect(object): select(conditions, choices) -class TestInsert(object): +class TestInsert: def test_basic(self): a = [1, 2, 3] @@ -543,7 +540,7 @@ class TestInsert(object): assert_array_equal(b[[0, 3]], np.array(val, dtype=b.dtype)) -class TestAmax(object): +class TestAmax: def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] @@ -555,7 +552,7 @@ class TestAmax(object): assert_equal(np.amax(b, axis=1), [9.0, 10.0, 8.0]) -class TestAmin(object): +class TestAmin: def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] @@ -567,7 +564,7 @@ class TestAmin(object): assert_equal(np.amin(b, axis=1), [3.0, 4.0, 2.0]) -class TestPtp(object): +class TestPtp: def test_basic(self): a = np.array([3, 4, 5, 10, -3, -5, 6.0]) @@ -582,7 +579,7 @@ class TestPtp(object): assert_equal(b.ptp(axis=(0,1), keepdims=True), [[8.0]]) -class TestCumsum(object): +class TestCumsum: def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] @@ -605,7 +602,7 @@ class TestCumsum(object): assert_array_equal(np.cumsum(a2, axis=1), tgt) -class TestProd(object): +class TestProd: def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] @@ -625,7 +622,7 @@ class TestProd(object): np.array([24, 1890, 600], ctype)) -class TestCumprod(object): +class TestCumprod: def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] @@ -652,7 +649,7 @@ class TestCumprod(object): [10, 30, 120, 600]], ctype)) -class TestDiff(object): +class TestDiff: def test_basic(self): x = [1, 4, 6, 7, 12] @@ -792,7 +789,7 @@ class TestDiff(object): assert_raises(np.AxisError, diff, x, append=0, axis=3) -class TestDelete(object): +class TestDelete: def setup(self): self.a = np.arange(5) @@ -867,7 +864,7 @@ class TestDelete(object): assert_equal(m.flags.f_contiguous, k.flags.f_contiguous) -class TestGradient(object): +class TestGradient: def test_basic(self): v = [[1, 1], [3, 4]] @@ -1084,8 +1081,42 @@ class TestGradient(object): assert_raises(ValueError, gradient, np.arange(1), edge_order=2) assert_raises(ValueError, gradient, np.arange(2), edge_order=2) - -class TestAngle(object): + @pytest.mark.parametrize('f_dtype', [np.uint8, np.uint16, + np.uint32, np.uint64]) + def test_f_decreasing_unsigned_int(self, f_dtype): + f = np.array([5, 4, 3, 2, 1], dtype=f_dtype) + g = gradient(f) + assert_array_equal(g, [-1]*len(f)) + + @pytest.mark.parametrize('f_dtype', [np.int8, np.int16, + np.int32, np.int64]) + def test_f_signed_int_big_jump(self, f_dtype): + maxint = np.iinfo(f_dtype).max + x = np.array([1, 3]) + f = np.array([-1, maxint], dtype=f_dtype) + dfdx = gradient(f, x) + assert_array_equal(dfdx, [(maxint + 1) // 2]*2) + + @pytest.mark.parametrize('x_dtype', [np.uint8, np.uint16, + np.uint32, np.uint64]) + def test_x_decreasing_unsigned(self, x_dtype): + x = np.array([3, 2, 1], dtype=x_dtype) + f = np.array([0, 2, 4]) + dfdx = gradient(f, x) + assert_array_equal(dfdx, [-2]*len(x)) + + @pytest.mark.parametrize('x_dtype', [np.int8, np.int16, + np.int32, np.int64]) + def test_x_signed_int_big_jump(self, x_dtype): + minint = np.iinfo(x_dtype).min + maxint = np.iinfo(x_dtype).max + x = np.array([-1, maxint], dtype=x_dtype) + f = np.array([minint // 2, 0]) + dfdx = gradient(f, x) + assert_array_equal(dfdx, [0.5, 0.5]) + + +class TestAngle: def test_basic(self): x = [1 + 3j, np.sqrt(2) / 2.0 + 1j * np.sqrt(2) / 2, @@ -1111,7 +1142,7 @@ class TestAngle(object): assert_equal(actual, expected) -class TestTrimZeros(object): +class TestTrimZeros: """ Only testing for integer splits. @@ -1134,7 +1165,7 @@ class TestTrimZeros(object): assert_array_equal(res, np.array([1, 0, 2, 3, 0, 4])) -class TestExtins(object): +class TestExtins: def test_basic(self): a = np.array([1, 3, 2, 1, 2, 3, 3]) @@ -1173,7 +1204,7 @@ class TestExtins(object): assert_array_equal(a, ac) -class TestVectorize(object): +class TestVectorize: def test_simple(self): def addsubtract(a, b): @@ -1505,8 +1536,8 @@ class TestVectorize(object): f(x) -class TestLeaks(object): - class A(object): +class TestLeaks: + class A: iters = 20 def bound(self, *args): @@ -1548,7 +1579,7 @@ class TestLeaks(object): finally: gc.enable() -class TestDigitize(object): +class TestDigitize: def test_forward(self): x = np.arange(-6, 5) @@ -1633,7 +1664,7 @@ class TestDigitize(object): assert_equal(np.digitize(x, [x + 1, x - 1]), 1) -class TestUnwrap(object): +class TestUnwrap: def test_simple(self): # check that unwrap removes jumps greater that 2*pi @@ -1642,7 +1673,7 @@ class TestUnwrap(object): assert_(np.all(diff(unwrap(rand(10) * 100)) < np.pi)) -class TestFilterwindows(object): +class TestFilterwindows: def test_hanning(self): # check symmetry @@ -1673,7 +1704,7 @@ class TestFilterwindows(object): assert_almost_equal(np.sum(w, axis=0), 3.7800, 4) -class TestTrapz(object): +class TestTrapz: def test_simple(self): x = np.arange(-10, 10, .1) @@ -1735,7 +1766,7 @@ class TestTrapz(object): assert_almost_equal(trapz(y, xm), r) -class TestSinc(object): +class TestSinc: def test_simple(self): assert_(sinc(0) == 1) @@ -1752,7 +1783,7 @@ class TestSinc(object): assert_array_equal(y1, y3) -class TestUnique(object): +class TestUnique: def test_simple(self): x = np.array([4, 3, 2, 1, 1, 2, 3, 4, 0]) @@ -1764,7 +1795,7 @@ class TestUnique(object): assert_(np.all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10])) -class TestCheckFinite(object): +class TestCheckFinite: def test_simple(self): a = [1, 2, 3] @@ -1781,7 +1812,7 @@ class TestCheckFinite(object): assert_(a.dtype == np.float64) -class TestCorrCoef(object): +class TestCorrCoef: A = np.array( [[0.15391142, 0.18045767, 0.14197213], [0.70461506, 0.96474128, 0.27906989], @@ -1866,7 +1897,7 @@ class TestCorrCoef(object): assert_(np.all(np.abs(c) <= 1.0)) -class TestCov(object): +class TestCov: x1 = np.array([[0, 2], [1, 1], [2, 0]]).T res1 = np.array([[1., -1.], [-1., 1.]]) x2 = np.array([0.0, 1.0, 2.0], ndmin=2) @@ -1966,7 +1997,7 @@ class TestCov(object): self.res1) -class Test_I0(object): +class Test_I0: def test_simple(self): assert_almost_equal( @@ -2012,7 +2043,7 @@ class Test_I0(object): assert_array_equal(exp, res) -class TestKaiser(object): +class TestKaiser: def test_simple(self): assert_(np.isfinite(kaiser(1, 1.0))) @@ -2031,7 +2062,7 @@ class TestKaiser(object): kaiser(3, 4) -class TestMsort(object): +class TestMsort: def test_simple(self): A = np.array([[0.44567325, 0.79115165, 0.54900530], @@ -2044,7 +2075,7 @@ class TestMsort(object): [0.64864341, 0.79115165, 0.96098397]])) -class TestMeshgrid(object): +class TestMeshgrid: def test_simple(self): [X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7]) @@ -2133,7 +2164,7 @@ class TestMeshgrid(object): assert_equal(x[1, :], X) -class TestPiecewise(object): +class TestPiecewise: def test_simple(self): # Condition is single bool list @@ -2225,7 +2256,7 @@ class TestPiecewise(object): [3., 3., 1.]])) -class TestBincount(object): +class TestBincount: def test_simple(self): y = np.bincount(np.arange(4)) @@ -2312,7 +2343,7 @@ class TestBincount(object): assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount) -class TestInterp(object): +class TestInterp: def test_exceptions(self): assert_raises(ValueError, interp, 0, [], []) @@ -2511,7 +2542,7 @@ def compare_results(res, desired): assert_array_equal(res[i], desired[i]) -class TestPercentile(object): +class TestPercentile: def test_basic(self): x = np.arange(8) * 0.5 @@ -2932,7 +2963,7 @@ class TestPercentile(object): a, [0.3, 0.6], (0, 2), interpolation='nearest'), b) -class TestQuantile(object): +class TestQuantile: # most of this is already tested by TestPercentile def test_basic(self): @@ -2974,7 +3005,7 @@ class TestQuantile(object): assert_array_equal(p, p0) -class TestMedian(object): +class TestMedian: def test_basic(self): a0 = np.array(1) @@ -3213,7 +3244,7 @@ class TestMedian(object): (1, 1, 7, 1)) -class TestAdd_newdoc_ufunc(object): +class TestAdd_newdoc_ufunc: def test_ufunc_arg(self): assert_raises(TypeError, add_newdoc_ufunc, 2, "blah") @@ -3223,7 +3254,7 @@ class TestAdd_newdoc_ufunc(object): assert_raises(TypeError, add_newdoc_ufunc, np.add, 3) -class TestAdd_newdoc(object): +class TestAdd_newdoc: @pytest.mark.skipif(sys.flags.optimize == 2, reason="Python running -OO") @pytest.mark.xfail(IS_PYPY, reason="PyPy does not modify tp_doc") @@ -3234,7 +3265,7 @@ class TestAdd_newdoc(object): assert_(len(np.core.ufunc.identity.__doc__) > 300) assert_(len(np.lib.index_tricks.mgrid.__doc__) > 300) -class TestSortComplex(object): +class TestSortComplex: @pytest.mark.parametrize("type_in, type_out", [ ('l', 'D'), diff --git a/numpy/lib/tests/test_histograms.py b/numpy/lib/tests/test_histograms.py index dbf189f3e..c21103891 100644 --- a/numpy/lib/tests/test_histograms.py +++ b/numpy/lib/tests/test_histograms.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.lib.histograms import histogram, histogramdd, histogram_bin_edges @@ -11,7 +9,7 @@ from numpy.testing import ( import pytest -class TestHistogram(object): +class TestHistogram: def setup(self): pass @@ -424,7 +422,7 @@ class TestHistogram(object): assert_array_equal(edges, e) -class TestHistogramOptimBinNums(object): +class TestHistogramOptimBinNums: """ Provide test coverage when using provided estimators for optimal number of bins @@ -612,7 +610,7 @@ class TestHistogramOptimBinNums(object): estimator, weights=[1, 2, 3]) -class TestHistogramdd(object): +class TestHistogramdd: def test_simple(self): x = np.array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5], diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index dbe445c2c..905165a99 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest import numpy as np @@ -14,7 +12,7 @@ from numpy.lib.index_tricks import ( ) -class TestRavelUnravelIndex(object): +class TestRavelUnravelIndex: def test_basic(self): assert_equal(np.unravel_index(2, (2, 2)), (1, 0)) @@ -194,7 +192,7 @@ class TestRavelUnravelIndex(object): with assert_raises(ValueError): np.unravel_index([1], (2, 1, 0)) -class TestGrid(object): +class TestGrid: def test_basic(self): a = mgrid[-1:1:10j] b = mgrid[-1:1:0.1] @@ -252,7 +250,7 @@ class TestGrid(object): assert_equal(grid_small.size, expected[1]) -class TestConcatenator(object): +class TestConcatenator: def test_1d(self): assert_array_equal(r_[1, 2, 3, 4, 5, 6], np.array([1, 2, 3, 4, 5, 6])) b = np.ones(5) @@ -290,14 +288,14 @@ class TestConcatenator(object): assert_equal(r_[np.array(0), [1, 2, 3]], [0, 1, 2, 3]) -class TestNdenumerate(object): +class TestNdenumerate: def test_basic(self): a = np.array([[1, 2], [3, 4]]) assert_equal(list(ndenumerate(a)), [((0, 0), 1), ((0, 1), 2), ((1, 0), 3), ((1, 1), 4)]) -class TestIndexExpression(object): +class TestIndexExpression: def test_regression_1(self): # ticket #1196 a = np.arange(2) @@ -311,7 +309,7 @@ class TestIndexExpression(object): assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]]) -class TestIx_(object): +class TestIx_: def test_regression_1(self): # Test empty untyped inputs create outputs of indexing type, gh-5804 a, = np.ix_(range(0)) @@ -358,7 +356,7 @@ def test_c_(): assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]]) -class TestFillDiagonal(object): +class TestFillDiagonal: def test_basic(self): a = np.zeros((3, 3), int) fill_diagonal(a, 5) @@ -457,7 +455,7 @@ def test_diag_indices(): ) -class TestDiagIndicesFrom(object): +class TestDiagIndicesFrom: def test_diag_indices_from(self): x = np.random.random((4, 4)) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index a095e250a..ba27eea6c 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import gzip import os @@ -71,7 +69,7 @@ def strptime(s, fmt=None): return datetime(*time.strptime(s, fmt)[:3]) -class RoundtripTest(object): +class RoundtripTest: def roundtrip(self, save_func, *args, **kwargs): """ save_func : callable @@ -318,7 +316,7 @@ class TestSavezLoad(RoundtripTest): assert_(fp.closed) -class TestSaveTxt(object): +class TestSaveTxt: def test_array(self): a = np.array([[1, 2], [3, 4]], float) fmt = "%.18e" @@ -584,7 +582,7 @@ class TestSaveTxt(object): with tempdir() as tmpdir: np.savez(os.path.join(tmpdir, 'test.npz'), test_data=test_data) -class LoadTxtBase(object): +class LoadTxtBase: def check_compressed(self, fopen, suffixes): # Test that we can load data from a compressed file wanted = np.arange(6).reshape((2, 3)) @@ -826,7 +824,7 @@ class TestLoadTxt(LoadTxtBase): assert_array_equal(x, a[:, 1]) # Testing with some crazy custom integer type - class CrazyInt(object): + class CrazyInt: def __index__(self): return 1 @@ -1158,7 +1156,7 @@ class TestLoadTxt(LoadTxtBase): a = np.array([[1, 2, 3, 5], [4, 5, 7, 8], [2, 1, 4, 5]], int) assert_array_equal(x, a) -class Testfromregex(object): +class Testfromregex: def test_record(self): c = TextIO() c.write('1.312 foo\n1.534 bar\n4.444 qux') @@ -2349,7 +2347,7 @@ M 33 21.99 @pytest.mark.skipif(Path is None, reason="No pathlib.Path") -class TestPathUsage(object): +class TestPathUsage: # Test that pathlib.Path can be used def test_loadtxt(self): with temppath(suffix='.txt') as path: @@ -2482,7 +2480,7 @@ def test_gzip_load(): # These next two classes encode the minimal API needed to save()/load() arrays. # The `test_ducktyping` ensures they work correctly -class JustWriter(object): +class JustWriter: def __init__(self, base): self.base = base @@ -2492,7 +2490,7 @@ class JustWriter(object): def flush(self): return self.base.flush() -class JustReader(object): +class JustReader: def __init__(self, base): self.base = base diff --git a/numpy/lib/tests/test_mixins.py b/numpy/lib/tests/test_mixins.py index 3dd5346b6..7c22dae94 100644 --- a/numpy/lib/tests/test_mixins.py +++ b/numpy/lib/tests/test_mixins.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numbers import operator import sys @@ -98,7 +96,7 @@ _ALL_BINARY_OPERATORS = [ ] -class TestNDArrayOperatorsMixin(object): +class TestNDArrayOperatorsMixin: def test_array_like_add(self): @@ -128,7 +126,7 @@ class TestNDArrayOperatorsMixin(object): def test_opt_out(self): - class OptOut(object): + class OptOut: """Object that opts out of __array_ufunc__.""" __array_ufunc__ = None diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index da2d0cc52..db563e30c 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import warnings import pytest @@ -37,7 +35,7 @@ _ndat_zeros = np.array([[0.6244, 0.0, 0.2692, 0.0116, 0.0, 0.1170], [0.1610, 0.0, 0.0, 0.1859, 0.3146, 0.0]]) -class TestNanFunctions_MinMax(object): +class TestNanFunctions_MinMax: nanfuncs = [np.nanmin, np.nanmax] stdfuncs = [np.min, np.max] @@ -171,7 +169,7 @@ class TestNanFunctions_MinMax(object): assert_(issubclass(w[0].category, RuntimeWarning)) -class TestNanFunctions_ArgminArgmax(object): +class TestNanFunctions_ArgminArgmax: nanfuncs = [np.nanargmin, np.nanargmax] @@ -233,7 +231,7 @@ class TestNanFunctions_ArgminArgmax(object): assert_(res.shape == ()) -class TestNanFunctions_IntTypes(object): +class TestNanFunctions_IntTypes: int_types = (np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64) @@ -308,7 +306,7 @@ class TestNanFunctions_IntTypes(object): assert_equal(np.nanstd(mat, ddof=1), tgt) -class SharedNanFunctionsTestsMixin(object): +class SharedNanFunctionsTestsMixin: def test_mutation(self): # Check that passed array is not modified. ndat = _ndat.copy() @@ -590,7 +588,7 @@ class TestNanFunctions_MeanVarStd(SharedNanFunctionsTestsMixin): assert_(len(w) == 0) -class TestNanFunctions_Median(object): +class TestNanFunctions_Median: def test_mutation(self): # Check that passed array is not modified. @@ -754,7 +752,7 @@ class TestNanFunctions_Median(object): ([np.nan] * i) + [-inf] * j) -class TestNanFunctions_Percentile(object): +class TestNanFunctions_Percentile: def test_mutation(self): # Check that passed array is not modified. @@ -893,7 +891,7 @@ class TestNanFunctions_Percentile(object): assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6)) -class TestNanFunctions_Quantile(object): +class TestNanFunctions_Quantile: # most of this is already tested by TestPercentile def test_regression(self): diff --git a/numpy/lib/tests/test_packbits.py b/numpy/lib/tests/test_packbits.py index 95a465c36..5b07f41c6 100644 --- a/numpy/lib/tests/test_packbits.py +++ b/numpy/lib/tests/test_packbits.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_array_equal, assert_equal, assert_raises import pytest diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py index 89759bd83..cd0b90dc4 100644 --- a/numpy/lib/tests/test_polynomial.py +++ b/numpy/lib/tests/test_polynomial.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_almost_equal, @@ -7,7 +5,7 @@ from numpy.testing import ( ) -class TestPolynomial(object): +class TestPolynomial: def test_poly1d_str_and_repr(self): p = np.poly1d([1., 2, 3]) assert_equal(repr(p), 'poly1d([1., 2., 3.])') diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index fa5f4dec2..2f3c14df3 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import pytest import numpy as np @@ -19,7 +17,7 @@ zip_descr = np.lib.recfunctions._zip_descr zip_dtype = np.lib.recfunctions._zip_dtype -class TestRecFunctions(object): +class TestRecFunctions: # Misc tests def setup(self): @@ -348,7 +346,7 @@ class TestRecFunctions(object): assert_equal(b[()], 3) -class TestRecursiveFillFields(object): +class TestRecursiveFillFields: # Test recursive_fill_fields. def test_simple_flexible(self): # Test recursive_fill_fields on flexible-array @@ -371,7 +369,7 @@ class TestRecursiveFillFields(object): assert_equal(test, control) -class TestMergeArrays(object): +class TestMergeArrays: # Test merge_arrays def setup(self): @@ -504,7 +502,7 @@ class TestMergeArrays(object): assert_equal(test, control) -class TestAppendFields(object): +class TestAppendFields: # Test append_fields def setup(self): @@ -558,7 +556,7 @@ class TestAppendFields(object): assert_equal(test, control) -class TestStackArrays(object): +class TestStackArrays: # Test stack_arrays def setup(self): x = np.array([1, 2, ]) @@ -729,7 +727,7 @@ class TestStackArrays(object): assert_equal(res.mask, expected.mask) -class TestJoinBy(object): +class TestJoinBy: def setup(self): self.a = np.array(list(zip(np.arange(10), np.arange(50, 60), np.arange(100, 110))), @@ -772,7 +770,6 @@ class TestJoinBy(object): def test_join_subdtype(self): # tests the bug in https://stackoverflow.com/q/44769632/102441 - from numpy.lib import recfunctions as rfn foo = np.array([(1,)], dtype=[('key', int)]) bar = np.array([(1, np.array([1,2,3]))], @@ -895,7 +892,7 @@ class TestJoinBy(object): assert_equal(res.dtype, expected_dtype) -class TestJoinBy2(object): +class TestJoinBy2: @classmethod def setup(cls): cls.a = np.array(list(zip(np.arange(10), np.arange(50, 60), @@ -960,7 +957,7 @@ class TestJoinBy2(object): assert_equal(test.dtype, control.dtype) assert_equal(test, control) -class TestAppendFieldsObj(object): +class TestAppendFieldsObj: """ Test append_fields with arrays containing objects """ diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 4cd812f5d..019b7595e 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys @@ -11,7 +9,7 @@ from numpy.testing import ( from numpy.compat import unicode -class TestRegression(object): +class TestRegression: def test_poly1d(self): # Ticket #28 assert_equal(np.poly1d([1]) - np.poly1d([1, 0]), diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index be1604a75..fb7ba7874 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -1,7 +1,4 @@ -from __future__ import division, absolute_import, print_function - import numpy as np -import warnings import functools import sys import pytest @@ -30,7 +27,7 @@ def _add_keepdims(func): return wrapped -class TestTakeAlongAxis(object): +class TestTakeAlongAxis: def test_argequivalent(self): """ Test it translates from arg<func> to <func> """ from numpy.random import rand @@ -82,7 +79,7 @@ class TestTakeAlongAxis(object): assert_equal(actual.shape, (3, 2, 5)) -class TestPutAlongAxis(object): +class TestPutAlongAxis: def test_replace_max(self): a_base = np.array([[10, 30, 20], [60, 40, 50]]) @@ -107,7 +104,7 @@ class TestPutAlongAxis(object): assert_equal(take_along_axis(a, ai, axis=1), 20) -class TestApplyAlongAxis(object): +class TestApplyAlongAxis: def test_simple(self): a = np.ones((20, 10), 'd') assert_array_equal( @@ -273,14 +270,14 @@ class TestApplyAlongAxis(object): assert_equal(type(actual[i]), type(expected[i])) -class TestApplyOverAxes(object): +class TestApplyOverAxes: def test_simple(self): a = np.arange(24).reshape(2, 3, 4) aoa_a = apply_over_axes(np.sum, a, [0, 2]) assert_array_equal(aoa_a, np.array([[[60], [92], [124]]])) -class TestExpandDims(object): +class TestExpandDims: def test_functionality(self): s = (2, 3, 4, 5) a = np.empty(s) @@ -320,7 +317,7 @@ class TestExpandDims(object): assert_equal(expanded.mask.shape, (2, 1, 5)) -class TestArraySplit(object): +class TestArraySplit: def test_integer_0_split(self): a = np.arange(10) assert_raises(ValueError, array_split, a, 0) @@ -454,7 +451,7 @@ class TestArraySplit(object): compare_results(res, desired) -class TestSplit(object): +class TestSplit: # The split function is essentially the same as array_split, # except that it test if splitting will result in an # equal split. Only test for this case. @@ -470,7 +467,7 @@ class TestSplit(object): assert_raises(ValueError, split, a, 3) -class TestColumnStack(object): +class TestColumnStack: def test_non_iterable(self): assert_raises(TypeError, column_stack, 1) @@ -499,7 +496,7 @@ class TestColumnStack(object): column_stack((np.arange(3) for _ in range(2))) -class TestDstack(object): +class TestDstack: def test_non_iterable(self): assert_raises(TypeError, dstack, 1) @@ -538,7 +535,7 @@ class TestDstack(object): # array_split has more comprehensive test of splitting. # only do simple test on hsplit, vsplit, and dsplit -class TestHsplit(object): +class TestHsplit: """Only testing for integer splits. """ @@ -567,7 +564,7 @@ class TestHsplit(object): compare_results(res, desired) -class TestVsplit(object): +class TestVsplit: """Only testing for integer splits. """ @@ -594,7 +591,7 @@ class TestVsplit(object): compare_results(res, desired) -class TestDsplit(object): +class TestDsplit: # Only testing for integer splits. def test_non_iterable(self): assert_raises(ValueError, dsplit, 1, 1) @@ -627,7 +624,7 @@ class TestDsplit(object): compare_results(res, desired) -class TestSqueeze(object): +class TestSqueeze: def test_basic(self): from numpy.random import rand @@ -646,7 +643,7 @@ class TestSqueeze(object): assert_equal(type(res), np.ndarray) -class TestKron(object): +class TestKron: def test_return_type(self): class myarray(np.ndarray): __array_priority__ = 0.0 @@ -659,7 +656,7 @@ class TestKron(object): assert_equal(type(kron(ma, a)), myarray) -class TestTile(object): +class TestTile: def test_basic(self): a = np.array([0, 1, 2]) b = [[1, 2], [3, 4]] @@ -699,7 +696,7 @@ class TestTile(object): assert_equal(large, klarge) -class TestMayShareMemory(object): +class TestMayShareMemory: def test_basic(self): d = np.ones((50, 60)) d2 = np.ones((30, 60, 6)) diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py index 85fcceedc..9d95eb9d0 100644 --- a/numpy/lib/tests/test_stride_tricks.py +++ b/numpy/lib/tests/test_stride_tricks.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.core._rational_tests import rational from numpy.testing import ( @@ -65,8 +63,7 @@ def test_broadcast_kwargs(): x = np.arange(10) y = np.arange(10) - with assert_raises_regex(TypeError, - r'broadcast_arrays\(\) got an unexpected keyword*'): + with assert_raises_regex(TypeError, 'got an unexpected keyword'): broadcast_arrays(x, y, dtype='float64') @@ -356,14 +353,12 @@ def as_strided_writeable(): class VerySimpleSubClass(np.ndarray): def __new__(cls, *args, **kwargs): - kwargs['subok'] = True - return np.array(*args, **kwargs).view(cls) + return np.array(*args, subok=True, **kwargs).view(cls) class SimpleSubClass(VerySimpleSubClass): def __new__(cls, *args, **kwargs): - kwargs['subok'] = True - self = np.array(*args, **kwargs).view(cls) + self = np.array(*args, subok=True, **kwargs).view(cls) self.info = 'simple' return self diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py index bb844e4bd..cce683bfe 100644 --- a/numpy/lib/tests/test_twodim_base.py +++ b/numpy/lib/tests/test_twodim_base.py @@ -1,8 +1,6 @@ """Test functions for matrix module """ -from __future__ import division, absolute_import, print_function - from numpy.testing import ( assert_equal, assert_array_equal, assert_array_max_ulp, assert_array_almost_equal, assert_raises, assert_ @@ -26,7 +24,7 @@ def get_mat(n): return data -class TestEye(object): +class TestEye: def test_basic(self): assert_equal(eye(4), array([[1, 0, 0, 0], @@ -108,7 +106,7 @@ class TestEye(object): assert mat_f.flags.f_contiguous -class TestDiag(object): +class TestDiag: def test_vector(self): vals = (100 * arange(5)).astype('l') b = zeros((5, 5)) @@ -155,7 +153,7 @@ class TestDiag(object): assert_raises(ValueError, diag, [[[1]]]) -class TestFliplr(object): +class TestFliplr: def test_basic(self): assert_raises(ValueError, fliplr, ones(4)) a = get_mat(4) @@ -168,7 +166,7 @@ class TestFliplr(object): assert_equal(fliplr(a), b) -class TestFlipud(object): +class TestFlipud: def test_basic(self): a = get_mat(4) b = a[::-1, :] @@ -180,7 +178,7 @@ class TestFlipud(object): assert_equal(flipud(a), b) -class TestHistogram2d(object): +class TestHistogram2d: def test_simple(self): x = array( [0.41702200, 0.72032449, 1.1437481e-4, 0.302332573, 0.146755891]) @@ -298,7 +296,7 @@ class TestHistogram2d(object): assert_(r, ((ShouldDispatch,), (xy, xy), dict(weights=s_d))) -class TestTri(object): +class TestTri: def test_dtype(self): out = array([[1, 0, 0], [1, 1, 0], @@ -436,7 +434,7 @@ def test_tril_indices(): [-10, -10, -10, -10, -10]])) -class TestTriuIndices(object): +class TestTriuIndices: def test_triu_indices(self): iu1 = triu_indices(4) iu2 = triu_indices(4, k=2) @@ -486,21 +484,21 @@ class TestTriuIndices(object): [16, 17, 18, -1, -1]])) -class TestTrilIndicesFrom(object): +class TestTrilIndicesFrom: def test_exceptions(self): assert_raises(ValueError, tril_indices_from, np.ones((2,))) assert_raises(ValueError, tril_indices_from, np.ones((2, 2, 2))) # assert_raises(ValueError, tril_indices_from, np.ones((2, 3))) -class TestTriuIndicesFrom(object): +class TestTriuIndicesFrom: def test_exceptions(self): assert_raises(ValueError, triu_indices_from, np.ones((2,))) assert_raises(ValueError, triu_indices_from, np.ones((2, 2, 2))) # assert_raises(ValueError, triu_indices_from, np.ones((2, 3))) -class TestVander(object): +class TestVander: def test_basic(self): c = np.array([0, 1, -2, 3]) v = vander(c) diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index b3f114b92..47685550a 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.compat import long from numpy.testing import ( @@ -15,7 +13,7 @@ def assert_all(x): assert_(np.all(x), x) -class TestCommonType(object): +class TestCommonType: def test_basic(self): ai32 = np.array([[1, 2], [3, 4]], dtype=np.int32) af16 = np.array([[1, 2], [3, 4]], dtype=np.float16) @@ -31,7 +29,7 @@ class TestCommonType(object): assert_(common_type(acd) == np.cdouble) -class TestMintypecode(object): +class TestMintypecode: def test_default_1(self): for itype in '1bcsuwil': @@ -81,7 +79,7 @@ class TestMintypecode(object): assert_equal(mintypecode('idD'), 'D') -class TestIsscalar(object): +class TestIsscalar: def test_basic(self): assert_(np.isscalar(3)) @@ -92,7 +90,7 @@ class TestIsscalar(object): assert_(np.isscalar(4.0)) -class TestReal(object): +class TestReal: def test_real(self): y = np.random.rand(10,) @@ -123,7 +121,7 @@ class TestReal(object): assert_(not isinstance(out, np.ndarray)) -class TestImag(object): +class TestImag: def test_real(self): y = np.random.rand(10,) @@ -154,7 +152,7 @@ class TestImag(object): assert_(not isinstance(out, np.ndarray)) -class TestIscomplex(object): +class TestIscomplex: def test_fail(self): z = np.array([-1, 0, 1]) @@ -167,7 +165,7 @@ class TestIscomplex(object): assert_array_equal(res, [1, 0, 0]) -class TestIsreal(object): +class TestIsreal: def test_pass(self): z = np.array([-1, 0, 1j]) @@ -180,7 +178,7 @@ class TestIsreal(object): assert_array_equal(res, [0, 1, 1]) -class TestIscomplexobj(object): +class TestIscomplexobj: def test_basic(self): z = np.array([-1, 0, 1]) @@ -209,7 +207,7 @@ class TestIscomplexobj(object): # (pandas.core.dtypes) class PdComplex(np.complex128): pass - class PdDtype(object): + class PdDtype: name = 'category' names = None type = PdComplex @@ -233,7 +231,7 @@ class TestIscomplexobj(object): assert_(iscomplexobj(a)) -class TestIsrealobj(object): +class TestIsrealobj: def test_basic(self): z = np.array([-1, 0, 1]) assert_(isrealobj(z)) @@ -241,7 +239,7 @@ class TestIsrealobj(object): assert_(not isrealobj(z)) -class TestIsnan(object): +class TestIsnan: def test_goodvalues(self): z = np.array((-1., 0., 1.)) @@ -271,7 +269,7 @@ class TestIsnan(object): assert_all(np.isnan(np.array(0+0j)/0.) == 1) -class TestIsfinite(object): +class TestIsfinite: # Fixme, wrong place, isfinite now ufunc def test_goodvalues(self): @@ -302,7 +300,7 @@ class TestIsfinite(object): assert_all(np.isfinite(np.array(1+1j)/0.) == 0) -class TestIsinf(object): +class TestIsinf: # Fixme, wrong place, isinf now ufunc def test_goodvalues(self): @@ -331,7 +329,7 @@ class TestIsinf(object): assert_all(np.isinf(np.array((0.,))/0.) == 0) -class TestIsposinf(object): +class TestIsposinf: def test_generic(self): with np.errstate(divide='ignore', invalid='ignore'): @@ -341,7 +339,7 @@ class TestIsposinf(object): assert_(vals[2] == 1) -class TestIsneginf(object): +class TestIsneginf: def test_generic(self): with np.errstate(divide='ignore', invalid='ignore'): @@ -351,7 +349,7 @@ class TestIsneginf(object): assert_(vals[2] == 0) -class TestNanToNum(object): +class TestNanToNum: def test_generic(self): with np.errstate(divide='ignore', invalid='ignore'): @@ -456,7 +454,7 @@ class TestNanToNum(object): assert_equal(type(vals), np.ndarray) -class TestRealIfClose(object): +class TestRealIfClose: def test_basic(self): a = np.random.rand(10) @@ -469,7 +467,7 @@ class TestRealIfClose(object): assert_all(isrealobj(b)) -class TestArrayConversion(object): +class TestArrayConversion: def test_asfarray(self): a = asfarray(np.array([1, 2, 3])) diff --git a/numpy/lib/tests/test_ufunclike.py b/numpy/lib/tests/test_ufunclike.py index 64280616f..c280b6969 100644 --- a/numpy/lib/tests/test_ufunclike.py +++ b/numpy/lib/tests/test_ufunclike.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np import numpy.core as nx import numpy.lib.ufunclike as ufl @@ -8,7 +6,7 @@ from numpy.testing import ( ) -class TestUfunclike(object): +class TestUfunclike: def test_isposinf(self): a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0]) diff --git a/numpy/lib/tests/test_utils.py b/numpy/lib/tests/test_utils.py index 9673a05fa..57c840342 100644 --- a/numpy/lib/tests/test_utils.py +++ b/numpy/lib/tests/test_utils.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import inspect import sys import pytest @@ -102,7 +100,7 @@ def test_safe_eval_nameconstant(): utils.safe_eval('None') -class TestByteBounds(object): +class TestByteBounds: def test_byte_bounds(self): # pointer difference matches size * itemsize diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py index f45392188..320a24856 100644 --- a/numpy/lib/twodim_base.py +++ b/numpy/lib/twodim_base.py @@ -1,18 +1,16 @@ """ Basic functions for manipulating 2d arrays """ -from __future__ import division, absolute_import, print_function - import functools from numpy.core.numeric import ( - absolute, asanyarray, arange, zeros, greater_equal, multiply, ones, + asanyarray, arange, zeros, greater_equal, multiply, ones, asarray, where, int8, int16, int32, int64, empty, promote_types, diagonal, nonzero ) from numpy.core.overrides import set_module from numpy.core import overrides -from numpy.core import iinfo, transpose +from numpy.core import iinfo __all__ = [ diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py index 977117235..13db9adc3 100644 --- a/numpy/lib/type_check.py +++ b/numpy/lib/type_check.py @@ -1,7 +1,6 @@ """Automatically adapted for numpy Sep 19, 2005 by convertcode.py """ -from __future__ import division, absolute_import, print_function import functools import warnings diff --git a/numpy/lib/ufunclike.py b/numpy/lib/ufunclike.py index 9f03b1329..8512669c2 100644 --- a/numpy/lib/ufunclike.py +++ b/numpy/lib/ufunclike.py @@ -3,8 +3,6 @@ Module of functions that are like ufuncs in acting on arrays and optionally storing results in an output array. """ -from __future__ import division, absolute_import, print_function - __all__ = ['fix', 'isneginf', 'isposinf'] import numpy.core.numeric as nx diff --git a/numpy/lib/user_array.py b/numpy/lib/user_array.py index f1510a7b1..9c266fd6b 100644 --- a/numpy/lib/user_array.py +++ b/numpy/lib/user_array.py @@ -5,8 +5,6 @@ Try to inherit from the ndarray instead of using this class as this is not complete. """ -from __future__ import division, absolute_import, print_function - from numpy.core import ( array, asarray, absolute, add, subtract, multiply, divide, remainder, power, left_shift, right_shift, bitwise_and, bitwise_or, @@ -16,7 +14,7 @@ from numpy.core import ( from numpy.compat import long -class container(object): +class container: """ container(data, dtype=None, copy=True) diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 3c71d2a7c..d41a6e541 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import os import sys import types @@ -55,7 +53,7 @@ def _set_function_name(func, name): return func -class _Deprecate(object): +class _Deprecate: """ Decorator class to deprecate old functions. diff --git a/numpy/linalg/__init__.py b/numpy/linalg/__init__.py index 55560815d..3a53ac6ec 100644 --- a/numpy/linalg/__init__.py +++ b/numpy/linalg/__init__.py @@ -69,8 +69,6 @@ Exceptions LinAlgError """ -from __future__ import division, absolute_import, print_function - # To get sub-modules from .linalg import * diff --git a/numpy/linalg/lapack_lite/README.rst b/numpy/linalg/lapack_lite/README.rst index 1343d25f8..ba30aa4ed 100644 --- a/numpy/linalg/lapack_lite/README.rst +++ b/numpy/linalg/lapack_lite/README.rst @@ -18,9 +18,9 @@ and is unlikely to ever be ported to python 3. The routines that ``lapack_litemodule.c`` wraps are listed in ``wrapped_routines``, along with a few exceptions that aren't picked up properly. Assuming that you have an unpacked LAPACK source tree in -``~/LAPACK``, you generate the new routines in a directory ``new-lite/`` with:: +``~/LAPACK``, you generate the new routines in this directory with:: -$ python2 ./make_lite.py wrapped_routines ~/LAPACK new-lite/ +$ python2 ./make_lite.py wrapped_routines ~/LAPACK This will grab the right routines, with dependencies, put them into the appropriate ``f2c_*.f`` files, run ``f2c`` over them, then do some scrubbing diff --git a/numpy/linalg/lapack_lite/clapack_scrub.py b/numpy/linalg/lapack_lite/clapack_scrub.py index 434586113..2ddd083ea 100644 --- a/numpy/linalg/lapack_lite/clapack_scrub.py +++ b/numpy/linalg/lapack_lite/clapack_scrub.py @@ -1,6 +1,4 @@ #!/usr/bin/env python -from __future__ import division, absolute_import, print_function - import sys, os import re from plex import Scanner, Str, Lexicon, Opt, Bol, State, AnyChar, TEXT, IGNORE @@ -106,7 +104,7 @@ def cleanSource(source): source = re.sub(r'\n\n\n\n+', r'\n\n\n', source) return source -class LineQueue(object): +class LineQueue: def __init__(self): object.__init__(self) self._queue = [] diff --git a/numpy/linalg/lapack_lite/f2c.c b/numpy/linalg/lapack_lite/f2c.c index 1114bef3b..9a1e9cec1 100644 --- a/numpy/linalg/lapack_lite/f2c.c +++ b/numpy/linalg/lapack_lite/f2c.c @@ -567,7 +567,7 @@ if( (abi = b->i) < 0.f) abi = - abi; if( abr <= abi ) { - /*Let IEEE Infinties handle this ;( */ + /*Let IEEE Infinities handle this ;( */ /*if(abi == 0) sig_die("complex division by zero", 1);*/ ratio = b->r / b->i ; @@ -603,7 +603,7 @@ if( (abi = b->i) < 0.) abi = - abi; if( abr <= abi ) { - /*Let IEEE Infinties handle this ;( */ + /*Let IEEE Infinities handle this ;( */ /*if(abi == 0) sig_die("complex division by zero", 1);*/ ratio = b->r / b->i ; diff --git a/numpy/linalg/lapack_lite/f2c.h b/numpy/linalg/lapack_lite/f2c.h index 80f1a12b1..4462eaa74 100644 --- a/numpy/linalg/lapack_lite/f2c.h +++ b/numpy/linalg/lapack_lite/f2c.h @@ -8,15 +8,17 @@ #define F2C_INCLUDE #include <math.h> +#include "numpy/npy_common.h" +#include "npy_cblas.h" -typedef int integer; +typedef CBLAS_INT integer; typedef char *address; typedef short int shortint; typedef float real; typedef double doublereal; typedef struct { real r, i; } complex; typedef struct { doublereal r, i; } doublecomplex; -typedef int logical; +typedef CBLAS_INT logical; typedef short int shortlogical; typedef char logical1; typedef char integer1; @@ -37,9 +39,9 @@ typedef short flag; typedef short ftnlen; typedef short ftnint; #else -typedef int flag; -typedef int ftnlen; -typedef int ftnint; +typedef CBLAS_INT flag; +typedef CBLAS_INT ftnlen; +typedef CBLAS_INT ftnint; #endif /*external read, write*/ @@ -352,7 +354,7 @@ extern void s_copy(char *, char *, ftnlen, ftnlen); extern int s_paus(char *, ftnlen); extern integer s_rdfe(cilist *); extern integer s_rdue(cilist *); -extern integer s_rnge(char *, integer, char *, integer); +extern int s_rnge(char *, int, char *, int); extern integer s_rsfe(cilist *); extern integer s_rsfi(icilist *); extern integer s_rsle(cilist *); diff --git a/numpy/linalg/lapack_lite/f2c_blas.c b/numpy/linalg/lapack_lite/f2c_blas.c index 3af506b71..44ad23bfe 100644 --- a/numpy/linalg/lapack_lite/f2c_blas.c +++ b/numpy/linalg/lapack_lite/f2c_blas.c @@ -4912,7 +4912,7 @@ L20: ( 1 + ( n - 1 )*abs( INCX ) ). Before entry, the incremented array X must contain the n element vector x. On exit, X is overwritten with the - tranformed vector x. + transformed vector x. INCX - INTEGER. On entry, INCX specifies the increment for the elements of @@ -9807,7 +9807,7 @@ L40: ( 1 + ( n - 1 )*abs( INCX ) ). Before entry, the incremented array X must contain the n element vector x. On exit, X is overwritten with the - tranformed vector x. + transformed vector x. INCX - INTEGER. On entry, INCX specifies the increment for the elements of @@ -14410,7 +14410,7 @@ L40: ( 1 + ( n - 1 )*abs( INCX ) ). Before entry, the incremented array X must contain the n element vector x. On exit, X is overwritten with the - tranformed vector x. + transformed vector x. INCX - INTEGER. On entry, INCX specifies the increment for the elements of @@ -19998,7 +19998,7 @@ L20: ( 1 + ( n - 1 )*abs( INCX ) ). Before entry, the incremented array X must contain the n element vector x. On exit, X is overwritten with the - tranformed vector x. + transformed vector x. INCX - INTEGER. On entry, INCX specifies the increment for the elements of diff --git a/numpy/linalg/lapack_lite/fortran.py b/numpy/linalg/lapack_lite/fortran.py index dc0a5ebd9..388c88daa 100644 --- a/numpy/linalg/lapack_lite/fortran.py +++ b/numpy/linalg/lapack_lite/fortran.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import re import itertools @@ -14,7 +12,7 @@ def isContinuation(line): COMMENT, STATEMENT, CONTINUATION = 0, 1, 2 def lineType(line): - """Return the type of a line of Fortan code.""" + """Return the type of a line of Fortran code.""" if isBlank(line): return COMMENT elif isLabel(line): @@ -26,7 +24,7 @@ def lineType(line): else: return STATEMENT -class LineIterator(object): +class LineIterator: """LineIterator(iterable) Return rstrip()'d lines from iterable, while keeping a count of the @@ -49,7 +47,7 @@ class LineIterator(object): next = __next__ -class PushbackIterator(object): +class PushbackIterator: """PushbackIterator(iterable) Return an iterator for which items can be pushed back into. diff --git a/numpy/linalg/lapack_lite/make_lite.py b/numpy/linalg/lapack_lite/make_lite.py index 61102d6ab..4b1a0ad82 100755 --- a/numpy/linalg/lapack_lite/make_lite.py +++ b/numpy/linalg/lapack_lite/make_lite.py @@ -11,8 +11,6 @@ Requires the following to be on the path: * patch """ -from __future__ import division, absolute_import, print_function - import sys import os import subprocess @@ -65,7 +63,7 @@ them. #endif ''' -class FortranRoutine(object): +class FortranRoutine: """Wrapper for a Fortran routine in a file. """ type = 'generic' @@ -97,7 +95,7 @@ class UnknownFortranRoutine(FortranRoutine): def dependencies(self): return [] -class FortranLibrary(object): +class FortranLibrary: """Container for a bunch of Fortran routines. """ def __init__(self, src_dirs): diff --git a/numpy/linalg/lapack_litemodule.c b/numpy/linalg/lapack_litemodule.c index 4c80317f5..362a593a6 100644 --- a/numpy/linalg/lapack_litemodule.c +++ b/numpy/linalg/lapack_litemodule.c @@ -377,7 +377,6 @@ static struct PyMethodDef lapack_lite_module_methods[] = { }; -#if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "lapack_lite", @@ -389,32 +388,25 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif /* Initialization function for the module */ -#if PY_MAJOR_VERSION >= 3 -#define RETVAL(x) x PyMODINIT_FUNC PyInit_lapack_lite(void) -#else -#define RETVAL(x) -PyMODINIT_FUNC -initlapack_lite(void) -#endif { PyObject *m,*d; -#if PY_MAJOR_VERSION >= 3 m = PyModule_Create(&moduledef); -#else - m = Py_InitModule4("lapack_lite", lapack_lite_module_methods, - "", (PyObject*)NULL,PYTHON_API_VERSION); -#endif if (m == NULL) { - return RETVAL(NULL); + return NULL; } import_array(); d = PyModule_GetDict(m); LapackError = PyErr_NewException("lapack_lite.LapackError", NULL, NULL); PyDict_SetItemString(d, "LapackError", LapackError); - return RETVAL(m); +#ifdef HAVE_BLAS_ILP64 + PyDict_SetItemString(d, "_ilp64", Py_True); +#else + PyDict_SetItemString(d, "_ilp64", Py_False); +#endif + + return m; } diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 6249a57c2..15615e1a3 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -8,8 +8,6 @@ version only accesses the following LAPACK functions: dgesv, zgesv, dgeev, zgeev, dgesdd, zgesdd, dgelsd, zgelsd, dsyevd, zheevd, dgetrf, zgetrf, dpotrf, zpotrf, dgeqrf, zgeqrf, zungqr, dorgqr. """ -from __future__ import division, absolute_import, print_function - __all__ = ['matrix_power', 'solve', 'tensorsolve', 'tensorinv', 'inv', 'cholesky', 'eigvals', 'eigvalsh', 'pinv', 'slogdet', 'det', @@ -345,6 +343,10 @@ def solve(a, b): LinAlgError If `a` is singular or not square. + See Also + -------- + scipy.linalg.solve : Similar function in SciPy. + Notes ----- @@ -502,6 +504,10 @@ def inv(a): LinAlgError If `a` is not square or inversion fails. + See Also + -------- + scipy.linalg.inv : Similar function in SciPy. + Notes ----- @@ -679,8 +685,10 @@ def cholesky(a): Return the Cholesky decomposition, `L * L.H`, of the square matrix `a`, where `L` is lower-triangular and .H is the conjugate transpose operator (which is the ordinary transpose if `a` is real-valued). `a` must be - Hermitian (symmetric if real-valued) and positive-definite. Only `L` is - actually returned. + Hermitian (symmetric if real-valued) and positive-definite. No + checking is performed to verify whether `a` is Hermitian or not. + In addition, only the lower-triangular and diagonal elements of `a` + are used. Only `L` is actually returned. Parameters ---------- @@ -700,6 +708,14 @@ def cholesky(a): If the decomposition fails, for example, if `a` is not positive-definite. + See Also + -------- + scipy.linalg.cholesky : Similar function in SciPy. + scipy.linalg.cholesky_banded : Cholesky decompose a banded Hermitian + positive-definite matrix. + scipy.linalg.cho_factor : Cholesky decomposition of a matrix, to use in + `scipy.linalg.cho_solve`. + Notes ----- @@ -812,6 +828,11 @@ def qr(a, mode='reduced'): LinAlgError If factoring fails. + See Also + -------- + scipy.linalg.qr : Similar function in SciPy. + scipy.linalg.rq : Compute RQ decomposition of a matrix. + Notes ----- This is an interface to the LAPACK routines ``dgeqrf``, ``zgeqrf``, @@ -1004,6 +1025,7 @@ def eigvals(a): (conjugate symmetric) arrays. eigh : eigenvalues and eigenvectors of real symmetric or complex Hermitian (conjugate symmetric) arrays. + scipy.linalg.eigvals : Similar function in SciPy. Notes ----- @@ -1105,6 +1127,7 @@ def eigvalsh(a, UPLO='L'): eigvals : eigenvalues of general real or complex arrays. eig : eigenvalues and right eigenvectors of general real or complex arrays. + scipy.linalg.eigvalsh : Similar function in SciPy. Notes ----- @@ -1203,12 +1226,12 @@ def eig(a): See Also -------- eigvals : eigenvalues of a non-symmetric array. - eigh : eigenvalues and eigenvectors of a real symmetric or complex Hermitian (conjugate symmetric) array. - eigvalsh : eigenvalues of a real symmetric or complex Hermitian (conjugate symmetric) array. + scipy.linalg.eig : Similar function in SciPy (but also solves the + generalized eigenvalue problem). Notes ----- @@ -1355,6 +1378,8 @@ def eigh(a, UPLO='L'): (conjugate symmetric) arrays. eig : eigenvalues and right eigenvectors for non-symmetric arrays. eigvals : eigenvalues of non-symmetric arrays. + scipy.linalg.eigh : Similar function in SciPy (but also solves the + generalized eigenvalue problem). Notes ----- @@ -1506,6 +1531,11 @@ def svd(a, full_matrices=True, compute_uv=True, hermitian=False): LinAlgError If SVD computation does not converge. + See Also + -------- + scipy.linalg.svd : Similar function in SciPy. + scipy.linalg.svdvals : Compute singular values of a matrix. + Notes ----- @@ -1917,6 +1947,13 @@ def pinv(a, rcond=1e-15, hermitian=False): LinAlgError If the SVD computation does not converge. + See Also + -------- + scipy.linalg.pinv : Similar function in SciPy. + scipy.linalg.pinv2 : Similar function in SciPy (SVD-based). + scipy.linalg.pinvh : Compute the (Moore-Penrose) pseudo-inverse of a + Hermitian matrix. + Notes ----- The pseudo-inverse of a matrix A, denoted :math:`A^+`, is @@ -2079,6 +2116,7 @@ def det(a): -------- slogdet : Another way to represent the determinant, more suitable for large matrices where underflow/overflow may occur. + scipy.linalg.det : Similar function in SciPy. Notes ----- @@ -2179,6 +2217,10 @@ def lstsq(a, b, rcond="warn"): LinAlgError If computation does not converge. + See Also + -------- + scipy.linalg.lstsq : Similar function in SciPy. + Notes ----- If `b` is a matrix, then all array results are returned as matrices. @@ -2353,6 +2395,10 @@ def norm(x, ord=None, axis=None, keepdims=False): n : float or ndarray Norm of the matrix or vector(s). + See Also + -------- + scipy.linalg.norm : Similar function in SciPy. + Notes ----- For values of ``ord < 1``, the result is, strictly speaking, not a diff --git a/numpy/linalg/setup.py b/numpy/linalg/setup.py index 6315a34b4..66eed41b0 100644 --- a/numpy/linalg/setup.py +++ b/numpy/linalg/setup.py @@ -1,11 +1,9 @@ -from __future__ import division, print_function - import os import sys def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration - from numpy.distutils.system_info import get_info + from numpy.distutils.system_info import get_info, system_info config = Configuration('linalg', parent_package, top_path) config.add_data_dir('tests') @@ -31,8 +29,23 @@ def configuration(parent_package='', top_path=None): else: lapack_info = get_info('lapack_opt', 0) # and {} + use_lapack_lite = not lapack_info + + if use_lapack_lite: + # This makes numpy.distutils write the fact that lapack_lite + # is being used to numpy.__config__ + class numpy_linalg_lapack_lite(system_info): + def calc_info(self): + info = {'language': 'c'} + if sys.maxsize > 2**32: + # Build lapack-lite in 64-bit integer mode + info['define_macros'] = [('HAVE_BLAS_ILP64', None)] + self.set_info(**info) + + lapack_info = numpy_linalg_lapack_lite().get_info(2) + def get_lapack_lite_sources(ext, build_dir): - if not lapack_info: + if use_lapack_lite: print("### Warning: Using unoptimized lapack ###") return all_sources else: diff --git a/numpy/linalg/tests/test_build.py b/numpy/linalg/tests/test_build.py index 921390da3..cbf3089bc 100644 --- a/numpy/linalg/tests/test_build.py +++ b/numpy/linalg/tests/test_build.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from subprocess import PIPE, Popen import sys import re @@ -9,7 +7,7 @@ from numpy.linalg import lapack_lite from numpy.testing import assert_ -class FindDependenciesLdd(object): +class FindDependenciesLdd: def __init__(self): self.cmd = ['ldd'] @@ -41,7 +39,7 @@ class FindDependenciesLdd(object): return founds -class TestF77Mismatch(object): +class TestF77Mismatch: @pytest.mark.skipif(not(sys.platform[:5] == 'linux'), reason="no fortran compiler on non-Linux platform") diff --git a/numpy/linalg/tests/test_deprecations.py b/numpy/linalg/tests/test_deprecations.py index e12755e0d..cd4c10832 100644 --- a/numpy/linalg/tests/test_deprecations.py +++ b/numpy/linalg/tests/test_deprecations.py @@ -1,8 +1,6 @@ """Test deprecation and future warnings. """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_warns diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py index bd3df1ca4..59c71d196 100644 --- a/numpy/linalg/tests/test_linalg.py +++ b/numpy/linalg/tests/test_linalg.py @@ -1,8 +1,6 @@ """ Test functions for linalg module """ -from __future__ import division, absolute_import, print_function - import os import sys import itertools @@ -68,7 +66,7 @@ all_tags = { } -class LinalgCase(object): +class LinalgCase: def __init__(self, name, a, b, tags=set()): """ A bundle of arguments to be passed to a test case, with an identifying @@ -333,7 +331,7 @@ CASES += _make_strided_cases() # # Test different routines against the above cases # -class LinalgTestCase(object): +class LinalgTestCase: TEST_CASES = CASES def check_cases(self, require=set(), exclude=set()): @@ -634,7 +632,7 @@ class TestEig(EigCases): assert_(isinstance(a, np.ndarray)) -class SVDBaseTests(object): +class SVDBaseTests: hermitian = False @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble]) @@ -976,7 +974,7 @@ class TestLstsq(LstsqCases): @pytest.mark.parametrize('dt', [np.dtype(c) for c in '?bBhHiIqQefdgFDGO']) -class TestMatrixPower(object): +class TestMatrixPower: rshft_0 = np.eye(4) rshft_1 = rshft_0[[3, 0, 1, 2]] @@ -1076,7 +1074,7 @@ class TestEigvalshCases(HermitianTestCase, HermitianGeneralizedTestCase): assert_allclose(ev2, evalues, rtol=get_rtol(ev.dtype)) -class TestEigvalsh(object): +class TestEigvalsh: @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble]) def test_types(self, dtype): x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype) @@ -1152,7 +1150,7 @@ class TestEighCases(HermitianTestCase, HermitianGeneralizedTestCase): rtol=get_rtol(ev.dtype), err_msg=repr(a)) -class TestEigh(object): +class TestEigh: @pytest.mark.parametrize('dtype', [single, double, csingle, cdouble]) def test_types(self, dtype): x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype) @@ -1211,7 +1209,7 @@ class TestEigh(object): assert_(isinstance(a, np.ndarray)) -class _TestNormBase(object): +class _TestNormBase: dt = None dec = None @@ -1497,7 +1495,7 @@ class _TestNorm(_TestNorm2D, _TestNormGeneral): pass -class TestNorm_NonSystematic(object): +class TestNorm_NonSystematic: def test_longdouble_norm(self): # Non-regression test: p-norm of longdouble would previously raise @@ -1552,7 +1550,7 @@ class TestNormInt64(_TestNorm, _TestNormInt64Base): pass -class TestMatrixRank(object): +class TestMatrixRank: def test_matrix_rank(self): # Full rank matrix @@ -1601,7 +1599,7 @@ def test_reduced_rank(): assert_equal(matrix_rank(X), 8) -class TestQR(object): +class TestQR: # Define the array class here, so run this on matrices elsewhere. array = np.array @@ -1701,7 +1699,7 @@ class TestQR(object): self.check_qr(m2.T) -class TestCholesky(object): +class TestCholesky: # TODO: are there no other tests for cholesky? def test_basic_property(self): @@ -1863,7 +1861,7 @@ def test_sdot_bug_8577(): subprocess.check_call([sys.executable, "-c", code]) -class TestMultiDot(object): +class TestMultiDot: def test_basic_function_with_three_arguments(self): # multi_dot with three arguments uses a fast hand coded algorithm to @@ -1957,7 +1955,7 @@ class TestMultiDot(object): assert_raises(ValueError, multi_dot, [np.random.random((3, 3))]) -class TestTensorinv(object): +class TestTensorinv: @pytest.mark.parametrize("arr, ind", [ (np.ones((4, 6, 8, 2)), 2), @@ -2016,3 +2014,31 @@ def test_blas64_dot(): a[0,-1] = 1 c = np.dot(b, a) assert_equal(c[0,-1], 1) + + +@pytest.mark.xfail(not HAS_LAPACK64, + reason="Numpy not compiled with 64-bit BLAS/LAPACK") +def test_blas64_geqrf_lwork_smoketest(): + # Smoke test LAPACK geqrf lwork call with 64-bit integers + dtype = np.float64 + lapack_routine = np.linalg.lapack_lite.dgeqrf + + m = 2**32 + 1 + n = 2**32 + 1 + lda = m + + # Dummy arrays, not referenced by the lapack routine, so don't + # need to be of the right size + a = np.zeros([1, 1], dtype=dtype) + work = np.zeros([1], dtype=dtype) + tau = np.zeros([1], dtype=dtype) + + # Size query + results = lapack_routine(m, n, a, lda, tau, work, -1, 0) + assert_equal(results['info'], 0) + assert_equal(results['m'], m) + assert_equal(results['n'], m) + + # Should result to an integer of a reasonable size + lwork = int(work.item()) + assert_(2**32 < lwork < 2**42) diff --git a/numpy/linalg/tests/test_regression.py b/numpy/linalg/tests/test_regression.py index bd3a45872..1ae207b49 100644 --- a/numpy/linalg/tests/test_regression.py +++ b/numpy/linalg/tests/test_regression.py @@ -1,7 +1,5 @@ """ Test functions for linalg module """ -from __future__ import division, absolute_import, print_function - import warnings import numpy as np @@ -12,7 +10,7 @@ from numpy.testing import ( ) -class TestRegression(object): +class TestRegression: def test_eig_build(self): # Ticket #652 diff --git a/numpy/linalg/umath_linalg.c.src b/numpy/linalg/umath_linalg.c.src index e864c541b..00ff6b7a8 100644 --- a/numpy/linalg/umath_linalg.c.src +++ b/numpy/linalg/umath_linalg.c.src @@ -3626,7 +3626,6 @@ static PyMethodDef UMath_LinAlgMethods[] = { {NULL, NULL, 0, NULL} /* Sentinel */ }; -#if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, UMATH_LINALG_MODULE_NAME, @@ -3638,29 +3637,17 @@ static struct PyModuleDef moduledef = { NULL, NULL }; -#endif -#if defined(NPY_PY3K) -#define RETVAL(x) x PyObject *PyInit__umath_linalg(void) -#else -#define RETVAL(x) -PyMODINIT_FUNC -init_umath_linalg(void) -#endif { PyObject *m; PyObject *d; PyObject *version; init_constants(); -#if defined(NPY_PY3K) m = PyModule_Create(&moduledef); -#else - m = Py_InitModule(UMATH_LINALG_MODULE_NAME, UMath_LinAlgMethods); -#endif if (m == NULL) { - return RETVAL(NULL); + return NULL; } import_array(); @@ -3678,8 +3665,8 @@ init_umath_linalg(void) if (PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "cannot load _umath_linalg module."); - return RETVAL(NULL); + return NULL; } - return RETVAL(m); + return m; } diff --git a/numpy/ma/README.txt b/numpy/ma/README.rst index 47f20d645..47f20d645 100644 --- a/numpy/ma/README.txt +++ b/numpy/ma/README.rst diff --git a/numpy/ma/__init__.py b/numpy/ma/__init__.py index 36ceb1f6e..870cc4ef2 100644 --- a/numpy/ma/__init__.py +++ b/numpy/ma/__init__.py @@ -39,8 +39,6 @@ may now proceed to calculate the mean of the other values: .. moduleauthor:: Jarrod Millman """ -from __future__ import division, absolute_import, print_function - from . import core from .core import * diff --git a/numpy/ma/bench.py b/numpy/ma/bench.py index a9ba42dea..a1363d4d9 100644 --- a/numpy/ma/bench.py +++ b/numpy/ma/bench.py @@ -1,8 +1,6 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- -from __future__ import division, print_function - import timeit import numpy diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 2baf547a4..d51d8e6ec 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -20,8 +20,6 @@ Released for unlimited redistribution. """ # pylint: disable-msg=E1002 -from __future__ import division, absolute_import, print_function - import sys import operator import warnings @@ -800,7 +798,7 @@ ufunc_domain = {} ufunc_fills = {} -class _DomainCheckInterval(object): +class _DomainCheckInterval: """ Define a valid interval, so that : @@ -825,7 +823,7 @@ class _DomainCheckInterval(object): umath.less(x, self.a)) -class _DomainTan(object): +class _DomainTan: """ Define a valid interval for the `tan` function, so that: @@ -843,7 +841,7 @@ class _DomainTan(object): return umath.less(umath.absolute(umath.cos(x)), self.eps) -class _DomainSafeDivide(object): +class _DomainSafeDivide: """ Define a domain for safe division. @@ -864,7 +862,7 @@ class _DomainSafeDivide(object): return umath.absolute(a) * self.tolerance >= umath.absolute(b) -class _DomainGreater(object): +class _DomainGreater: """ DomainGreater(v)(x) is True where x <= v. @@ -880,7 +878,7 @@ class _DomainGreater(object): return umath.less_equal(x, self.critical_value) -class _DomainGreaterEqual(object): +class _DomainGreaterEqual: """ DomainGreaterEqual(v)(x) is True where x < v. @@ -896,7 +894,7 @@ class _DomainGreaterEqual(object): return umath.less(x, self.critical_value) -class _MaskedUFunc(object): +class _MaskedUFunc: def __init__(self, ufunc): self.f = ufunc self.__doc__ = ufunc.__doc__ @@ -2386,7 +2384,7 @@ def masked_invalid(a, copy=True): ############################################################################### -class _MaskedPrintOption(object): +class _MaskedPrintOption: """ Handle the string used to represent missing data in a masked array. @@ -2604,7 +2602,7 @@ def _arraymethod(funcname, onmask=True): return wrapped_method -class MaskedIterator(object): +class MaskedIterator: """ Flat iterator object to iterate over masked arrays. @@ -6646,7 +6644,7 @@ ptp.__doc__ = MaskedArray.ptp.__doc__ ############################################################################## -class _frommethod(object): +class _frommethod: """ Define functions from existing MaskedArray methods. @@ -7980,7 +7978,7 @@ def fromflex(fxarray): return masked_array(fxarray['_data'], mask=fxarray['_mask']) -class _convert2ma(object): +class _convert2ma: """ Convert functions from numpy to numpy.ma. diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index f4a914471..31648fb2e 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -8,8 +8,6 @@ A collection of utilities for `numpy.ma`. :version: $Id: extras.py 3473 2007-10-29 15:18:13Z jarrod.millman $ """ -from __future__ import division, absolute_import, print_function - __all__ = [ 'apply_along_axis', 'apply_over_axes', 'atleast_1d', 'atleast_2d', 'atleast_3d', 'average', 'clump_masked', 'clump_unmasked', @@ -214,7 +212,7 @@ def masked_all_like(arr): #####-------------------------------------------------------------------------- #---- --- Standard functions --- #####-------------------------------------------------------------------------- -class _fromnxfunction(object): +class _fromnxfunction: """ Defines a wrapper to adapt NumPy functions to masked arrays. diff --git a/numpy/ma/mrecords.py b/numpy/ma/mrecords.py index ae1a12c2c..4ff7866ab 100644 --- a/numpy/ma/mrecords.py +++ b/numpy/ma/mrecords.py @@ -8,8 +8,6 @@ and the masking of individual fields. .. moduleauthor:: Pierre Gerard-Marchant """ -from __future__ import division, absolute_import, print_function - # We should make sure that no field is called '_mask','mask','_fieldmask', # or whatever restricted keywords. An idea would be to no bother in the # first place, and then rename the invalid fields with a trailing @@ -87,7 +85,7 @@ def _get_fieldmask(self): return fdmask -class MaskedRecords(MaskedArray, object): +class MaskedRecords(MaskedArray): """ Attributes diff --git a/numpy/ma/setup.py b/numpy/ma/setup.py index d1d6c89b5..af1e419b4 100644 --- a/numpy/ma/setup.py +++ b/numpy/ma/setup.py @@ -1,6 +1,4 @@ #!/usr/bin/env python -from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('ma', parent_package, top_path) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index b72ce56aa..458b78580 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4,8 +4,6 @@ :author: Pierre Gerard-Marchant :contact: pierregm_at_uga_dot_edu """ -from __future__ import division, absolute_import, print_function - __author__ = "Pierre GF Gerard-Marchant" import sys @@ -65,7 +63,7 @@ num_dts = [np.dtype(dt_) for dt_ in '?bhilqBHILQefdgFD'] num_ids = [dt_.char for dt_ in num_dts] -class TestMaskedArray(object): +class TestMaskedArray: # Base test class for MaskedArrays. def setup(self): @@ -946,7 +944,7 @@ class TestMaskedArray(object): assert_(mx2[0] == 0.) -class TestMaskedArrayArithmetic(object): +class TestMaskedArrayArithmetic: # Base test class for MaskedArrays. def setup(self): @@ -1715,7 +1713,7 @@ class TestMaskedArrayArithmetic(object): assert_equal(a.mask, [0, 0, 0, 0, 1]) -class TestMaskedArrayAttributes(object): +class TestMaskedArrayAttributes: def test_keepmask(self): # Tests the keep mask flag @@ -1891,7 +1889,7 @@ class TestMaskedArrayAttributes(object): assert_equal(m._mask, np.ma.nomask) -class TestFillingValues(object): +class TestFillingValues: def test_check_on_scalar(self): # Test _check_fill_value set to valid and invalid values @@ -2229,7 +2227,7 @@ class TestFillingValues(object): assert_equal(a["f1"].fill_value, default_fill_value("eggs")) -class TestUfuncs(object): +class TestUfuncs: # Test class for the application of ufuncs on MaskedArrays. def setup(self): @@ -2309,7 +2307,7 @@ class TestUfuncs(object): assert_raises(TypeError, operator.mul, a, "abc") assert_raises(TypeError, operator.truediv, a, "abc") - class MyClass(object): + class MyClass: __array_priority__ = a.__array_priority__ + 1 def __mul__(self, other): @@ -2323,7 +2321,7 @@ class TestUfuncs(object): assert_(a * me == "My rmul") # and that __array_priority__ is respected - class MyClass2(object): + class MyClass2: __array_priority__ = 100 def __mul__(self, other): @@ -2373,7 +2371,7 @@ class TestUfuncs(object): # also check that allclose uses ma ufuncs, to avoid warning allclose(m, 0.5) -class TestMaskedArrayInPlaceArithmetics(object): +class TestMaskedArrayInPlaceArithmetics: # Test MaskedArray Arithmetics def setup(self): @@ -2875,7 +2873,7 @@ class TestMaskedArrayInPlaceArithmetics(object): assert_equal(len(w), 0, "Failed on type=%s." % t) -class TestMaskedArrayMethods(object): +class TestMaskedArrayMethods: # Test class for miscellaneous MaskedArrays methods. def setup(self): # Base data definition. @@ -3582,7 +3580,7 @@ class TestMaskedArrayMethods(object): assert_equal(xd.data, x.diagonal().data) -class TestMaskedArrayMathMethods(object): +class TestMaskedArrayMathMethods: def setup(self): # Base data definition. @@ -3860,7 +3858,7 @@ class TestMaskedArrayMathMethods(object): assert_equal(a.max(1), [3, 6]) -class TestMaskedArrayMathMethodsComplex(object): +class TestMaskedArrayMathMethodsComplex: # Test class for miscellaneous MaskedArrays methods. def setup(self): # Base data definition. @@ -3913,7 +3911,7 @@ class TestMaskedArrayMathMethodsComplex(object): mX[:, k].compressed().std()) -class TestMaskedArrayFunctions(object): +class TestMaskedArrayFunctions: # Test class for miscellaneous functions. def setup(self): @@ -4552,7 +4550,7 @@ class TestMaskedArrayFunctions(object): assert_equal(test, masked_equal([-1, -1, -1, -1, -1], -1)) -class TestMaskedFields(object): +class TestMaskedFields: def setup(self): ilist = [1, 2, 3, 4, 5] @@ -4714,7 +4712,7 @@ class TestMaskedFields(object): assert_equal(len(rec), len(self.data['ddtype'])) -class TestMaskedObjectArray(object): +class TestMaskedObjectArray: def test_getitem(self): arr = np.ma.array([None, None]) @@ -4762,7 +4760,7 @@ class TestMaskedObjectArray(object): assert_(arr[0] is np.ma.masked) -class TestMaskedView(object): +class TestMaskedView: def setup(self): iterator = list(zip(np.arange(10), np.random.rand(10))) @@ -4840,7 +4838,7 @@ class TestMaskedView(object): assert_(not isinstance(test, MaskedArray)) -class TestOptionalArgs(object): +class TestOptionalArgs: def test_ndarrayfuncs(self): # test axis arg behaves the same as ndarray (including multiple axes) @@ -4927,7 +4925,7 @@ class TestOptionalArgs(object): assert_raises(np.AxisError, count, np.ma.array(1), axis=1) -class TestMaskedConstant(object): +class TestMaskedConstant: def _do_add_test(self, add): # sanity check assert_(add(np.ma.masked, 1) is np.ma.masked) @@ -5044,7 +5042,7 @@ class TestMaskedConstant(object): assert_raises(AttributeError, setattr, np.ma.masked, 'dtype', np.int64) -class TestMaskedWhereAliases(object): +class TestMaskedWhereAliases: # TODO: Test masked_object, masked_equal, ... diff --git a/numpy/ma/tests/test_deprecations.py b/numpy/ma/tests/test_deprecations.py index 72cc29aa0..14f697375 100644 --- a/numpy/ma/tests/test_deprecations.py +++ b/numpy/ma/tests/test_deprecations.py @@ -1,14 +1,12 @@ """Test deprecation and future warnings. """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_warns from numpy.ma.testutils import assert_equal from numpy.ma.core import MaskedArrayFutureWarning -class TestArgsort(object): +class TestArgsort: """ gh-8701 """ def _test_base(self, argsort, cls): arr_0d = np.array(1).view(cls) @@ -37,7 +35,7 @@ class TestArgsort(object): return self._test_base(np.ma.MaskedArray.argsort, np.ma.MaskedArray) -class TestMinimumMaximum(object): +class TestMinimumMaximum: def test_minimum(self): assert_warns(DeprecationWarning, np.ma.minimum, np.ma.array([1, 2])) diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index c75c47801..1c8610625 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -7,8 +7,6 @@ Adapted from the original test_ma by Pierre Gerard-Marchant :version: $Id: test_extras.py 3473 2007-10-29 15:18:13Z jarrod.millman $ """ -from __future__ import division, absolute_import, print_function - import warnings import itertools import pytest @@ -34,7 +32,7 @@ from numpy.ma.extras import ( ) -class TestGeneric(object): +class TestGeneric: # def test_masked_all(self): # Tests masked_all @@ -142,7 +140,7 @@ class TestGeneric(object): assert_equal(test, []) -class TestAverage(object): +class TestAverage: # Several tests of average. Why so many ? Good point... def test_testAverage1(self): # Test of average. @@ -273,7 +271,7 @@ class TestAverage(object): assert_almost_equal(wav1.imag, expected1.imag) -class TestConcatenator(object): +class TestConcatenator: # Tests for mr_, the equivalent of r_ for masked arrays. def test_1d(self): @@ -317,7 +315,7 @@ class TestConcatenator(object): assert_equal(actual.data[:2], [1, 2]) -class TestNotMasked(object): +class TestNotMasked: # Tests notmasked_edges and notmasked_contiguous. def test_edges(self): @@ -387,7 +385,7 @@ class TestNotMasked(object): ]) -class TestCompressFunctions(object): +class TestCompressFunctions: def test_compress_nd(self): # Tests compress_nd @@ -652,7 +650,7 @@ class TestCompressFunctions(object): assert_equal(a, res) -class TestApplyAlongAxis(object): +class TestApplyAlongAxis: # Tests 2D functions def test_3d(self): a = arange(12.).reshape(2, 2, 3) @@ -674,7 +672,7 @@ class TestApplyAlongAxis(object): assert_equal(xa, [[2, 5], [8, 11]]) -class TestApplyOverAxes(object): +class TestApplyOverAxes: # Tests apply_over_axes def test_basic(self): a = arange(24).reshape(2, 3, 4) @@ -687,7 +685,7 @@ class TestApplyOverAxes(object): assert_equal(test, ctrl) -class TestMedian(object): +class TestMedian: def test_pytype(self): r = np.ma.median([[np.inf, np.inf], [np.inf, np.inf]], axis=-1) assert_equal(r, np.inf) @@ -1066,7 +1064,7 @@ class TestMedian(object): assert_(type(np.ma.median(o.astype(object))), float) -class TestCov(object): +class TestCov: def setup(self): self.data = array(np.random.rand(12)) @@ -1133,7 +1131,7 @@ class TestCov(object): x.shape[0] / frac)) -class TestCorrcoef(object): +class TestCorrcoef: def setup(self): self.data = array(np.random.rand(12)) @@ -1240,7 +1238,7 @@ class TestCorrcoef(object): control[:-1, :-1]) -class TestPolynomial(object): +class TestPolynomial: # def test_polyfit(self): # Tests polyfit @@ -1298,7 +1296,7 @@ class TestPolynomial(object): assert_almost_equal(a, a_) -class TestArraySetOps(object): +class TestArraySetOps: def test_unique_onlist(self): # Test unique on list @@ -1530,7 +1528,7 @@ class TestArraySetOps(object): assert_array_equal(setdiff1d(a, b), np.array(['c'])) -class TestShapeBase(object): +class TestShapeBase: def test_atleast_2d(self): # Test atleast_2d @@ -1586,7 +1584,7 @@ class TestShapeBase(object): assert_equal(b.mask.shape, b.data.shape) -class TestStack(object): +class TestStack: def test_stack_1d(self): a = masked_array([0, 1, 2], mask=[0, 1, 0]) diff --git a/numpy/ma/tests/test_mrecords.py b/numpy/ma/tests/test_mrecords.py index 94e772d55..c2f859273 100644 --- a/numpy/ma/tests/test_mrecords.py +++ b/numpy/ma/tests/test_mrecords.py @@ -5,8 +5,6 @@ :contact: pierregm_at_uga_dot_edu """ -from __future__ import division, absolute_import, print_function - import numpy as np import numpy.ma as ma from numpy import recarray @@ -26,7 +24,7 @@ from numpy.ma.testutils import ( from numpy.compat import pickle -class TestMRecords(object): +class TestMRecords: ilist = [1, 2, 3, 4, 5] flist = [1.1, 2.2, 3.3, 4.4, 5.5] @@ -348,7 +346,7 @@ class TestMRecords(object): dtype=mult.dtype)) -class TestView(object): +class TestView: def setup(self): (a, b) = (np.arange(10), np.random.rand(10)) @@ -386,7 +384,7 @@ class TestView(object): ############################################################################## -class TestMRecordsImport(object): +class TestMRecordsImport: _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) diff --git a/numpy/ma/tests/test_old_ma.py b/numpy/ma/tests/test_old_ma.py index 7100eccbb..96c7e3609 100644 --- a/numpy/ma/tests/test_old_ma.py +++ b/numpy/ma/tests/test_old_ma.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - from functools import reduce import numpy as np @@ -33,7 +31,7 @@ def eq(v, w, msg=''): return result -class TestMa(object): +class TestMa: def setup(self): x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.]) @@ -700,7 +698,7 @@ class TestMa(object): assert_equal(b[1].shape, ()) -class TestUfuncs(object): +class TestUfuncs: def setup(self): self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6), array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),) @@ -765,7 +763,7 @@ class TestUfuncs(object): assert_(eq(nonzero(x), [0])) -class TestArrayMethods(object): +class TestArrayMethods: def setup(self): x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928, diff --git a/numpy/ma/tests/test_regression.py b/numpy/ma/tests/test_regression.py index b83873a5a..9f3368489 100644 --- a/numpy/ma/tests/test_regression.py +++ b/numpy/ma/tests/test_regression.py @@ -1,12 +1,10 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import ( assert_, assert_array_equal, assert_allclose, suppress_warnings ) -class TestRegression(object): +class TestRegression: def test_masked_array_create(self): # Ticket #17 x = np.ma.masked_array([0, 1, 2, 3, 0, 4, 5, 6], diff --git a/numpy/ma/tests/test_subclassing.py b/numpy/ma/tests/test_subclassing.py index 440b36722..02aeebd17 100644 --- a/numpy/ma/tests/test_subclassing.py +++ b/numpy/ma/tests/test_subclassing.py @@ -6,8 +6,6 @@ :version: $Id: test_subclassing.py 3473 2007-10-29 15:18:13Z jarrod.millman $ """ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_, assert_raises from numpy.ma.testutils import assert_equal @@ -80,7 +78,7 @@ msubarray = MSubArray # and overrides __array_wrap__, updating the info dict, to check that this # doesn't get destroyed by MaskedArray._update_from. But this one also needs # its own iterator... -class CSAIterator(object): +class CSAIterator: """ Flat iterator object that uses its own setter/getter (works around ndarray.flat not propagating subclass setters/getters @@ -154,7 +152,7 @@ class ComplicatedSubArray(SubArray): return obj -class TestSubclassing(object): +class TestSubclassing: # Test suite for masked subclasses of ndarray. def setup(self): diff --git a/numpy/ma/testutils.py b/numpy/ma/testutils.py index c0deaa9f4..51ab03948 100644 --- a/numpy/ma/testutils.py +++ b/numpy/ma/testutils.py @@ -5,8 +5,6 @@ :version: $Id: testutils.py 3529 2007-11-13 08:01:14Z jarrod.millman $ """ -from __future__ import division, absolute_import, print_function - import operator import numpy as np diff --git a/numpy/ma/timer_comparison.py b/numpy/ma/timer_comparison.py index 4ad635e38..83bd7852e 100644 --- a/numpy/ma/timer_comparison.py +++ b/numpy/ma/timer_comparison.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import timeit from functools import reduce @@ -15,7 +13,7 @@ np.seterr(all='ignore') pi = np.pi -class ModuleTester(object): +class ModuleTester: def __init__(self, module): self.module = module self.allequal = module.allequal diff --git a/numpy/matlib.py b/numpy/matlib.py index b1b155586..f3eb8eb4b 100644 --- a/numpy/matlib.py +++ b/numpy/matlib.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.matrixlib.defmatrix import matrix, asmatrix # need * as we're copying the numpy namespace (FIXME: this makes little sense) diff --git a/numpy/matrixlib/__init__.py b/numpy/matrixlib/__init__.py index 777e0cd33..54154d11f 100644 --- a/numpy/matrixlib/__init__.py +++ b/numpy/matrixlib/__init__.py @@ -1,8 +1,6 @@ """Sub-package containing the matrix class and related functions. """ -from __future__ import division, absolute_import, print_function - from .defmatrix import * __all__ = defmatrix.__all__ diff --git a/numpy/matrixlib/defmatrix.py b/numpy/matrixlib/defmatrix.py index cabd41367..12ac74cb2 100644 --- a/numpy/matrixlib/defmatrix.py +++ b/numpy/matrixlib/defmatrix.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __all__ = ['matrix', 'bmat', 'mat', 'asmatrix'] import sys diff --git a/numpy/matrixlib/setup.py b/numpy/matrixlib/setup.py index d0981d658..c4eee4be4 100644 --- a/numpy/matrixlib/setup.py +++ b/numpy/matrixlib/setup.py @@ -1,6 +1,4 @@ #!/usr/bin/env python -from __future__ import division, print_function - def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('matrixlib', parent_package, top_path) diff --git a/numpy/matrixlib/tests/test_defmatrix.py b/numpy/matrixlib/tests/test_defmatrix.py index aa6e08d64..a8070898f 100644 --- a/numpy/matrixlib/tests/test_defmatrix.py +++ b/numpy/matrixlib/tests/test_defmatrix.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - try: # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 @@ -16,7 +14,7 @@ from numpy.testing import ( from numpy.linalg import matrix_power from numpy.matrixlib import mat -class TestCtor(object): +class TestCtor: def test_basic(self): A = np.array([[1, 2], [3, 4]]) mA = matrix(A) @@ -63,7 +61,7 @@ class TestCtor(object): assert_(np.all(b2 == mixresult)) -class TestProperties(object): +class TestProperties: def test_sum(self): """Test whether matrix.sum(axis=1) preserves orientation. Fails in NumPy <= 0.9.6.2127. @@ -196,7 +194,7 @@ class TestProperties(object): B = matrix([[True], [True], [False]]) assert_array_equal(A, B) -class TestCasting(object): +class TestCasting: def test_basic(self): A = np.arange(100).reshape(10, 10) mA = matrix(A) @@ -215,7 +213,7 @@ class TestCasting(object): assert_(np.all(mA != mB)) -class TestAlgebra(object): +class TestAlgebra: def test_basic(self): import numpy.linalg as linalg @@ -274,7 +272,7 @@ class TestAlgebra(object): A*object() -class TestMatrixReturn(object): +class TestMatrixReturn: def test_instance_methods(self): a = matrix([1.0], dtype='f8') methodargs = { @@ -316,7 +314,7 @@ class TestMatrixReturn(object): assert_(type(d) is np.ndarray) -class TestIndexing(object): +class TestIndexing: def test_basic(self): x = asmatrix(np.zeros((3, 2), float)) y = np.zeros((3, 1), float) @@ -325,7 +323,7 @@ class TestIndexing(object): assert_equal(x, [[0, 1], [0, 0], [0, 0]]) -class TestNewScalarIndexing(object): +class TestNewScalarIndexing: a = matrix([[1, 2], [3, 4]]) def test_dimesions(self): @@ -392,7 +390,7 @@ class TestNewScalarIndexing(object): assert_array_equal(x[[2, 1, 0],:], x[::-1,:]) -class TestPower(object): +class TestPower: def test_returntype(self): a = np.array([[0, 1], [0, 0]]) assert_(type(matrix_power(a, 2)) is np.ndarray) @@ -403,7 +401,7 @@ class TestPower(object): assert_array_equal(matrix_power([[0, 1], [0, 0]], 2), [[0, 0], [0, 0]]) -class TestShape(object): +class TestShape: a = np.array([[1], [2]]) m = matrix([[1], [2]]) diff --git a/numpy/matrixlib/tests/test_interaction.py b/numpy/matrixlib/tests/test_interaction.py index 088ae3c6a..608416ed7 100644 --- a/numpy/matrixlib/tests/test_interaction.py +++ b/numpy/matrixlib/tests/test_interaction.py @@ -2,8 +2,6 @@ Note that tests with MaskedArray and linalg are done in separate files. """ -from __future__ import division, absolute_import, print_function - import pytest import textwrap @@ -290,7 +288,7 @@ def test_kron_matrix(): assert_equal(type(np.kron(m, a)), np.matrix) -class TestConcatenatorMatrix(object): +class TestConcatenatorMatrix: # 2018-04-29: moved here from core.tests.test_index_tricks. def test_matrix(self): a = [1, 2] diff --git a/numpy/matrixlib/tests/test_masked_matrix.py b/numpy/matrixlib/tests/test_masked_matrix.py index d3911d2e1..45424ecf0 100644 --- a/numpy/matrixlib/tests/test_masked_matrix.py +++ b/numpy/matrixlib/tests/test_masked_matrix.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.ma.testutils import (assert_, assert_equal, assert_raises, assert_array_equal) @@ -29,7 +27,7 @@ class MMatrix(MaskedArray, np.matrix,): return _view -class TestMaskedMatrix(object): +class TestMaskedMatrix: def test_matrix_indexing(self): # Tests conversions and indexing x1 = np.matrix([[1, 2, 3], [4, 3, 2]]) @@ -171,7 +169,7 @@ class TestMaskedMatrix(object): assert_(not isinstance(test, MaskedArray)) -class TestSubclassing(object): +class TestSubclassing: # Test suite for masked subclasses of ndarray. def setup(self): @@ -212,7 +210,7 @@ class TestSubclassing(object): assert_(isinstance(divide(mx, x), MMatrix)) assert_equal(divide(mx, mx), divide(xmx, xmx)) -class TestConcatenator(object): +class TestConcatenator: # Tests for mr_, the equivalent of r_ for masked arrays. def test_matrix_builder(self): diff --git a/numpy/matrixlib/tests/test_matrix_linalg.py b/numpy/matrixlib/tests/test_matrix_linalg.py index 6fc733c2e..106c2e382 100644 --- a/numpy/matrixlib/tests/test_matrix_linalg.py +++ b/numpy/matrixlib/tests/test_matrix_linalg.py @@ -1,6 +1,4 @@ """ Test functions for linalg module using the matrix class.""" -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.linalg.tests.test_linalg import ( diff --git a/numpy/matrixlib/tests/test_multiarray.py b/numpy/matrixlib/tests/test_multiarray.py index 6d84bd477..638d0d153 100644 --- a/numpy/matrixlib/tests/test_multiarray.py +++ b/numpy/matrixlib/tests/test_multiarray.py @@ -1,9 +1,7 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_, assert_equal, assert_array_equal -class TestView(object): +class TestView: def test_type(self): x = np.array([1, 2, 3]) assert_(isinstance(x.view(np.matrix), np.matrix)) diff --git a/numpy/matrixlib/tests/test_numeric.py b/numpy/matrixlib/tests/test_numeric.py index 95e1c8001..a772bb388 100644 --- a/numpy/matrixlib/tests/test_numeric.py +++ b/numpy/matrixlib/tests/test_numeric.py @@ -1,9 +1,7 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_equal -class TestDot(object): +class TestDot: def test_matscalar(self): b1 = np.matrix(np.ones((3, 3), dtype=complex)) assert_equal(b1*1.0, b1) diff --git a/numpy/matrixlib/tests/test_regression.py b/numpy/matrixlib/tests/test_regression.py index 70e147279..a54d44020 100644 --- a/numpy/matrixlib/tests/test_regression.py +++ b/numpy/matrixlib/tests/test_regression.py @@ -1,10 +1,8 @@ -from __future__ import division, absolute_import, print_function - import numpy as np from numpy.testing import assert_, assert_equal, assert_raises -class TestRegression(object): +class TestRegression: def test_kron_matrix(self): # Ticket #71 x = np.matrix('[1 0; 1 0]') diff --git a/numpy/polynomial/__init__.py b/numpy/polynomial/__init__.py index 85cee9ce6..4ff2df57e 100644 --- a/numpy/polynomial/__init__.py +++ b/numpy/polynomial/__init__.py @@ -13,8 +13,6 @@ implemented as operations on the coefficients. Additional (module-specific) information can be found in the docstring for the module of interest. """ -from __future__ import division, absolute_import, print_function - from .polynomial import Polynomial from .chebyshev import Chebyshev from .legendre import Legendre diff --git a/numpy/polynomial/_polybase.py b/numpy/polynomial/_polybase.py index bfa030714..28bd50ec6 100644 --- a/numpy/polynomial/_polybase.py +++ b/numpy/polynomial/_polybase.py @@ -6,8 +6,6 @@ for the various polynomial classes. It operates as a mixin, but uses the abc module from the stdlib, hence it is only available for Python >= 2.6. """ -from __future__ import division, absolute_import, print_function - import abc import numbers diff --git a/numpy/polynomial/chebyshev.py b/numpy/polynomial/chebyshev.py index 0cd9c4d23..0bb297807 100644 --- a/numpy/polynomial/chebyshev.py +++ b/numpy/polynomial/chebyshev.py @@ -87,8 +87,6 @@ References (preprint: https://www.math.hmc.edu/~benjamin/papers/CombTrig.pdf, pg. 4) """ -from __future__ import division, absolute_import, print_function - import warnings import numpy as np import numpy.linalg as la diff --git a/numpy/polynomial/hermite.py b/numpy/polynomial/hermite.py index 9b1aea239..cb98b7e1f 100644 --- a/numpy/polynomial/hermite.py +++ b/numpy/polynomial/hermite.py @@ -58,8 +58,6 @@ See also `numpy.polynomial` """ -from __future__ import division, absolute_import, print_function - import warnings import numpy as np import numpy.linalg as la diff --git a/numpy/polynomial/hermite_e.py b/numpy/polynomial/hermite_e.py index c5a0a05a2..1f4a93c24 100644 --- a/numpy/polynomial/hermite_e.py +++ b/numpy/polynomial/hermite_e.py @@ -58,8 +58,6 @@ See also `numpy.polynomial` """ -from __future__ import division, absolute_import, print_function - import warnings import numpy as np import numpy.linalg as la diff --git a/numpy/polynomial/laguerre.py b/numpy/polynomial/laguerre.py index 538a1d449..bf8e11623 100644 --- a/numpy/polynomial/laguerre.py +++ b/numpy/polynomial/laguerre.py @@ -58,8 +58,6 @@ See also `numpy.polynomial` """ -from __future__ import division, absolute_import, print_function - import warnings import numpy as np import numpy.linalg as la diff --git a/numpy/polynomial/legendre.py b/numpy/polynomial/legendre.py index c11824761..d74b87d5a 100644 --- a/numpy/polynomial/legendre.py +++ b/numpy/polynomial/legendre.py @@ -81,8 +81,6 @@ numpy.polynomial.hermite numpy.polynomial.hermite_e """ -from __future__ import division, absolute_import, print_function - import warnings import numpy as np import numpy.linalg as la diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py index 315ea1495..92fdc06fa 100644 --- a/numpy/polynomial/polynomial.py +++ b/numpy/polynomial/polynomial.py @@ -55,8 +55,6 @@ See Also `numpy.polynomial` """ -from __future__ import division, absolute_import, print_function - __all__ = [ 'polyzero', 'polyone', 'polyx', 'polydomain', 'polyline', 'polyadd', 'polysub', 'polymulx', 'polymul', 'polydiv', 'polypow', 'polyval', diff --git a/numpy/polynomial/polyutils.py b/numpy/polynomial/polyutils.py index 5dcfa7a7a..b65e88a83 100644 --- a/numpy/polynomial/polyutils.py +++ b/numpy/polynomial/polyutils.py @@ -43,8 +43,6 @@ Functions mapparms parameters of the linear map between domains. """ -from __future__ import division, absolute_import, print_function - import operator import functools import warnings @@ -80,7 +78,7 @@ class PolyDomainError(PolyError): # Base class for all polynomial types # -class PolyBase(object): +class PolyBase: """ Base class for all polynomial types. diff --git a/numpy/polynomial/setup.py b/numpy/polynomial/setup.py index cb59ee1e5..8fc82cba1 100644 --- a/numpy/polynomial/setup.py +++ b/numpy/polynomial/setup.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('polynomial', parent_package, top_path) diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py index c8d2d6dba..ec0a7839a 100644 --- a/numpy/polynomial/tests/test_chebyshev.py +++ b/numpy/polynomial/tests/test_chebyshev.py @@ -1,8 +1,6 @@ """Tests for chebyshev module. """ -from __future__ import division, absolute_import, print_function - from functools import reduce import numpy as np @@ -30,7 +28,7 @@ T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256] Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9] -class TestPrivate(object): +class TestPrivate: def test__cseries_to_zseries(self): for i in range(5): @@ -47,7 +45,7 @@ class TestPrivate(object): assert_equal(res, tgt) -class TestConstants(object): +class TestConstants: def test_chebdomain(self): assert_equal(cheb.chebdomain, [-1, 1]) @@ -62,7 +60,7 @@ class TestConstants(object): assert_equal(cheb.chebx, [0, 1]) -class TestArithmetic(object): +class TestArithmetic: def test_chebadd(self): for i in range(5): @@ -123,7 +121,7 @@ class TestArithmetic(object): assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(object): +class TestEvaluation: # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([2.5, 2., 1.5]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -217,7 +215,7 @@ class TestEvaluation(object): assert_(res.shape == (2, 3)*3) -class TestIntegral(object): +class TestIntegral: def test_chebint(self): # check exceptions @@ -319,7 +317,7 @@ class TestIntegral(object): assert_almost_equal(res, tgt) -class TestDerivative(object): +class TestDerivative: def test_chebder(self): # check exceptions @@ -359,7 +357,7 @@ class TestDerivative(object): assert_almost_equal(res, tgt) -class TestVander(object): +class TestVander: # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 @@ -407,7 +405,7 @@ class TestVander(object): assert_(van.shape == (1, 5, 24)) -class TestFitting(object): +class TestFitting: def test_chebfit(self): def f(x): @@ -484,7 +482,7 @@ class TestFitting(object): assert_almost_equal(coef1, coef2) -class TestInterpolate(object): +class TestInterpolate: def f(self, x): return x * (x - 1) * (x - 2) @@ -509,7 +507,7 @@ class TestInterpolate(object): assert_almost_equal(cheb.chebval(x, c), powx(x, p), decimal=12) -class TestCompanion(object): +class TestCompanion: def test_raises(self): assert_raises(ValueError, cheb.chebcompanion, []) @@ -524,7 +522,7 @@ class TestCompanion(object): assert_(cheb.chebcompanion([1, 2])[0, 0] == -.5) -class TestGauss(object): +class TestGauss: def test_100(self): x, w = cheb.chebgauss(100) @@ -543,7 +541,7 @@ class TestGauss(object): assert_almost_equal(w.sum(), tgt) -class TestMisc(object): +class TestMisc: def test_chebfromroots(self): res = cheb.chebfromroots([]) diff --git a/numpy/polynomial/tests/test_classes.py b/numpy/polynomial/tests/test_classes.py index 2261f960b..68656bc98 100644 --- a/numpy/polynomial/tests/test_classes.py +++ b/numpy/polynomial/tests/test_classes.py @@ -3,8 +3,6 @@ This tests the convert and cast methods of all the polynomial classes. """ -from __future__ import division, absolute_import, print_function - import operator as op from numbers import Number @@ -574,7 +572,7 @@ def test_ufunc_override(Poly): -class TestLatexRepr(object): +class TestLatexRepr: """Test the latex repr used by ipython """ def as_latex(self, obj): @@ -628,7 +626,7 @@ class TestLatexRepr(object): # -class TestInterpolate(object): +class TestInterpolate: def f(self, x): return x * (x - 1) * (x - 2) diff --git a/numpy/polynomial/tests/test_hermite.py b/numpy/polynomial/tests/test_hermite.py index 271c1964b..4b67c1b18 100644 --- a/numpy/polynomial/tests/test_hermite.py +++ b/numpy/polynomial/tests/test_hermite.py @@ -1,8 +1,6 @@ """Tests for hermite module. """ -from __future__ import division, absolute_import, print_function - from functools import reduce import numpy as np @@ -30,7 +28,7 @@ def trim(x): return herm.hermtrim(x, tol=1e-6) -class TestConstants(object): +class TestConstants: def test_hermdomain(self): assert_equal(herm.hermdomain, [-1, 1]) @@ -45,7 +43,7 @@ class TestConstants(object): assert_equal(herm.hermx, [0, .5]) -class TestArithmetic(object): +class TestArithmetic: x = np.linspace(-3, 3, 100) def test_hermadd(self): @@ -111,7 +109,7 @@ class TestArithmetic(object): assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(object): +class TestEvaluation: # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([2.5, 1., .75]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -205,7 +203,7 @@ class TestEvaluation(object): assert_(res.shape == (2, 3)*3) -class TestIntegral(object): +class TestIntegral: def test_hermint(self): # check exceptions @@ -307,7 +305,7 @@ class TestIntegral(object): assert_almost_equal(res, tgt) -class TestDerivative(object): +class TestDerivative: def test_hermder(self): # check exceptions @@ -347,7 +345,7 @@ class TestDerivative(object): assert_almost_equal(res, tgt) -class TestVander(object): +class TestVander: # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 @@ -395,7 +393,7 @@ class TestVander(object): assert_(van.shape == (1, 5, 24)) -class TestFitting(object): +class TestFitting: def test_hermfit(self): def f(x): @@ -472,7 +470,7 @@ class TestFitting(object): assert_almost_equal(coef1, coef2) -class TestCompanion(object): +class TestCompanion: def test_raises(self): assert_raises(ValueError, herm.hermcompanion, []) @@ -487,7 +485,7 @@ class TestCompanion(object): assert_(herm.hermcompanion([1, 2])[0, 0] == -.25) -class TestGauss(object): +class TestGauss: def test_100(self): x, w = herm.hermgauss(100) @@ -506,7 +504,7 @@ class TestGauss(object): assert_almost_equal(w.sum(), tgt) -class TestMisc(object): +class TestMisc: def test_hermfromroots(self): res = herm.hermfromroots([]) diff --git a/numpy/polynomial/tests/test_hermite_e.py b/numpy/polynomial/tests/test_hermite_e.py index 434b30e7b..3052500cc 100644 --- a/numpy/polynomial/tests/test_hermite_e.py +++ b/numpy/polynomial/tests/test_hermite_e.py @@ -1,8 +1,6 @@ """Tests for hermite_e module. """ -from __future__ import division, absolute_import, print_function - from functools import reduce import numpy as np @@ -30,7 +28,7 @@ def trim(x): return herme.hermetrim(x, tol=1e-6) -class TestConstants(object): +class TestConstants: def test_hermedomain(self): assert_equal(herme.hermedomain, [-1, 1]) @@ -45,7 +43,7 @@ class TestConstants(object): assert_equal(herme.hermex, [0, 1]) -class TestArithmetic(object): +class TestArithmetic: x = np.linspace(-3, 3, 100) def test_hermeadd(self): @@ -111,7 +109,7 @@ class TestArithmetic(object): assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(object): +class TestEvaluation: # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([4., 2., 3.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -205,7 +203,7 @@ class TestEvaluation(object): assert_(res.shape == (2, 3)*3) -class TestIntegral(object): +class TestIntegral: def test_hermeint(self): # check exceptions @@ -307,7 +305,7 @@ class TestIntegral(object): assert_almost_equal(res, tgt) -class TestDerivative(object): +class TestDerivative: def test_hermeder(self): # check exceptions @@ -348,7 +346,7 @@ class TestDerivative(object): assert_almost_equal(res, tgt) -class TestVander(object): +class TestVander: # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 @@ -396,7 +394,7 @@ class TestVander(object): assert_(van.shape == (1, 5, 24)) -class TestFitting(object): +class TestFitting: def test_hermefit(self): def f(x): @@ -473,7 +471,7 @@ class TestFitting(object): assert_almost_equal(coef1, coef2) -class TestCompanion(object): +class TestCompanion: def test_raises(self): assert_raises(ValueError, herme.hermecompanion, []) @@ -488,7 +486,7 @@ class TestCompanion(object): assert_(herme.hermecompanion([1, 2])[0, 0] == -.5) -class TestGauss(object): +class TestGauss: def test_100(self): x, w = herme.hermegauss(100) @@ -507,7 +505,7 @@ class TestGauss(object): assert_almost_equal(w.sum(), tgt) -class TestMisc(object): +class TestMisc: def test_hermefromroots(self): res = herme.hermefromroots([]) diff --git a/numpy/polynomial/tests/test_laguerre.py b/numpy/polynomial/tests/test_laguerre.py index 4b9b28637..ec103c258 100644 --- a/numpy/polynomial/tests/test_laguerre.py +++ b/numpy/polynomial/tests/test_laguerre.py @@ -1,8 +1,6 @@ """Tests for laguerre module. """ -from __future__ import division, absolute_import, print_function - from functools import reduce import numpy as np @@ -27,7 +25,7 @@ def trim(x): return lag.lagtrim(x, tol=1e-6) -class TestConstants(object): +class TestConstants: def test_lagdomain(self): assert_equal(lag.lagdomain, [0, 1]) @@ -42,7 +40,7 @@ class TestConstants(object): assert_equal(lag.lagx, [1, -1]) -class TestArithmetic(object): +class TestArithmetic: x = np.linspace(-3, 3, 100) def test_lagadd(self): @@ -108,7 +106,7 @@ class TestArithmetic(object): assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(object): +class TestEvaluation: # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([9., -14., 6.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -202,7 +200,7 @@ class TestEvaluation(object): assert_(res.shape == (2, 3)*3) -class TestIntegral(object): +class TestIntegral: def test_lagint(self): # check exceptions @@ -304,7 +302,7 @@ class TestIntegral(object): assert_almost_equal(res, tgt) -class TestDerivative(object): +class TestDerivative: def test_lagder(self): # check exceptions @@ -344,7 +342,7 @@ class TestDerivative(object): assert_almost_equal(res, tgt) -class TestVander(object): +class TestVander: # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 @@ -392,7 +390,7 @@ class TestVander(object): assert_(van.shape == (1, 5, 24)) -class TestFitting(object): +class TestFitting: def test_lagfit(self): def f(x): @@ -454,7 +452,7 @@ class TestFitting(object): assert_almost_equal(lag.lagfit(x, x, [0, 1]), [1, -1]) -class TestCompanion(object): +class TestCompanion: def test_raises(self): assert_raises(ValueError, lag.lagcompanion, []) @@ -469,7 +467,7 @@ class TestCompanion(object): assert_(lag.lagcompanion([1, 2])[0, 0] == 1.5) -class TestGauss(object): +class TestGauss: def test_100(self): x, w = lag.laggauss(100) @@ -488,7 +486,7 @@ class TestGauss(object): assert_almost_equal(w.sum(), tgt) -class TestMisc(object): +class TestMisc: def test_lagfromroots(self): res = lag.lagfromroots([]) diff --git a/numpy/polynomial/tests/test_legendre.py b/numpy/polynomial/tests/test_legendre.py index 917a7e03a..8846ca6f2 100644 --- a/numpy/polynomial/tests/test_legendre.py +++ b/numpy/polynomial/tests/test_legendre.py @@ -1,8 +1,6 @@ """Tests for legendre module. """ -from __future__ import division, absolute_import, print_function - from functools import reduce import numpy as np @@ -30,7 +28,7 @@ def trim(x): return leg.legtrim(x, tol=1e-6) -class TestConstants(object): +class TestConstants: def test_legdomain(self): assert_equal(leg.legdomain, [-1, 1]) @@ -45,7 +43,7 @@ class TestConstants(object): assert_equal(leg.legx, [0, 1]) -class TestArithmetic(object): +class TestArithmetic: x = np.linspace(-1, 1, 100) def test_legadd(self): @@ -112,7 +110,7 @@ class TestArithmetic(object): assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(object): +class TestEvaluation: # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([2., 2., 2.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -206,7 +204,7 @@ class TestEvaluation(object): assert_(res.shape == (2, 3)*3) -class TestIntegral(object): +class TestIntegral: def test_legint(self): # check exceptions @@ -308,7 +306,7 @@ class TestIntegral(object): assert_almost_equal(res, tgt) -class TestDerivative(object): +class TestDerivative: def test_legder(self): # check exceptions @@ -348,7 +346,7 @@ class TestDerivative(object): assert_almost_equal(res, tgt) -class TestVander(object): +class TestVander: # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 @@ -396,7 +394,7 @@ class TestVander(object): assert_(van.shape == (1, 5, 24)) -class TestFitting(object): +class TestFitting: def test_legfit(self): def f(x): @@ -473,7 +471,7 @@ class TestFitting(object): assert_almost_equal(coef1, coef2) -class TestCompanion(object): +class TestCompanion: def test_raises(self): assert_raises(ValueError, leg.legcompanion, []) @@ -488,7 +486,7 @@ class TestCompanion(object): assert_(leg.legcompanion([1, 2])[0, 0] == -.5) -class TestGauss(object): +class TestGauss: def test_100(self): x, w = leg.leggauss(100) @@ -507,7 +505,7 @@ class TestGauss(object): assert_almost_equal(w.sum(), tgt) -class TestMisc(object): +class TestMisc: def test_legfromroots(self): res = leg.legfromroots([]) diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py index 1436963c6..c90075dfe 100644 --- a/numpy/polynomial/tests/test_polynomial.py +++ b/numpy/polynomial/tests/test_polynomial.py @@ -1,8 +1,6 @@ """Tests for polynomial module. """ -from __future__ import division, absolute_import, print_function - from functools import reduce import numpy as np @@ -29,7 +27,7 @@ T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256] Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9] -class TestConstants(object): +class TestConstants: def test_polydomain(self): assert_equal(poly.polydomain, [-1, 1]) @@ -44,7 +42,7 @@ class TestConstants(object): assert_equal(poly.polyx, [0, 1]) -class TestArithmetic(object): +class TestArithmetic: def test_polyadd(self): for i in range(5): @@ -114,7 +112,7 @@ class TestArithmetic(object): assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(object): +class TestEvaluation: # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([1., 2., 3.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -287,7 +285,7 @@ class TestEvaluation(object): assert_(res.shape == (2, 3)*3) -class TestIntegral(object): +class TestIntegral: def test_polyint(self): # check exceptions @@ -386,7 +384,7 @@ class TestIntegral(object): assert_almost_equal(res, tgt) -class TestDerivative(object): +class TestDerivative: def test_polyder(self): # check exceptions @@ -426,7 +424,7 @@ class TestDerivative(object): assert_almost_equal(res, tgt) -class TestVander(object): +class TestVander: # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 @@ -474,7 +472,7 @@ class TestVander(object): assert_(van.shape == (1, 5, 24)) -class TestCompanion(object): +class TestCompanion: def test_raises(self): assert_raises(ValueError, poly.polycompanion, []) @@ -489,7 +487,7 @@ class TestCompanion(object): assert_(poly.polycompanion([1, 2])[0, 0] == -.5) -class TestMisc(object): +class TestMisc: def test_polyfromroots(self): res = poly.polyfromroots([]) diff --git a/numpy/polynomial/tests/test_polyutils.py b/numpy/polynomial/tests/test_polyutils.py index 801c558cc..1b27f53b5 100644 --- a/numpy/polynomial/tests/test_polyutils.py +++ b/numpy/polynomial/tests/test_polyutils.py @@ -1,8 +1,6 @@ """Tests for polyutils module. """ -from __future__ import division, absolute_import, print_function - import numpy as np import numpy.polynomial.polyutils as pu from numpy.testing import ( @@ -10,7 +8,7 @@ from numpy.testing import ( ) -class TestMisc(object): +class TestMisc: def test_trimseq(self): for i in range(5): @@ -43,7 +41,7 @@ class TestMisc(object): assert_equal(pu.trimcoef(coef, 2), [0]) -class TestDomain(object): +class TestDomain: def test_getdomain(self): # test for real values diff --git a/numpy/polynomial/tests/test_printing.py b/numpy/polynomial/tests/test_printing.py index 3f1236402..049d3af2f 100644 --- a/numpy/polynomial/tests/test_printing.py +++ b/numpy/polynomial/tests/test_printing.py @@ -1,10 +1,8 @@ -from __future__ import division, absolute_import, print_function - import numpy.polynomial as poly from numpy.testing import assert_equal -class TestStr(object): +class TestStr: def test_polynomial_str(self): res = str(poly.Polynomial([0, 1])) tgt = 'poly([0. 1.])' @@ -36,7 +34,7 @@ class TestStr(object): assert_equal(res, tgt) -class TestRepr(object): +class TestRepr: def test_polynomial_str(self): res = repr(poly.Polynomial([0, 1])) tgt = 'Polynomial([0., 1.], domain=[-1, 1], window=[-1, 1])' diff --git a/numpy/random/__init__.py b/numpy/random/__init__.py index 1ceb5c4dd..0b80999d8 100644 --- a/numpy/random/__init__.py +++ b/numpy/random/__init__.py @@ -122,8 +122,6 @@ set_state Set state of generator. """ -from __future__ import division, absolute_import, print_function - __all__ = [ 'beta', 'binomial', diff --git a/numpy/random/_bounded_integers.pyx.in b/numpy/random/_bounded_integers.pyx.in index 7e19471e4..9e639b53b 100644 --- a/numpy/random/_bounded_integers.pyx.in +++ b/numpy/random/_bounded_integers.pyx.in @@ -314,8 +314,7 @@ cdef object _rand_{{nptype}}(object low, object high, object size, high_arr = <np.ndarray>np.array(high, copy=False) low_ndim = np.PyArray_NDIM(low_arr) high_ndim = np.PyArray_NDIM(high_arr) - if ((low_ndim == 0 or (low_ndim == 1 and low_arr.size == 1 and size is not None)) and - (high_ndim == 0 or (high_ndim == 1 and high_arr.size == 1 and size is not None))): + if low_ndim == 0 and high_ndim == 0: low = int(low_arr) high = int(high_arr) # Subtract 1 since internal generator produces on closed interval [low, high] diff --git a/numpy/random/_examples/cython/setup.py b/numpy/random/_examples/cython/setup.py index 19f045fc0..20cedc4e3 100644 --- a/numpy/random/_examples/cython/setup.py +++ b/numpy/random/_examples/cython/setup.py @@ -9,9 +9,10 @@ import numpy as np from distutils.core import setup from Cython.Build import cythonize from setuptools.extension import Extension -from os.path import join, abspath, dirname +from os.path import join, dirname -path = abspath(dirname(__file__)) +path = dirname(__file__) +defs = [('NPY_NO_DEPRECATED_API', 0)] extending = Extension("extending", sources=[join(path, 'extending.pyx')], @@ -19,10 +20,13 @@ extending = Extension("extending", np.get_include(), join(path, '..', '..') ], + define_macros=defs, ) distributions = Extension("extending_distributions", sources=[join(path, 'extending_distributions.pyx')], - include_dirs=[np.get_include()]) + include_dirs=[np.get_include()], + define_macros=defs, + ) extensions = [extending, distributions] diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index d76cde44c..0ba403138 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -224,7 +224,7 @@ cdef class Generator: capsule = bit_generator.capsule cdef const char *name = "BitGenerator" if not PyCapsule_IsValid(capsule, name): - raise ValueError("Invalid bit generator'. The bit generator must " + raise ValueError("Invalid bit generator. The bit generator must " "be instantiated.") self._bitgen = (<bitgen_t *> PyCapsule_GetPointer(capsule, name))[0] self.lock = bit_generator.lock @@ -629,32 +629,32 @@ cdef class Generator: @cython.wraparound(True) def choice(self, a, size=None, replace=True, p=None, axis=0, bint shuffle=True): """ - choice(a, size=None, replace=True, p=None, axis=0): + choice(a, size=None, replace=True, p=None, axis=0, shuffle=True): Generates a random sample from a given 1-D array Parameters ---------- - a : 1-D array-like or int + a : {array_like, int} If an ndarray, a random sample is generated from its elements. - If an int, the random sample is generated as if a were np.arange(a) - size : int or tuple of ints, optional + If an int, the random sample is generated from np.arange(a). + size : {int, tuple[int]}, optional Output shape. If the given shape is, e.g., ``(m, n, k)``, then ``m * n * k`` samples are drawn from the 1-d `a`. If `a` has more than one dimension, the `size` shape will be inserted into the `axis` dimension, so the output ``ndim`` will be ``a.ndim - 1 + len(size)``. Default is None, in which case a single value is returned. - replace : boolean, optional + replace : bool, optional Whether the sample is with or without replacement - p : 1-D array-like, optional + p : 1-D array_like, optional The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a. axis : int, optional The axis along which the selection is performed. The default, 0, selects by row. - shuffle : boolean, optional + shuffle : bool, optional Whether the sample is shuffled when sampling without replacement. Default is True, False provides a speedup. @@ -725,13 +725,15 @@ cdef class Generator: # __index__ must return an integer by python rules. pop_size = operator.index(a.item()) except TypeError: - raise ValueError("a must be 1-dimensional or an integer") + raise ValueError("a must an array or an integer") if pop_size <= 0 and np.prod(size) != 0: - raise ValueError("a must be greater than 0 unless no samples are taken") + raise ValueError("a must be a positive integer unless no" + "samples are taken") else: pop_size = a.shape[axis] if pop_size == 0 and np.prod(size) != 0: - raise ValueError("'a' cannot be empty unless no samples are taken") + raise ValueError("a cannot be empty unless no samples are" + "taken") if p is not None: d = len(p) @@ -746,9 +748,9 @@ cdef class Generator: pix = <double*>np.PyArray_DATA(p) if p.ndim != 1: - raise ValueError("'p' must be 1-dimensional") + raise ValueError("p must be 1-dimensional") if p.size != pop_size: - raise ValueError("'a' and 'p' must have same size") + raise ValueError("a and p must have same size") p_sum = kahan_sum(pix, d) if np.isnan(p_sum): raise ValueError("probabilities contain NaN") @@ -770,13 +772,14 @@ cdef class Generator: cdf /= cdf[-1] uniform_samples = self.random(shape) idx = cdf.searchsorted(uniform_samples, side='right') - idx = np.array(idx, copy=False, dtype=np.int64) # searchsorted returns a scalar + # searchsorted returns a scalar + idx = np.array(idx, copy=False, dtype=np.int64) else: idx = self.integers(0, pop_size, size=shape, dtype=np.int64) else: if size > pop_size: raise ValueError("Cannot take a larger sample than " - "population when 'replace=False'") + "population when replace is False") elif size < 0: raise ValueError("negative dimensions are not allowed") diff --git a/numpy/random/setup.py b/numpy/random/setup.py index 1b093d6d3..42c00ee5e 100644 --- a/numpy/random/setup.py +++ b/numpy/random/setup.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - import os import platform import sys diff --git a/numpy/random/src/philox/philox-benchmark.c b/numpy/random/src/philox/philox-benchmark.c index df5814d5f..9856a9b8e 100644 --- a/numpy/random/src/philox/philox-benchmark.c +++ b/numpy/random/src/philox/philox-benchmark.c @@ -1,5 +1,5 @@ /* - * Simple benchamrk command + * Simple benchmark command * * cl philox-benchmark.c /Ox * diff --git a/numpy/random/tests/test_direct.py b/numpy/random/tests/test_direct.py index 9f77f0ad2..4fa69a402 100644 --- a/numpy/random/tests/test_direct.py +++ b/numpy/random/tests/test_direct.py @@ -145,7 +145,7 @@ def test_seedsequence(): assert len(dummy.spawn(10)) == 10 -class Base(object): +class Base: dtype = np.uint64 data2 = data1 = {} @@ -410,7 +410,7 @@ class TestSFC64(Base): cls.invalid_init_values = [(-1,)] -class TestDefaultRNG(object): +class TestDefaultRNG: def test_seed(self): for args in [(), (None,), (1234,), ([1234, 5678],)]: rg = default_rng(*args) diff --git a/numpy/random/tests/test_extending.py b/numpy/random/tests/test_extending.py index 807de1a25..23a874fb1 100644 --- a/numpy/random/tests/test_extending.py +++ b/numpy/random/tests/test_extending.py @@ -1,6 +1,8 @@ import os, sys import pytest import warnings +import shutil +import subprocess try: import cffi @@ -22,30 +24,33 @@ except ImportError: try: import cython + from Cython.Compiler.Version import version as cython_version except ImportError: cython = None +else: + from distutils.version import LooseVersion + # Cython 0.29.14 is required for Python 3.8 and there are + # other fixes in the 0.29 series that are needed even for earlier + # Python versions. + # Note: keep in sync with the one in pyproject.toml + required_version = LooseVersion('0.29.14') + if LooseVersion(cython_version) < required_version: + # too old or wrong cython, skip the test + cython = None @pytest.mark.skipif(cython is None, reason="requires cython") -def test_cython(): - curdir = os.getcwd() - argv = sys.argv - examples = (os.path.dirname(__file__), '..', '_examples') - try: - os.chdir(os.path.join(*examples)) - sys.argv = argv[:1] + ['build'] - with warnings.catch_warnings(record=True) as w: - # setuptools issue gh-1885 - warnings.filterwarnings('always', '', DeprecationWarning) - from numpy.random._examples.cython import setup - finally: - sys.argv = argv - os.chdir(curdir) +@pytest.mark.slow +def test_cython(tmp_path): + examples = os.path.join(os.path.dirname(__file__), '..', '_examples') + shutil.copytree(examples, tmp_path / '_examples') + subprocess.check_call([sys.executable, 'setup.py', 'build'], + cwd=str(tmp_path / '_examples' / 'cython')) @pytest.mark.skipif(numba is None or cffi is None, reason="requires numba and cffi") def test_numba(): - from numpy.random._examples.numba import extending + from numpy.random._examples.numba import extending @pytest.mark.skipif(cffi is None, reason="requires cffi") def test_cffi(): - from numpy.random._examples.cffi import extending + from numpy.random._examples.cffi import extending diff --git a/numpy/random/tests/test_generator_mt19937.py b/numpy/random/tests/test_generator_mt19937.py index 5b3d5f08a..6f4407373 100644 --- a/numpy/random/tests/test_generator_mt19937.py +++ b/numpy/random/tests/test_generator_mt19937.py @@ -19,7 +19,7 @@ def endpoint(request): return request.param -class TestSeed(object): +class TestSeed: def test_scalar(self): s = Generator(MT19937(0)) assert_equal(s.integers(1000), 479) @@ -55,7 +55,7 @@ class TestSeed(object): assert_raises(ValueError, Generator, MT19937) -class TestBinomial(object): +class TestBinomial: def test_n_zero(self): # Tests the corner case of n == 0 for the binomial distribution. # binomial(0, p) should be zero for any p in [0, 1]. @@ -70,7 +70,7 @@ class TestBinomial(object): assert_raises(ValueError, random.binomial, 1, np.nan) -class TestMultinomial(object): +class TestMultinomial: def test_basic(self): random.multinomial(100, [0.2, 0.8]) @@ -116,7 +116,7 @@ class TestMultinomial(object): assert_array_equal(non_contig, contig) -class TestMultivariateHypergeometric(object): +class TestMultivariateHypergeometric: def setup(self): self.seed = 8675309 @@ -250,7 +250,7 @@ class TestMultivariateHypergeometric(object): assert_array_equal(sample, expected) -class TestSetState(object): +class TestSetState: def setup(self): self.seed = 1234567890 self.rg = Generator(MT19937(self.seed)) @@ -284,7 +284,7 @@ class TestSetState(object): self.rg.negative_binomial(0.5, 0.5) -class TestIntegers(object): +class TestIntegers: rfunc = random.integers # valid integer/boolean types @@ -637,7 +637,7 @@ class TestIntegers(object): assert chi2 < chi2max -class TestRandomDist(object): +class TestRandomDist: # Make sure the random distribution returns the correct value for a # given seed @@ -1565,7 +1565,7 @@ class TestRandomDist(object): assert_array_equal(actual, desired) -class TestBroadcast(object): +class TestBroadcast: # tests that functions that broadcast behave # correctly when presented with non-scalar arguments def setup(self): @@ -2117,7 +2117,7 @@ class TestBroadcast(object): assert_array_equal(actual, desired) -class TestThread(object): +class TestThread: # make sure each state produces the same sequence even in threads def setup(self): self.seeds = range(4) @@ -2164,7 +2164,7 @@ class TestThread(object): # See Issue #4263 -class TestSingleEltArrayInput(object): +class TestSingleEltArrayInput: def setup(self): self.argOne = np.array([2]) self.argTwo = np.array([3]) diff --git a/numpy/random/tests/test_generator_mt19937_regressions.py b/numpy/random/tests/test_generator_mt19937_regressions.py index 7ca8b9f3c..fb0aac335 100644 --- a/numpy/random/tests/test_generator_mt19937_regressions.py +++ b/numpy/random/tests/test_generator_mt19937_regressions.py @@ -7,7 +7,7 @@ from numpy.random import Generator, MT19937 mt19937 = Generator(MT19937()) -class TestRegression(object): +class TestRegression: def test_VonMises_range(self): # Make sure generated random variables are in [-pi, pi]. @@ -136,7 +136,7 @@ class TestRegression(object): assert_array_equal(perm, np.array([2, 0, 1])) assert_array_equal(orig, np.arange(3).view(N)) - class M(object): + class M: a = np.arange(5) def __array__(self): diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 2e2ecedf8..a9aa15083 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -1,4 +1,3 @@ -from __future__ import division, absolute_import, print_function import warnings import numpy as np @@ -11,7 +10,7 @@ from numpy import random import sys -class TestSeed(object): +class TestSeed: def test_scalar(self): s = np.random.RandomState(0) assert_equal(s.randint(1000), 684) @@ -50,7 +49,7 @@ class TestSeed(object): [4, 5, 6]]) -class TestBinomial(object): +class TestBinomial: def test_n_zero(self): # Tests the corner case of n == 0 for the binomial distribution. # binomial(0, p) should be zero for any p in [0, 1]. @@ -65,7 +64,7 @@ class TestBinomial(object): assert_raises(ValueError, random.binomial, 1, np.nan) -class TestMultinomial(object): +class TestMultinomial: def test_basic(self): random.multinomial(100, [0.2, 0.8]) @@ -93,7 +92,7 @@ class TestMultinomial(object): float(1)) -class TestSetState(object): +class TestSetState: def setup(self): self.seed = 1234567890 self.prng = random.RandomState(self.seed) @@ -141,7 +140,7 @@ class TestSetState(object): self.prng.negative_binomial(0.5, 0.5) -class TestRandint(object): +class TestRandint: rfunc = np.random.randint @@ -279,7 +278,7 @@ class TestRandint(object): assert_equal(type(sample), dt) -class TestRandomDist(object): +class TestRandomDist: # Make sure the random distribution returns the correct value for a # given seed @@ -974,7 +973,7 @@ class TestRandomDist(object): assert_array_equal(actual, desired) -class TestBroadcast(object): +class TestBroadcast: # tests that functions that broadcast behave # correctly when presented with non-scalar arguments def setup(self): @@ -1544,7 +1543,7 @@ class TestBroadcast(object): assert_raises(ValueError, logseries, bad_p_two * 3) -class TestThread(object): +class TestThread: # make sure each state produces the same sequence even in threads def setup(self): self.seeds = range(4) @@ -1588,7 +1587,7 @@ class TestThread(object): # See Issue #4263 -class TestSingleEltArrayInput(object): +class TestSingleEltArrayInput: def setup(self): self.argOne = np.array([2]) self.argTwo = np.array([3]) diff --git a/numpy/random/tests/test_randomstate.py b/numpy/random/tests/test_randomstate.py index c12b685ad..ebe8558ba 100644 --- a/numpy/random/tests/test_randomstate.py +++ b/numpy/random/tests/test_randomstate.py @@ -61,7 +61,7 @@ def assert_mt19937_state_equal(a, b): assert_equal(a['gauss'], b['gauss']) -class TestSeed(object): +class TestSeed: def test_scalar(self): s = random.RandomState(0) assert_equal(s.randint(1000), 684) @@ -108,7 +108,7 @@ class TestSeed(object): assert_raises(ValueError, random.RandomState, MT19937) -class TestBinomial(object): +class TestBinomial: def test_n_zero(self): # Tests the corner case of n == 0 for the binomial distribution. # binomial(0, p) should be zero for any p in [0, 1]. @@ -123,7 +123,7 @@ class TestBinomial(object): assert_raises(ValueError, random.binomial, 1, np.nan) -class TestMultinomial(object): +class TestMultinomial: def test_basic(self): random.multinomial(100, [0.2, 0.8]) @@ -168,7 +168,7 @@ class TestMultinomial(object): assert_array_equal(non_contig, contig) -class TestSetState(object): +class TestSetState: def setup(self): self.seed = 1234567890 self.random_state = random.RandomState(self.seed) @@ -255,7 +255,7 @@ class TestSetState(object): assert repr(self.random_state).startswith('RandomState(MT19937)') -class TestRandint(object): +class TestRandint: rfunc = random.randint @@ -392,7 +392,7 @@ class TestRandint(object): assert_equal(type(sample), dt) -class TestRandomDist(object): +class TestRandomDist: # Make sure the random distribution returns the correct value for a # given seed @@ -1245,7 +1245,7 @@ class TestRandomDist(object): assert_array_equal(actual, desired) -class TestBroadcast(object): +class TestBroadcast: # tests that functions that broadcast behave # correctly when presented with non-scalar arguments def setup(self): @@ -1832,7 +1832,7 @@ class TestBroadcast(object): assert_raises(ValueError, logseries, bad_p_two * 3) -class TestThread(object): +class TestThread: # make sure each state produces the same sequence even in threads def setup(self): self.seeds = range(4) @@ -1879,7 +1879,7 @@ class TestThread(object): # See Issue #4263 -class TestSingleEltArrayInput(object): +class TestSingleEltArrayInput: def setup(self): self.argOne = np.array([2]) self.argTwo = np.array([3]) diff --git a/numpy/random/tests/test_randomstate_regression.py b/numpy/random/tests/test_randomstate_regression.py index bdc2214b6..3be9edf02 100644 --- a/numpy/random/tests/test_randomstate_regression.py +++ b/numpy/random/tests/test_randomstate_regression.py @@ -11,7 +11,7 @@ import numpy as np from numpy import random -class TestRegression(object): +class TestRegression: def test_VonMises_range(self): # Make sure generated random variables are in [-pi, pi]. @@ -147,7 +147,7 @@ class TestRegression(object): assert_array_equal(perm, np.array([0, 2, 1])) assert_array_equal(orig, np.arange(3).view(N)) - class M(object): + class M: a = np.arange(5) def __array__(self): diff --git a/numpy/random/tests/test_regression.py b/numpy/random/tests/test_regression.py index 509e2d57f..7d77a31d8 100644 --- a/numpy/random/tests/test_regression.py +++ b/numpy/random/tests/test_regression.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys from numpy.testing import ( assert_, assert_array_equal, assert_raises, @@ -9,7 +7,7 @@ from numpy.compat import long import numpy as np -class TestRegression(object): +class TestRegression: def test_VonMises_range(self): # Make sure generated random variables are in [-pi, pi]. @@ -145,7 +143,7 @@ class TestRegression(object): assert_array_equal(perm, np.array([0, 2, 1])) assert_array_equal(orig, np.arange(3).view(N)) - class M(object): + class M: a = np.arange(5) def __array__(self): diff --git a/numpy/random/tests/test_smoke.py b/numpy/random/tests/test_smoke.py index cdaac5ebb..ebfc6825e 100644 --- a/numpy/random/tests/test_smoke.py +++ b/numpy/random/tests/test_smoke.py @@ -91,7 +91,7 @@ def warmup(rg, n=None): rg.random(n, dtype=np.float32) -class RNG(object): +class RNG: @classmethod def setup_class(cls): # Overridden in test classes. Place holder to silence IDE noise diff --git a/numpy/setup.py b/numpy/setup.py index 4ccdaeea5..742de2cae 100644 --- a/numpy/setup.py +++ b/numpy/setup.py @@ -1,6 +1,4 @@ #!/usr/bin/env python -from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration diff --git a/numpy/testing/__init__.py b/numpy/testing/__init__.py index a8bd4fc15..e1f87621f 100644 --- a/numpy/testing/__init__.py +++ b/numpy/testing/__init__.py @@ -5,8 +5,6 @@ in a single location, so that test scripts can just import it and work right away. """ -from __future__ import division, absolute_import, print_function - from unittest import TestCase from ._private.utils import * diff --git a/numpy/testing/_private/decorators.py b/numpy/testing/_private/decorators.py index 24c4e385d..2012b80d3 100644 --- a/numpy/testing/_private/decorators.py +++ b/numpy/testing/_private/decorators.py @@ -13,8 +13,6 @@ function name, setup and teardown functions and so on - see ``nose.tools`` for more information. """ -from __future__ import division, absolute_import, print_function - try: # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 diff --git a/numpy/testing/_private/noseclasses.py b/numpy/testing/_private/noseclasses.py index e99bbc97d..493bacfdd 100644 --- a/numpy/testing/_private/noseclasses.py +++ b/numpy/testing/_private/noseclasses.py @@ -4,8 +4,6 @@ # Because this module imports nose directly, it should not # be used except by nosetester.py to avoid a general NumPy # dependency on nose. -from __future__ import division, absolute_import, print_function - import os import sys import doctest @@ -268,7 +266,7 @@ class NumpyDoctest(npd.Doctest): return npd.Doctest.wantFile(self, file) -class Unplugger(object): +class Unplugger: """ Nose plugin to remove named plugin late in loading By default it removes the "doctest" plugin. diff --git a/numpy/testing/_private/nosetester.py b/numpy/testing/_private/nosetester.py index 19569a509..4ca5267ce 100644 --- a/numpy/testing/_private/nosetester.py +++ b/numpy/testing/_private/nosetester.py @@ -4,8 +4,6 @@ Nose test running. This module implements ``test()`` and ``bench()`` functions for NumPy modules. """ -from __future__ import division, absolute_import, print_function - import os import sys import warnings @@ -112,7 +110,7 @@ def run_module_suite(file_to_run=None, argv=None): nose.run(argv=argv, addplugins=[KnownFailurePlugin()]) -class NoseTester(object): +class NoseTester: """ Nose test runner. @@ -456,11 +454,6 @@ class NoseTester(object): # This is very specific, so using the fragile module filter # is fine import threading - sup.filter(DeprecationWarning, - r"sys\.exc_clear\(\) not supported in 3\.x", - module=threading) - sup.filter(DeprecationWarning, message=r"in 3\.x, __setslice__") - sup.filter(DeprecationWarning, message=r"in 3\.x, __getslice__") sup.filter(DeprecationWarning, message=r"buffer\(\) not supported in 3\.x") sup.filter(DeprecationWarning, message=r"CObject type is not supported in 3\.x") sup.filter(DeprecationWarning, message=r"comparing unequal types not supported in 3\.x") diff --git a/numpy/testing/_private/parameterized.py b/numpy/testing/_private/parameterized.py index 489d8e09a..dbfb4807c 100644 --- a/numpy/testing/_private/parameterized.py +++ b/numpy/testing/_private/parameterized.py @@ -271,7 +271,7 @@ def set_test_runner(name): def detect_runner(): """ Guess which test runner we're using by traversing the stack and looking for the first matching module. This *should* be reasonably safe, as - it's done during test disocvery where the test runner should be the + it's done during test discovery where the test runner should be the stack frame immediately outside. """ if _test_runner_override is not None: return _test_runner_override @@ -293,10 +293,10 @@ def detect_runner(): _test_runner_guess = None return _test_runner_guess -class parameterized(object): +class parameterized: """ Parameterize a test case:: - class TestInt(object): + class TestInt: @parameterized([ ("A", 10), ("F", 15), diff --git a/numpy/testing/_private/utils.py b/numpy/testing/_private/utils.py index 23267a9e1..914491b71 100644 --- a/numpy/testing/_private/utils.py +++ b/numpy/testing/_private/utils.py @@ -2,8 +2,6 @@ Utility function to facilitate testing. """ -from __future__ import division, absolute_import, print_function - import os import sys import platform @@ -21,7 +19,7 @@ import pprint from numpy.core import( intp, float32, empty, arange, array_repr, ndarray, isnat, array) -import numpy.__config__ +import numpy.linalg.lapack_lite if sys.version_info[0] >= 3: from io import StringIO @@ -54,7 +52,7 @@ verbose = 0 IS_PYPY = platform.python_implementation() == 'PyPy' HAS_REFCOUNT = getattr(sys, 'getrefcount', None) is not None -HAS_LAPACK64 = hasattr(numpy.__config__, 'lapack_ilp64_opt_info') +HAS_LAPACK64 = numpy.linalg.lapack_lite._ilp64 def import_nose(): @@ -1636,8 +1634,9 @@ def assert_array_max_ulp(a, b, maxulp=1, dtype=None): import numpy as np ret = nulp_diff(a, b, dtype) if not np.all(ret <= maxulp): - raise AssertionError("Arrays are not almost equal up to %g ULP" % - maxulp) + raise AssertionError("Arrays are not almost equal up to %g " + "ULP (max difference is %g ULP)" % + (maxulp, np.max(ret))) return ret @@ -2002,7 +2001,7 @@ class clear_and_catch_warnings(warnings.catch_warnings): mod.__warningregistry__.update(self._warnreg_copies[mod]) -class suppress_warnings(object): +class suppress_warnings: """ Context manager and decorator doing much the same as ``warnings.catch_warnings``. @@ -2217,8 +2216,7 @@ class suppress_warnings(object): del self._filters def _showwarning(self, message, category, filename, lineno, - *args, **kwargs): - use_warnmsg = kwargs.pop("use_warnmsg", None) + *args, use_warnmsg=None, **kwargs): for cat, _, pattern, mod, rec in ( self._suppressions + self._tmp_suppressions)[::-1]: if (issubclass(category, cat) and diff --git a/numpy/testing/print_coercion_tables.py b/numpy/testing/print_coercion_tables.py index 72b22cee1..84d46b59b 100755 --- a/numpy/testing/print_coercion_tables.py +++ b/numpy/testing/print_coercion_tables.py @@ -2,12 +2,10 @@ """Prints type-coercion tables for the built-in NumPy types """ -from __future__ import division, absolute_import, print_function - import numpy as np # Generic object that can be added, but doesn't do anything else -class GenericObject(object): +class GenericObject: def __init__(self, v): self.v = v diff --git a/numpy/testing/setup.py b/numpy/testing/setup.py index 7c3f2fbdf..c061b688a 100755 --- a/numpy/testing/setup.py +++ b/numpy/testing/setup.py @@ -1,6 +1,4 @@ #!/usr/bin/env python -from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration diff --git a/numpy/testing/tests/test_decorators.py b/numpy/testing/tests/test_decorators.py index c029bf90c..77f8b66ba 100644 --- a/numpy/testing/tests/test_decorators.py +++ b/numpy/testing/tests/test_decorators.py @@ -2,8 +2,6 @@ Test the decorators from ``testing.decorators``. """ -from __future__ import division, absolute_import, print_function - import warnings import pytest @@ -23,7 +21,7 @@ else: @pytest.mark.skipif(not HAVE_NOSE, reason="Needs nose") -class TestNoseDecorators(object): +class TestNoseDecorators: # These tests are run in a class for simplicity while still # getting a report on each, skipped or success. diff --git a/numpy/testing/tests/test_doctesting.py b/numpy/testing/tests/test_doctesting.py index b77cd93e0..92c2156d8 100644 --- a/numpy/testing/tests/test_doctesting.py +++ b/numpy/testing/tests/test_doctesting.py @@ -1,8 +1,6 @@ """ Doctests for NumPy-specific nose/doctest modifications """ -from __future__ import division, absolute_import, print_function - #FIXME: None of these tests is run, because 'check' is not a recognized # testing prefix. diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index 44f93a693..232ca0e83 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import warnings import sys import os @@ -20,7 +18,7 @@ from numpy.testing import ( from numpy.core.overrides import ARRAY_FUNCTION_ENABLED -class _GenericTest(object): +class _GenericTest: def _test_equal(self, a, b): self._assert_func(a, b) @@ -90,6 +88,21 @@ class TestArrayEqual(_GenericTest): for t in ['S1', 'U1']: foo(t) + def test_0_ndim_array(self): + x = np.array(473963742225900817127911193656584771) + y = np.array(18535119325151578301457182298393896) + assert_raises(AssertionError, self._assert_func, x, y) + + y = x + self._assert_func(x, y) + + x = np.array(43) + y = np.array(10) + assert_raises(AssertionError, self._assert_func, x, y) + + y = x + self._assert_func(x, y) + def test_generic_rank3(self): """Test rank 3 array for all dtypes.""" def foo(t): @@ -196,7 +209,7 @@ class TestArrayEqual(_GenericTest): self._test_not_equal(b, a) -class TestBuildErrorMessage(object): +class TestBuildErrorMessage: def test_build_err_msg_defaults(self): x = np.array([1.00001, 2.00002, 3.00003]) @@ -603,14 +616,14 @@ class TestAlmostEqual(_GenericTest): self._assert_func(a, a) -class TestApproxEqual(object): +class TestApproxEqual: def setup(self): self._assert_func = assert_approx_equal - def test_simple_arrays(self): - x = np.array([1234.22]) - y = np.array([1234.23]) + def test_simple_0d_arrays(self): + x = np.array(1234.22) + y = np.array(1234.23) self._assert_func(x, y, significant=5) self._assert_func(x, y, significant=6) @@ -646,7 +659,7 @@ class TestApproxEqual(object): assert_raises(AssertionError, lambda: self._assert_func(ainf, anan)) -class TestArrayAssertLess(object): +class TestArrayAssertLess: def setup(self): self._assert_func = assert_array_less @@ -756,7 +769,7 @@ class TestArrayAssertLess(object): @pytest.mark.skip(reason="The raises decorator depends on Nose") -class TestRaises(object): +class TestRaises: def setup(self): class MyException(Exception): @@ -790,7 +803,7 @@ class TestRaises(object): raise AssertionError("should have raised an AssertionError") -class TestWarns(object): +class TestWarns: def test_warn(self): def f(): @@ -841,7 +854,7 @@ class TestWarns(object): raise AssertionError("wrong warning caught by assert_warn") -class TestAssertAllclose(object): +class TestAssertAllclose: def test_simple(self): x = 1e-3 @@ -911,7 +924,7 @@ class TestAssertAllclose(object): assert_('Max relative difference: 0.5' in msg) -class TestArrayAlmostEqualNulp(object): +class TestArrayAlmostEqualNulp: def test_float64_pass(self): # The number of units of least precision @@ -1108,7 +1121,7 @@ class TestArrayAlmostEqualNulp(object): xi, y + y*1j, nulp) -class TestULP(object): +class TestULP: def test_equal(self): x = np.random.randn(10) @@ -1164,7 +1177,7 @@ class TestULP(object): maxulp=maxulp)) -class TestStringEqual(object): +class TestStringEqual: def test_simple(self): assert_string_equal("hello", "hello") assert_string_equal("hello\nmultiline", "hello\nmultiline") @@ -1226,7 +1239,7 @@ def test_warn_len_equal_call_scenarios(): # check that no assertion is uncaught # parallel scenario -- no warning issued yet - class mod(object): + class mod: pass mod_inst = mod() @@ -1236,7 +1249,7 @@ def test_warn_len_equal_call_scenarios(): # serial test scenario -- the __warningregistry__ # attribute should be present - class mod(object): + class mod: def __init__(self): self.__warningregistry__ = {'warning1':1, 'warning2':2} @@ -1511,7 +1524,7 @@ def test_clear_and_catch_warnings_inherit(): @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") -class TestAssertNoGcCycles(object): +class TestAssertNoGcCycles: """ Test assert_no_gc_cycles """ def test_passes(self): def no_cycle(): @@ -1545,7 +1558,7 @@ class TestAssertNoGcCycles(object): error, instead of hanging forever trying to clear it. """ - class ReferenceCycleInDel(object): + class ReferenceCycleInDel: """ An object that not only contains a reference cycle, but creates new cycles whenever it's garbage-collected and its __del__ runs diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 975f6ad5d..753258c13 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -3,8 +3,6 @@ Back compatibility utils module. It will import the appropriate set of tools """ -from __future__ import division, absolute_import, print_function - import warnings # 2018-04-04, numpy 1.15.0 ImportWarning diff --git a/numpy/tests/test_ctypeslib.py b/numpy/tests/test_ctypeslib.py index 521208c36..af3730df1 100644 --- a/numpy/tests/test_ctypeslib.py +++ b/numpy/tests/test_ctypeslib.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import pytest import weakref @@ -37,7 +35,7 @@ else: reason="ctypes not available in this python") @pytest.mark.skipif(sys.platform == 'cygwin', reason="Known to fail on cygwin") -class TestLoadLibrary(object): +class TestLoadLibrary: def test_basic(self): try: # Should succeed @@ -63,7 +61,7 @@ class TestLoadLibrary(object): print(msg) -class TestNdpointer(object): +class TestNdpointer: def test_dtype(self): dt = np.intc p = ndpointer(dtype=dt) @@ -130,7 +128,7 @@ class TestNdpointer(object): @pytest.mark.skipif(ctypes is None, reason="ctypes not available on this python installation") -class TestNdpointerCFunc(object): +class TestNdpointerCFunc: def test_arguments(self): """ Test that arguments are coerced from arrays """ c_forward_pointer.restype = ctypes.c_void_p @@ -186,7 +184,7 @@ class TestNdpointerCFunc(object): @pytest.mark.skipif(ctypes is None, reason="ctypes not available on this python installation") -class TestAsArray(object): +class TestAsArray: def test_array(self): from ctypes import c_int @@ -277,7 +275,7 @@ class TestAsArray(object): @pytest.mark.skipif(ctypes is None, reason="ctypes not available on this python installation") -class TestAsCtypesType(object): +class TestAsCtypesType: """ Test conversion from dtypes to ctypes types """ def test_scalar(self): dt = np.dtype('<u2') diff --git a/numpy/tests/test_matlib.py b/numpy/tests/test_matlib.py index 38a7e39df..e04947a2e 100644 --- a/numpy/tests/test_matlib.py +++ b/numpy/tests/test_matlib.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - # As we are testing matrices, we ignore its PendingDeprecationWarnings try: import pytest diff --git a/numpy/tests/test_numpy_version.py b/numpy/tests/test_numpy_version.py index 7fac8fd22..916ab9383 100644 --- a/numpy/tests/test_numpy_version.py +++ b/numpy/tests/test_numpy_version.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import re import numpy as np diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 0484bb8cd..48dd42a9f 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys import subprocess import pkgutil @@ -50,7 +48,6 @@ def test_numpy_namespace(): 'fastCopyAndTranspose': 'numpy.core._multiarray_umath._fastCopyAndTranspose', 'get_array_wrap': 'numpy.lib.shape_base.get_array_wrap', 'get_include': 'numpy.lib.utils.get_include', - 'int_asbuffer': 'numpy.core._multiarray_umath.int_asbuffer', 'mafromtxt': 'numpy.lib.npyio.mafromtxt', 'ndfromtxt': 'numpy.lib.npyio.ndfromtxt', 'recfromcsv': 'numpy.lib.npyio.recfromcsv', @@ -228,7 +225,6 @@ PRIVATE_BUT_PRESENT_MODULES = ['numpy.' + s for s in [ "distutils.command.install_data", "distutils.command.install_headers", "distutils.command.sdist", - "distutils.compat", "distutils.conv_template", "distutils.core", "distutils.extension", diff --git a/numpy/tests/test_reloading.py b/numpy/tests/test_reloading.py index e378d1463..2b5a324ba 100644 --- a/numpy/tests/test_reloading.py +++ b/numpy/tests/test_reloading.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - import sys from numpy.testing import assert_raises, assert_, assert_equal diff --git a/numpy/tests/test_scripts.py b/numpy/tests/test_scripts.py index e42dc25f9..20447bcf3 100644 --- a/numpy/tests/test_scripts.py +++ b/numpy/tests/test_scripts.py @@ -2,8 +2,6 @@ Test that we can run executable scripts that have been installed with numpy. """ -from __future__ import division, print_function, absolute_import - import sys import os import pytest diff --git a/numpy/tests/test_warnings.py b/numpy/tests/test_warnings.py index f5560a099..48896f4b7 100644 --- a/numpy/tests/test_warnings.py +++ b/numpy/tests/test_warnings.py @@ -2,8 +2,6 @@ Tests which scan for certain occurrences in the code, they may not find all of these occurrences but should catch almost all. """ -from __future__ import division, absolute_import, print_function - import sys import pytest diff --git a/pavement.py b/pavement.py index 3637bc66d..494d8b79d 100644 --- a/pavement.py +++ b/pavement.py @@ -22,8 +22,6 @@ TODO - fix bdist_mpkg: we build the same source twice -> how to make sure we use the same underlying python for egg install in venv and for bdist_mpkg """ -from __future__ import division, print_function - import os import sys import shutil @@ -41,7 +39,7 @@ from paver.easy import Bunch, options, task, sh #----------------------------------- # Path to the release notes -RELEASE_NOTES = 'doc/release/1.18.0-notes.rst' +RELEASE_NOTES = 'doc/source/release/1.18.0-notes.rst' #------------------------------------------------------- @@ -182,6 +180,18 @@ def compute_sha256(idirs): def write_release_task(options, filename='README'): """Append hashes of release files to release notes. + This appends file hashes to the release notes ane creates + four README files of the result in various formats: + + - README.rst + - README.rst.gpg + - README.md + - README.md.gpg + + The md file are created using `pandoc` so that the links are + properly updated. The gpg files are kept separate, so that + the unsigned files may be edited before signing if needed. + Parameters ---------- options : @@ -192,46 +202,44 @@ def write_release_task(options, filename='README'): """ idirs = options.installers.installersdir - source = paver.path.path(RELEASE_NOTES) - target = paver.path.path(filename + '.rst') - if target.exists(): - target.remove() + notes = paver.path.path(RELEASE_NOTES) + rst_readme = paver.path.path(filename + '.rst') + md_readme = paver.path.path(filename + '.md') - tmp_target = paver.path.path(filename + '.md') - source.copy(tmp_target) + # append hashes + with open(rst_readme, 'w') as freadme: + with open(notes) as fnotes: + freadme.write(fnotes.read()) - with open(str(tmp_target), 'a') as ftarget: - ftarget.writelines(""" + freadme.writelines(""" Checksums ========= MD5 --- +:: """) - ftarget.writelines([' %s\n' % c for c in compute_md5(idirs)]) - ftarget.writelines(""" + freadme.writelines([f' {c}\n' for c in compute_md5(idirs)]) + freadme.writelines(""" SHA256 ------ +:: """) - ftarget.writelines([' %s\n' % c for c in compute_sha256(idirs)]) + freadme.writelines([f' {c}\n' for c in compute_sha256(idirs)]) - # Sign release - cmd = ['gpg', '--clearsign', '--armor'] + # generate md file using pandoc before signing + sh(f"pandoc -s -o {md_readme} {rst_readme}") + + # Sign files if hasattr(options, 'gpg_key'): - cmd += ['--default-key', options.gpg_key] - cmd += ['--output', str(target), str(tmp_target)] - subprocess.check_call(cmd) - print("signed %s" % (target,)) - - # Change PR links for github posting, don't sign this - # as the signing isn't markdown compatible. - with open(str(tmp_target), 'r') as ftarget: - mdtext = ftarget.read() - mdtext = re.sub(r'^\* `(\#[0-9]*).*?`__', r'* \1', mdtext, flags=re.M) - with open(str(tmp_target), 'w') as ftarget: - ftarget.write(mdtext) + cmd = f'gpg --clearsign --armor --default_key {options.gpg_key}' + else: + cmd = 'gpg --clearsign --armor' + + sh(cmd + f' --output {rst_readme}.gpg {rst_readme}') + sh(cmd + f' --output {md_readme}.gpg {md_readme}') @task diff --git a/pyproject.toml b/pyproject.toml index 918cbb278..d81b731d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = [ "setuptools", "wheel", - "Cython>=0.29.13", # Note: keep in sync with tools/cythonize.py + "Cython>=0.29.14", # Note: keep in sync with tools/cythonize.py ] diff --git a/pytest.ini b/pytest.ini index 74faefd6e..141c2f6ef 100644 --- a/pytest.ini +++ b/pytest.ini @@ -14,9 +14,6 @@ filterwarnings = # Matrix PendingDeprecationWarning. ignore:the matrix subclass is not # Ignore python2.7 -3 warnings - ignore:sys\.exc_clear\(\) not supported in 3\.x:DeprecationWarning - ignore:in 3\.x, __setslice__:DeprecationWarning - ignore:in 3\.x, __getslice__:DeprecationWarning ignore:buffer\(\) not supported in 3\.x:DeprecationWarning ignore:CObject type is not supported in 3\.x:DeprecationWarning ignore:comparing unequal types not supported in 3\.x:DeprecationWarning diff --git a/runtests.py b/runtests.py index a38054f86..d36e5bd39 100755 --- a/runtests.py +++ b/runtests.py @@ -29,8 +29,6 @@ Generate C code coverage listing under build/lcov/: $ python runtests.py --lcov-html """ -from __future__ import division, print_function - # # This is a generic test runner script for projects using NumPy's test # framework. Change the following values to adapt to your project: @@ -17,14 +17,13 @@ variety of databases. All NumPy wheels distributed on PyPI are BSD licensed. """ -from __future__ import division, print_function - DOCLINES = (__doc__ or '').split("\n") import os import sys import subprocess import textwrap +import sysconfig if sys.version_info[:2] < (3, 6): @@ -227,6 +226,40 @@ class sdist_checked(sdist): sdist.run(self) +def get_build_overrides(): + """ + Custom build commands to add `-std=c99` to compilation + """ + from numpy.distutils.command.build_clib import build_clib + from numpy.distutils.command.build_ext import build_ext + + def _is_using_gcc(obj): + is_gcc = False + if obj.compiler.compiler_type == 'unix': + cc = sysconfig.get_config_var("CC") + if not cc: + cc = "" + compiler_name = os.path.basename(cc) + is_gcc = "gcc" in compiler_name + return is_gcc + + class new_build_clib(build_clib): + def build_a_library(self, build_info, lib_name, libraries): + if _is_using_gcc(self): + args = build_info.get('extra_compiler_args') or [] + args.append('-std=c99') + build_info['extra_compiler_args'] = args + build_clib.build_a_library(self, build_info, lib_name, libraries) + + class new_build_ext(build_ext): + def build_extension(self, ext): + if _is_using_gcc(self): + if '-std=c99' not in ext.extra_compile_args: + ext.extra_compile_args.append('-std=c99') + build_ext.build_extension(self, ext) + return new_build_clib, new_build_ext + + def generate_cython(): cwd = os.path.abspath(os.path.dirname(__file__)) print("Cythonizing sources") @@ -389,6 +422,8 @@ def setup_package(): 'f2py%s.%s = numpy.f2py.f2py2e:main' % sys.version_info[:2], ] + cmdclass={"sdist": sdist_checked, + } metadata = dict( name = 'numpy', maintainer = "NumPy Developers", @@ -407,8 +442,7 @@ def setup_package(): classifiers=[_f for _f in CLASSIFIERS.split('\n') if _f], platforms = ["Windows", "Linux", "Solaris", "Mac OS-X", "Unix"], test_suite='nose.collector', - cmdclass={"sdist": sdist_checked, - }, + cmdclass=cmdclass, python_requires='>=3.5', zip_safe=False, entry_points={ @@ -432,6 +466,8 @@ def setup_package(): generate_cython() metadata['configuration'] = configuration + # Customize extension building + cmdclass['build_clib'], cmdclass['build_ext'] = get_build_overrides() else: # Version number is added to metadata inside configuration() if build # is run. diff --git a/shippable.yml b/shippable.yml index b5ce17751..4313a6de2 100644 --- a/shippable.yml +++ b/shippable.yml @@ -25,31 +25,32 @@ build: - sudo apt-get update - sudo apt-get install gcc gfortran - target=$(python tools/openblas_support.py) - - sudo cp -r "${target}"/64/lib/* /usr/lib - - sudo cp "${target}"/64/include/* /usr/include - - pip install --upgrade pip + - ls -lR "${target}" + - sudo cp -r "${target}"/lib/* /usr/lib + - sudo cp "${target}"/include/* /usr/include + - python -m pip install --upgrade pip # we will pay the ~13 minute cost of compiling Cython only when a new # version is scraped in by pip; otherwise, use the cached # wheel shippable places on Amazon S3 after we build it once - - pip install -r test_requirements.txt --cache-dir=/root/.cache/pip/wheels/$SHIPPABLE_PYTHON_VERSION + - python -m pip install -r test_requirements.txt --cache-dir=/root/.cache/pip/wheels/$SHIPPABLE_PYTHON_VERSION # install pytest-xdist to leverage a second core # for unit tests - - pip install pytest-xdist + - python -m pip install pytest-xdist # build and test numpy - export PATH=$PATH:$SHIPPABLE_REPO_DIR # build first and adjust PATH so f2py is found in scripts dir # use > 1 core for build sometimes slows down a fair bit, # other times modestly speeds up, so avoid for now - - pip install . + - python -m pip install . - extra_directories=($SHIPPABLE_REPO_DIR/build/*scripts*) - extra_path=$(printf "%s:" "${extra_directories[@]}") - export PATH="${extra_path}${PATH}" # check OpenBLAS version - python tools/openblas_support.py --check_version 0.3.7 # run the test suite - - python runtests.py --debug-info --show-build-log -- -rsx --junit-xml=$SHIPPABLE_REPO_DIR/shippable/testresults/tests.xml -n 2 --durations=10 + - python runtests.py -n --debug-info --show-build-log -- -rsx --junit-xml=$SHIPPABLE_REPO_DIR/shippable/testresults/tests.xml -n 2 --durations=10 cache: true cache_dir_list: diff --git a/tools/allocation_tracking/alloc_hook.pyx b/tools/allocation_tracking/alloc_hook.pyx index d1e656f90..eeefe1704 100644 --- a/tools/allocation_tracking/alloc_hook.pyx +++ b/tools/allocation_tracking/alloc_hook.pyx @@ -22,7 +22,7 @@ cdef void pyhook(void *old, void *new, size_t size, void *user_data): PyLong_FromVoidPtr(new), size) -class NumpyAllocHook(object): +class NumpyAllocHook: def __init__(self, callback): self.callback = callback diff --git a/tools/allocation_tracking/setup.py b/tools/allocation_tracking/setup.py index a75c95e91..4462f9f4e 100644 --- a/tools/allocation_tracking/setup.py +++ b/tools/allocation_tracking/setup.py @@ -1,5 +1,3 @@ -from __future__ import division, print_function - from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext diff --git a/tools/allocation_tracking/track_allocations.py b/tools/allocation_tracking/track_allocations.py index d25993800..94d07d50e 100644 --- a/tools/allocation_tracking/track_allocations.py +++ b/tools/allocation_tracking/track_allocations.py @@ -1,11 +1,9 @@ -from __future__ import division, absolute_import, print_function - import numpy as np import gc import inspect from alloc_hook import NumpyAllocHook -class AllocationTracker(object): +class AllocationTracker: def __init__(self, threshold=0): '''track numpy allocations of size threshold bytes or more.''' diff --git a/tools/c_coverage/c_coverage_report.py b/tools/c_coverage/c_coverage_report.py index 327f6dc05..28425054f 100755 --- a/tools/c_coverage/c_coverage_report.py +++ b/tools/c_coverage/c_coverage_report.py @@ -4,8 +4,6 @@ A script to create C code-coverage reports based on the output of valgrind's callgrind tool. """ -from __future__ import division, absolute_import, print_function - import optparse import os import re diff --git a/tools/changelog.py b/tools/changelog.py index b135b14e5..00ffdd9eb 100755 --- a/tools/changelog.py +++ b/tools/changelog.py @@ -33,8 +33,6 @@ From the bash command line with $GITHUB token:: $ ./tools/announce $GITHUB v1.13.0..v1.14.0 > 1.14.0-changelog.rst """ -from __future__ import print_function, division - import os import sys import re diff --git a/tools/commitstats.py b/tools/commitstats.py index a35d7b724..14c37d4d2 100644 --- a/tools/commitstats.py +++ b/tools/commitstats.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - # Run svn log -l <some number> import re diff --git a/tools/cythonize.py b/tools/cythonize.py index 5bea2d4ec..4d04906ea 100755 --- a/tools/cythonize.py +++ b/tools/cythonize.py @@ -30,8 +30,6 @@ Note: this script does not check any of the dependent C libraries; it only operates on the Cython .pyx files. """ -from __future__ import division, print_function, absolute_import - import os import re import sys @@ -68,11 +66,11 @@ def process_pyx(fromfile, tofile): # check the version, and invoke through python from distutils.version import LooseVersion - # Cython 0.29.13 is required for Python 3.8 and there are + # Cython 0.29.14 is required for Python 3.8 and there are # other fixes in the 0.29 series that are needed even for earlier # Python versions. # Note: keep in sync with that in pyproject.toml - required_version = LooseVersion('0.29.13') + required_version = LooseVersion('0.29.14') if LooseVersion(cython_version) < required_version: raise RuntimeError('Building {} requires Cython >= {}'.format( diff --git a/tools/find_deprecated_escaped_characters.py b/tools/find_deprecated_escaped_characters.py index 6f90001ca..401366bca 100644 --- a/tools/find_deprecated_escaped_characters.py +++ b/tools/find_deprecated_escaped_characters.py @@ -7,8 +7,6 @@ were accepted before. For instance, '\(' was previously accepted but must now be written as '\\(' or r'\('. """ -from __future__ import division, absolute_import, print_function - import sys def main(root): diff --git a/tools/npy_tempita/__init__.py b/tools/npy_tempita/__init__.py index f75f23a21..97905a16e 100644 --- a/tools/npy_tempita/__init__.py +++ b/tools/npy_tempita/__init__.py @@ -32,8 +32,6 @@ This copy of tempita was taken from https://github.com/gjhiggins/tempita with a few changes to remove the six dependency. """ -from __future__ import absolute_import, division, print_function - import re import sys try: @@ -92,7 +90,7 @@ def get_file_template(name, from_template): get_template=from_template.get_template) -class Template(object): +class Template: default_namespace = { 'start_braces': '{{', @@ -440,7 +438,7 @@ class bunch(dict): ############################################################ -class html(object): +class html: def __init__(self, value): self.value = value @@ -522,7 +520,7 @@ def sub_html(content, **kw): return tmpl.substitute(kw) -class TemplateDef(object): +class TemplateDef: def __init__(self, template, func_name, func_signature, body, ns, pos, bound_self=None): self._template = template @@ -599,7 +597,7 @@ class TemplateDef(object): return values -class TemplateObject(object): +class TemplateObject: def __init__(self, name): self.__name = name @@ -609,7 +607,7 @@ class TemplateObject(object): return '<%s %s>' % (self.__class__.__name__, self.__name) -class TemplateObjectGetter(object): +class TemplateObjectGetter: def __init__(self, template_obj): self.__template_obj = template_obj @@ -622,7 +620,7 @@ class TemplateObjectGetter(object): self.__class__.__name__, self.__template_obj) -class _Empty(object): +class _Empty: def __call__(self, *args, **kw): return self diff --git a/tools/npy_tempita/_looper.py b/tools/npy_tempita/_looper.py index 047bf5292..23121fe9e 100644 --- a/tools/npy_tempita/_looper.py +++ b/tools/npy_tempita/_looper.py @@ -17,15 +17,13 @@ looper you can get a better sense of the context. Use like:: 3 c """ -from __future__ import absolute_import, division, print_function - import sys from .compat3 import basestring_ __all__ = ['looper'] -class looper(object): +class looper: """ Helper for looping (particularly in templates) @@ -47,7 +45,7 @@ class looper(object): self.__class__.__name__, self.seq) -class looper_iter(object): +class looper_iter: def __init__(self, seq): self.seq = list(seq) @@ -67,7 +65,7 @@ class looper_iter(object): next = __next__ -class loop_pos(object): +class loop_pos: def __init__(self, seq, pos): self.seq = seq diff --git a/tools/npy_tempita/compat3.py b/tools/npy_tempita/compat3.py index 01d771345..d9d682ff5 100644 --- a/tools/npy_tempita/compat3.py +++ b/tools/npy_tempita/compat3.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - import sys __all__ = ['PY3', 'b', 'basestring_', 'bytes', 'next', 'is_unicode', diff --git a/tools/openblas_support.py b/tools/openblas_support.py index 964adce6e..2c1b70d6f 100644 --- a/tools/openblas_support.py +++ b/tools/openblas_support.py @@ -1,6 +1,7 @@ -from __future__ import division, absolute_import, print_function import os import sys +import glob +import shutil import textwrap import platform try: @@ -40,7 +41,15 @@ def get_arch(): assert ret in ARCHITECTURES return ret -def download_openblas(target, arch): +def get_ilp64(): + if os.environ.get("NPY_USE_BLAS_ILP64", "0") == "0": + return None + if IS_32BIT: + raise RuntimeError("NPY_USE_BLAS_ILP64 set on 32-bit arch") + return "64_" + +def download_openblas(target, arch, ilp64): + fnsuffix = {None: "", "64_": "64_"}[ilp64] filename = '' if arch == 'arm': # ARMv8 OpenBLAS built using script available here: @@ -48,7 +57,7 @@ def download_openblas(target, arch): # build done on GCC compile farm machine named gcc115 # tarball uploaded manually to an unshared Dropbox location filename = ('https://www.dropbox.com/s/vdeckao4omss187/' - 'openblas-{}-armv8.tar.gz?dl=1'.format(OPENBLAS_V)) + 'openblas{}-{}-armv8.tar.gz?dl=1'.format(fnsuffix, OPENBLAS_V)) typ = 'tar.gz' elif arch == 'ppc64': # build script for POWER8 OpenBLAS available here: @@ -56,28 +65,29 @@ def download_openblas(target, arch): # built on GCC compile farm machine named gcc112 # manually uploaded tarball to an unshared Dropbox location filename = ('https://www.dropbox.com/s/yt0d2j86x1j8nh1/' - 'openblas-{}-ppc64le-power8.tar.gz?dl=1'.format(OPENBLAS_V)) + 'openblas{}-{}-ppc64le-power8.tar.gz?dl=1'.format(fnsuffix, OPENBLAS_V)) typ = 'tar.gz' elif arch == 'darwin': - filename = '{0}/openblas-{1}-macosx_10_9_x86_64-gf_1becaaa.tar.gz'.format( - RACKSPACE, OPENBLAS_LONG) + filename = '{0}/openblas{1}-{2}-macosx_10_9_x86_64-gf_1becaaa.tar.gz'.format( + RACKSPACE, fnsuffix, OPENBLAS_LONG) typ = 'tar.gz' elif arch == 'windows': if IS_32BIT: suffix = 'win32-gcc_7_1_0.zip' else: suffix = 'win_amd64-gcc_7_1_0.zip' - filename = '{0}/openblas-{1}-{2}'.format(RACKSPACE, OPENBLAS_LONG, suffix) + filename = '{0}/openblas{1}-{2}-{3}'.format(RACKSPACE, fnsuffix, OPENBLAS_LONG, suffix) typ = 'zip' elif arch == 'x86': if IS_32BIT: suffix = 'manylinux1_i686.tar.gz' else: suffix = 'manylinux1_x86_64.tar.gz' - filename = '{0}/openblas-{1}-{2}'.format(RACKSPACE, OPENBLAS_LONG, suffix) + filename = '{0}/openblas{1}-{2}-{3}'.format(RACKSPACE, fnsuffix, OPENBLAS_LONG, suffix) typ = 'tar.gz' if not filename: return None + print("Downloading:", filename, file=sys.stderr) try: with open(target, 'wb') as fid: fid.write(urlopen(filename).read()) @@ -86,7 +96,7 @@ def download_openblas(target, arch): return None return typ -def setup_openblas(arch=get_arch()): +def setup_openblas(arch=get_arch(), ilp64=get_ilp64()): ''' Download and setup an openblas library for building. If successful, the configuration script will find it automatically. @@ -100,7 +110,7 @@ def setup_openblas(arch=get_arch()): _, tmp = mkstemp() if not arch: raise ValueError('unknown architecture') - typ = download_openblas(tmp, arch) + typ = download_openblas(tmp, arch, ilp64) if not typ: return '' if arch == 'windows': @@ -132,10 +142,33 @@ def unpack_targz(fname): if not os.path.exists(target): os.mkdir(target) with tarfile.open(fname, 'r') as zf: - # TODO: check that all the zf.getnames() files do not escape the - # extract directory (no leading '../', '/') - zf.extractall(target) - return target + # Strip common prefix from paths when unpacking + prefix = os.path.commonpath(zf.getnames()) + extract_tarfile_to(zf, target, prefix) + return target + +def extract_tarfile_to(tarfileobj, target_path, archive_path): + """Extract TarFile contents under archive_path/ to target_path/""" + + target_path = os.path.abspath(target_path) + + def get_members(): + for member in tarfileobj.getmembers(): + if archive_path: + norm_path = os.path.normpath(member.name) + if norm_path.startswith(archive_path + os.path.sep): + member.name = norm_path[len(archive_path)+1:] + else: + continue + + dst_path = os.path.abspath(os.path.join(target_path, member.name)) + if os.path.commonpath([target_path, dst_path]) != target_path: + # Path not under target_path, probably contains ../ + continue + + yield member + + tarfileobj.extractall(target_path, members=get_members()) def make_init(dirname): ''' @@ -180,19 +213,41 @@ def test_setup(arches): ''' Make sure all the downloadable files exist and can be opened ''' - for arch in arches: + def items(): + for arch in arches: + yield arch, None + if arch in ('x86', 'darwin', 'windows'): + yield arch, '64_' + + for arch, ilp64 in items(): if arch == '': continue + + target = None try: - target = setup_openblas(arch) - except: - print('Could not setup %s' % arch) - raise - if not target: - raise RuntimeError('Could not setup %s' % arch) - print(target) - -def test_version(expected_version): + try: + target = setup_openblas(arch, ilp64) + except: + print('Could not setup %s' % arch) + raise + if not target: + raise RuntimeError('Could not setup %s' % arch) + print(target) + if arch == 'windows': + if not target.endswith('.a'): + raise RuntimeError("Not .a extracted!") + else: + files = glob.glob(os.path.join(target, "lib", "*.a")) + if not files: + raise RuntimeError("No lib/*.a unpacked!") + finally: + if target is not None: + if os.path.isfile(target): + os.unlink(target) + else: + shutil.rmtree(target) + +def test_version(expected_version, ilp64=get_ilp64()): """ Assert that expected OpenBLAS version is actually available via NumPy @@ -201,12 +256,19 @@ def test_version(expected_version): import ctypes dll = ctypes.CDLL(numpy.core._multiarray_umath.__file__) - get_config = dll.openblas_get_config + if ilp64 == "64_": + get_config = dll.openblas_get_config64_ + else: + get_config = dll.openblas_get_config get_config.restype=ctypes.c_char_p res = get_config() print('OpenBLAS get_config returned', str(res)) check_str = b'OpenBLAS %s' % expected_version[0].encode() assert check_str in res + if ilp64: + assert b"USE64BITINT" in res + else: + assert b"USE64BITINT" not in res if __name__ == '__main__': import argparse diff --git a/tools/pypy-test.sh b/tools/pypy-test.sh index f4d56ba1a..33a97ad17 100755 --- a/tools/pypy-test.sh +++ b/tools/pypy-test.sh @@ -11,19 +11,20 @@ sudo apt-get -yq install libatlas-base-dev liblapack-dev gfortran-5 F77=gfortran-5 F90=gfortran-5 \ # Download the proper OpenBLAS x64 precompiled library -target=$(python tools/openblas_support.py) +target=$(python3 tools/openblas_support.py) +ls -lR "$target" echo getting OpenBLAS into $target -export LD_LIBRARY_PATH=$target/usr/local/lib -export LIB=$target/usr/local/lib -export INCLUDE=$target/usr/local/include +export LD_LIBRARY_PATH=$target/lib +export LIB=$target/lib +export INCLUDE=$target/include # Use a site.cfg to build with local openblas cat << EOF > site.cfg [openblas] libraries = openblas -library_dirs = $target/usr/local/lib:$LIB -include_dirs = $target/usr/local/lib:$LIB -runtime_library_dirs = $target/usr/local/lib +library_dirs = $target/lib:$LIB +include_dirs = $target/lib:$LIB +runtime_library_dirs = $target/lib EOF echo getting PyPy 3.6 nightly diff --git a/tools/refguide_check.py b/tools/refguide_check.py index ba045cebf..3b587b77b 100644 --- a/tools/refguide_check.py +++ b/tools/refguide_check.py @@ -25,8 +25,6 @@ or in RST-based documentations:: $ python refguide_check.py --rst docs """ -from __future__ import print_function - import sys import os import re @@ -797,7 +795,7 @@ def _run_doctests(tests, full_name, verbose, doctest_warnings): def out(msg): output.append(msg) - class MyStderr(object): + class MyStderr: """ Redirect stderr to the current stdout """ diff --git a/tools/swig/numpy.i b/tools/swig/numpy.i index 36bb55c98..8416e82f3 100644 --- a/tools/swig/numpy.i +++ b/tools/swig/numpy.i @@ -120,11 +120,6 @@ if (PyDict_Check( py_obj)) return "dict" ; if (PyList_Check( py_obj)) return "list" ; if (PyTuple_Check( py_obj)) return "tuple" ; -%#if PY_MAJOR_VERSION < 3 - if (PyFile_Check( py_obj)) return "file" ; - if (PyModule_Check( py_obj)) return "module" ; - if (PyInstance_Check(py_obj)) return "instance" ; -%#endif return "unknown type"; } @@ -545,7 +540,7 @@ const npy_intp *dims = array_dimensions(ary); for (i=0; i < nd; ++i) n_non_one += (dims[i] != 1) ? 1 : 0; - if (n_non_one > 1) + if (n_non_one > 1) array_clearflags(ary,NPY_ARRAY_CARRAY); array_enableflags(ary,NPY_ARRAY_FARRAY); /* Recompute the strides */ diff --git a/tools/swig/pyfragments.swg b/tools/swig/pyfragments.swg index 97ca8cf97..ce2452f80 100644 --- a/tools/swig/pyfragments.swg +++ b/tools/swig/pyfragments.swg @@ -75,21 +75,6 @@ SWIGINTERN int SWIG_AsVal_dec(unsigned long)(PyObject *obj, unsigned long *val) { - %#if PY_VERSION_HEX < 0x03000000 - if (PyInt_Check(obj)) - { - long v = PyInt_AsLong(obj); - if (v >= 0) - { - if (val) *val = v; - return SWIG_OK; - } - else - { - return SWIG_OverflowError; - } - } else - %#endif if (PyLong_Check(obj)) { unsigned long v = PyLong_AsUnsignedLong(obj); if (!PyErr_Occurred()) { diff --git a/tools/swig/test/setup.py b/tools/swig/test/setup.py index 4ff870e19..6bbfac2de 100755 --- a/tools/swig/test/setup.py +++ b/tools/swig/test/setup.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, print_function - # System imports from distutils.core import * from distutils import sysconfig diff --git a/tools/swig/test/testArray.py b/tools/swig/test/testArray.py index 8d9c79772..c8d0d0edb 100755 --- a/tools/swig/test/testArray.py +++ b/tools/swig/test/testArray.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, absolute_import, print_function - # System imports from distutils.util import get_platform import os diff --git a/tools/swig/test/testFarray.py b/tools/swig/test/testFarray.py index e8bf711c5..67dace5b1 100755 --- a/tools/swig/test/testFarray.py +++ b/tools/swig/test/testFarray.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, absolute_import, print_function - # System imports from distutils.util import get_platform import os diff --git a/tools/swig/test/testFlat.py b/tools/swig/test/testFlat.py index 71be277b1..470f157d8 100755 --- a/tools/swig/test/testFlat.py +++ b/tools/swig/test/testFlat.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, absolute_import, print_function - # System imports from distutils.util import get_platform import os diff --git a/tools/swig/test/testFortran.py b/tools/swig/test/testFortran.py index 426e8943d..76bf1dd2a 100644 --- a/tools/swig/test/testFortran.py +++ b/tools/swig/test/testFortran.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, absolute_import, print_function - # System imports from distutils.util import get_platform import os diff --git a/tools/swig/test/testMatrix.py b/tools/swig/test/testMatrix.py index 065be0d44..24c64e281 100755 --- a/tools/swig/test/testMatrix.py +++ b/tools/swig/test/testMatrix.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, absolute_import, print_function - # System imports from distutils.util import get_platform import os diff --git a/tools/swig/test/testSuperTensor.py b/tools/swig/test/testSuperTensor.py index 97fe80c33..f6e15b6ac 100644 --- a/tools/swig/test/testSuperTensor.py +++ b/tools/swig/test/testSuperTensor.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, print_function - # System imports from distutils.util import get_platform from math import sqrt diff --git a/tools/swig/test/testTensor.py b/tools/swig/test/testTensor.py index ac1b7491a..f8f492937 100755 --- a/tools/swig/test/testTensor.py +++ b/tools/swig/test/testTensor.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, absolute_import, print_function - # System imports from distutils.util import get_platform from math import sqrt diff --git a/tools/swig/test/testVector.py b/tools/swig/test/testVector.py index 45e763b36..e28bfcc15 100755 --- a/tools/swig/test/testVector.py +++ b/tools/swig/test/testVector.py @@ -1,6 +1,4 @@ #! /usr/bin/env python -from __future__ import division, absolute_import, print_function - # System imports from distutils.util import get_platform import os diff --git a/tools/travis-before-install.sh b/tools/travis-before-install.sh index 072ad3bf6..9f8b66a47 100755 --- a/tools/travis-before-install.sh +++ b/tools/travis-before-install.sh @@ -5,12 +5,12 @@ free -m df -h ulimit -a -if [ -n "$PPC64_LE" ]; then +if [ -n "$DOWNLOAD_OPENBLAS" ]; then pwd ls -ltrh target=$(python tools/openblas_support.py) - sudo cp -r $target/64/lib/* /usr/lib - sudo cp $target/64/include/* /usr/include + sudo cp -r $target/lib/* /usr/lib + sudo cp $target/include/* /usr/include fi mkdir builds |