summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2017-03-25 10:11:43 +0000
committerEric Wieser <wieser.eric@gmail.com>2017-03-25 10:36:28 +0000
commitb87fca27261f79be20ab06a222ed2330d60d9f2c (patch)
tree00907ad5fffa7d13f99b1cf60398c624063f807e /numpy/lib
parenta2c4d3230d58a5c4569ab2c7f13bdf9499b71dd4 (diff)
downloadnumpy-b87fca27261f79be20ab06a222ed2330d60d9f2c.tar.gz
MAINT: Remove asbytes where a b prefix would suffice
Since we only need to support python 2, we can remove any case where we just pass a single string literal and use the b prefix instead. What we can't do is transform asbytes("tests %d" % num), because %-formatting fails on bytes in python 3.x < 3.5.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/_iotools.py26
-rw-r--r--numpy/lib/format.py2
-rw-r--r--numpy/lib/npyio.py18
-rw-r--r--numpy/lib/tests/test__datasource.py3
-rw-r--r--numpy/lib/tests/test__iotools.py84
-rw-r--r--numpy/lib/tests/test_format.py8
6 files changed, 70 insertions, 71 deletions
diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py
index 312a9f02a..304bba3d3 100644
--- a/numpy/lib/_iotools.py
+++ b/numpy/lib/_iotools.py
@@ -44,7 +44,7 @@ def _is_bytes_like(obj):
Check whether obj behaves like a bytes object.
"""
try:
- obj + asbytes('')
+ obj + b''
except (TypeError, ValueError):
return False
return True
@@ -189,7 +189,7 @@ class LineSplitter(object):
return lambda input: [_.strip() for _ in method(input)]
#
- def __init__(self, delimiter=None, comments=asbytes('#'), autostrip=True):
+ def __init__(self, delimiter=None, comments=b'#', autostrip=True):
self.comments = comments
# Delimiter is a character
if isinstance(delimiter, unicode):
@@ -218,7 +218,7 @@ class LineSplitter(object):
def _delimited_splitter(self, line):
if self.comments is not None:
line = line.split(self.comments)[0]
- line = line.strip(asbytes(" \r\n"))
+ line = line.strip(b" \r\n")
if not line:
return []
return line.split(self.delimiter)
@@ -227,7 +227,7 @@ class LineSplitter(object):
def _fixedwidth_splitter(self, line):
if self.comments is not None:
line = line.split(self.comments)[0]
- line = line.strip(asbytes("\r\n"))
+ line = line.strip(b"\r\n")
if not line:
return []
fixed = self.delimiter
@@ -434,9 +434,9 @@ def str2bool(value):
"""
value = value.upper()
- if value == asbytes('TRUE'):
+ if value == b'TRUE':
return True
- elif value == asbytes('FALSE'):
+ elif value == b'FALSE':
return False
else:
raise ValueError("Invalid boolean")
@@ -529,7 +529,7 @@ class StringConverter(object):
_mapper.extend([(nx.floating, float, nx.nan),
(complex, _bytes_to_complex, nx.nan + 0j),
(nx.longdouble, nx.longdouble, nx.nan),
- (nx.string_, bytes, asbytes('???'))])
+ (nx.string_, bytes, b'???')])
(_defaulttype, _defaultfunc, _defaultfill) = zip(*_mapper)
@@ -631,7 +631,7 @@ class StringConverter(object):
# None
if default is None:
try:
- default = self.func(asbytes('0'))
+ default = self.func(b'0')
except ValueError:
default = None
dtype = self._getdtype(default)
@@ -676,11 +676,11 @@ class StringConverter(object):
self.func = lambda x: int(float(x))
# Store the list of strings corresponding to missing values.
if missing_values is None:
- self.missing_values = set([asbytes('')])
+ self.missing_values = set([b''])
else:
if isinstance(missing_values, bytes):
- missing_values = missing_values.split(asbytes(","))
- self.missing_values = set(list(missing_values) + [asbytes('')])
+ missing_values = missing_values.split(b",")
+ self.missing_values = set(list(missing_values) + [b''])
#
self._callingfunction = self._strict_call
self.type = self._dtypeortype(dtype)
@@ -801,7 +801,7 @@ class StringConverter(object):
self.iterupgrade(value)
def update(self, func, default=None, testing_value=None,
- missing_values=asbytes(''), locked=False):
+ missing_values=b'', locked=False):
"""
Set StringConverter attributes directly.
@@ -838,7 +838,7 @@ class StringConverter(object):
self.type = self._dtypeortype(self._getdtype(default))
else:
try:
- tester = func(testing_value or asbytes('1'))
+ tester = func(testing_value or b'1')
except (TypeError, ValueError):
tester = None
self.type = self._dtypeortype(self._getdtype(tester))
diff --git a/numpy/lib/format.py b/numpy/lib/format.py
index 633aee675..14dec01d5 100644
--- a/numpy/lib/format.py
+++ b/numpy/lib/format.py
@@ -161,7 +161,7 @@ if sys.version_info[0] >= 3:
else:
import cPickle as pickle
-MAGIC_PREFIX = asbytes('\x93NUMPY')
+MAGIC_PREFIX = b'\x93NUMPY'
MAGIC_LEN = len(MAGIC_PREFIX) + 2
BUFFER_SIZE = 2**18 # size of buffer for reading npz files in bytes
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 54a37fbad..dc1c951e7 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -397,7 +397,7 @@ def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True,
try:
# Code to distinguish from NumPy binary files and pickles.
- _ZIP_PREFIX = asbytes('PK\x03\x04')
+ _ZIP_PREFIX = b'PK\x03\x04'
N = len(format.MAGIC_PREFIX)
magic = fid.read(N)
# If the file size is less than N, we need to make sure not
@@ -856,7 +856,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
# Compile regex for comments beforehand
comments = (re.escape(comment) for comment in comments)
- regex_comments = re.compile(asbytes('|').join(comments))
+ regex_comments = re.compile(b'|'.join(comments))
user_converters = converters
if delimiter is not None:
delimiter = asbytes(delimiter)
@@ -958,7 +958,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
line = asbytes(line)
if comments is not None:
line = regex_comments.split(asbytes(line), maxsplit=1)[0]
- line = line.strip(asbytes('\r\n'))
+ line = line.strip(b'\r\n')
if line:
return line.split(delimiter)
else:
@@ -1576,11 +1576,11 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
if names is True:
if comments in first_line:
first_line = (
- asbytes('').join(first_line.split(comments)[1:]))
+ b''.join(first_line.split(comments)[1:]))
first_values = split_line(first_line)
except StopIteration:
# return an empty array if the datafile is empty
- first_line = asbytes('')
+ first_line = b''
first_values = []
warnings.warn('genfromtxt: Empty input file: "%s"' % fname, stacklevel=2)
@@ -1605,7 +1605,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
if names is True:
names = validate_names([_bytes_to_name(_.strip())
for _ in first_values])
- first_line = asbytes('')
+ first_line = b''
elif _is_string_like(names):
names = validate_names([_.strip() for _ in names.split(',')])
elif names:
@@ -1644,7 +1644,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
user_missing_values = missing_values or ()
# Define the list of missing_values (one column: one list)
- missing_values = [list([asbytes('')]) for _ in range(nbcols)]
+ missing_values = [list([b'']) for _ in range(nbcols)]
# We have a dictionary: process it field by field
if isinstance(user_missing_values, dict):
@@ -1684,7 +1684,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
entry.append(value)
# We have a string : apply it to all entries
elif isinstance(user_missing_values, bytes):
- user_value = user_missing_values.split(asbytes(","))
+ user_value = user_missing_values.split(b",")
for entry in missing_values:
entry.extend(user_value)
# We have something else: apply it to all entries
@@ -1977,7 +1977,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
if usemask and names:
for (name, conv) in zip(names or (), converters):
missing_values = [conv(_) for _ in conv.missing_values
- if _ != asbytes('')]
+ if _ != b'']
for mval in missing_values:
outputmask[name] |= (output[name] == mval)
# Construct the final array
diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py
index f4bece352..f2ad0344a 100644
--- a/numpy/lib/tests/test__datasource.py
+++ b/numpy/lib/tests/test__datasource.py
@@ -5,7 +5,6 @@ import sys
from tempfile import mkdtemp, mkstemp, NamedTemporaryFile
from shutil import rmtree
-from numpy.compat import asbytes
from numpy.testing import (
run_module_suite, TestCase, assert_, SkipTest
)
@@ -53,7 +52,7 @@ http_fakefile = 'fake.txt'
malicious_files = ['/etc/shadow', '../../shadow',
'..\\system.dat', 'c:\\windows\\system.dat']
-magic_line = asbytes('three is the magic number')
+magic_line = b'three is the magic number'
# Utility functions used by many TestCases
diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py
index e0a917a21..db3ee4f90 100644
--- a/numpy/lib/tests/test__iotools.py
+++ b/numpy/lib/tests/test__iotools.py
@@ -5,7 +5,7 @@ import time
from datetime import date
import numpy as np
-from numpy.compat import asbytes, asbytes_nested
+from numpy.compat import asbytes_nested
from numpy.testing import (
run_module_suite, TestCase, assert_, assert_equal, assert_allclose,
assert_raises
@@ -21,7 +21,7 @@ class TestLineSplitter(TestCase):
def test_no_delimiter(self):
"Test LineSplitter w/o delimiter"
- strg = asbytes(" 1 2 3 4 5 # test")
+ strg = b" 1 2 3 4 5 # test"
test = LineSplitter()(strg)
assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5']))
test = LineSplitter('')(strg)
@@ -29,51 +29,51 @@ class TestLineSplitter(TestCase):
def test_space_delimiter(self):
"Test space delimiter"
- strg = asbytes(" 1 2 3 4 5 # test")
- test = LineSplitter(asbytes(' '))(strg)
+ strg = b" 1 2 3 4 5 # test"
+ test = LineSplitter(b' ')(strg)
assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
- test = LineSplitter(asbytes(' '))(strg)
+ test = LineSplitter(b' ')(strg)
assert_equal(test, asbytes_nested(['1 2 3 4', '5']))
def test_tab_delimiter(self):
"Test tab delimiter"
- strg = asbytes(" 1\t 2\t 3\t 4\t 5 6")
- test = LineSplitter(asbytes('\t'))(strg)
+ strg = b" 1\t 2\t 3\t 4\t 5 6"
+ test = LineSplitter(b'\t')(strg)
assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5 6']))
- strg = asbytes(" 1 2\t 3 4\t 5 6")
- test = LineSplitter(asbytes('\t'))(strg)
+ strg = b" 1 2\t 3 4\t 5 6"
+ test = LineSplitter(b'\t')(strg)
assert_equal(test, asbytes_nested(['1 2', '3 4', '5 6']))
def test_other_delimiter(self):
"Test LineSplitter on delimiter"
- strg = asbytes("1,2,3,4,,5")
- test = LineSplitter(asbytes(','))(strg)
+ strg = b"1,2,3,4,,5"
+ test = LineSplitter(b',')(strg)
assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
#
- strg = asbytes(" 1,2,3,4,,5 # test")
- test = LineSplitter(asbytes(','))(strg)
+ strg = b" 1,2,3,4,,5 # test"
+ test = LineSplitter(b',')(strg)
assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
def test_constant_fixed_width(self):
"Test LineSplitter w/ fixed-width fields"
- strg = asbytes(" 1 2 3 4 5 # test")
+ strg = b" 1 2 3 4 5 # test"
test = LineSplitter(3)(strg)
assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5', '']))
#
- strg = asbytes(" 1 3 4 5 6# test")
+ strg = b" 1 3 4 5 6# test"
test = LineSplitter(20)(strg)
assert_equal(test, asbytes_nested(['1 3 4 5 6']))
#
- strg = asbytes(" 1 3 4 5 6# test")
+ strg = b" 1 3 4 5 6# test"
test = LineSplitter(30)(strg)
assert_equal(test, asbytes_nested(['1 3 4 5 6']))
def test_variable_fixed_width(self):
- strg = asbytes(" 1 3 4 5 6# test")
+ strg = b" 1 3 4 5 6# test"
test = LineSplitter((3, 6, 6, 3))(strg)
assert_equal(test, asbytes_nested(['1', '3', '4 5', '6']))
#
- strg = asbytes(" 1 3 4 5 6# test")
+ strg = b" 1 3 4 5 6# test"
test = LineSplitter((6, 6, 9))(strg)
assert_equal(test, asbytes_nested(['1', '3 4', '5 6']))
@@ -157,7 +157,7 @@ class TestStringConverter(TestCase):
assert_equal(converter._status, 0)
# test int
- assert_equal(converter.upgrade(asbytes('0')), 0)
+ assert_equal(converter.upgrade(b'0'), 0)
assert_equal(converter._status, 1)
# On systems where integer defaults to 32-bit, the statuses will be
@@ -166,30 +166,30 @@ class TestStringConverter(TestCase):
status_offset = int(nx.dtype(nx.integer).itemsize < nx.dtype(nx.int64).itemsize)
# test int > 2**32
- assert_equal(converter.upgrade(asbytes('17179869184')), 17179869184)
+ assert_equal(converter.upgrade(b'17179869184'), 17179869184)
assert_equal(converter._status, 1 + status_offset)
# test float
- assert_allclose(converter.upgrade(asbytes('0.')), 0.0)
+ assert_allclose(converter.upgrade(b'0.'), 0.0)
assert_equal(converter._status, 2 + status_offset)
# test complex
- assert_equal(converter.upgrade(asbytes('0j')), complex('0j'))
+ assert_equal(converter.upgrade(b'0j'), complex('0j'))
assert_equal(converter._status, 3 + status_offset)
# test str
- assert_equal(converter.upgrade(asbytes('a')), asbytes('a'))
+ assert_equal(converter.upgrade(b'a'), b'a')
assert_equal(converter._status, len(converter._mapper) - 1)
def test_missing(self):
"Tests the use of missing values."
- converter = StringConverter(missing_values=(asbytes('missing'),
- asbytes('missed')))
- converter.upgrade(asbytes('0'))
- assert_equal(converter(asbytes('0')), 0)
- assert_equal(converter(asbytes('')), converter.default)
- assert_equal(converter(asbytes('missing')), converter.default)
- assert_equal(converter(asbytes('missed')), converter.default)
+ converter = StringConverter(missing_values=(b'missing',
+ b'missed'))
+ converter.upgrade(b'0')
+ assert_equal(converter(b'0'), 0)
+ assert_equal(converter(b''), converter.default)
+ assert_equal(converter(b'missing'), converter.default)
+ assert_equal(converter(b'missed'), converter.default)
try:
converter('miss')
except ValueError:
@@ -200,11 +200,11 @@ class TestStringConverter(TestCase):
dateparser = _bytes_to_date
StringConverter.upgrade_mapper(dateparser, date(2000, 1, 1))
convert = StringConverter(dateparser, date(2000, 1, 1))
- test = convert(asbytes('2001-01-01'))
+ test = convert(b'2001-01-01')
assert_equal(test, date(2001, 1, 1))
- test = convert(asbytes('2009-01-01'))
+ test = convert(b'2009-01-01')
assert_equal(test, date(2009, 1, 1))
- test = convert(asbytes(''))
+ test = convert(b'')
assert_equal(test, date(2000, 1, 1))
def test_string_to_object(self):
@@ -215,43 +215,43 @@ class TestStringConverter(TestCase):
def test_keep_default(self):
"Make sure we don't lose an explicit default"
- converter = StringConverter(None, missing_values=asbytes(''),
+ converter = StringConverter(None, missing_values=b'',
default=-999)
- converter.upgrade(asbytes('3.14159265'))
+ converter.upgrade(b'3.14159265')
assert_equal(converter.default, -999)
assert_equal(converter.type, np.dtype(float))
#
converter = StringConverter(
- None, missing_values=asbytes(''), default=0)
- converter.upgrade(asbytes('3.14159265'))
+ None, missing_values=b'', default=0)
+ converter.upgrade(b'3.14159265')
assert_equal(converter.default, 0)
assert_equal(converter.type, np.dtype(float))
def test_keep_default_zero(self):
"Check that we don't lose a default of 0"
converter = StringConverter(int, default=0,
- missing_values=asbytes("N/A"))
+ missing_values=b"N/A")
assert_equal(converter.default, 0)
def test_keep_missing_values(self):
"Check that we're not losing missing values"
converter = StringConverter(int, default=0,
- missing_values=asbytes("N/A"))
+ missing_values=b"N/A")
assert_equal(
converter.missing_values, set(asbytes_nested(['', 'N/A'])))
def test_int64_dtype(self):
"Check that int64 integer types can be specified"
converter = StringConverter(np.int64, default=0)
- val = asbytes("-9223372036854775807")
+ val = b"-9223372036854775807"
assert_(converter(val) == -9223372036854775807)
- val = asbytes("9223372036854775807")
+ val = b"9223372036854775807"
assert_(converter(val) == 9223372036854775807)
def test_uint64_dtype(self):
"Check that uint64 integer types can be specified"
converter = StringConverter(np.uint64, default=0)
- val = asbytes("9223372043271415339")
+ val = b"9223372043271415339"
assert_(converter(val) == 9223372043271415339)
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py
index 7cc72e775..77ac5942a 100644
--- a/numpy/lib/tests/test_format.py
+++ b/numpy/lib/tests/test_format.py
@@ -284,7 +284,7 @@ import warnings
from io import BytesIO
import numpy as np
-from numpy.compat import asbytes, asbytes_nested, sixu
+from numpy.compat import asbytes_nested, sixu
from numpy.testing import (
run_module_suite, assert_, assert_array_equal, assert_raises, raises,
dec, SkipTest
@@ -546,7 +546,7 @@ def test_pickle_python2_python3():
xrange = __builtin__.xrange
expected = np.array([None, xrange, sixu('\u512a\u826f'),
- asbytes('\xe4\xb8\x8d\xe8\x89\xaf')],
+ b'\xe4\xb8\x8d\xe8\x89\xaf'],
dtype=object)
for fname in ['py2-objarr.npy', 'py2-objarr.npz',
@@ -778,11 +778,11 @@ def test_bad_header():
# header of length less than 2 should fail
s = BytesIO()
assert_raises(ValueError, format.read_array_header_1_0, s)
- s = BytesIO(asbytes('1'))
+ s = BytesIO(b'1')
assert_raises(ValueError, format.read_array_header_1_0, s)
# header shorter than indicated size should fail
- s = BytesIO(asbytes('\x01\x00'))
+ s = BytesIO(b'\x01\x00')
assert_raises(ValueError, format.read_array_header_1_0, s)
# headers without the exact keys required should fail