summaryrefslogtreecommitdiff
path: root/numpy/lib/npyio.py
diff options
context:
space:
mode:
authorWarren Weckesser <warren.weckesser@gmail.com>2020-06-18 07:36:36 -0400
committerWarren Weckesser <warren.weckesser@gmail.com>2020-06-18 07:36:36 -0400
commitfa1a3ecd22c849f9066e2b712cc6bb21c36e57ee (patch)
treeb7aa69db45f0efbcd9e5bc2da548f743220d1aeb /numpy/lib/npyio.py
parent2d31d06d5f03782f4319aefd20a66bd4196db7c3 (diff)
downloadnumpy-fa1a3ecd22c849f9066e2b712cc6bb21c36e57ee.tar.gz
MAINT: lib: In loadtxt, move some code out of a try/finally block.
In loadtxt, there is a try/finally block that ensures that the file is closed if it was opened in the function. Some code that did not need to be in that block was moved up, outside the try/finally block.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r--numpy/lib/npyio.py13
1 files changed, 8 insertions, 5 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index d681d7d12..f138c4cca 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1064,6 +1064,12 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
# Fall back to existing code
usecols = usecols_as_list
+ # Make sure we're dealing with a proper dtype
+ dtype = np.dtype(dtype)
+ defconv = _getconv(dtype)
+
+ dtype_types, packing = flatten_dtype_internal(dtype)
+
fown = False
try:
if isinstance(fname, os_PathLike):
@@ -1089,10 +1095,6 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
fencoding = locale.getpreferredencoding()
try:
- # Make sure we're dealing with a proper dtype
- dtype = np.dtype(dtype)
- defconv = _getconv(dtype)
-
# Skip the first `skiprows` lines
for i in range(skiprows):
next(fh)
@@ -1111,7 +1113,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
warnings.warn('loadtxt: Empty input file: "%s"' % fname, stacklevel=2)
N = len(usecols or first_vals)
- dtype_types, packing = flatten_dtype_internal(dtype)
+ # Now that we know N, create the default converters list, and
+ # set packing, if necessary.
if len(dtype_types) > 1:
# We're dealing with a structured array, each field of
# the dtype matches a column