diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-04-06 07:42:08 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-04-06 07:42:08 -0700 |
commit | 49a8902a673d6fb2ba9ca446fc652aa9d2e55e1b (patch) | |
tree | e71c0d8f6123307860a58796ed840bf32526b3fe /numpy/lib/tests | |
parent | 3c8fc14665548c71a9cd144b2e16d9309a92e255 (diff) | |
parent | 4394515cd5632a7f110993ff75033d407d10861d (diff) | |
download | numpy-49a8902a673d6fb2ba9ca446fc652aa9d2e55e1b.tar.gz |
Merge pull request #3191 from charris/2to3-apply-imports-fixer
2to3: Apply `imports` fixer.
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r-- | numpy/lib/tests/test__datasource.py | 13 | ||||
-rw-r--r-- | numpy/lib/tests/test__iotools.py | 11 | ||||
-rw-r--r-- | numpy/lib/tests/test_format.py | 48 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 461 | ||||
-rw-r--r-- | numpy/lib/tests/test_regression.py | 7 | ||||
-rw-r--r-- | numpy/lib/tests/test_utils.py | 6 |
6 files changed, 277 insertions, 269 deletions
diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py index e784e4296..ccd7405fe 100644 --- a/numpy/lib/tests/test__datasource.py +++ b/numpy/lib/tests/test__datasource.py @@ -1,17 +1,20 @@ from __future__ import division, absolute_import import os +import urllib2 +import sys +import numpy.lib._datasource as datasource from tempfile import mkdtemp, mkstemp, NamedTemporaryFile from shutil import rmtree -from urlparse import urlparse from urllib2 import URLError -import urllib2 - +from numpy.compat import asbytes from numpy.testing import * -from numpy.compat import asbytes -import numpy.lib._datasource as datasource +if sys.version_info[0] >= 3: + from urllib.parse import urlparse +else: + from urlparse import urlparse def urlopen_stub(url, data=None): '''Stub to replace urlopen for testing.''' diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py index ebb01ad3a..1cd9a25da 100644 --- a/numpy/lib/tests/test__iotools.py +++ b/numpy/lib/tests/test__iotools.py @@ -1,22 +1,13 @@ from __future__ import division, absolute_import import sys - -if sys.version_info[0] >= 3: - from io import BytesIO - def StringIO(s=""): - return BytesIO(asbytes(s)) -else: - from StringIO import StringIO - -from datetime import date import time +from datetime import date import numpy as np from numpy.lib._iotools import LineSplitter, NameValidator, StringConverter, \ has_nested_fields, easy_dtype, flatten_dtype from numpy.testing import * - from numpy.compat import asbytes, asbytes_nested class TestLineSplitter(TestCase): diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 5c07da1c3..aae3b1ba8 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -5,10 +5,7 @@ r''' Test the .npy file format. Set up: >>> import sys - >>> if sys.version_info[0] >= 3: - ... from io import BytesIO as StringIO - ... else: - ... from cStringIO import StringIO + >>> from io import BytesIO >>> from numpy.lib import format >>> >>> scalars = [ @@ -101,19 +98,19 @@ Test the magic string writing. Test the magic string reading. - >>> format.read_magic(StringIO(format.magic(1, 0))) + >>> format.read_magic(BytesIO(format.magic(1, 0))) (1, 0) - >>> format.read_magic(StringIO(format.magic(0, 0))) + >>> format.read_magic(BytesIO(format.magic(0, 0))) (0, 0) - >>> format.read_magic(StringIO(format.magic(255, 255))) + >>> format.read_magic(BytesIO(format.magic(255, 255))) (255, 255) - >>> format.read_magic(StringIO(format.magic(2, 5))) + >>> format.read_magic(BytesIO(format.magic(2, 5))) (2, 5) Test the header writing. >>> for arr in basic_arrays + record_arrays: - ... f = StringIO() + ... f = BytesIO() ... format.write_array_header_1_0(f, arr) # XXX: arr is not a dict, items gets called on it ... print repr(f.getvalue()) ... @@ -279,22 +276,15 @@ Test the header writing. "\x16\x02{'descr': [('x', '>i4', (2,)),\n ('Info',\n [('value', '>c16'),\n ('y2', '>f8'),\n ('Info2',\n [('name', '|S2'),\n ('value', '>c16', (2,)),\n ('y3', '>f8', (2,)),\n ('z3', '>u4', (2,))]),\n ('name', '|S2'),\n ('z2', '|b1')]),\n ('color', '|S2'),\n ('info', [('Name', '>U8'), ('Value', '>c16')]),\n ('y', '>f8', (2, 2)),\n ('z', '|u1')],\n 'fortran_order': False,\n 'shape': (2,)} \n" ''' - import sys import os import shutil import tempfile - -if sys.version_info[0] >= 3: - from io import BytesIO as StringIO -else: - from cStringIO import StringIO +from io import BytesIO import numpy as np from numpy.testing import * - from numpy.lib import format - from numpy.compat import asbytes, asbytes_nested @@ -416,9 +406,9 @@ record_arrays = [ ] def roundtrip(arr): - f = StringIO() + f = BytesIO() format.write_array(f, arr) - f2 = StringIO(f.getvalue()) + f2 = BytesIO(f.getvalue()) arr2 = format.read_array(f2) return arr2 @@ -469,7 +459,7 @@ def test_memmap_roundtrip(): def test_write_version_1_0(): - f = StringIO() + f = BytesIO() arr = np.arange(1) # These should pass. format.write_array(f, arr, version=(1, 0)) @@ -513,12 +503,12 @@ malformed_magic = asbytes_nested([ def test_read_magic_bad_magic(): for magic in malformed_magic: - f = StringIO(magic) + f = BytesIO(magic) yield raises(ValueError)(format.read_magic), f def test_read_version_1_0_bad_magic(): for magic in bad_version_magic + malformed_magic: - f = StringIO(magic) + f = BytesIO(magic) yield raises(ValueError)(format.read_array), f def test_bad_magic_args(): @@ -528,29 +518,29 @@ def test_bad_magic_args(): assert_raises(ValueError, format.magic, 1, 256) def test_large_header(): - s = StringIO() + s = BytesIO() d = {'a':1,'b':2} format.write_array_header_1_0(s,d) - s = StringIO() + s = BytesIO() d = {'a':1,'b':2,'c':'x'*256*256} assert_raises(ValueError, format.write_array_header_1_0, s, d) def test_bad_header(): # header of length less than 2 should fail - s = StringIO() + s = BytesIO() assert_raises(ValueError, format.read_array_header_1_0, s) - s = StringIO(asbytes('1')) + s = BytesIO(asbytes('1')) assert_raises(ValueError, format.read_array_header_1_0, s) # header shorter than indicated size should fail - s = StringIO(asbytes('\x01\x00')) + s = BytesIO(asbytes('\x01\x00')) assert_raises(ValueError, format.read_array_header_1_0, s) # headers without the exact keys required should fail d = {"shape":(1,2), "descr":"x"} - s = StringIO() + s = BytesIO() format.write_array_header_1_0(s,d) assert_raises(ValueError, format.read_array_header_1_0, s) @@ -558,7 +548,7 @@ def test_bad_header(): "fortran_order":False, "descr":"x", "extrakey":-1} - s = StringIO() + s = BytesIO() format.write_array_header_1_0(s,d) assert_raises(ValueError, format.read_array_header_1_0, s) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index ab990976e..6b5173890 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -4,31 +4,43 @@ import sys import gzip import os import threading -from tempfile import mkstemp, NamedTemporaryFile import time -from datetime import datetime import warnings import gc +from tempfile import mkstemp, NamedTemporaryFile +from io import BytesIO +from datetime import datetime from numpy.testing.utils import WarningManager import numpy as np import numpy.ma as ma from numpy.lib._iotools import ConverterError, ConverterLockError, \ ConversionWarning -from numpy.compat import asbytes, asbytes_nested, bytes - +from numpy.compat import asbytes, asbytes_nested, bytes, asstr from nose import SkipTest from numpy.ma.testutils import (TestCase, assert_equal, assert_array_equal, assert_raises, run_module_suite) from numpy.testing import assert_warns, assert_, build_err_msg -if sys.version_info[0] >= 3: - from io import BytesIO - def StringIO(s=""): - return BytesIO(asbytes(s)) -else: - from StringIO import StringIO - BytesIO = StringIO + +class TextIO(BytesIO): + """Helper IO class. + + Writes encode strings to bytes if needed, reads return bytes. + This makes it easier to emulate files opened in binary mode + without needing to explicitly convert strings to bytes in + setting up the test data. + + """ + def __init__(self, s=""): + BytesIO.__init__(self, asbytes(s)) + + def write(self, s): + BytesIO.write(self, asbytes(s)) + + def writelines(self, lines): + BytesIO.writelines(self, [asbytes(s) for s in lines]) + MAJVER, MINVER = sys.version_info[:2] @@ -73,7 +85,7 @@ class RoundtripTest(object): target_file = NamedTemporaryFile() load_file = target_file.name else: - target_file = StringIO() + target_file = BytesIO() load_file = target_file arr = args @@ -135,7 +147,7 @@ class TestSavezLoad(RoundtripTest, TestCase): def test_named_arrays(self): a = np.array([[1, 2], [3, 4]], float) b = np.array([[1 + 2j, 2 + 7j], [3 - 6j, 4 + 12j]], complex) - c = StringIO() + c = BytesIO() np.savez(c, file_a=a, file_b=b) c.seek(0) l = np.load(c) @@ -228,95 +240,94 @@ class TestSaveTxt(TestCase): def test_array(self): a = np.array([[1, 2], [3, 4]], float) fmt = "%.18e" - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt=fmt) c.seek(0) assert_equal(c.readlines(), - asbytes_nested( - [(fmt + ' ' + fmt + '\n') % (1, 2), - (fmt + ' ' + fmt + '\n') % (3, 4)])) + [asbytes((fmt + ' ' + fmt + '\n') % (1, 2)), + asbytes((fmt + ' ' + fmt + '\n') % (3, 4))]) a = np.array([[1, 2], [3, 4]], int) - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt='%d') c.seek(0) - assert_equal(c.readlines(), asbytes_nested(['1 2\n', '3 4\n'])) + assert_equal(c.readlines(), [b'1 2\n', b'3 4\n']) def test_1D(self): a = np.array([1, 2, 3, 4], int) - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt='%d') c.seek(0) lines = c.readlines() - assert_equal(lines, asbytes_nested(['1\n', '2\n', '3\n', '4\n'])) + assert_equal(lines, [b'1\n', b'2\n', b'3\n', b'4\n']) def test_record(self): a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt='%d') c.seek(0) - assert_equal(c.readlines(), asbytes_nested(['1 2\n', '3 4\n'])) + assert_equal(c.readlines(), [b'1 2\n', b'3 4\n']) def test_delimiter(self): a = np.array([[1., 2.], [3., 4.]]) - c = StringIO() - np.savetxt(c, a, delimiter=asbytes(','), fmt='%d') + c = BytesIO() + np.savetxt(c, a, delimiter=',', fmt='%d') c.seek(0) - assert_equal(c.readlines(), asbytes_nested(['1,2\n', '3,4\n'])) + assert_equal(c.readlines(), [b'1,2\n', b'3,4\n']) def test_format(self): a = np.array([(1, 2), (3, 4)]) - c = StringIO() + c = BytesIO() # Sequence of formats np.savetxt(c, a, fmt=['%02d', '%3.1f']) c.seek(0) - assert_equal(c.readlines(), asbytes_nested(['01 2.0\n', '03 4.0\n'])) + assert_equal(c.readlines(), [b'01 2.0\n', b'03 4.0\n']) # A single multiformat string - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt='%02d : %3.1f') c.seek(0) lines = c.readlines() - assert_equal(lines, asbytes_nested(['01 : 2.0\n', '03 : 4.0\n'])) + assert_equal(lines, [b'01 : 2.0\n', b'03 : 4.0\n']) # Specify delimiter, should be overiden - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt='%02d : %3.1f', delimiter=',') c.seek(0) lines = c.readlines() - assert_equal(lines, asbytes_nested(['01 : 2.0\n', '03 : 4.0\n'])) + assert_equal(lines, [b'01 : 2.0\n', b'03 : 4.0\n']) def test_header_footer(self): """ Test the functionality of the header and footer keyword argument. """ - c = StringIO() + c = BytesIO() a = np.array([(1, 2), (3, 4)], dtype=np.int) test_header_footer = 'Test header / footer' # Test the header keyword argument np.savetxt(c, a, fmt='%1d', header=test_header_footer) c.seek(0) assert_equal(c.read(), - asbytes('# ' + test_header_footer +'\n1 2\n3 4\n' )) + asbytes('# ' + test_header_footer + '\n1 2\n3 4\n')) # Test the footer keyword argument - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt='%1d', footer=test_header_footer) c.seek(0) assert_equal(c.read(), asbytes('1 2\n3 4\n# ' + test_header_footer + '\n')) # Test the commentstr keyword argument used on the header - c = StringIO() + c = BytesIO() commentstr = '% ' - np.savetxt(c, a, fmt='%1d', header=test_header_footer, - comments=commentstr) + np.savetxt(c, a, fmt='%1d', + header=test_header_footer, comments=commentstr) c.seek(0) assert_equal(c.read(), asbytes(commentstr + test_header_footer + '\n' + '1 2\n3 4\n')) # Test the commentstr keyword argument used on the footer - c = StringIO() + c = BytesIO() commentstr = '% ' - np.savetxt(c, a, fmt='%1d', footer=test_header_footer, - comments=commentstr) + np.savetxt(c, a, fmt='%1d', + footer=test_header_footer, comments=commentstr) c.seek(0) assert_equal(c.read(), asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n')) @@ -340,29 +351,29 @@ class TestSaveTxt(TestCase): im = np.e a[:] = re + 1.0j * im # One format only - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt=' %+.3e') c.seek(0) lines = c.readlines() - _assert_floatstr_lines_equal(lines, asbytes_nested([ - ' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n', - ' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n'])) + _assert_floatstr_lines_equal(lines, + [b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n', + b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n']) # One format for each real and imaginary part - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt=' %+.3e' * 2 * ncols) c.seek(0) lines = c.readlines() - _assert_floatstr_lines_equal(lines, asbytes_nested([ - ' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n', - ' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n'])) + _assert_floatstr_lines_equal(lines, + [b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n', + b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n']) # One format for each complex number - c = StringIO() + c = BytesIO() np.savetxt(c, a, fmt=['(%.3e%+.3ej)'] * ncols) c.seek(0) lines = c.readlines() - _assert_floatstr_lines_equal(lines, asbytes_nested([ - '(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n', - '(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n'])) + _assert_floatstr_lines_equal(lines, + [b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n', + b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n']) def _assert_floatstr_lines_equal(actual_lines, expected_lines): @@ -387,15 +398,15 @@ def _assert_floatstr_lines_equal(actual_lines, expected_lines): class TestLoadTxt(TestCase): def test_record(self): - c = StringIO() - c.write(asbytes('1 2\n3 4')) + c = TextIO() + c.write('1 2\n3 4') c.seek(0) x = np.loadtxt(c, dtype=[('x', np.int32), ('y', np.int32)]) a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) assert_array_equal(x, a) - d = StringIO() - d.write(asbytes('M 64.0 75.0\nF 25.0 60.0')) + d = TextIO() + d.write('M 64.0 75.0\nF 25.0 60.0') d.seek(0) mydescriptor = {'names': ('gender', 'age', 'weight'), 'formats': ('S1', @@ -406,8 +417,8 @@ class TestLoadTxt(TestCase): assert_array_equal(y, b) def test_array(self): - c = StringIO() - c.write(asbytes('1 2\n3 4')) + c = TextIO() + c.write('1 2\n3 4') c.seek(0) x = np.loadtxt(c, dtype=int) @@ -420,23 +431,23 @@ class TestLoadTxt(TestCase): assert_array_equal(x, a) def test_1D(self): - c = StringIO() - c.write(asbytes('1\n2\n3\n4\n')) + c = TextIO() + c.write('1\n2\n3\n4\n') c.seek(0) x = np.loadtxt(c, dtype=int) a = np.array([1, 2, 3, 4], int) assert_array_equal(x, a) - c = StringIO() - c.write(asbytes('1,2,3,4\n')) + c = TextIO() + c.write('1,2,3,4\n') c.seek(0) x = np.loadtxt(c, dtype=int, delimiter=',') a = np.array([1, 2, 3, 4], int) assert_array_equal(x, a) def test_missing(self): - c = StringIO() - c.write(asbytes('1,2,3,,5\n')) + c = TextIO() + c.write('1,2,3,,5\n') c.seek(0) x = np.loadtxt(c, dtype=int, delimiter=',', \ converters={3:lambda s: int(s or - 999)}) @@ -444,8 +455,8 @@ class TestLoadTxt(TestCase): assert_array_equal(x, a) def test_converters_with_usecols(self): - c = StringIO() - c.write(asbytes('1,2,3,,5\n6,7,8,9,10\n')) + c = TextIO() + c.write('1,2,3,,5\n6,7,8,9,10\n') c.seek(0) x = np.loadtxt(c, dtype=int, delimiter=',', \ converters={3:lambda s: int(s or - 999)}, \ @@ -454,8 +465,8 @@ class TestLoadTxt(TestCase): assert_array_equal(x, a) def test_comments(self): - c = StringIO() - c.write(asbytes('# comment\n1,2,3,5\n')) + c = TextIO() + c.write('# comment\n1,2,3,5\n') c.seek(0) x = np.loadtxt(c, dtype=int, delimiter=',', \ comments='#') @@ -463,16 +474,16 @@ class TestLoadTxt(TestCase): assert_array_equal(x, a) def test_skiprows(self): - c = StringIO() - c.write(asbytes('comment\n1,2,3,5\n')) + c = TextIO() + c.write('comment\n1,2,3,5\n') c.seek(0) x = np.loadtxt(c, dtype=int, delimiter=',', \ skiprows=1) a = np.array([1, 2, 3, 5], int) assert_array_equal(x, a) - c = StringIO() - c.write(asbytes('# comment\n1,2,3,5\n')) + c = TextIO() + c.write('# comment\n1,2,3,5\n') c.seek(0) x = np.loadtxt(c, dtype=int, delimiter=',', \ skiprows=1) @@ -481,14 +492,14 @@ class TestLoadTxt(TestCase): def test_usecols(self): a = np.array([[1, 2], [3, 4]], float) - c = StringIO() + c = BytesIO() np.savetxt(c, a) c.seek(0) x = np.loadtxt(c, dtype=float, usecols=(1,)) assert_array_equal(x, a[:, 1]) a = np.array([[1, 2, 3], [3, 4, 5]], float) - c = StringIO() + c = BytesIO() np.savetxt(c, a) c.seek(0) x = np.loadtxt(c, dtype=float, usecols=(1, 2)) @@ -503,16 +514,16 @@ class TestLoadTxt(TestCase): data = '''JOE 70.1 25.3 BOB 60.5 27.9 ''' - c = StringIO(data) + c = TextIO(data) names = ['stid', 'temp'] dtypes = ['S4', 'f8'] arr = np.loadtxt(c, usecols=(0, 2), dtype=zip(names, dtypes)) - assert_equal(arr['stid'], asbytes_nested(["JOE", "BOB"])) + assert_equal(arr['stid'], [b"JOE", b"BOB"]) assert_equal(arr['temp'], [25.3, 27.9]) def test_fancy_dtype(self): - c = StringIO() - c.write(asbytes('1,2,3.0\n4,5,6.0\n')) + c = TextIO() + c.write('1,2,3.0\n4,5,6.0\n') c.seek(0) dt = np.dtype([('x', int), ('y', [('t', int), ('s', float)])]) x = np.loadtxt(c, dtype=dt, delimiter=',') @@ -520,7 +531,7 @@ class TestLoadTxt(TestCase): assert_array_equal(x, a) def test_shaped_dtype(self): - c = StringIO("aaaa 1.0 8.0 1 2 3 4 5 6") + c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6") dt = np.dtype([('name', 'S4'), ('x', float), ('y', float), ('block', int, (2, 3))]) x = np.loadtxt(c, dtype=dt) @@ -529,7 +540,7 @@ class TestLoadTxt(TestCase): assert_array_equal(x, a) def test_3d_shaped_dtype(self): - c = StringIO("aaaa 1.0 8.0 1 2 3 4 5 6 7 8 9 10 11 12") + c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6 7 8 9 10 11 12") dt = np.dtype([('name', 'S4'), ('x', float), ('y', float), ('block', int, (2, 2, 3))]) x = np.loadtxt(c, dtype=dt) @@ -543,7 +554,7 @@ class TestLoadTxt(TestCase): try: warnings.filterwarnings("ignore", message="loadtxt: Empty input file:") - c = StringIO() + c = TextIO() x = np.loadtxt(c) assert_equal(x.shape, (0,)) x = np.loadtxt(c, dtype=np.int64) @@ -554,8 +565,8 @@ class TestLoadTxt(TestCase): def test_unused_converter(self): - c = StringIO() - c.writelines([asbytes('1 21\n'), asbytes('3 42\n')]) + c = TextIO() + c.writelines(['1 21\n', '3 42\n']) c.seek(0) data = np.loadtxt(c, usecols=(1,), converters={0: lambda s: int(s, 16)}) @@ -570,12 +581,12 @@ class TestLoadTxt(TestCase): "Test using an explicit dtype with an object" from datetime import date import time - data = asbytes(""" 1; 2001-01-01 - 2; 2002-01-31 """) + data = """ 1; 2001-01-01 + 2; 2002-01-31 """ ndtype = [('idx', int), ('code', np.object)] func = lambda s: strptime(s.strip(), "%Y-%m-%d") converters = {1: func} - test = np.loadtxt(StringIO(data), delimiter=";", dtype=ndtype, + test = np.loadtxt(TextIO(data), delimiter=";", dtype=ndtype, converters=converters) control = np.array([(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))], dtype=ndtype) @@ -583,23 +594,23 @@ class TestLoadTxt(TestCase): def test_uint64_type(self): tgt = (9223372043271415339, 9223372043271415853) - c = StringIO() - c.write(asbytes("%s %s" % tgt)) + c = TextIO() + c.write("%s %s" % tgt) c.seek(0) res = np.loadtxt(c, dtype=np.uint64) assert_equal(res, tgt) def test_int64_type(self): tgt = (-9223372036854775807, 9223372036854775807) - c = StringIO() - c.write(asbytes("%s %s" % tgt)) + c = TextIO() + c.write("%s %s" % tgt) c.seek(0) res = np.loadtxt(c, dtype=np.int64) assert_equal(res, tgt) def test_universal_newline(self): f, name = mkstemp() - os.write(f, asbytes('1 21\r3 42\r')) + os.write(f, b'1 21\r3 42\r') os.close(f) try: @@ -609,29 +620,29 @@ class TestLoadTxt(TestCase): os.unlink(name) def test_empty_field_after_tab(self): - c = StringIO() - c.write(asbytes('1 \t2 \t3\tstart \n4\t5\t6\t \n7\t8\t9.5\t')) + c = TextIO() + c.write('1 \t2 \t3\tstart \n4\t5\t6\t \n7\t8\t9.5\t') c.seek(0) dt = { 'names': ('x', 'y', 'z', 'comment'), 'formats': ('<i4', '<i4', '<f4', '|S8')} x = np.loadtxt(c, dtype=dt, delimiter='\t') - a = np.array([asbytes('start '), asbytes(' '), asbytes('')]) + a = np.array([b'start ', b' ', b'']) assert_array_equal(x['comment'], a) def test_structure_unpack(self): - txt = StringIO(asbytes("M 21 72\nF 35 58")) + txt = TextIO("M 21 72\nF 35 58") dt = { 'names': ('a', 'b', 'c'), 'formats': ('|S1', '<i4', '<f4')} a, b, c = np.loadtxt(txt, dtype=dt, unpack=True) assert_(a.dtype.str == '|S1') assert_(b.dtype.str == '<i4') assert_(c.dtype.str == '<f4') - assert_array_equal(a, np.array([asbytes('M'), asbytes('F')])) + assert_array_equal(a, np.array([b'M', b'F'])) assert_array_equal(b, np.array([21, 35])) assert_array_equal(c, np.array([ 72., 58.])) def test_ndmin_keyword(self): - c = StringIO() - c.write(asbytes('1,2,3\n4,5,6')) + c = TextIO() + c.write('1,2,3\n4,5,6') c.seek(0) assert_raises(ValueError, np.loadtxt, c, ndmin=3) c.seek(0) @@ -640,8 +651,9 @@ class TestLoadTxt(TestCase): x = np.loadtxt(c, dtype=int, delimiter=',', ndmin=1) a = np.array([[1, 2, 3], [4, 5, 6]]) assert_array_equal(x, a) - d = StringIO() - d.write(asbytes('0,1,2')) + + d = TextIO() + d.write('0,1,2') d.seek(0) x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=2) assert_(x.shape == (1, 3)) @@ -651,8 +663,9 @@ class TestLoadTxt(TestCase): d.seek(0) x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=0) assert_(x.shape == (3,)) - e = StringIO() - e.write(asbytes('0\n1\n2')) + + e = TextIO() + e.write('0\n1\n2') e.seek(0) x = np.loadtxt(e, dtype=int, delimiter=',', ndmin=2) assert_(x.shape == (3, 1)) @@ -669,7 +682,7 @@ class TestLoadTxt(TestCase): try: warnings.filterwarnings("ignore", message="loadtxt: Empty input file:") - f = StringIO() + f = TextIO() assert_(np.loadtxt(f, ndmin=2).shape == (0, 1,)) assert_(np.loadtxt(f, ndmin=1).shape == (0,)) finally: @@ -678,15 +691,16 @@ class TestLoadTxt(TestCase): def test_generator_source(self): def count(): for i in range(10): - yield asbytes("%d" % i) + yield "%d" % i res = np.loadtxt(count()) assert_array_equal(res, np.arange(10)) class Testfromregex(TestCase): + # np.fromregex expects files opened in binary mode. def test_record(self): - c = StringIO() - c.write(asbytes('1.312 foo\n1.534 bar\n4.444 qux')) + c = TextIO() + c.write('1.312 foo\n1.534 bar\n4.444 qux') c.seek(0) dt = [('num', np.float64), ('val', 'S3')] @@ -696,8 +710,8 @@ class Testfromregex(TestCase): assert_array_equal(x, a) def test_record_2(self): - c = StringIO() - c.write(asbytes('1312 foo\n1534 bar\n4444 qux')) + c = TextIO() + c.write('1312 foo\n1534 bar\n4444 qux') c.seek(0) dt = [('num', np.int32), ('val', 'S3')] @@ -707,8 +721,8 @@ class Testfromregex(TestCase): assert_array_equal(x, a) def test_record_3(self): - c = StringIO() - c.write(asbytes('1312 foo\n1534 bar\n4444 qux')) + c = TextIO() + c.write('1312 foo\n1534 bar\n4444 qux') c.seek(0) dt = [('num', np.float64)] @@ -724,13 +738,13 @@ class TestFromTxt(TestCase): # def test_record(self): "Test w/ explicit dtype" - data = StringIO(asbytes('1 2\n3 4')) + data = TextIO('1 2\n3 4') # data.seek(0) test = np.ndfromtxt(data, dtype=[('x', np.int32), ('y', np.int32)]) control = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) assert_equal(test, control) # - data = StringIO('M 64.0 75.0\nF 25.0 60.0') + data = TextIO('M 64.0 75.0\nF 25.0 60.0') # data.seek(0) descriptor = {'names': ('gender', 'age', 'weight'), 'formats': ('S1', 'i4', 'f4')} @@ -741,7 +755,7 @@ class TestFromTxt(TestCase): def test_array(self): "Test outputing a standard ndarray" - data = StringIO('1 2\n3 4') + data = TextIO('1 2\n3 4') control = np.array([[1, 2], [3, 4]], dtype=int) test = np.ndfromtxt(data, dtype=int) assert_array_equal(test, control) @@ -755,36 +769,36 @@ class TestFromTxt(TestCase): "Test squeezing to 1D" control = np.array([1, 2, 3, 4], int) # - data = StringIO('1\n2\n3\n4\n') + data = TextIO('1\n2\n3\n4\n') test = np.ndfromtxt(data, dtype=int) assert_array_equal(test, control) # - data = StringIO('1,2,3,4\n') - test = np.ndfromtxt(data, dtype=int, delimiter=asbytes(',')) + data = TextIO('1,2,3,4\n') + test = np.ndfromtxt(data, dtype=int, delimiter=',') assert_array_equal(test, control) def test_comments(self): "Test the stripping of comments" control = np.array([1, 2, 3, 5], int) # Comment on its own line - data = StringIO('# comment\n1,2,3,5\n') - test = np.ndfromtxt(data, dtype=int, delimiter=asbytes(','), comments=asbytes('#')) + data = TextIO('# comment\n1,2,3,5\n') + test = np.ndfromtxt(data, dtype=int, delimiter=',', comments='#') assert_equal(test, control) # Comment at the end of a line - data = StringIO('1,2,3,5# comment\n') - test = np.ndfromtxt(data, dtype=int, delimiter=asbytes(','), comments=asbytes('#')) + data = TextIO('1,2,3,5# comment\n') + test = np.ndfromtxt(data, dtype=int, delimiter=',', comments='#') assert_equal(test, control) def test_skiprows(self): "Test row skipping" control = np.array([1, 2, 3, 5], int) - kwargs = dict(dtype=int, delimiter=asbytes(',')) + kwargs = dict(dtype=int, delimiter=',') # - data = StringIO('comment\n1,2,3,5\n') + data = TextIO('comment\n1,2,3,5\n') test = np.ndfromtxt(data, skip_header=1, **kwargs) assert_equal(test, control) # - data = StringIO('# comment\n1,2,3,5\n') + data = TextIO('# comment\n1,2,3,5\n') test = np.loadtxt(data, skiprows=1, **kwargs) assert_equal(test, control) @@ -794,7 +808,7 @@ class TestFromTxt(TestCase): data.extend(["%i,%3.1f,%03s" % (i, i, i) for i in range(51)]) data[-1] = "99,99" kwargs = dict(delimiter=",", names=True, skip_header=5, skip_footer=10) - test = np.genfromtxt(StringIO(asbytes("\n".join(data))), **kwargs) + test = np.genfromtxt(TextIO("\n".join(data)), **kwargs) ctrl = np.array([("%f" % i, "%f" % i, "%f" % i) for i in range(41)], dtype=[(_, float) for _ in "ABC"]) assert_equal(test, ctrl) @@ -807,19 +821,19 @@ class TestFromTxt(TestCase): warnings.filterwarnings("ignore") # Footer too small to get rid of all invalid values assert_raises(ValueError, np.genfromtxt, - StringIO(basestr), skip_footer=1) + TextIO(basestr), skip_footer=1) # except ValueError: # pass - a = np.genfromtxt(StringIO(basestr), skip_footer=1, invalid_raise=False) + a = np.genfromtxt(TextIO(basestr), skip_footer=1, invalid_raise=False) assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])) # - a = np.genfromtxt(StringIO(basestr), skip_footer=3) + a = np.genfromtxt(TextIO(basestr), skip_footer=3) assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])) # basestr = '1 1\n2 \n3 3\n4 4\n5 \n6 6\n7 7\n' - a = np.genfromtxt(StringIO(basestr), skip_footer=1, invalid_raise=False) + a = np.genfromtxt(TextIO(basestr), skip_footer=1, invalid_raise=False) assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]])) - a = np.genfromtxt(StringIO(basestr), skip_footer=3, invalid_raise=False) + a = np.genfromtxt(TextIO(basestr), skip_footer=3, invalid_raise=False) assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]])) finally: warn_ctx.__exit__() @@ -827,9 +841,9 @@ class TestFromTxt(TestCase): def test_header(self): "Test retrieving a header" - data = StringIO('gender age weight\nM 64.0 75.0\nF 25.0 60.0') + data = TextIO('gender age weight\nM 64.0 75.0\nF 25.0 60.0') test = np.ndfromtxt(data, dtype=None, names=True) - control = {'gender': np.array(asbytes_nested(['M', 'F'])), + control = {'gender': np.array([b'M', b'F']), 'age': np.array([64.0, 25.0]), 'weight': np.array([75.0, 60.0])} assert_equal(test['gender'], control['gender']) @@ -838,9 +852,9 @@ class TestFromTxt(TestCase): def test_auto_dtype(self): "Test the automatic definition of the output dtype" - data = StringIO('A 64 75.0 3+4j True\nBCD 25 60.0 5+6j False') + data = TextIO('A 64 75.0 3+4j True\nBCD 25 60.0 5+6j False') test = np.ndfromtxt(data, dtype=None) - control = [np.array(asbytes_nested(['A', 'BCD'])), + control = [np.array([b'A', b'BCD']), np.array([64, 25]), np.array([75.0, 60.0]), np.array([3 + 4j, 5 + 6j]), @@ -852,7 +866,7 @@ class TestFromTxt(TestCase): def test_auto_dtype_uniform(self): "Tests whether the output dtype can be uniformized" - data = StringIO('1 2 3 4\n5 6 7 8\n') + data = TextIO('1 2 3 4\n5 6 7 8\n') test = np.ndfromtxt(data, dtype=None) control = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) assert_equal(test, control) @@ -860,7 +874,7 @@ class TestFromTxt(TestCase): def test_fancy_dtype(self): "Check that a nested dtype isn't MIA" - data = StringIO('1,2,3.0\n4,5,6.0\n') + data = TextIO('1,2,3.0\n4,5,6.0\n') fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])]) test = np.ndfromtxt(data, dtype=fancydtype, delimiter=',') control = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype) @@ -871,7 +885,7 @@ class TestFromTxt(TestCase): "Test overwriting the names of the dtype" descriptor = {'names': ('g', 'a', 'w'), 'formats': ('S1', 'i4', 'f4')} - data = StringIO('M 64.0 75.0\nF 25.0 60.0') + data = TextIO(b'M 64.0 75.0\nF 25.0 60.0') names = ('gender', 'age', 'weight') test = np.ndfromtxt(data, dtype=descriptor, names=names) descriptor['names'] = names @@ -882,7 +896,7 @@ class TestFromTxt(TestCase): def test_commented_header(self): "Check that names can be retrieved even if the line is commented out." - data = StringIO(""" + data = TextIO(""" #gender age weight M 21 72.100000 F 35 58.330000 @@ -894,7 +908,7 @@ M 33 21.99 dtype=[('gender', '|S1'), ('age', int), ('weight', float)]) assert_equal(test, ctrl) # Ditto, but we should get rid of the first element - data = StringIO(""" + data = TextIO(b""" # gender age weight M 21 72.100000 F 35 58.330000 @@ -906,7 +920,7 @@ M 33 21.99 def test_autonames_and_usecols(self): "Tests names and usecols" - data = StringIO('A B C D\n aaaa 121 45 9.1') + data = TextIO('A B C D\n aaaa 121 45 9.1') test = np.ndfromtxt(data, usecols=('A', 'C', 'D'), names=True, dtype=None) control = np.array(('aaaa', 45, 9.1), @@ -916,7 +930,7 @@ M 33 21.99 def test_converters_with_usecols(self): "Test the combination user-defined converters and usecol" - data = StringIO('1,2,3,,5\n6,7,8,9,10\n') + data = TextIO('1,2,3,,5\n6,7,8,9,10\n') test = np.ndfromtxt(data, dtype=int, delimiter=',', converters={3:lambda s: int(s or - 999)}, usecols=(1, 3,)) @@ -925,7 +939,7 @@ M 33 21.99 def test_converters_with_usecols_and_names(self): "Tests names and usecols" - data = StringIO('A B C D\n aaaa 121 45 9.1') + data = TextIO('A B C D\n aaaa 121 45 9.1') test = np.ndfromtxt(data, usecols=('A', 'C', 'D'), names=True, dtype=None, converters={'C':lambda s: 2 * int(s)}) control = np.array(('aaaa', 90, 9.1), @@ -935,26 +949,26 @@ M 33 21.99 def test_converters_cornercases(self): "Test the conversion to datetime." converter = {'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')} - data = StringIO('2009-02-03 12:00:00Z, 72214.0') + data = TextIO('2009-02-03 12:00:00Z, 72214.0') test = np.ndfromtxt(data, delimiter=',', dtype=None, names=['date', 'stid'], converters=converter) - control = np.array((datetime(2009, 02, 03), 72214.), + control = np.array((datetime(2009, 2, 3), 72214.), dtype=[('date', np.object_), ('stid', float)]) assert_equal(test, control) def test_converters_cornercases2(self): "Test the conversion to datetime64." converter = {'date': lambda s: np.datetime64(strptime(s, '%Y-%m-%d %H:%M:%SZ'))} - data = StringIO('2009-02-03 12:00:00Z, 72214.0') + data = TextIO('2009-02-03 12:00:00Z, 72214.0') test = np.ndfromtxt(data, delimiter=',', dtype=None, names=['date', 'stid'], converters=converter) - control = np.array((datetime(2009, 02, 03), 72214.), + control = np.array((datetime(2009, 2, 3), 72214.), dtype=[('date', 'datetime64[us]'), ('stid', float)]) assert_equal(test, control) def test_unused_converter(self): "Test whether unused converters are forgotten" - data = StringIO("1 21\n 3 42\n") + data = TextIO("1 21\n 3 42\n") test = np.ndfromtxt(data, usecols=(1,), converters={0: lambda s: int(s, 16)}) assert_equal(test, [21, 42]) @@ -966,11 +980,11 @@ M 33 21.99 def test_invalid_converter(self): - strip_rand = lambda x : float((asbytes('r') in x.lower() and x.split()[-1]) or - (not asbytes('r') in x.lower() and x.strip() or 0.0)) - strip_per = lambda x : float((asbytes('%') in x.lower() and x.split()[0]) or - (not asbytes('%') in x.lower() and x.strip() or 0.0)) - s = StringIO("D01N01,10/1/2003 ,1 %,R 75,400,600\r\n" \ + strip_rand = lambda x : float((b'r' in x.lower() and x.split()[-1]) or + (b'r' not in x.lower() and x.strip() or 0.0)) + strip_per = lambda x : float((b'%' in x.lower() and x.split()[0]) or + (b'%' not in x.lower() and x.strip() or 0.0)) + s = TextIO("D01N01,10/1/2003 ,1 %,R 75,400,600\r\n" "L24U05,12/5/2003, 2 %,1,300, 150.5\r\n" "D02N03,10/10/2004,R 1,,7,145.55") kwargs = dict(converters={2 : strip_per, 3 : strip_rand}, delimiter=",", @@ -979,7 +993,7 @@ M 33 21.99 def test_tricky_converter_bug1666(self): "Test some corner case" - s = StringIO('q1,2\nq3,4') + s = TextIO('q1,2\nq3,4') cnv = lambda s:float(s[1:]) test = np.genfromtxt(s, delimiter=',', converters={0:cnv}) control = np.array([[1., 2.], [3., 4.]]) @@ -989,12 +1003,12 @@ M 33 21.99 def test_dtype_with_converters(self): dstr = "2009; 23; 46" - test = np.ndfromtxt(StringIO(dstr,), + test = np.ndfromtxt(TextIO(dstr,), delimiter=";", dtype=float, converters={0:bytes}) control = np.array([('2009', 23., 46)], dtype=[('f0', '|S4'), ('f1', float), ('f2', float)]) assert_equal(test, control) - test = np.ndfromtxt(StringIO(dstr,), + test = np.ndfromtxt(TextIO(dstr,), delimiter=";", dtype=float, converters={0:float}) control = np.array([2009., 23., 46],) assert_equal(test, control) @@ -1004,12 +1018,12 @@ M 33 21.99 "Test using an explicit dtype with an object" from datetime import date import time - data = asbytes(""" 1; 2001-01-01 - 2; 2002-01-31 """) + data = """ 1; 2001-01-01 + 2; 2002-01-31 """ ndtype = [('idx', int), ('code', np.object)] func = lambda s: strptime(s.strip(), "%Y-%m-%d") converters = {1: func} - test = np.genfromtxt(StringIO(data), delimiter=";", dtype=ndtype, + test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype, converters=converters) control = np.array([(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))], dtype=ndtype) @@ -1017,7 +1031,7 @@ M 33 21.99 # ndtype = [('nest', [('idx', int), ('code', np.object)])] try: - test = np.genfromtxt(StringIO(data), delimiter=";", + test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype, converters=converters) except NotImplementedError: pass @@ -1028,7 +1042,7 @@ M 33 21.99 def test_userconverters_with_explicit_dtype(self): "Test user_converters w/ explicit (standard) dtype" - data = StringIO('skip,skip,2001-01-01,1.0,skip') + data = TextIO('skip,skip,2001-01-01,1.0,skip') test = np.genfromtxt(data, delimiter=",", names=None, dtype=float, usecols=(2, 3), converters={2: bytes}) control = np.array([('2001-01-01', 1.)], @@ -1038,7 +1052,7 @@ M 33 21.99 def test_spacedelimiter(self): "Test space delimiter" - data = StringIO("1 2 3 4 5\n6 7 8 9 10") + data = TextIO("1 2 3 4 5\n6 7 8 9 10") test = np.ndfromtxt(data) control = np.array([[ 1., 2., 3., 4., 5.], [ 6., 7., 8., 9., 10.]]) @@ -1047,13 +1061,13 @@ M 33 21.99 def test_integer_delimiter(self): "Test using an integer for delimiter" data = " 1 2 3\n 4 5 67\n890123 4" - test = np.genfromtxt(StringIO(data), delimiter=3) + test = np.genfromtxt(TextIO(data), delimiter=3) control = np.array([[1, 2, 3], [4, 5, 67], [890, 123, 4]]) assert_equal(test, control) def test_missing(self): - data = StringIO('1,2,3,,5\n') + data = TextIO('1,2,3,,5\n') test = np.ndfromtxt(data, dtype=int, delimiter=',', \ converters={3:lambda s: int(s or - 999)}) control = np.array([1, 2, 3, -999, 5], int) @@ -1063,7 +1077,7 @@ M 33 21.99 def test_missing_with_tabs(self): "Test w/ a delimiter tab" txt = "1\t2\t3\n\t2\t\n1\t\t3" - test = np.genfromtxt(StringIO(txt), delimiter="\t", + test = np.genfromtxt(TextIO(txt), delimiter="\t", usemask=True,) ctrl_d = np.array([(1, 2, 3), (np.nan, 2, np.nan), (1, np.nan, 3)],) ctrl_m = np.array([(0, 0, 0), (1, 0, 1), (0, 1, 0)], dtype=bool) @@ -1075,14 +1089,14 @@ M 33 21.99 "Test the selection of columns" # Select 1 column control = np.array([[1, 2], [3, 4]], float) - data = StringIO() + data = TextIO() np.savetxt(data, control) data.seek(0) test = np.ndfromtxt(data, dtype=float, usecols=(1,)) assert_equal(test, control[:, 1]) # control = np.array([[1, 2, 3], [3, 4, 5]], float) - data = StringIO() + data = TextIO() np.savetxt(data, control) data.seek(0) test = np.ndfromtxt(data, dtype=float, usecols=(1, 2)) @@ -1095,23 +1109,23 @@ M 33 21.99 def test_usecols_as_css(self): "Test giving usecols with a comma-separated string" data = "1 2 3\n4 5 6" - test = np.genfromtxt(StringIO(data), + test = np.genfromtxt(TextIO(data), names="a, b, c", usecols="a, c") ctrl = np.array([(1, 3), (4, 6)], dtype=[(_, float) for _ in "ac"]) assert_equal(test, ctrl) def test_usecols_with_structured_dtype(self): "Test usecols with an explicit structured dtype" - data = StringIO("""JOE 70.1 25.3\nBOB 60.5 27.9""") + data = TextIO("JOE 70.1 25.3\nBOB 60.5 27.9") names = ['stid', 'temp'] dtypes = ['S4', 'f8'] test = np.ndfromtxt(data, usecols=(0, 2), dtype=zip(names, dtypes)) - assert_equal(test['stid'], asbytes_nested(["JOE", "BOB"])) + assert_equal(test['stid'], [b"JOE", b"BOB"]) assert_equal(test['temp'], [25.3, 27.9]) def test_usecols_with_integer(self): "Test usecols with an integer" - test = np.genfromtxt(StringIO("1 2 3\n4 5 6"), usecols=0) + test = np.genfromtxt(TextIO(b"1 2 3\n4 5 6"), usecols=0) assert_equal(test, np.array([1., 4.])) def test_usecols_with_named_columns(self): @@ -1119,9 +1133,9 @@ M 33 21.99 ctrl = np.array([(1, 3), (4, 6)], dtype=[('a', float), ('c', float)]) data = "1 2 3\n4 5 6" kwargs = dict(names="a, b, c") - test = np.genfromtxt(StringIO(data), usecols=(0, -1), **kwargs) + test = np.genfromtxt(TextIO(data), usecols=(0, -1), **kwargs) assert_equal(test, ctrl) - test = np.genfromtxt(StringIO(data), + test = np.genfromtxt(TextIO(data), usecols=('a', 'c'), **kwargs) assert_equal(test, ctrl) @@ -1131,7 +1145,7 @@ M 33 21.99 warn_ctx.__enter__() try: warnings.filterwarnings("ignore", message="genfromtxt: Empty input file:") - data = StringIO() + data = TextIO() test = np.genfromtxt(data) assert_equal(test, np.array([])) finally: @@ -1139,7 +1153,7 @@ M 33 21.99 def test_fancy_dtype_alt(self): "Check that a nested dtype isn't MIA" - data = StringIO('1,2,3.0\n4,5,6.0\n') + data = TextIO('1,2,3.0\n4,5,6.0\n') fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])]) test = np.mafromtxt(data, dtype=fancydtype, delimiter=',') control = ma.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype) @@ -1147,7 +1161,7 @@ M 33 21.99 def test_shaped_dtype(self): - c = StringIO("aaaa 1.0 8.0 1 2 3 4 5 6") + c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6") dt = np.dtype([('name', 'S4'), ('x', float), ('y', float), ('block', int, (2, 3))]) x = np.ndfromtxt(c, dtype=dt) @@ -1156,7 +1170,7 @@ M 33 21.99 assert_array_equal(x, a) def test_withmissing(self): - data = StringIO('A,B\n0,1\n2,N/A') + data = TextIO('A,B\n0,1\n2,N/A') kwargs = dict(delimiter=",", missing_values="N/A", names=True) test = np.mafromtxt(data, dtype=None, **kwargs) control = ma.array([(0, 1), (2, -1)], @@ -1179,7 +1193,7 @@ M 33 21.99 basekwargs = dict(dtype=None, delimiter=",", names=True,) mdtype = [('A', int), ('B', float), ('C', complex)] # - test = np.mafromtxt(StringIO(data), missing_values="N/A", + test = np.mafromtxt(TextIO(data), missing_values="N/A", **basekwargs) control = ma.array([(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j), (3, -99, 3j)], @@ -1188,7 +1202,7 @@ M 33 21.99 assert_equal(test, control) # basekwargs['dtype'] = mdtype - test = np.mafromtxt(StringIO(data), + test = np.mafromtxt(TextIO(data), missing_values={0:-9, 1:-99, 2:-999j}, **basekwargs) control = ma.array([(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j), (3, -99, 3j)], @@ -1196,7 +1210,7 @@ M 33 21.99 dtype=mdtype) assert_equal(test, control) # - test = np.mafromtxt(StringIO(data), + test = np.mafromtxt(TextIO(data), missing_values={0:-9, 'B':-99, 'C':-999j}, **basekwargs) control = ma.array([(0, 0.0, 0j), (1, -999, 1j), @@ -1214,18 +1228,18 @@ M 33 21.99 names="a,b,c", missing_values={0:"N/A", 'b':" ", 2:"???"}, filling_values={0:0, 'b':0, 2:-999}) - test = np.genfromtxt(StringIO(data), **kwargs) + test = np.genfromtxt(TextIO(data), **kwargs) ctrl = np.array([(0, 2, 3), (4, 0, -999)], dtype=[(_, int) for _ in "abc"]) assert_equal(test, ctrl) # - test = np.genfromtxt(StringIO(data), usecols=(0, -1), **kwargs) + test = np.genfromtxt(TextIO(data), usecols=(0, -1), **kwargs) ctrl = np.array([(0, 3), (4, -999)], dtype=[(_, int) for _ in "ac"]) assert_equal(test, ctrl) def test_withmissing_float(self): - data = StringIO('A,B\n0,1.5\n2,-999.00') + data = TextIO('A,B\n0,1.5\n2,-999.00') test = np.mafromtxt(data, dtype=None, delimiter=',', missing_values='-999.0', names=True,) control = ma.array([(0, 1.5), (2, -1.)], @@ -1237,7 +1251,7 @@ M 33 21.99 def test_with_masked_column_uniform(self): "Test masked column" - data = StringIO('1 2 3\n4 5 6\n') + data = TextIO('1 2 3\n4 5 6\n') test = np.genfromtxt(data, dtype=None, missing_values='2,5', usemask=True) control = ma.array([[1, 2, 3], [4, 5, 6]], mask=[[0, 1, 0], [0, 1, 0]]) @@ -1245,7 +1259,7 @@ M 33 21.99 def test_with_masked_column_various(self): "Test masked column" - data = StringIO('True 2 3\nFalse 5 6\n') + data = TextIO('True 2 3\nFalse 5 6\n') test = np.genfromtxt(data, dtype=None, missing_values='2,5', usemask=True) control = ma.array([(1, 2, 3), (0, 5, 6)], @@ -1260,7 +1274,7 @@ M 33 21.99 for i in range(5): data[10 * i] = "2, 2, 2, 2 2" data.insert(0, "a, b, c, d, e") - mdata = StringIO("\n".join(data)) + mdata = TextIO("\n".join(data)) # kwargs = dict(delimiter=",", dtype=None, names=True) # XXX: is there a better way to get the return value of the callable in @@ -1283,7 +1297,7 @@ M 33 21.99 for i in range(5): data[10 * i] = "2, 2, 2, 2 2" data.insert(0, "a, b, c, d, e") - mdata = StringIO("\n".join(data)) + mdata = TextIO("\n".join(data)) kwargs = dict(delimiter=",", dtype=None, names=True, invalid_raise=False) # XXX: is there a better way to get the return value of the callable in @@ -1307,7 +1321,7 @@ M 33 21.99 def test_inconsistent_dtype(self): "Test inconsistent dtype" data = ["1, 1, 1, 1, -1.1"] * 50 - mdata = StringIO("\n".join(data)) + mdata = TextIO("\n".join(data)) converters = {4: lambda x:"(%s)" % x} kwargs = dict(delimiter=",", converters=converters, @@ -1318,7 +1332,7 @@ M 33 21.99 def test_default_field_format(self): "Test default format" data = "0, 1, 2.3\n4, 5, 6.7" - mtest = np.ndfromtxt(StringIO(data), + mtest = np.ndfromtxt(TextIO(data), delimiter=",", dtype=None, defaultfmt="f%02i") ctrl = np.array([(0, 1, 2.3), (4, 5, 6.7)], dtype=[("f00", int), ("f01", int), ("f02", float)]) @@ -1327,7 +1341,7 @@ M 33 21.99 def test_single_dtype_wo_names(self): "Test single dtype w/o names" data = "0, 1, 2.3\n4, 5, 6.7" - mtest = np.ndfromtxt(StringIO(data), + mtest = np.ndfromtxt(TextIO(data), delimiter=",", dtype=float, defaultfmt="f%02i") ctrl = np.array([[0., 1., 2.3], [4., 5., 6.7]], dtype=float) assert_equal(mtest, ctrl) @@ -1335,7 +1349,7 @@ M 33 21.99 def test_single_dtype_w_explicit_names(self): "Test single dtype w explicit names" data = "0, 1, 2.3\n4, 5, 6.7" - mtest = np.ndfromtxt(StringIO(data), + mtest = np.ndfromtxt(TextIO(data), delimiter=",", dtype=float, names="a, b, c") ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)], dtype=[(_, float) for _ in "abc"]) @@ -1344,7 +1358,7 @@ M 33 21.99 def test_single_dtype_w_implicit_names(self): "Test single dtype w implicit names" data = "a, b, c\n0, 1, 2.3\n4, 5, 6.7" - mtest = np.ndfromtxt(StringIO(data), + mtest = np.ndfromtxt(TextIO(data), delimiter=",", dtype=float, names=True) ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)], dtype=[(_, float) for _ in "abc"]) @@ -1353,7 +1367,7 @@ M 33 21.99 def test_easy_structured_dtype(self): "Test easy structured dtype" data = "0, 1, 2.3\n4, 5, 6.7" - mtest = np.ndfromtxt(StringIO(data), delimiter=",", + mtest = np.ndfromtxt(TextIO(data), delimiter=",", dtype=(int, float, float), defaultfmt="f_%02i") ctrl = np.array([(0, 1., 2.3), (4, 5., 6.7)], dtype=[("f_00", int), ("f_01", float), ("f_02", float)]) @@ -1363,11 +1377,11 @@ M 33 21.99 "Test autostrip" data = "01/01/2003 , 1.3, abcde" kwargs = dict(delimiter=",", dtype=None) - mtest = np.ndfromtxt(StringIO(data), **kwargs) + mtest = np.ndfromtxt(TextIO(data), **kwargs) ctrl = np.array([('01/01/2003 ', 1.3, ' abcde')], dtype=[('f0', '|S12'), ('f1', float), ('f2', '|S8')]) assert_equal(mtest, ctrl) - mtest = np.ndfromtxt(StringIO(data), autostrip=True, **kwargs) + mtest = np.ndfromtxt(TextIO(data), autostrip=True, **kwargs) ctrl = np.array([('01/01/2003', 1.3, 'abcde')], dtype=[('f0', '|S10'), ('f1', float), ('f2', '|S5')]) assert_equal(mtest, ctrl) @@ -1376,20 +1390,20 @@ M 33 21.99 "Test the 'replace_space' option" txt = "A.A, B (B), C:C\n1, 2, 3.14" # Test default: replace ' ' by '_' and delete non-alphanum chars - test = np.genfromtxt(StringIO(txt), + test = np.genfromtxt(TextIO(txt), delimiter=",", names=True, dtype=None) ctrl_dtype = [("AA", int), ("B_B", int), ("CC", float)] ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype) assert_equal(test, ctrl) # Test: no replace, no delete - test = np.genfromtxt(StringIO(txt), + test = np.genfromtxt(TextIO(txt), delimiter=",", names=True, dtype=None, replace_space='', deletechars='') ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", float)] ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype) assert_equal(test, ctrl) # Test: no delete (spaces are replaced by _) - test = np.genfromtxt(StringIO(txt), + test = np.genfromtxt(TextIO(txt), delimiter=",", names=True, dtype=None, deletechars='') ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", float)] @@ -1403,17 +1417,17 @@ M 33 21.99 # w/ dtype=None ctrl = np.array([(0, 1, 2), (3, 4, 5)], dtype=[(_, int) for _ in ('A', 'f0', 'C')]) - test = np.ndfromtxt(StringIO(data), dtype=None, **kwargs) + test = np.ndfromtxt(TextIO(data), dtype=None, **kwargs) assert_equal(test, ctrl) # w/ default dtype ctrl = np.array([(0, 1, 2), (3, 4, 5)], dtype=[(_, float) for _ in ('A', 'f0', 'C')]) - test = np.ndfromtxt(StringIO(data), **kwargs) + test = np.ndfromtxt(TextIO(data), **kwargs) def test_names_auto_completion(self): "Make sure that names are properly completed" data = "1 2 3\n 4 5 6" - test = np.genfromtxt(StringIO(data), + test = np.genfromtxt(TextIO(data), dtype=(int, float, int), names="a") ctrl = np.array([(1, 2, 3), (4, 5, 6)], dtype=[('a', int), ('f0', float), ('f1', int)]) @@ -1423,17 +1437,17 @@ M 33 21.99 "Make sure we pick up the right names w/ usecols" data = "A,B,C,D,E\n0,1,2,3,4\n0,1,2,3,4\n0,1,2,3,4" ctrl_names = ("A", "C", "E") - test = np.genfromtxt(StringIO(data), + test = np.genfromtxt(TextIO(data), dtype=(int, int, int), delimiter=",", usecols=(0, 2, 4), names=True) assert_equal(test.dtype.names, ctrl_names) # - test = np.genfromtxt(StringIO(data), + test = np.genfromtxt(TextIO(data), dtype=(int, int, int), delimiter=",", usecols=("A", "C", "E"), names=True) assert_equal(test.dtype.names, ctrl_names) # - test = np.genfromtxt(StringIO(data), + test = np.genfromtxt(TextIO(data), dtype=int, delimiter=",", usecols=("A", "C", "E"), names=True) assert_equal(test.dtype.names, ctrl_names) @@ -1444,35 +1458,35 @@ M 33 21.99 kwargs = dict(delimiter=(5, 5, 4), names=True, dtype=None) ctrl = np.array([(0, 1, 2.3), (45, 67, 9.)], dtype=[('A', int), ('B', int), ('C', float)]) - test = np.ndfromtxt(StringIO(data), **kwargs) + test = np.ndfromtxt(TextIO(data), **kwargs) assert_equal(test, ctrl) # kwargs = dict(delimiter=5, names=True, dtype=None) ctrl = np.array([(0, 1, 2.3), (45, 67, 9.)], dtype=[('A', int), ('B', int), ('C', float)]) - test = np.ndfromtxt(StringIO(data), **kwargs) + test = np.ndfromtxt(TextIO(data), **kwargs) assert_equal(test, ctrl) def test_filling_values(self): "Test missing values" - data = "1, 2, 3\n1, , 5\n0, 6, \n" + data = b"1, 2, 3\n1, , 5\n0, 6, \n" kwargs = dict(delimiter=",", dtype=None, filling_values= -999) ctrl = np.array([[1, 2, 3], [1, -999, 5], [0, 6, -999]], dtype=int) - test = np.ndfromtxt(StringIO(data), **kwargs) + test = np.ndfromtxt(TextIO(data), **kwargs) assert_equal(test, ctrl) def test_comments_is_none(self): # Github issue 329 (None was previously being converted to 'None'). - test = np.genfromtxt(StringIO("test1,testNonetherestofthedata"), + test = np.genfromtxt(TextIO("test1,testNonetherestofthedata"), dtype=None, comments=None, delimiter=',') - assert_equal(test[1], asbytes('testNonetherestofthedata')) - test = np.genfromtxt(StringIO("test1, testNonetherestofthedata"), + assert_equal(test[1], b'testNonetherestofthedata') + test = np.genfromtxt(TextIO("test1, testNonetherestofthedata"), dtype=None, comments=None, delimiter=',') - assert_equal(test[1], asbytes(' testNonetherestofthedata')) + assert_equal(test[1], b' testNonetherestofthedata') def test_recfromtxt(self): # - data = StringIO('A,B\n0,1\n2,3') + data = TextIO('A,B\n0,1\n2,3') kwargs = dict(delimiter=",", missing_values="N/A", names=True) test = np.recfromtxt(data, **kwargs) control = np.array([(0, 1), (2, 3)], @@ -1480,7 +1494,7 @@ M 33 21.99 self.assertTrue(isinstance(test, np.recarray)) assert_equal(test, control) # - data = StringIO('A,B\n0,1\n2,N/A') + data = TextIO('A,B\n0,1\n2,N/A') test = np.recfromtxt(data, dtype=None, usemask=True, **kwargs) control = ma.array([(0, 1), (2, -1)], mask=[(False, False), (False, True)], @@ -1491,7 +1505,7 @@ M 33 21.99 def test_recfromcsv(self): # - data = StringIO('A,B\n0,1\n2,3') + data = TextIO('A,B\n0,1\n2,3') kwargs = dict(missing_values="N/A", names=True, case_sensitive=True) test = np.recfromcsv(data, dtype=None, **kwargs) control = np.array([(0, 1), (2, 3)], @@ -1499,7 +1513,7 @@ M 33 21.99 self.assertTrue(isinstance(test, np.recarray)) assert_equal(test, control) # - data = StringIO('A,B\n0,1\n2,N/A') + data = TextIO('A,B\n0,1\n2,N/A') test = np.recfromcsv(data, dtype=None, usemask=True, **kwargs) control = ma.array([(0, 1), (2, -1)], mask=[(False, False), (False, True)], @@ -1508,7 +1522,7 @@ M 33 21.99 assert_equal(test.mask, control.mask) assert_equal(test.A, [0, 2]) # - data = StringIO('A,B\n0,1\n2,3') + data = TextIO('A,B\n0,1\n2,3') test = np.recfromcsv(data, missing_values='N/A',) control = np.array([(0, 1), (2, 3)], dtype=[('a', np.int), ('b', np.int)]) @@ -1537,6 +1551,7 @@ M 33 21.99 os.unlink(name) def test_gft_using_generator(self): + # gft doesn't work with unicode. def count(): for i in range(10): yield asbytes("%d" % i) @@ -1548,7 +1563,7 @@ M 33 21.99 def test_gzip_load(): a = np.random.random((5, 5)) - s = StringIO() + s = BytesIO() f = gzip.GzipFile(fileobj=s, mode="w") np.save(f, a) @@ -1565,9 +1580,9 @@ def test_gzip_loadtxt(): # reopened by another open call. So we first put the gzipped string # of the test reference array, write it to a securely opened file, # which is then read from by the loadtxt function - s = StringIO() + s = BytesIO() g = gzip.GzipFile(fileobj=s, mode='w') - g.write(asbytes('1 2 3\n')) + g.write(b'1 2 3\n') g.close() s.seek(0) @@ -1581,9 +1596,9 @@ def test_gzip_loadtxt(): os.unlink(name) def test_gzip_loadtxt_from_string(): - s = StringIO() + s = BytesIO() f = gzip.GzipFile(fileobj=s, mode="w") - f.write(asbytes('1 2 3\n')) + f.write(b'1 2 3\n') f.close() s.seek(0) @@ -1591,7 +1606,7 @@ def test_gzip_loadtxt_from_string(): assert_array_equal(np.loadtxt(f), [1, 2, 3]) def test_npzfile_dict(): - s = StringIO() + s = BytesIO() x = np.zeros((3, 3)) y = np.zeros((3, 3)) @@ -1620,7 +1635,7 @@ def test_load_refcount(): # Check that objects returned by np.load are directly freed based on # their refcount, rather than needing the gc to collect them. - f = StringIO() + f = BytesIO() np.savez(f, [1, 2, 3]) f.seek(0) diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 9e303d2b1..2e76b320c 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -1,5 +1,6 @@ from __future__ import division, absolute_import +import sys from numpy.testing import * from numpy.testing.utils import _assert_valid_refcount import numpy as np @@ -202,7 +203,11 @@ class TestRegression(TestCase): def test_loadtxt_fields_subarrays(self): # For ticket #1936 - from StringIO import StringIO + if sys.version_info[0] >= 3: + from io import StringIO + else: + from StringIO import StringIO + dt = [("a", 'u1', 2), ("b", 'u1', 2)] x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt) assert_equal(x, np.array([((0, 1), (2, 3))], dtype=dt)) diff --git a/numpy/lib/tests/test_utils.py b/numpy/lib/tests/test_utils.py index 4b386e0b1..8cec1887b 100644 --- a/numpy/lib/tests/test_utils.py +++ b/numpy/lib/tests/test_utils.py @@ -1,10 +1,14 @@ from __future__ import division, absolute_import +import sys from numpy.testing import * import numpy.lib.utils as utils from numpy.lib import deprecate -from StringIO import StringIO +if sys.version_info[0] >= 3: + from io import StringIO +else: + from StringIO import StringIO def test_lookfor(): out = StringIO() |