summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorSeth Troisi <sethtroisi@google.com>2020-01-15 17:03:58 -0800
committerSeth Troisi <sethtroisi@google.com>2020-01-20 15:22:57 -0800
commit9a21ec857b22ff0140a7f71a12f2cc943f163404 (patch)
tree1f8b26a1bb346fab26d2210de286d29011bf2bf1 /numpy/lib
parentb753aa7a3a2c958e70826fb8af3b56db5c758819 (diff)
downloadnumpy-9a21ec857b22ff0140a7f71a12f2cc943f163404.tar.gz
[MAINT] Cleanup python2 sys.version checks
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/_datasource.py84
-rw-r--r--numpy/lib/format.py26
-rw-r--r--numpy/lib/mixins.py6
-rw-r--r--numpy/lib/npyio.py61
-rw-r--r--numpy/lib/recfunctions.py11
-rw-r--r--numpy/lib/utils.py6
6 files changed, 36 insertions, 158 deletions
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py
index aa793958e..139b8c0ca 100644
--- a/numpy/lib/_datasource.py
+++ b/numpy/lib/_datasource.py
@@ -70,70 +70,6 @@ def _check_mode(mode, encoding, newline):
raise ValueError("Argument 'newline' not supported in binary mode")
-def _python2_bz2open(fn, mode, encoding, newline):
- """Wrapper to open bz2 in text mode.
-
- Parameters
- ----------
- fn : str
- File name
- mode : {'r', 'w'}
- File mode. Note that bz2 Text files are not supported.
- encoding : str
- Ignored, text bz2 files not supported in Python2.
- newline : str
- Ignored, text bz2 files not supported in Python2.
- """
- import bz2
-
- _check_mode(mode, encoding, newline)
-
- if "t" in mode:
- # BZ2File is missing necessary functions for TextIOWrapper
- warnings.warn("Assuming latin1 encoding for bz2 text file in Python2",
- RuntimeWarning, stacklevel=5)
- mode = mode.replace("t", "")
- return bz2.BZ2File(fn, mode)
-
-def _python2_gzipopen(fn, mode, encoding, newline):
- """ Wrapper to open gzip in text mode.
-
- Parameters
- ----------
- fn : str, bytes, file
- File path or opened file.
- mode : str
- File mode. The actual files are opened as binary, but will decoded
- using the specified `encoding` and `newline`.
- encoding : str
- Encoding to be used when reading/writing as text.
- newline : str
- Newline to be used when reading/writing as text.
-
- """
- import gzip
- # gzip is lacking read1 needed for TextIOWrapper
- class GzipWrap(gzip.GzipFile):
- def read1(self, n):
- return self.read(n)
-
- _check_mode(mode, encoding, newline)
-
- gz_mode = mode.replace("t", "")
-
- if isinstance(fn, (str, bytes)):
- binary_file = GzipWrap(fn, gz_mode)
- elif hasattr(fn, "read") or hasattr(fn, "write"):
- binary_file = GzipWrap(None, gz_mode, fileobj=fn)
- else:
- raise TypeError("filename must be a str or bytes object, or a file")
-
- if "t" in mode:
- return io.TextIOWrapper(binary_file, encoding, newline=newline)
- else:
- return binary_file
-
-
# Using a class instead of a module-level dictionary
# to reduce the initial 'import numpy' overhead by
# deferring the import of lzma, bz2 and gzip until needed
@@ -174,19 +110,13 @@ class _FileOpeners:
try:
import bz2
- if sys.version_info[0] >= 3:
- self._file_openers[".bz2"] = bz2.open
- else:
- self._file_openers[".bz2"] = _python2_bz2open
+ self._file_openers[".bz2"] = bz2.open
except ImportError:
pass
try:
import gzip
- if sys.version_info[0] >= 3:
- self._file_openers[".gz"] = gzip.open
- else:
- self._file_openers[".gz"] = _python2_gzipopen
+ self._file_openers[".gz"] = gzip.open
except ImportError:
pass
@@ -547,14 +477,10 @@ class DataSource:
if os.path.exists(path):
return True
- # We import this here because importing urllib2 is slow and
+ # We import this here because importing urllib is slow and
# a significant fraction of numpy's total import time.
- if sys.version_info[0] >= 3:
- from urllib.request import urlopen
- from urllib.error import URLError
- else:
- from urllib2 import urlopen
- from urllib2 import URLError
+ from urllib.request import urlopen
+ from urllib.error import URLError
# Test cached url
upath = self.abspath(path)
diff --git a/numpy/lib/format.py b/numpy/lib/format.py
index 15a74518b..114bae287 100644
--- a/numpy/lib/format.py
+++ b/numpy/lib/format.py
@@ -162,7 +162,6 @@ evolved with time and this document is more current.
"""
import numpy
-import sys
import io
import warnings
from numpy.lib.utils import safe_eval
@@ -213,10 +212,7 @@ def magic(major, minor):
raise ValueError("major version must be 0 <= major < 256")
if minor < 0 or minor > 255:
raise ValueError("minor version must be 0 <= minor < 256")
- if sys.version_info[0] < 3:
- return MAGIC_PREFIX + chr(major) + chr(minor)
- else:
- return MAGIC_PREFIX + bytes([major, minor])
+ return MAGIC_PREFIX + bytes([major, minor])
def read_magic(fp):
""" Read the magic string to get the version of the file format.
@@ -234,10 +230,7 @@ def read_magic(fp):
if magic_str[:-2] != MAGIC_PREFIX:
msg = "the magic string is not correct; expected %r, got %r"
raise ValueError(msg % (MAGIC_PREFIX, magic_str[:-2]))
- if sys.version_info[0] < 3:
- major, minor = map(ord, magic_str[-2:])
- else:
- major, minor = magic_str[-2:]
+ major, minor = magic_str[-2:]
return major, minor
def _has_metadata(dt):
@@ -542,10 +535,7 @@ def _filter_header(s):
"""
import tokenize
- if sys.version_info[0] >= 3:
- from io import StringIO
- else:
- from StringIO import StringIO
+ from io import StringIO
tokens = []
last_token_was_number = False
@@ -738,12 +728,10 @@ def read_array(fp, allow_pickle=False, pickle_kwargs=None):
try:
array = pickle.load(fp, **pickle_kwargs)
except UnicodeError as err:
- if sys.version_info[0] >= 3:
- # Friendlier error message
- raise UnicodeError("Unpickling a python object failed: %r\n"
- "You may need to pass the encoding= option "
- "to numpy.load" % (err,))
- raise
+ # Friendlier error message
+ raise UnicodeError("Unpickling a python object failed: %r\n"
+ "You may need to pass the encoding= option "
+ "to numpy.load" % (err,))
else:
if isfileobj(fp):
# We can use the fast fromfile() function.
diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py
index d4811b94d..50157069c 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."""
-import sys
-
from numpy.core import umath as um
@@ -152,9 +150,7 @@ class NDArrayOperatorsMixin:
__mul__, __rmul__, __imul__ = _numeric_methods(um.multiply, 'mul')
__matmul__, __rmatmul__, __imatmul__ = _numeric_methods(
um.matmul, 'matmul')
- if sys.version_info.major < 3:
- # Python 3 uses only __truediv__ and __floordiv__
- __div__, __rdiv__, __idiv__ = _numeric_methods(um.divide, 'div')
+ # Python 3 does not use __div__, __rdiv__, or __idiv__
__truediv__, __rtruediv__, __itruediv__ = _numeric_methods(
um.true_divide, 'truediv')
__floordiv__, __rfloordiv__, __ifloordiv__ = _numeric_methods(
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index c47e388c0..29af488d2 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -7,6 +7,7 @@ import warnings
import weakref
import contextlib
from operator import itemgetter, index as opindex
+from collections.abc import Mapping
import numpy as np
from . import format
@@ -26,12 +27,6 @@ from numpy.compat import (
pickle, contextlib_nullcontext
)
-if sys.version_info[0] >= 3:
- from collections.abc import Mapping
-else:
- from future_builtins import map
- from collections import Mapping
-
@set_module('numpy')
def loads(*args, **kwargs):
@@ -264,26 +259,25 @@ class NpzFile(Mapping):
raise KeyError("%s is not a file in the archive" % key)
- if sys.version_info.major == 3:
- # deprecate the python 2 dict apis that we supported by accident in
- # python 3. We forgot to implement itervalues() at all in earlier
- # versions of numpy, so no need to deprecated it here.
+ # deprecate the python 2 dict apis that we supported by accident in
+ # python 3. We forgot to implement itervalues() at all in earlier
+ # versions of numpy, so no need to deprecated it here.
- def iteritems(self):
- # Numpy 1.15, 2018-02-20
- warnings.warn(
- "NpzFile.iteritems is deprecated in python 3, to match the "
- "removal of dict.itertems. Use .items() instead.",
- DeprecationWarning, stacklevel=2)
- return self.items()
+ def iteritems(self):
+ # Numpy 1.15, 2018-02-20
+ warnings.warn(
+ "NpzFile.iteritems is deprecated in python 3, to match the "
+ "removal of dict.itertems. Use .items() instead.",
+ DeprecationWarning, stacklevel=2)
+ return self.items()
- def iterkeys(self):
- # Numpy 1.15, 2018-02-20
- warnings.warn(
- "NpzFile.iterkeys is deprecated in python 3, to match the "
- "removal of dict.iterkeys. Use .keys() instead.",
- DeprecationWarning, stacklevel=2)
- return self.keys()
+ def iterkeys(self):
+ # Numpy 1.15, 2018-02-20
+ warnings.warn(
+ "NpzFile.iterkeys is deprecated in python 3, to match the "
+ "removal of dict.iterkeys. Use .keys() instead.",
+ DeprecationWarning, stacklevel=2)
+ return self.keys()
@set_module('numpy')
@@ -412,11 +406,7 @@ def load(file, mmap_mode=None, allow_pickle=False, fix_imports=True,
# result can similarly silently corrupt numerical data.
raise ValueError("encoding must be 'ASCII', 'latin1', or 'bytes'")
- if sys.version_info[0] >= 3:
- pickle_kwargs = dict(encoding=encoding, fix_imports=fix_imports)
- else:
- # Nothing to do on Python 2
- pickle_kwargs = {}
+ pickle_kwargs = dict(encoding=encoding, fix_imports=fix_imports)
# TODO: Use contextlib.ExitStack once we drop Python 2
if hasattr(file, 'read'):
@@ -539,16 +529,10 @@ def save(file, arr, allow_pickle=True, fix_imports=True):
fid = open(file, "wb")
own_fid = True
- if sys.version_info[0] >= 3:
- pickle_kwargs = dict(fix_imports=fix_imports)
- else:
- # Nothing to do on Python 2
- pickle_kwargs = None
-
try:
arr = np.asanyarray(arr)
format.write_array(fid, arr, allow_pickle=allow_pickle,
- pickle_kwargs=pickle_kwargs)
+ pickle_kwargs=dict(fix_imports=fix_imports))
finally:
if own_fid:
fid.close()
@@ -691,7 +675,7 @@ def savez_compressed(file, *args, **kwds):
The ``.npz`` file format is a zipped archive of files named after the
variables they contain. The archive is compressed with
``zipfile.ZIP_DEFLATED`` and each file in the archive contains one variable
- in ``.npy`` format. For a description of the ``.npy`` format, see
+ in ``.npy`` format. For a description of the ``.npy`` format, see
:py:mod:`numpy.lib.format`.
@@ -1375,9 +1359,6 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='',
open(fname, 'wt').close()
fh = np.lib._datasource.open(fname, 'wt', encoding=encoding)
own_fh = True
- # need to convert str to unicode for text io output
- if sys.version_info[0] == 2:
- fh = WriteWrap(fh, encoding or 'latin1')
elif hasattr(fname, 'write'):
# wrap to handle byte output streams
fh = WriteWrap(fname, encoding or 'latin1')
diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py
index 4e62169f4..af4cfa09d 100644
--- a/numpy/lib/recfunctions.py
+++ b/numpy/lib/recfunctions.py
@@ -5,7 +5,6 @@ Most of these functions were initially implemented by John Hunter for
matplotlib. They have been rewritten and extended for convenience.
"""
-import sys
import itertools
import numpy as np
import numpy.ma as ma
@@ -17,9 +16,6 @@ from numpy.lib._iotools import _is_string_like
from numpy.compat import basestring
from numpy.testing import suppress_warnings
-if sys.version_info[0] < 3:
- from future_builtins import zip
-
_check_fill_value = np.ma.core._check_fill_value
@@ -333,12 +329,7 @@ def _izip_records(seqarrays, fill_value=None, flatten=True):
else:
zipfunc = _izip_fields
- if sys.version_info[0] >= 3:
- zip_longest = itertools.zip_longest
- else:
- zip_longest = itertools.izip_longest
-
- for tup in zip_longest(*seqarrays, fillvalue=fill_value):
+ for tup in itertools.zip_longest(*seqarrays, fillvalue=fill_value):
yield tuple(zipfunc(tup))
diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py
index d41a6e541..152322115 100644
--- a/numpy/lib/utils.py
+++ b/numpy/lib/utils.py
@@ -871,11 +871,7 @@ def _lookfor_generate_cache(module, import_modules, regenerate):
# Local import to speed up numpy's import time.
import inspect
- if sys.version_info[0] >= 3:
- # In Python3 stderr, stdout are text files.
- from io import StringIO
- else:
- from StringIO import StringIO
+ from io import StringIO
if module is None:
module = "numpy"