Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

2to3: Apply `imports` fixer.

The `imports` fixer deals with the standard packages that have been
renamed, removed, or methods that have moved.

cPickle      -- removed, use pickle
commands     -- removed, getoutput, getstatusoutput moved to subprocess
urlparse     -- removed, urlparse moved to urllib.parse
cStringIO    -- removed, use StringIO or io.StringIO
copy_reg     -- renamed copyreg
_winreg      -- renamed winreg
ConfigParser -- renamed configparser
__builtin__  -- renamed builtins

In the case of `cPickle`, it is imported as `pickle` when python < 3 and
performance may be a consideration, but otherwise plain old `pickle` is
used.

Dealing with `StringIO` is a bit tricky. There is an `io.StringIO`
function in the `io` module, available since Python 2.6, but it expects
unicode whereas `StringIO.StringIO` expects ascii. The Python 3
equivalent is then `io.BytesIO`. What I have done here is used BytesIO
for anything that is emulating a file for testing purposes. That is more
explicit than using a redefined StringIO as was done before we dropped
support for Python 2.4 and 2.5.

Closes #3180.
  • Loading branch information...
commit 09a52ed47bb26498c97a579ce1147861df696d84 1 parent a939f2a
@charris charris authored
Showing with 513 additions and 451 deletions.
  1. +5 −1 doc/cdoc/numpyfilter.py
  2. +3 −3 doc/numpybook/runcode.py
  3. +1 −1  doc/sphinxext/numpydoc/comment_eater.py
  4. +1 −1  doc/sphinxext/numpydoc/compiler_unparse.py
  5. +1 −1  doc/sphinxext/numpydoc/docscrape.py
  6. +1 −1  doc/sphinxext/numpydoc/plot_directive.py
  7. +7 −2 numpy/__init__.py
  8. +3 −3 numpy/core/__init__.py
  9. +20 −15 numpy/core/numeric.py
  10. +5 −1 numpy/core/numerictypes.py
  11. +2 −2 numpy/core/records.py
  12. +13 −13 numpy/core/setup.py
  13. +5 −1 numpy/core/tests/test_print.py
  14. +6 −11 numpy/core/tests/test_regression.py
  15. +16 −10 numpy/distutils/cpuinfo.py
  16. +0 −1  numpy/distutils/mingw32ccompiler.py
  17. +6 −2 numpy/distutils/misc_util.py
  18. +4 −3 numpy/distutils/npy_pkg_config.py
  19. +1 −0  numpy/distutils/system_info.py
  20. +10 −5 numpy/distutils/tests/test_exec_command.py
  21. +1 −1  numpy/f2py/__init__.py
  22. +6 −2 numpy/f2py/doc/collectinput.py
  23. +9 −3 numpy/lib/_datasource.py
  24. +5 −1 numpy/lib/_iotools.py
  25. +7 −4 numpy/lib/format.py
  26. +10 −8 numpy/lib/npyio.py
  27. +8 −5 numpy/lib/tests/test__datasource.py
  28. +1 −10 numpy/lib/tests/test__iotools.py
  29. +19 −29 numpy/lib/tests/test_format.py
  30. +238 −223 numpy/lib/tests/test_io.py
  31. +6 −1 numpy/lib/tests/test_regression.py
  32. +5 −1 numpy/lib/tests/test_utils.py
  33. +6 −1 numpy/lib/utils.py
  34. +2 −2 numpy/linalg/lapack_lite/clapack_scrub.py
  35. +21 −21 numpy/ma/core.py
  36. +8 −20 numpy/ma/tests/test_core.py
  37. +13 −14 numpy/ma/tests/test_mrecords.py
  38. +2 −1  numpy/numarray/functions.py
  39. +13 −12 numpy/oldnumeric/compat.py
  40. +6 −7 numpy/oldnumeric/misc.py
  41. +6 −2 numpy/testing/utils.py
  42. +3 −3 setup.py
  43. +2 −1  tools/py3tool.py
  44. +6 −2 tools/win32build/build.py
