Skip to content

Commit d44f24d

Browse files
mdcutonemdcutone
authored andcommitted
BF: Fixes exceptions in the console being displayed in an error dialog.
1 parent 3954fa4 commit d44f24d

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

psychopy/app/coder/coder.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from ..utils import FileDropTarget, PsychopyToolbar, FrameSwitcher
4040
from psychopy.projects import pavlovia
4141
import psychopy.app.pavlovia_ui.menu
42+
from psychopy.app.errorDlg import exceptionCallback
4243
from psychopy.app.coder.codeEditorBase import BaseCodeEditor
4344
from psychopy.app.coder.fileBrowser import FileBrowserPanel
4445
from psychopy.app.coder.sourceTree import SourceTreePanel
@@ -103,17 +104,39 @@ def fromPickle(filename):
103104

104105

105106
class PsychopyPyShell(wx.py.shell.Shell, ThemeMixin):
106-
'''Simple class wrapper for Pyshell which uses the Psychopy ThemeMixin'''
107+
"""Simple class wrapper for Pyshell which uses the Psychopy ThemeMixin."""
107108
def __init__(self, coder):
108109
msg = _translate('PyShell in PsychoPy - type some commands!')
109-
wx.py.shell.Shell.__init__(self, coder.shelf, -1, introText=msg + '\n\n', style=wx.BORDER_NONE)
110+
wx.py.shell.Shell.__init__(
111+
self, coder.shelf, -1, introText=msg + '\n\n', style=wx.BORDER_NONE)
110112
self.prefs = coder.prefs
111113
self.paths = coder.paths
112114
self.app = coder.app
113115

116+
self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
117+
self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
118+
114119
# Set theme to match code editor
115120
self._applyAppTheme()
116121

122+
def OnSetFocus(self, evt=None):
123+
"""Called when the shell gets focus."""
124+
# Switch to the default callback when in console, prevents the PsychoPy
125+
# error dialog from opening.
126+
sys.excepthook = sys.__excepthook__
127+
128+
if evt:
129+
evt.Skip()
130+
131+
def OnKillFocus(self, evt=None):
132+
"""Called when the shell loses focus."""
133+
# Set the callback to use the dialog when errors occur outside the
134+
# shell.
135+
sys.excepthook = exceptionCallback
136+
137+
if evt:
138+
evt.Skip()
139+
117140
def GetContextMenu(self):
118141
"""Override original method (wx.py.shell.Shell.GetContextMenu)
119142
to localize context menu. Simply added _translate() to
@@ -1069,9 +1092,6 @@ def DoFindNext(self, findData, findDlg=None):
10691092
start = 0
10701093
loc = textstring.find(findstring, start)
10711094

1072-
# Adjust for offset
1073-
loc += 2
1074-
10751095
# was it still not found?
10761096
if loc == -1:
10771097
dlg = dialogs.MessageDialog(self, message=_translate(

0 commit comments

Comments
 (0)