diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2018-08-10 13:23:23 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2018-08-10 16:06:41 -0600 |
commit | 007db13f45b378b3c7d6fe9c2d79d8ef1c700bbd (patch) | |
tree | 3b68fc838dc58db27720db4768d3e2510992fe0b | |
parent | c0b4340c698c486e459a0a2c80706c78329c64fc (diff) | |
download | numpy-007db13f45b378b3c7d6fe9c2d79d8ef1c700bbd.tar.gz |
BUG: Fix regression in loadtxt for bz2 text files in Python 2.
Text bz2 files are not well supported in Python2, and, following the
NumPy upgrade in text handling, loadtxt was raising an error when they
were detected. This led to problems for those few who were using such
files. This patch is a quick fix that issues a RuntimeWarning, then
opens those files under the assumption that they are latin1 encoded.
Caveat emptor.
Closes #11633.
-rw-r--r-- | numpy/lib/_datasource.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test__datasource.py | 23 |
2 files changed, 27 insertions, 4 deletions
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py index 6f1295f09..ab00b1444 100644 --- a/numpy/lib/_datasource.py +++ b/numpy/lib/_datasource.py @@ -37,6 +37,7 @@ from __future__ import division, absolute_import, print_function import os import sys +import warnings import shutil import io @@ -85,9 +86,10 @@ def _python2_bz2open(fn, mode, encoding, newline): if "t" in mode: # BZ2File is missing necessary functions for TextIOWrapper - raise ValueError("bz2 text files not supported in python2") - else: - return bz2.BZ2File(fn, mode) + warnings.warn("Assuming latin1 encoding for bz2 text file in Python2", + RuntimeWarning, stacklevel=5) + mode = mode.replace("t", "") + return bz2.BZ2File(fn, mode) def _python2_gzipopen(fn, mode, encoding, newline): """ Wrapper to open gzip in text mode. diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py index 32812990c..70fff3bb0 100644 --- a/numpy/lib/tests/test__datasource.py +++ b/numpy/lib/tests/test__datasource.py @@ -2,11 +2,14 @@ from __future__ import division, absolute_import, print_function import os import sys +import pytest from tempfile import mkdtemp, mkstemp, NamedTemporaryFile from shutil import rmtree -from numpy.testing import assert_, assert_equal, assert_raises, SkipTest import numpy.lib._datasource as datasource +from numpy.testing import ( + assert_, assert_equal, assert_raises, assert_warns, SkipTest + ) if sys.version_info[0] >= 3: import urllib.request as urllib_request @@ -161,6 +164,24 @@ class TestDataSourceOpen(object): fp.close() assert_equal(magic_line, result) + @pytest.mark.skipif(sys.version_info[0] >= 3, reason="Python 2 only") + def test_Bz2File_text_mode_warning(self): + try: + import bz2 + except ImportError: + # We don't have the bz2 capabilities to test. + raise SkipTest + # Test datasource's internal file_opener for BZip2 files. + filepath = os.path.join(self.tmpdir, 'foobar.txt.bz2') + fp = bz2.BZ2File(filepath, 'w') + fp.write(magic_line) + fp.close() + with assert_warns(RuntimeWarning): + fp = self.ds.open(filepath, 'rt') + result = fp.readline() + fp.close() + assert_equal(magic_line, result) + class TestDataSourceExists(object): def setup(self): |