numpy/numpy — pull request #3953 by WarrenWeckesser (2013-10-20 15:17:23+00:00)
Title: MAINT: io: handle a bad fmt argument in savetxt by raising a ValueError.

Raise a ValueError when the `fmt` argument to `savetxt` is not a list, tuple or string.

Currenty, a bad `fmt` results in an `UnboundLocalError`:

```
In [1]: x = linspace(0,1,5)

In [2]: savetxt('x.txt', x, fmt=99)
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
in ()
----> 1 savetxt('x.txt', x, fmt=99)

/home/warren/anaconda/lib/python2.7/site-packages/numpy/lib/npyio.pyc in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments)
1045 else:
1046 for row in X:
-> 1047 fh.write(asbytes(format % tuple(row) + newline))
1048 if len(footer) > 0:
1049 footer = footer.replace('\n', '\n' + comments)

UnboundLocalError: local variable 'format' referenced before assignment
```
With this PR, a ValueError that states the problem is raised instead:

```
In [1]: x = linspace(0,1,5)

In [2]: savetxt('x.txt', x, fmt=99)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
----> 1 savetxt('x.txt', x, fmt=99)

/home/warren/local_scipy/lib/python2.7/site-packages/numpy/lib/npyio.pyc in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments)
1049 format = fmt
1050 else:
-> 1051 raise ValueError('invalid fmt: %r' % (fmt,))
1052
1053 if len(header) > 0:

ValueError: invalid fmt: 99
```