Skip to content

Commit dcfa60e

Browse files
authored
Merge pull request #4765 from ccordoba12/fix-crash-on-imports
PR: Prevent potential problems when importing third-party modules
2 parents a69e9c0 + ec9fdbf commit dcfa60e

File tree

8 files changed

+42
-23
lines changed

8 files changed

+42
-23
lines changed

Diff for: spyder/app/start.py

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ def main():
7676
else:
7777
os.environ['QT_SCREEN_SCALE_FACTORS'] = ''
7878

79+
# Prevent Spyder from crashing in macOS if locale is not defined
80+
if sys.platform == 'darwin':
81+
LANG = os.environ.get('LANG')
82+
LC_ALL = os.environ.get('LC_ALL')
83+
if bool(LANG) and not bool(LC_ALL):
84+
LC_ALL = LANG
85+
elif not bool(LANG) and bool(LC_ALL):
86+
LANG = LC_ALL
87+
else:
88+
LANG = LC_ALL = 'en_US.UTF-8'
89+
90+
os.environ['LANG'] = LANG
91+
os.environ['LC_ALL'] = LC_ALL
92+
7993
if CONF.get('main', 'single_instance') and not options.new_instance \
8094
and not options.reset_config_files and not running_in_mac_app():
8195
# Minimal delay (0.1-0.2 secs) to avoid that several

Diff for: spyder/config/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -386,18 +386,18 @@ def get_supported_types():
386386
try:
387387
from numpy import ndarray, matrix, generic
388388
editable_types += [ndarray, matrix, generic]
389-
except ImportError:
389+
except:
390390
pass
391391
try:
392392
from pandas import DataFrame, Series, DatetimeIndex
393393
editable_types += [DataFrame, Series, DatetimeIndex]
394-
except ImportError:
394+
except:
395395
pass
396396
picklable_types = editable_types[:]
397397
try:
398398
from spyder.pil_patch import Image
399399
editable_types.append(Image.Image)
400-
except ImportError:
400+
except:
401401
pass
402402
return dict(picklable=picklable_types, editable=editable_types)
403403

Diff for: spyder/pyplot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
try:
77
from guiqwt.pyplot import *
8-
except (ImportError, AssertionError):
8+
except:
99
from matplotlib.pyplot import *

Diff for: spyder/utils/ipython/spyder_kernel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,15 @@ def _is_array(self, var):
328328
try:
329329
import numpy
330330
return isinstance(var, numpy.ndarray)
331-
except ImportError:
331+
except:
332332
return False
333333

334334
def _is_image(self, var):
335335
"""Return True if variable is a PIL.Image image"""
336336
try:
337337
from PIL import Image
338338
return isinstance(var, Image.Image)
339-
except ImportError:
339+
except:
340340
return False
341341

342342
def _is_data_frame(self, var):

Diff for: spyder/utils/site/sitecustomize.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def execfile(filename, namespace):
133133
except (ValueError, TypeError):
134134
# Code page number in locale is not valid
135135
pass
136-
except ImportError:
136+
except:
137137
pass
138138

139139

@@ -173,15 +173,15 @@ def execfile(filename, namespace):
173173
import pyximport
174174
HAS_PYXIMPORT = True
175175
pyx_setup_args = {}
176-
except ImportError:
176+
except:
177177
HAS_PYXIMPORT = False
178178

179179
if HAS_PYXIMPORT:
180180
# Add Numpy include dir to pyximport/distutils
181181
try:
182182
import numpy
183183
pyx_setup_args['include_dirs'] = numpy.get_include()
184-
except ImportError:
184+
except:
185185
pass
186186

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

197197

@@ -200,7 +200,7 @@ def execfile(filename, namespace):
200200
#==============================================================================
201201
try:
202202
import sitecustomize #analysis:ignore
203-
except ImportError:
203+
except:
204204
pass
205205

206206

