Skip to content
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

PR: Prevent potential problems when importing third-party modules #4765

Merged
merged 3 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions spyder/app/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def main():
else:
os.environ['QT_SCREEN_SCALE_FACTORS'] = ''

# Prevent Spyder from crashing in macOS if locale is not defined
if sys.platform == 'darwin':
LANG = os.environ.get('LANG')
LC_ALL = os.environ.get('LC_ALL')
if bool(LANG) and not bool(LC_ALL):
LC_ALL = LANG
elif not bool(LANG) and bool(LC_ALL):
LANG = LC_ALL
else:
LANG = LC_ALL = 'en_US.UTF-8'

os.environ['LANG'] = LANG
os.environ['LC_ALL'] = LC_ALL

if CONF.get('main', 'single_instance') and not options.new_instance \
and not options.reset_config_files and not running_in_mac_app():
# Minimal delay (0.1-0.2 secs) to avoid that several
Expand Down
6 changes: 3 additions & 3 deletions spyder/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,18 @@ def get_supported_types():
try:
from numpy import ndarray, matrix, generic
editable_types += [ndarray, matrix, generic]
except ImportError:
except:
pass
try:
from pandas import DataFrame, Series, DatetimeIndex
editable_types += [DataFrame, Series, DatetimeIndex]
except ImportError:
except:
pass
picklable_types = editable_types[:]
try:
from spyder.pil_patch import Image
editable_types.append(Image.Image)
except ImportError:
except:
pass
return dict(picklable=picklable_types, editable=editable_types)

Expand Down
2 changes: 1 addition & 1 deletion spyder/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

try:
from guiqwt.pyplot import *
except (ImportError, AssertionError):
except:
from matplotlib.pyplot import *
4 changes: 2 additions & 2 deletions spyder/utils/ipython/spyder_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,15 @@ def _is_array(self, var):
try:
import numpy
return isinstance(var, numpy.ndarray)
except ImportError:
except:
return False

def _is_image(self, var):
"""Return True if variable is a PIL.Image image"""
try:
from PIL import Image
return isinstance(var, Image.Image)
except ImportError:
except:
return False

def _is_data_frame(self, var):
Expand Down
14 changes: 7 additions & 7 deletions spyder/utils/site/sitecustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def execfile(filename, namespace):
except (ValueError, TypeError):
# Code page number in locale is not valid
pass
except ImportError:
except:
pass


Expand Down Expand Up @@ -173,15 +173,15 @@ def execfile(filename, namespace):
import pyximport
HAS_PYXIMPORT = True
pyx_setup_args = {}
except ImportError:
except:
HAS_PYXIMPORT = False

if HAS_PYXIMPORT:
# Add Numpy include dir to pyximport/distutils
try:
import numpy
pyx_setup_args['include_dirs'] = numpy.get_include()
except ImportError:
except:
pass

# Setup pyximport and enable Cython files reload
Expand All @@ -191,7 +191,7 @@ def execfile(filename, namespace):
# Import cython_inline for runfile function
from Cython.Build.Inline import cython_inline
HAS_CYTHON = True
except ImportError:
except:
HAS_CYTHON = False


Expand All @@ -200,7 +200,7 @@ def execfile(filename, namespace):
#==============================================================================
try:
import sitecustomize #analysis:ignore
except ImportError:
except:
pass


Expand Down Expand Up @@ -234,7 +234,7 @@ def _getfilesystemencoding_wrapper():
#==============================================================================
try:
import matplotlib
except (ImportError, UnicodeDecodeError):
except:
matplotlib = None # analysis:ignore


Expand Down Expand Up @@ -290,7 +290,7 @@ def __init__(self, *args, **kwargs):
warnings.filterwarnings(action='ignore', category=RuntimeWarning,
module='pandas.formats.format',
message=".*invalid value encountered in.*")
except (ImportError, AttributeError):
except:
pass


Expand Down
4 changes: 2 additions & 2 deletions spyder/widgets/variableexplorer/collectionseditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,13 +1007,13 @@ def __prepare_plot(self):
try:
import guiqwt.pyplot #analysis:ignore
return True
except (ImportError, AssertionError):
except:
try:
if 'matplotlib' not in sys.modules:
import matplotlib
matplotlib.use("Qt4Agg")
return True
except ImportError:
except:
QMessageBox.warning(self, _("Import error"),
_("Please install <b>matplotlib</b>"
" or <b>guiqwt</b>."))
Expand Down
8 changes: 5 additions & 3 deletions spyder/widgets/variableexplorer/importwizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
QSizePolicy, QSpacerItem, QTableView, QTabWidget,
QTextEdit, QVBoxLayout, QWidget)

# If pandas fails to import here (for any reason), Spyder
# will crash at startup.
try:
import pandas as pd
except ImportError:
except:
pd = None

# Local import
Expand Down Expand Up @@ -58,7 +60,7 @@ class FakeObject(object):
pass
try:
from numpy import ndarray, array
except ImportError:
except:
class ndarray(FakeObject): # analysis:ignore
"""Fake ndarray"""
pass
Expand All @@ -67,7 +69,7 @@ class ndarray(FakeObject): # analysis:ignore
import datetime
try:
from dateutil.parser import parse as dateparse
except ImportError:
except:
def dateparse(datestr, dayfirst=True): # analysis:ignore
"""Just for 'day/month/year' strings"""
_a, _b, _c = list(map(int, datestr.split('/')))
Expand Down
13 changes: 8 additions & 5 deletions spyder/widgets/variableexplorer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FakeObject(object):
from numpy.ma import MaskedArray
from numpy import savetxt as np_savetxt
from numpy import get_printoptions, set_printoptions
except ImportError:
except:
ndarray = array = matrix = recarray = MaskedArray = np_savetxt = \
int64 = int32 = float64 = float32 = complex64 = complex128 = FakeObject

Expand Down Expand Up @@ -81,7 +81,10 @@ def get_numpy_dtype(obj):
# Pandas support
#==============================================================================
if programs.is_module_installed('pandas', PANDAS_REQVER):
from pandas import DataFrame, DatetimeIndex, Series
try:
from pandas import DataFrame, DatetimeIndex, Series
except:
DataFrame = DatetimeIndex = Series = FakeObject
else:
DataFrame = DatetimeIndex = Series = FakeObject # analysis:ignore

Expand All @@ -92,7 +95,7 @@ def get_numpy_dtype(obj):
try:
from spyder import pil_patch
Image = pil_patch.Image.Image
except ImportError:
except:
Image = FakeObject # analysis:ignore


Expand All @@ -102,7 +105,7 @@ def get_numpy_dtype(obj):
try:
import bs4
NavigableString = bs4.element.NavigableString
except (ImportError, AttributeError):
except:
NavigableString = FakeObject # analysis:ignore


Expand Down Expand Up @@ -168,7 +171,7 @@ def get_object_attrs(obj):

try:
from dateutil.parser import parse as dateparse
except ImportError:
except:
def dateparse(datestr): # analysis:ignore
"""Just for 'year, month, day' strings"""
return datetime.datetime( *list(map(int, datestr.split(','))) )
Expand Down