Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WIP, MAINT: Improve import time #14083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
python3 -m venv venv
ln -s $(which python3) venv/bin/python3.6
. venv/bin/activate
pip install cython sphinx==1.8.5 matplotlib ipython
pip install cython sphinx==1.8.5 matplotlib ipython fastrlock
sudo apt-get update
sudo apt-get install -y graphviz texlive-fonts-recommended texlive-latex-recommended texlive-latex-extra texlive-generic-extra latexmk texlive-xetex

Expand All @@ -30,7 +30,7 @@ jobs:
command: |
. venv/bin/activate
pip install --upgrade pip setuptools
pip install cython
pip install cython fastrlock
pip install .
pip install scipy

Expand Down
8 changes: 4 additions & 4 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
apt-get -y update && \
apt-get -y install python3.6-dev python3-pip locales python3-certifi && \
locale-gen fr_FR && update-locale && \
pip3 install setuptools nose cython==0.29.0 pytest pytz pickle5 && \
pip3 install setuptools nose cython==0.29.0 pytest pytz pickle5 fastrlock && \
apt-get -y install gfortran-5 wget && \
target=\$(python3 tools/openblas_support.py) && \
cp -r \$target/usr/local/lib/* /usr/lib && \
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
displayName: 'install pre-built openblas'
- script: python -m pip install --upgrade pip setuptools wheel
displayName: 'Install tools'
- script: python -m pip install cython nose pytz pytest pickle5 vulture docutils sphinx==1.8.5 numpydoc
- script: python -m pip install cython fastrlock nose pytz pytest pickle5 vulture docutils sphinx==1.8.5 numpydoc
displayName: 'Install dependencies; some are optional to avoid test skips'
- script: /bin/bash -c "! vulture . --min-confidence 100 --exclude doc/,numpy/distutils/ | grep 'unreachable'"
displayName: 'Check for unreachable code paths in Python modules'
Expand Down Expand Up @@ -156,13 +156,13 @@ jobs:
architecture: $(PYTHON_ARCH)
- script: python -m pip install --upgrade pip setuptools wheel
displayName: 'Install tools'
- script: python -m pip install cython nose pytz pytest
- script: python -m pip install cython fastrlock nose pytz pytest
displayName: 'Install dependencies; some are optional to avoid test skips'
- script: if [%INSTALL_PICKLE5%]==[1] python -m pip install pickle5
displayName: 'Install optional pickle5 backport (only for python3.6 and 3.7)'

- powershell: |
$pyversion = python -c "from __future__ import print_function; import sys; print(sys.version.split()[0])"
$pyversion = python -c "
Write-Host "Python Version: $pyversion"
$target = "C:\\hostedtoolcache\\windows\\Python\\$pyversion\\$(PYTHON_ARCH)\\lib\\openblas.a"
Write-Host "target path: $target"
Expand Down
4 changes: 0 additions & 4 deletions numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@
oldnumeric = 'removed'
numarray = 'removed'

# We don't actually use this ourselves anymore, but I'm not 100% sure that
# no-one else in the world is using it (though I hope not)
from .testing import Tester

# Pytest testing
from numpy._pytesttester import PytestTester
test = PytestTester(__name__)
Expand Down
27 changes: 16 additions & 11 deletions numpy/compat/py3k.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@
__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested',
'asstr', 'open_latin1', 'long', 'basestring', 'sixu',
'integer_types', 'is_pathlib_path', 'npy_load_module', 'Path',
'pickle', 'contextlib_nullcontext', 'os_fspath', 'os_PathLike']
'integer_types', 'is_pathlib_path', 'npy_load_module',
'get_pickle', 'contextlib_nullcontext', 'os_fspath', 'os_PathLike']

import sys
import os
try:
from pathlib import Path, PurePath
except ImportError:
Path = PurePath = None

if sys.version_info[0] >= 3:
import io

try:
import pickle5 as pickle
except ImportError:
import pickle
def get_pickle():
try:
import pickle5 as pickle
except ImportError:
import pickle
return pickle

long = int
integer_types = (int,)
Expand Down Expand Up @@ -58,7 +56,9 @@ def sixu(s):
strchar = 'U'

else:
import cpickle as pickle
def get_pickle():
import cpickle as pickle
return pickle

bytes = str
long = long
Expand Down Expand Up @@ -104,6 +104,7 @@ def is_pathlib_path(obj):

Prefer using `isinstance(obj, os_PathLike)` instead of this function.
"""
from pathlib import Path
return Path is not None and isinstance(obj, Path)

# from Python 3.7
Expand Down Expand Up @@ -201,6 +202,10 @@ def npy_load_module(name, fn, info=None):
def _PurePath__fspath__(self):
return str(self)

# Pathlib isn't exactly a cheap import. Lazy import it here in case
# users are running python older 3.6
from pathlib import Pathlike

class os_PathLike(abc_ABC):
"""Abstract base class for implementing the file system path protocol."""

Expand Down
7 changes: 4 additions & 3 deletions numpy/core/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import re
import sys
import platform

from numpy.compat import unicode
from .multiarray import dtype, array, ndarray
Expand All @@ -17,7 +16,10 @@
except ImportError:
ctypes = None

IS_PYPY = platform.python_implementation() == 'PyPy'
# While one could use platform.python_implementation
# importing platform is quite slow
IS_PYPY = "PyPy" in sys.version


if (sys.byteorder == 'little'):
_nbo = b'<'
Expand Down Expand Up @@ -909,4 +911,3 @@ def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
return self.func(self, *args, **kwargs)

4 changes: 3 additions & 1 deletion numpy/core/_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from numpy.core import numerictypes as nt
from numpy.core import _exceptions
from numpy._globals import _NoValue
from numpy.compat import pickle, os_fspath, contextlib_nullcontext
from numpy.compat import os_fspath, contextlib_nullcontext, get_pickle

# save those O(100) nanoseconds!
umr_maximum = um.maximum.reduce
Expand Down Expand Up @@ -233,6 +233,7 @@ def _ptp(a, axis=None, out=None, keepdims=False):
)

def _dump(self, file, protocol=2):
pickle = get_pickle()
if hasattr(file, 'write'):
ctx = contextlib_nullcontext(file)
else:
Expand All @@ -241,4 +242,5 @@ def _dump(self, file, protocol=2):
pickle.dump(self, f, protocol=protocol)

def _dumps(self, protocol=2):
pickle = get_pickle()
return pickle.dumps(self, protocol=protocol)
6 changes: 4 additions & 2 deletions numpy/core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import contextlib

import numpy as np
from numpy.compat import pickle, basestring
from numpy.compat import get_pickle, basestring
from . import multiarray
from .multiarray import (
_fastCopyAndTranspose as fastCopyAndTranspose, ALLOW_THREADS,
Expand Down Expand Up @@ -49,6 +49,7 @@


def loads(*args, **kwargs):
pickle = get_pickle()
# NumPy 1.15.0, 2017-12-10
warnings.warn(
"np.core.numeric.loads is deprecated, use pickle.loads instead",
Expand Down Expand Up @@ -937,7 +938,7 @@ def tensordot(a, b, axes=2):
Returns
-------
output : ndarray
The tensor dot product of the input.
The tensor dot product of the input.

See Also
--------
Expand Down Expand Up @@ -2038,6 +2039,7 @@ def load(file):
load, save

"""
pickle = get_pickle()
# NumPy 1.15.0, 2017-12-10
warnings.warn(
"np.core.numeric.load is deprecated, use pickle.load instead",
Expand Down
15 changes: 7 additions & 8 deletions numpy/core/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import collections
import functools
import os
import textwrap

from numpy.core._multiarray_umath import (
add_docstring, implement_array_function, _get_implementing_args)
Expand Down Expand Up @@ -163,13 +162,13 @@ def decorator(implementation):
# more interpettable name. Otherwise, the original function does not
# show up at all in many cases, e.g., if it's written in C or if the
# dispatcher gets an invalid keyword argument.
source = textwrap.dedent("""
@functools.wraps(implementation)
def {name}(*args, **kwargs):
relevant_args = dispatcher(*args, **kwargs)
return implement_array_function(
implementation, {name}, relevant_args, args, kwargs)
""").format(name=implementation.__name__)
source = """
@functools.wraps(implementation)
def {name}(*args, **kwargs):
relevant_args = dispatcher(*args, **kwargs)
return implement_array_function(
implementation, {name}, relevant_args, args, kwargs)
""".format(name=implementation.__name__)

source_object = compile(
source, filename='<__array_function__ internals>', mode='exec')
Expand Down
3 changes: 2 additions & 1 deletion numpy/core/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
assert_, assert_equal, assert_raises, assert_warns, suppress_warnings,
assert_raises_regex,
)
from numpy.compat import pickle
from numpy.compat import get_pickle
pickle = get_pickle()

# Use pytz to test out various time zones if available
try:
Expand Down
12 changes: 6 additions & 6 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from numpy.core._rational_tests import rational
from numpy.testing import (
assert_, assert_equal, assert_array_equal, assert_raises, HAS_REFCOUNT)
from numpy.compat import pickle
from numpy.compat import get_pickle
pickle = get_pickle()
from itertools import permutations

def assert_dtype_equal(a, b):
Expand Down Expand Up @@ -138,11 +139,11 @@ def test_bad_param(self):
'offsets':[0, 2]}, align=True)

def test_field_order_equality(self):
x = np.dtype({'names': ['A', 'B'],
'formats': ['i4', 'f4'],
x = np.dtype({'names': ['A', 'B'],
'formats': ['i4', 'f4'],
'offsets': [0, 4]})
y = np.dtype({'names': ['B', 'A'],
'formats': ['f4', 'i4'],
y = np.dtype({'names': ['B', 'A'],
'formats': ['f4', 'i4'],
'offsets': [4, 0]})
assert_equal(x == y, False)

Expand Down Expand Up @@ -1279,4 +1280,3 @@ def test_pairs(self, pair):
pair_type = np.dtype('{},{}'.format(*pair))
expected = np.dtype([('f0', pair[0]), ('f1', pair[1])])
assert_equal(pair_type, expected)

2 changes: 1 addition & 1 deletion numpy/core/tests/test_memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from numpy import (
memmap, sum, average, product, ndarray, isscalar, add, subtract, multiply)
from numpy.compat import Path
from pathlib import Path

from numpy import arange, allclose, asarray
from numpy.testing import (
Expand Down
15 changes: 8 additions & 7 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import pytest
from contextlib import contextmanager

from numpy.compat import pickle
from numpy.compat import get_pickle
pickle = get_pickle()

try:
import pathlib
Expand Down Expand Up @@ -114,7 +115,7 @@ def test_writeable_any_base(self):
# Ensure that any base being writeable is sufficient to change flag;
# this is especially interesting for arrays from an array interface.
arr = np.arange(10)

class subclass(np.ndarray):
pass

Expand Down Expand Up @@ -3967,13 +3968,13 @@ def test_subarray_int_shape(self):

def test_datetime64_byteorder(self):
original = np.array([['2015-02-24T00:00:00.000000000']], dtype='datetime64[ns]')

original_byte_reversed = original.copy(order='K')
original_byte_reversed.dtype = original_byte_reversed.dtype.newbyteorder('S')
original_byte_reversed.byteswap(inplace=True)

new = pickle.loads(pickle.dumps(original_byte_reversed))

assert_equal(original.dtype, new.dtype)


Expand Down Expand Up @@ -4868,7 +4869,7 @@ def test_fromfile_offset(self):
offset_bytes = self.dtype.itemsize
z = np.fromfile(f, dtype=self.dtype, offset=offset_bytes)
assert_array_equal(z, self.x.flat[offset_items+count_items+1:])

with open(self.filename, 'wb') as f:
self.x.tofile(f, sep=",")

Expand Down Expand Up @@ -6225,14 +6226,14 @@ def test_dot_equivalent(self, args):

r3 = np.matmul(args[0].copy(), args[1].copy())
assert_equal(r1, r3)

def test_matmul_object(self):
import fractions

f = np.vectorize(fractions.Fraction)
def random_ints():
return np.random.randint(1, 1000, size=(10, 3, 3))
M1 = f(random_ints(), random_ints())
M1 = f(random_ints(), random_ints())
M2 = f(random_ints(), random_ints())

M3 = self.matmul(M1, M2)
Expand Down
3 changes: 2 additions & 1 deletion numpy/core/tests/test_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from numpy.core.overrides import (
_get_implementing_args, array_function_dispatch,
verify_matching_signatures, ARRAY_FUNCTION_ENABLED)
from numpy.compat import pickle
from numpy.compat import get_pickle
pickle = get_pickle()
import pytest


Expand Down
6 changes: 3 additions & 3 deletions numpy/core/tests/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import pytest

import numpy as np
from numpy.compat import Path
from pathlib import Path
from numpy.testing import (
assert_, assert_equal, assert_array_equal, assert_array_almost_equal,
assert_raises, temppath
)
from numpy.compat import pickle

from numpy.compat import get_pickle
pickle = get_pickle()

class TestFromrecords(object):
def test_fromrecords(self):
Expand Down
3 changes: 2 additions & 1 deletion numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
assert_raises_regex, assert_warns, suppress_warnings,
_assert_valid_refcount, HAS_REFCOUNT,
)
from numpy.compat import asbytes, asunicode, long, pickle
from numpy.compat import asbytes, asunicode, long, get_pickle
pickle = get_pickle()

try:
RecursionError
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/tests/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
assert_almost_equal, assert_array_almost_equal, assert_no_warnings,
assert_allclose,
)
from numpy.compat import pickle

from numpy.compat import get_pickle
pickle = get_pickle()

class TestUfuncKwargs(object):
def test_kwarg_exact(self):
Expand Down
Loading