diff --git a/pandas/io/clipboards.py b/pandas/io/clipboards.py index 0d564069c681f..70c978a3b62ed 100644 --- a/pandas/io/clipboards.py +++ b/pandas/io/clipboards.py @@ -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 diff --git a/pandas/io/formats/console.py b/pandas/io/formats/console.py index b8b28a0b0c98c..e347f6bce0168 100644 --- a/pandas/io/formats/console.py +++ b/pandas/io/formats/console.py @@ -100,7 +100,7 @@ def check_main(): try: return __IPYTHON__ or check_main() # noqa - except: + except NameError: return check_main() @@ -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 @@ -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 @@ -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 diff --git a/pandas/io/formats/terminal.py b/pandas/io/formats/terminal.py index dcd6f2cf4a718..2846525adbe6b 100644 --- a/pandas/io/formats/terminal.py +++ b/pandas/io/formats/terminal.py @@ -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 @@ -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 @@ -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) @@ -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, KeyError): return None return int(cr[1]), int(cr[0]) diff --git a/pandas/io/packers.py b/pandas/io/packers.py index 7a1e72637f4ce..77fbb27d01f86 100644 --- a/pandas/io/packers.py +++ b/pandas/io/packers.py @@ -194,7 +194,7 @@ def read(fh): if should_close: try: path_or_buf.close() - except: # noqa: flake8 + except IOError: pass return l @@ -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') diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index a4f1155117b12..2def3b81c9518 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -459,7 +459,7 @@ def _read(filepath_or_buffer, kwds): if should_close: try: filepath_or_buffer.close() - except: # noqa: flake8 + except ValueError: pass return data @@ -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): @@ -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: @@ -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 @@ -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) diff --git a/pandas/io/pickle.py b/pandas/io/pickle.py index 9c219d7fd6997..d52a571da0d61 100644 --- a/pandas/io/pickle.py +++ b/pandas/io/pickle.py @@ -163,18 +163,20 @@ def try_read(path, encoding=None): # We want to silence any warnings about, e.g. moved modules. warnings.simplefilter("ignore", Warning) return read_wrapper(lambda f: pkl.load(f)) - except Exception: + except Exception: # noqa: E722 # reg/patched pickle + # compat not used in pandas/compat/pickle_compat.py::load + # TODO: remove except block OR modify pc.load to use compat try: return read_wrapper( lambda f: pc.load(f, encoding=encoding, compat=False)) # compat pickle - except: + except Exception: # noqa: E722 return read_wrapper( lambda f: pc.load(f, encoding=encoding, compat=True)) try: return try_read(path) - except: + except Exception: # noqa: E722 if PY3: return try_read(path, encoding='latin1') raise diff --git a/pandas/io/sas/sas_xport.py b/pandas/io/sas/sas_xport.py index 14e7ad9682db6..385396909a07b 100644 --- a/pandas/io/sas/sas_xport.py +++ b/pandas/io/sas/sas_xport.py @@ -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) diff --git a/pandas/io/sas/sasreader.py b/pandas/io/sas/sasreader.py index b8a0bf5733158..d72996a8e6157 100644 --- a/pandas/io/sas/sasreader.py +++ b/pandas/io/sas/sasreader.py @@ -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': diff --git a/pandas/io/sql.py b/pandas/io/sql.py index a582d32741ae9..882fa0092b2cf 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -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: @@ -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 " @@ -1360,7 +1360,7 @@ def run_transaction(self): try: yield cur self.con.commit() - except: + except Exception: self.con.rollback() raise finally: diff --git a/pandas/io/stata.py b/pandas/io/stata.py index efd5f337fdf69..a321e315f5225 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -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)))