View
6 doc/cdoc/numpyfilter.py
@@ -13,7 +13,11 @@
import os
import textwrap
import optparse
-import cPickle as pickle
+
+if sys.version_info[0] >= 3:
+ import pickle
+else:
+ import cPickle as pickle.
CACHE_FILE = 'build/rst-cache.pck'
View
6 doc/numpybook/runcode.py
@@ -18,7 +18,7 @@
import sys
import optparse
-import cStringIO
+import io
import re
import os
@@ -27,7 +27,7 @@
def getoutput(tstr, dic):
print "\n\nRunning..."
print tstr,
- tempstr = cStringIO.StringIO()
+ tempstr = io.StringIO()
sys.stdout = tempstr
code = compile(tstr, '<input>', 'exec')
try:
@@ -82,7 +82,7 @@ def getnewcodestr(substr, dic):
def runpycode(lyxstr, name='MyCode'):
schobj = re.compile(r"\\layout %s\s+>>> " % name)
- outstr = cStringIO.StringIO()
+ outstr = io.StringIO()
num = 0
indx = []
for it in schobj.finditer(lyxstr):
View
2  doc/sphinxext/numpydoc/comment_eater.py
@@ -4,7 +4,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
- from cStringIO import StringIO
+ from io import StringIO
import compiler
import inspect
View
2  doc/sphinxext/numpydoc/compiler_unparse.py
@@ -18,7 +18,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
- from cStringIO import StringIO
+ from io import StringIO
def unparse(ast, single_line_functions=False):
s = StringIO()
View
2  doc/sphinxext/numpydoc/docscrape.py
@@ -14,7 +14,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
- from cStringIO import StringIO
+ from io import StringIO
class Reader(object):
"""A line-based string reader.
View
2  doc/sphinxext/numpydoc/plot_directive.py
@@ -82,7 +82,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
- from cStringIO import StringIO
+ from io import StringIO
import warnings
warnings.warn("A plot_directive module is also available under "
View
9 numpy/__init__.py
@@ -106,6 +106,8 @@
"""
from __future__ import division, absolute_import
+import sys
+
# We first need to detect if we're being called as part of the numpy setup
# procedure itself in a reliable manner.
try:
@@ -160,8 +162,11 @@ def pkgload(*packages, **options):
# Make these accessible from numpy name-space
# but not imported in from numpy import *
- from __builtin__ import bool, int, long, float, complex, \
- object, unicode, str
+ if sys.version_info[0] >= 3:
+ from builtins import bool, int, long, float, complex, object, unicode, str
+ else:
+ from __builtin__ import bool, int, long, float, complex, object, unicode, str
+
from .core import round, abs, max, min
__all__.extend(['__version__', 'pkgload', 'PackageLoader',
View
6 numpy/core/__init__.py
@@ -62,10 +62,10 @@ def _ufunc_reduce(func):
import sys
-if sys.version_info[0] < 3:
- import copy_reg as copyreg
-else:
+if sys.version_info[0] >= 3:
import copyreg
+else:
+ import copy_reg as copyreg
copyreg.pickle(ufunc, _ufunc_reduce, _ufunc_reconstruct)
# Unclutter namespace (must keep _ufunc_reconstruct for unpickling)
View
35 numpy/core/numeric.py
@@ -1,5 +1,22 @@
from __future__ import division, absolute_import
+import sys
+import warnings
+from . import multiarray
+from . import umath
+from .umath import *
+from . import numerictypes
+from .numerictypes import *
+import collections
+
+if sys.version_info[0] >= 3:
+ import pickle
+else:
+ import cPickle as pickle
+
+loads = pickle.loads
+
+
__all__ = ['newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc',
'arange', 'array', 'zeros', 'count_nonzero',
'empty', 'broadcast', 'dtype', 'fromstring', 'fromfile',
@@ -24,19 +41,10 @@
'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE', 'ALLOW_THREADS',
'ComplexWarning']
-import sys
-import warnings
-from . import multiarray
-from . import umath
-from .umath import *
-from . import numerictypes
-from .numerictypes import *
-import collections
-
-
if sys.version_info[0] < 3:
__all__.extend(['getbuffer', 'newbuffer'])
+
class ComplexWarning(RuntimeWarning):
"""
The warning raised when casting a complex dtype to a real dtype.
@@ -1861,9 +1869,6 @@ def base_repr(number, base=2, padding=0):
res.append('-')
return ''.join(reversed(res or '0'))
-from cPickle import load, loads
-_cload = load
-_file = open
def load(file):
"""
@@ -1880,8 +1885,8 @@ def load(file):
"""
if isinstance(file, type("")):
- file = _file(file,"rb")
- return _cload(file)
+ file = open(file, "rb")
+ return pickle.load(file)
# These are all essentially abbreviations
# These might wind up in a special abbreviations module
View
6 numpy/core/numerictypes.py
@@ -98,7 +98,11 @@
# we don't export these for import *, but we do want them accessible
# as numerictypes.bool, etc.
-from __builtin__ import bool, int, long, float, complex, object, unicode, str
+if sys.version_info[0] >= 3:
+ from builtins import bool, int, long, float, complex, object, unicode, str
+else:
+ from __builtin__ import bool, int, long, float, complex, object, unicode, str
+
from numpy.compat import bytes
if sys.version_info[0] >= 3:
View
4 numpy/core/records.py
@@ -595,8 +595,8 @@ def fromrecords(recList, dtype=None, shape=None, formats=None, names=None,
>>> r.col2
chararray(['dbe', 'de'],
dtype='|S3')
- >>> import cPickle
- >>> print cPickle.loads(cPickle.dumps(r))
+ >>> import pickle
+ >>> print pickle.loads(pickle.dumps(r))
[(456, 'dbe', 1.2) (2, 'de', 1.3)]
"""
View
26 numpy/core/setup.py
@@ -4,12 +4,14 @@
import os
import sys
import shutil
+import pickle
+import copy
+import warnings
+import re
from os.path import join
from numpy.distutils import log
from distutils.dep_util import newer
from distutils.sysconfig import get_config_var
-import warnings
-import re
from setup_common import *
@@ -22,11 +24,9 @@
# configuration informations between extensions is not easy.
# Using a pickled-based memoize does not work because config_cmd is an instance
# method, which cPickle does not like.
-try:
- import cPickle as _pik
-except ImportError:
- import pickle as _pik
-import copy
+#
+# Use pickle in all cases, as cPickle is gone in python3 and the difference
+# in time is only in build. -- Charles Harris, 2013-03-30
class CallOnceOnly(object):
def __init__(self):
@@ -37,25 +37,25 @@ def __init__(self):
def check_types(self, *a, **kw):
if self._check_types is None:
out = check_types(*a, **kw)
- self._check_types = _pik.dumps(out)
+ self._check_types = pickle.dumps(out)
else:
- out = copy.deepcopy(_pik.loads(self._check_types))
+ out = copy.deepcopy(pickle.loads(self._check_types))
return out
def check_ieee_macros(self, *a, **kw):
if self._check_ieee_macros is None:
out = check_ieee_macros(*a, **kw)
- self._check_ieee_macros = _pik.dumps(out)
+ self._check_ieee_macros = pickle.dumps(out)
else:
- out = copy.deepcopy(_pik.loads(self._check_ieee_macros))
+ out = copy.deepcopy(pickle.loads(self._check_ieee_macros))
return out
def check_complex(self, *a, **kw):
if self._check_complex is None:
out = check_complex(*a, **kw)
- self._check_complex = _pik.dumps(out)
+ self._check_complex = pickle.dumps(out)
else:
- out = copy.deepcopy(_pik.loads(self._check_complex))
+ out = copy.deepcopy(pickle.loads(self._check_complex))
return out
PYTHON_HAS_UNICODE_WIDE = True
View
6 numpy/core/tests/test_print.py
@@ -6,7 +6,11 @@
import locale
import sys
-from StringIO import StringIO
+
+if sys.version_info[0] >= 3:
+ from io import StringIO
+else:
+ from StringIO import StringIO
_REF = {np.inf: 'inf', -np.inf: '-inf', np.nan: 'nan'}
View
17 numpy/core/tests/test_regression.py
@@ -7,8 +7,9 @@
import copy
import warnings
import tempfile
-from StringIO import StringIO
from os import path
+from io import BytesIO
+
import numpy as np
from numpy.testing import (
run_module_suite, TestCase, assert_, assert_equal,
@@ -18,10 +19,6 @@
from numpy.testing.utils import _assert_valid_refcount, WarningManager
from numpy.compat import asbytes, asunicode, asbytes_nested
-if sys.version_info[0] >= 3:
- import io
- StringIO = io.BytesIO
-
rlevel = 1
class TestRegression(TestCase):
@@ -37,7 +34,7 @@ def test_mem_empty(self,level=rlevel):
def test_pickle_transposed(self,level=rlevel):
"""Ticket #16"""
a = np.transpose(np.array([[2,9],[7,0],[3,8]]))
- f = StringIO()
+ f = BytesIO()
pickle.dump(a,f)
f.seek(0)
b = pickle.load(f)
@@ -90,7 +87,7 @@ def test_negative_nd_indexing(self,level=rlevel):
def test_char_dump(self,level=rlevel):
"""Ticket #50"""
- f = StringIO()
+ f = BytesIO()
ca = np.char.array(np.arange(1000,1010),itemsize=4)
ca.dump(f)
f.seek(0)
@@ -322,7 +319,7 @@ def assign(a, b, c):
def test_unpickle_dtype_with_object(self,level=rlevel):
"""Implemented in r2840"""
dt = np.dtype([('x',int),('y',np.object_),('z','O')])
- f = StringIO()
+ f = BytesIO()
pickle.dump(dt,f)
f.seek(0)
dt_ = pickle.load(f)
@@ -386,7 +383,6 @@ def test_lexsort(self,level=rlevel):
def test_pickle_dtype(self,level=rlevel):
"""Ticket #251"""
- import pickle
pickle.dumps(np.float)
def test_swap_real(self, level=rlevel):
@@ -722,10 +718,9 @@ def rs():
def test_unicode_scalar(self, level=rlevel):
"""Ticket #600"""
- import cPickle
x = np.array(["DROND", "DROND1"], dtype="U6")
el = x[1]
- new = cPickle.loads(cPickle.dumps(el))
+ new = pickle.loads(pickle.dumps(el))
assert_equal(new, el)
def test_arange_non_native_dtype(self, level=rlevel):
View
26 numpy/distutils/cpuinfo.py
@@ -18,10 +18,12 @@
import sys, re, types
import os
-if sys.version_info[0] < 3:
- from commands import getstatusoutput
-else:
+
+if sys.version_info[0] >= 3:
from subprocess import getstatusoutput
+else:
+ from commands import getstatusoutput
+
import warnings
import platform
@@ -488,25 +490,29 @@ def __init__(self):
info = []
try:
#XXX: Bad style to use so long `try:...except:...`. Fix it!
- import _winreg
+ if sys.version_info[0] >= 3:
+ import winreg
+ else:
+ import _winreg as winreg
+
prgx = re.compile(r"family\s+(?P<FML>\d+)\s+model\s+(?P<MDL>\d+)"\
"\s+stepping\s+(?P<STP>\d+)",re.IGNORECASE)
- chnd=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, self.pkey)
+ chnd=winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, self.pkey)
pnum=0
while 1:
try:
- proc=_winreg.EnumKey(chnd,pnum)
- except _winreg.error:
+ proc=winreg.EnumKey(chnd,pnum)
+ except winreg.error:
break
else:
pnum+=1
info.append({"Processor":proc})
- phnd=_winreg.OpenKey(chnd,proc)
+ phnd=winreg.OpenKey(chnd,proc)
pidx=0
while True:
try:
- name,value,vtpe=_winreg.EnumValue(phnd,pidx)
- except _winreg.error:
+ name,value,vtpe=winreg.EnumValue(phnd,pidx)
+ except winreg.error:
break
else:
pidx=pidx+1
View
1  numpy/distutils/mingw32ccompiler.py
@@ -10,7 +10,6 @@
from __future__ import division, absolute_import
import os
-import subprocess
import sys
import subprocess
import re
View
8 numpy/distutils/misc_util.py
@@ -2125,9 +2125,13 @@ def get_info(pkgname, dirs=None):
return info
def is_bootstrapping():
- import __builtin__
+ if sys.version_info[0] >= 3:
+ import builtins
+ else:
+ import __builtin__ as builtins
+
try:
- __builtin__.__NUMPY_SETUP__
+ builtins.__NUMPY_SETUP__
return True
except AttributeError:
return False
View
7 numpy/distutils/npy_pkg_config.py
@@ -1,13 +1,14 @@
from __future__ import division, absolute_import
import sys
+import re
+import os
+import shlex
+
if sys.version_info[0] < 3:
from ConfigParser import SafeConfigParser, NoOptionError
else:
from configparser import ConfigParser, SafeConfigParser, NoOptionError
-import re
-import os
-import shlex
__all__ = ['FormatError', 'PkgNotFound', 'LibraryInfo', 'VariableSet',
'read_config', 'parse_flags']
View
1  numpy/distutils/system_info.py
@@ -118,6 +118,7 @@
import copy
import warnings
from glob import glob
+
if sys.version_info[0] < 3:
from ConfigParser import NoOptionError, ConfigParser
else:
View
15 numpy/distutils/tests/test_exec_command.py
@@ -2,11 +2,16 @@
import os
import sys
-import StringIO
from tempfile import TemporaryFile
from numpy.distutils import exec_command
+# In python 3 stdout, stderr are text (unicode compliant) devices, so to
+# emulate them import StringIO from the io module.
+if sys.version_info[0] >= 3:
+ from io import StringIO
+else:
+ from StringIO import StringIO
class redirect_stdout(object):
"""Context manager to redirect stdout for exec_command test."""
@@ -62,26 +67,26 @@ def test_exec_command_stdout():
# both that the special case works and that the generic code works.
# Test posix version:
- with redirect_stdout(StringIO.StringIO()):
+ with redirect_stdout(StringIO()):
with redirect_stderr(TemporaryFile()):
exec_command.exec_command("cd '.'")
if os.name == 'posix':
# Test general (non-posix) version:
with emulate_nonposix():
- with redirect_stdout(StringIO.StringIO()):
+ with redirect_stdout(StringIO()):
with redirect_stderr(TemporaryFile()):
exec_command.exec_command("cd '.'")
def test_exec_command_stderr():
# Test posix version:
with redirect_stdout(TemporaryFile(mode='w+')):
- with redirect_stderr(StringIO.StringIO()):
+ with redirect_stderr(StringIO()):
exec_command.exec_command("cd '.'")
if os.name == 'posix':
# Test general (non-posix) version:
with emulate_nonposix():
with redirect_stdout(TemporaryFile()):
- with redirect_stderr(StringIO.StringIO()):
+ with redirect_stderr(StringIO()):
exec_command.exec_command("cd '.'")
View
2  numpy/f2py/__init__.py
@@ -5,7 +5,7 @@
import os
import sys
-import commands
+import subprocess
from . import f2py2e
from . import f2py_testing
View
8 numpy/f2py/doc/collectinput.py
@@ -27,7 +27,11 @@
import sys
import fileinput
import re
-import commands
+
+if sys.version_info[0] >= 3:
+ from subprocess import getoutput
+else:
+ from commands import getoutput
try: fn=sys.argv[2]
except:
@@ -69,7 +73,7 @@
elif flag==1:
sys.stderr.write(fn+'\n')
print '%%%%% Begin of '+fn
- print commands.getoutput(sys.argv[0]+' < '+fn)
+ print getoutput(sys.argv[0]+' < '+fn)
print '%%%%% End of '+fn
else:
sys.stderr.write('Could not extract a file name from: '+l)
View
12 numpy/lib/_datasource.py
@@ -36,6 +36,7 @@
__docformat__ = "restructuredtext en"
import os
+import sys
from shutil import rmtree, copyfile, copyfileobj
_open = open
@@ -252,7 +253,10 @@ def _isurl(self, path):
"""Test if path is a net location. Tests the scheme and netloc."""
# We do this here to reduce the 'import numpy' initial import time.
- from urlparse import urlparse
+ if sys.version_info[0] >= 3:
+ from urllib.parse import urlparse
+ else:
+ from urlparse import urlparse
# BUG : URLs require a scheme string ('http://') to be used.
# www.google.com will fail.
@@ -351,8 +355,10 @@ def abspath(self, path):
"""
# We do this here to reduce the 'import numpy' initial import time.
- from urlparse import urlparse
-
+ if sys.version_info[0] >= 3:
+ from urllib.parse import urlparse
+ else:
+ from urlparse import urlparse
# TODO: This should be more robust. Handles case where path includes
# the destpath, but not other sub-paths. Failing case:
View
6 numpy/lib/_iotools.py
@@ -8,7 +8,11 @@
import sys
import numpy as np
import numpy.core.numeric as nx
-from __builtin__ import bool, int, long, float, complex, object, unicode, str
+
+if sys.version_info[0] >= 3:
+ from builtins import bool, int, long, float, complex, object, unicode, str
+else:
+ from __builtin__ import bool, int, long, float, complex, object, unicode, str
from numpy.compat import asbytes, bytes, asbytes_nested
View
11 numpy/lib/format.py
@@ -136,13 +136,16 @@
"""
from __future__ import division, absolute_import
-import cPickle
-
import numpy
import sys
from numpy.lib.utils import safe_eval
from numpy.compat import asbytes, isfileobj
+if sys.version_info[0] >= 3:
+ import pickle
+else:
+ import cPickle as pickle
+
MAGIC_PREFIX = asbytes('\x93NUMPY')
MAGIC_LEN = len(MAGIC_PREFIX) + 2
@@ -399,7 +402,7 @@ def write_array(fp, array, version=(1,0)):
if array.dtype.hasobject:
# We contain Python objects so we cannot write out the data directly.
# Instead, we will pickle it out with version 2 of the pickle protocol.
- cPickle.dump(array, fp, protocol=2)
+ pickle.dump(array, fp, protocol=2)
elif array.flags.f_contiguous and not array.flags.c_contiguous:
if isfileobj(fp):
array.T.tofile(fp)
@@ -447,7 +450,7 @@ def read_array(fp):
# Now read the actual data.
if dtype.hasobject:
# The array contained Python objects. We need to unpickle the data.
- array = cPickle.load(fp)
+ array = pickle.load(fp)
else:
if isfileobj(fp):
# We can use the fast fromfile() function.
View
18 numpy/lib/npyio.py
@@ -1,9 +1,5 @@
from __future__ import division, absolute_import
-__all__ = ['savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt',
- 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez',
- 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource']
-
import numpy as np
from . import format
import sys
@@ -15,7 +11,6 @@
import weakref
from operator import itemgetter
-from cPickle import load as _cload, loads
from ._datasource import DataSource
from ._compiled_base import packbits, unpackbits
@@ -25,11 +20,18 @@
easy_dtype, _bytes_to_name
from numpy.compat import asbytes, asstr, asbytes_nested, bytes
+from io import BytesIO
if sys.version_info[0] >= 3:
- from io import BytesIO
+ import pickle
else:
- from cStringIO import StringIO as BytesIO
+ import cPickle as pickle
+
+loads = pickle.loads
+
+__all__ = ['savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt',
+ 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez',
+ 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource']
_string_like = _is_string_like
@@ -380,7 +382,7 @@ def load(file, mmap_mode=None):
return format.read_array(fid)
else: # Try a pickle
try:
- return _cload(fid)
+ return pickle.load(fid)
except:
raise IOError(
"Failed to interpret file %s as a pickle" % repr(file))
View
13 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.'''
View
11 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):
View
48 numpy/lib/tests/test_format.py
@@ -5,10 +5,7 @@
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 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 @@
"\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 @@ def teardown_module():
]
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 @@ def test_write_version_1_0():
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)
View
461 numpy/lib/tests/test_io.py
@@ -4,31 +4,43 @@
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 @@ def roundtrip(self, save_func, *args, **kwargs):
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 @@ def test_multiple_arrays(self):
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 @@ def test_complex_arrays(self):
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 @@ def test_record(self):
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 @@ def test_array(self):
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 @@ def test_missing(self):
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 @@ def test_converters_with_usecols(self):
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 @@ def test_comments(self):
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 @@ def test_skiprows(self):
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 @@ def test_usecols(self):
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 @@ def test_fancy_dtype(self):
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 @@ def test_shaped_dtype(self):
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 @@ def test_empty_file(self):
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 @@ def test_empty_file(self):
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 @@ def test_dtype_with_object(self):
"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 @@ def test_dtype_with_object(self):
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 @@ def test_universal_newline(self):
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 @@ def test_ndmin_keyword(self):
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 @@ def test_ndmin_keyword(self):
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 @@ def test_ndmin_keyword(self):
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 @@ def test_ndmin_keyword(self):
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 @@ def test_record(self):
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 @@ def test_record_2(self):
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 @@ def test_record(self):
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 @@ def test_1D(self):
"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 @@ def test_skip_footer(self):
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 @@ def test_skip_footer_with_invalid(self):
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 @@ def test_skip_footer_with_invalid(self):
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 @@ def test_header(self):
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 @@ def test_auto_dtype(self):
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 @@ def test_auto_dtype_uniform(self):
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 @@ def test_names_overwrite(self):
"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 @@ def test_names_overwrite(self):
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 @@ def test_commented_header(self):
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 @@ def test_commented_header(self):
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 @@ def test_autonames_and_usecols(self):
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 @@ def test_converters_with_usecols(self):
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 @@ def test_converters_with_usecols_and_names(self):
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 @@ def test_unused_converter(self):
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 @@ def test_invalid_converter(self):
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 @@ def test_tricky_converter_bug1666(self):
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 @@ def test_dtype_with_object(self):
"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 @@ def test_dtype_with_object(self):
#
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 @@ def test_dtype_with_object(self):
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 @@ def test_userconverters_with_explicit_dtype(self):
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 @@ def test_spacedelimiter(self):
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 @@ def test_missing(self):
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)],)