Skip to content

Commit

Permalink
BUG: ticket #1793, fix failing npyio test under py3k. Thanks to Derek…
Browse files Browse the repository at this point in the history
… Homeier.
  • Loading branch information
rgommers authored and charris committed Apr 30, 2012
1 parent 9a495e7 commit 2f33723
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
7 changes: 5 additions & 2 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ def split_line(line):
# End of lines reached
first_line = ''
first_vals = []
warnings.warn('loadtxt: Empty input file: "%s"' % fname)
N = len(usecols or first_vals)

dtype_types, packing = flatten_dtype(dtype)
Expand Down Expand Up @@ -1299,8 +1300,10 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
first_line = asbytes('').join(first_line.split(comments)[1:])
first_values = split_line(first_line)
except StopIteration:
# might want to return empty array instead of raising error.
raise IOError('End-of-file reached before encountering data.')
# return an empty array if the datafile is empty
first_line = asbytes('')
first_values = []
warnings.warn('genfromtxt: Empty input file: "%s"' % fname)

# Should we take the first values as names ?
if names is True:
Expand Down
15 changes: 11 additions & 4 deletions numpy/lib/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ def test_3d_shaped_dtype(self):
assert_array_equal(x, a)

def test_empty_file(self):
warnings.filterwarnings("ignore", message="loadtxt: Empty input file:")
c = StringIO()
x = np.loadtxt(c)
assert_equal(x.shape, (0,))
Expand Down Expand Up @@ -987,11 +988,17 @@ def test_usecols_with_named_columns(self):
usecols=('a', 'c'), **kwargs)
assert_equal(test, ctrl)


def test_empty_file(self):
"Test that an empty file raises the proper exception"
data = StringIO()
assert_raises(IOError, np.ndfromtxt, data)
"Test that an empty file raises the proper warning."
warn_ctx = WarningManager()
warn_ctx.__enter__()
try:
warnings.filterwarnings("ignore", message="genfromtxt: Empty input file:")
data = StringIO()
test = np.genfromtxt(data)
assert_equal(test, np.array([]))
finally:
warn_ctx.__exit__()


def test_fancy_dtype_alt(self):
Expand Down

0 comments on commit 2f33723

Please sign in to comment.