Skip to content

CLN GH22875 Replace bare excepts by explicit excepts in pandas/io/ #22916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/io/clipboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
text, encoding=(kwargs.get('encoding') or
get_option('display.encoding'))
)
except:
except AttributeError:
pass

# Excel copies into clipboard with \t separation
Expand Down
8 changes: 4 additions & 4 deletions pandas/io/formats/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def check_main():

try:
return __IPYTHON__ or check_main() # noqa
except:
except NameError:
return check_main()


Expand All @@ -118,7 +118,7 @@ def in_qtconsole():
ip.config.get('IPKernelApp', {}).get('parent_appname', ""))
if 'qtconsole' in front_end.lower():
return True
except:
except NameError:
return False
return False

Expand All @@ -137,7 +137,7 @@ def in_ipnb():
ip.config.get('IPKernelApp', {}).get('parent_appname', ""))
if 'notebook' in front_end.lower():
return True
except:
except NameError:
return False
return False

Expand All @@ -149,7 +149,7 @@ def in_ipython_frontend():
try:
ip = get_ipython() # noqa
return 'zmq' in str(type(ip)).lower()
except:
except NameError:
pass

return False
10 changes: 5 additions & 5 deletions pandas/io/formats/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _get_terminal_size_windows():
h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
except:
except (AttributeError, ValueError):
return None
if res:
import struct
Expand Down Expand Up @@ -108,7 +108,7 @@ def _get_terminal_size_tput():
output = proc.communicate(input=None)
rows = int(output[0])
return (cols, rows)
except:
except OSError:
return None


Expand All @@ -120,7 +120,7 @@ def ioctl_GWINSZ(fd):
import struct
cr = struct.unpack(
'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
except (struct.error, IOError):
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
Expand All @@ -129,13 +129,13 @@ def ioctl_GWINSZ(fd):
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
except OSError:
pass
if not cr or cr == (0, 0):
try:
from os import environ as env
cr = (env['LINES'], env['COLUMNS'])
except:
except ValueError:
return None
return int(cr[1]), int(cr[0])

Expand Down
4 changes: 2 additions & 2 deletions pandas/io/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def read(fh):
if should_close:
try:
path_or_buf.close()
except: # noqa: flake8
except IOError: # noqa: flake8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the noqa shouldn't be necessary anymore

pass
return l

Expand Down Expand Up @@ -703,7 +703,7 @@ def create_block(b):
dtype = dtype_for(obj[u'dtype'])
try:
return dtype(obj[u'data'])
except:
except (ValueError, TypeError):
return dtype.type(obj[u'data'])
elif typ == u'np_complex':
return complex(obj[u'real'] + u'+' + obj[u'imag'] + u'j')
Expand Down
12 changes: 6 additions & 6 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def _read(filepath_or_buffer, kwds):
if should_close:
try:
filepath_or_buffer.close()
except: # noqa: flake8
except ValueError: # noqa: flake8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the noqa shouldn't be necessary anymore

pass

return data
Expand Down Expand Up @@ -1808,7 +1808,7 @@ def close(self):
# close additional handles opened by C parser (for compression)
try:
self._reader.close()
except:
except ValueError:
pass

def _set_noconvert_columns(self):
Expand Down Expand Up @@ -3034,7 +3034,7 @@ def converter(*date_cols):
errors='ignore',
infer_datetime_format=infer_datetime_format
)
except:
except ValueError:
return tools.to_datetime(
parsing.try_parse_dates(strs, dayfirst=dayfirst))
else:
Expand Down Expand Up @@ -3263,7 +3263,7 @@ def _floatify_na_values(na_values):
v = float(v)
if not np.isnan(v):
result.add(v)
except:
except (TypeError, ValueError, OverflowError):
pass
return result

Expand All @@ -3284,11 +3284,11 @@ def _stringify_na_values(na_values):
result.append(str(v))

result.append(v)
except:
except (TypeError, ValueError, OverflowError):
pass
try:
result.append(int(x))
except:
except (TypeError, ValueError, OverflowError):
pass
return set(result)

Expand Down
11 changes: 3 additions & 8 deletions pandas/io/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,11 @@ def try_read(path, encoding=None):
return read_wrapper(lambda f: pkl.load(f))
except Exception:
# reg/patched pickle
try:
return read_wrapper(
lambda f: pc.load(f, encoding=encoding, compat=False))
# compat pickle
except:
return read_wrapper(
lambda f: pc.load(f, encoding=encoding, compat=True))
return read_wrapper(
lambda f: pc.load(f, encoding=encoding, compat=False))
try:
return try_read(path)
except:
except Exception:
if PY3:
return try_read(path, encoding='latin1')
raise
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/sas/sas_xport.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def __init__(self, filepath_or_buffer, index=None, encoding='ISO-8859-1',
contents = filepath_or_buffer.read()
try:
contents = contents.encode(self._encoding)
except:
except UnicodeEncodeError:
pass
self.filepath_or_buffer = compat.BytesIO(contents)

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/sas/sasreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def read_sas(filepath_or_buffer, format=None, index=None, encoding=None,
format = "sas7bdat"
else:
raise ValueError("unable to infer format of SAS file")
except:
except ValueError:
pass

if format.lower() == 'xport':
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,

try:
_is_table_name = pandas_sql.has_table(sql)
except:
except (ImportError, AttributeError):
_is_table_name = False

if _is_table_name:
Expand Down Expand Up @@ -847,7 +847,7 @@ def _sqlalchemy_type(self, col):
try:
tz = col.tzinfo # noqa
return DateTime(timezone=True)
except:
except AttributeError:
return DateTime
if col_type == 'timedelta64':
warnings.warn("the 'timedelta' type is not supported, and will be "
Expand Down Expand Up @@ -1360,7 +1360,7 @@ def run_transaction(self):
try:
yield cur
self.con.commit()
except:
except Exception:
self.con.rollback()
raise
finally:
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,12 +1252,12 @@ def _read_old_header(self, first_char):

try:
self.typlist = [self.TYPE_MAP[typ] for typ in typlist]
except:
except ValueError:
raise ValueError("cannot convert stata types [{0}]"
.format(','.join(str(x) for x in typlist)))
try:
self.dtyplist = [self.DTYPE_MAP[typ] for typ in typlist]
except:
except ValueError:
raise ValueError("cannot convert stata dtypes [{0}]"
.format(','.join(str(x) for x in typlist)))

Expand Down