@@ -234,7 +234,7 @@ def _getfilesystemencoding_wrapper():
234234
#==============================================================================
235235
try:
236236
import matplotlib
237-
except (ImportError, UnicodeDecodeError):
237+
except:
238238
matplotlib = None # analysis:ignore
239239

240240

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

296296

Diff for: spyder/widgets/variableexplorer/collectionseditor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1007,13 +1007,13 @@ def __prepare_plot(self):
10071007
try:
10081008
import guiqwt.pyplot #analysis:ignore
10091009
return True
1010-
except (ImportError, AssertionError):
1010+
except:
10111011
try:
10121012
if 'matplotlib' not in sys.modules:
10131013
import matplotlib
10141014
matplotlib.use("Qt4Agg")
10151015
return True
1016-
except ImportError:
1016+
except:
10171017
QMessageBox.warning(self, _("Import error"),
10181018
_("Please install <b>matplotlib</b>"
10191019
" or <b>guiqwt</b>."))

Diff for: spyder/widgets/variableexplorer/importwizard.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
QSizePolicy, QSpacerItem, QTableView, QTabWidget,
2323
QTextEdit, QVBoxLayout, QWidget)
2424

25+
# If pandas fails to import here (for any reason), Spyder
26+
# will crash at startup.
2527
try:
2628
import pandas as pd
27-
except ImportError:
29+
except:
2830
pd = None
2931

3032
# Local import
@@ -58,7 +60,7 @@ class FakeObject(object):
5860
pass
5961
try:
6062
from numpy import ndarray, array
61-
except ImportError:
63+
except:
6264
class ndarray(FakeObject): # analysis:ignore
6365
"""Fake ndarray"""
6466
pass
@@ -67,7 +69,7 @@ class ndarray(FakeObject): # analysis:ignore
6769
import datetime
6870
try:
6971
from dateutil.parser import parse as dateparse
70-
except ImportError:
72+
except:
7173
def dateparse(datestr, dayfirst=True): # analysis:ignore
7274
"""Just for 'day/month/year' strings"""
7375
_a, _b, _c = list(map(int, datestr.split('/')))

Diff for: spyder/widgets/variableexplorer/utils.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class FakeObject(object):
5353
from numpy.ma import MaskedArray
5454
from numpy import savetxt as np_savetxt
5555
from numpy import get_printoptions, set_printoptions
56-
except ImportError:
56+
except:
5757
ndarray = array = matrix = recarray = MaskedArray = np_savetxt = \
5858
int64 = int32 = float64 = float32 = complex64 = complex128 = FakeObject
5959

@@ -81,7 +81,10 @@ def get_numpy_dtype(obj):
8181
# Pandas support
8282
#==============================================================================
8383
if programs.is_module_installed('pandas', PANDAS_REQVER):
84-
from pandas import DataFrame, DatetimeIndex, Series
84+
try:
85+
from pandas import DataFrame, DatetimeIndex, Series
86+
except:
87+
DataFrame = DatetimeIndex = Series = FakeObject
8588
else:
8689
DataFrame = DatetimeIndex = Series = FakeObject # analysis:ignore
8790

@@ -92,7 +95,7 @@ def get_numpy_dtype(obj):
9295
try:
9396
from spyder import pil_patch
9497
Image = pil_patch.Image.Image
95-
except ImportError:
98+
except:
9699
Image = FakeObject # analysis:ignore
97100

98101

@@ -102,7 +105,7 @@ def get_numpy_dtype(obj):
102105
try:
103106
import bs4
104107
NavigableString = bs4.element.NavigableString
105-
except (ImportError, AttributeError):
108+
except:
106109
NavigableString = FakeObject # analysis:ignore
107110

108111

@@ -168,7 +171,7 @@ def get_object_attrs(obj):
168171

169172
try:
170173
from dateutil.parser import parse as dateparse
171-
except ImportError:
174+
except:
172175
def dateparse(datestr): # analysis:ignore
173176
"""Just for 'year, month, day' strings"""
174177
return datetime.datetime( *list(map(int, datestr.split(','))) )

0 commit comments

Comments
 (0)