Skip to content

Commit 5ed1c6b

Browse files
authored
Merge f678111 into 563e38f
2 parents 563e38f + f678111 commit 5ed1c6b

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

source/gui/speechDict.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# -*- coding: UTF-8 -*-
22
# A part of NonVisual Desktop Access (NVDA)
3-
# Copyright (C) 2007-2022 NV Access Limited, Peter Vágner, Mesar Hameed, Joseph Lee,
3+
# Copyright (C) 2007-2023 NV Access Limited, Peter Vágner, Mesar Hameed, Joseph Lee,
44
# Aaron Cannon, Ethan Holliger, Julien Cochuyt, Thomas Stivers, Cyrille Bougot, Aleksey Sadovoy
55
# This file is covered by the GNU General Public License.
66
# See the file COPYING for more details.
77

8+
from re import error as RegexpError
9+
810
from abc import abstractmethod
911
import wx
1012

@@ -94,25 +96,44 @@ def onOk(self, evt):
9496
)
9597
self.patternTextCtrl.SetFocus()
9698
return
99+
entryType = self.getType()
97100
try:
98101
dictEntry = self.dictEntry = speechDictHandler.SpeechDictEntry(
99102
self.patternTextCtrl.GetValue(),
100103
self.replacementTextCtrl.GetValue(),
101104
self.commentTextCtrl.GetValue(),
102105
bool(self.caseSensitiveCheckBox.GetValue()),
103-
self.getType()
106+
entryType,
104107
)
108+
except RegexpError as e:
109+
log.debugWarning(f"Could not add dictionary entry due to regex error in the pattern field : {e}")
110+
if entryType != speechDictHandler.ENTRY_TYPE_REGEXP:
111+
raise e
112+
gui.messageBox(
113+
# Translators: This is an error message to let the user know that the dictionary entry is not valid.
114+
_("Regular Expression error in the pattern field: \"{error}\".").format(error=e),
115+
# Translators: The title of an error message raised by the Dictionary Entry dialog
116+
_("Dictionary Entry Error"),
117+
wx.OK | wx.ICON_WARNING,
118+
self
119+
)
120+
self.patternTextCtrl.SetFocus()
121+
return
122+
try:
105123
dictEntry.sub("test") # Ensure there are no grouping error (#11407)
106-
except Exception as e:
107-
log.debugWarning("Could not add dictionary entry due to (regex error) : %s" % e)
124+
except RegexpError as e:
125+
log.debugWarning(f"Could not add dictionary entry due to regex error in the replacement field : {e}")
126+
if entryType != speechDictHandler.ENTRY_TYPE_REGEXP:
127+
raise e
108128
gui.messageBox(
109129
# Translators: This is an error message to let the user know that the dictionary entry is not valid.
110-
_("Regular Expression error: \"%s\".") % e,
130+
_("Regular Expression error in the replacement field: \"{error}\".").format(error=e),
111131
# Translators: The title of an error message raised by the Dictionary Entry dialog
112132
_("Dictionary Entry Error"),
113133
wx.OK | wx.ICON_WARNING,
114134
self
115135
)
136+
self.replacementTextCtrl.SetFocus()
116137
return
117138
evt.Skip()
118139

source/speechDictHandler/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#speechDictHandler.py
2-
#A part of NonVisual Desktop Access (NVDA)
3-
#Copyright (C) 2006-2017 NVDA Contributors <http://www.nvda-project.org/>
4-
#This file is covered by the GNU General Public License.
5-
#See the file COPYING for more details.
1+
# A part of NonVisual Desktop Access (NVDA)
2+
# Copyright (C) 2006-2023 NVDA Contributors <http://www.nvda-project.org/>
3+
# This file is covered by the GNU General Public License.
4+
# See the file COPYING for more details.
65

76
import re
87
import globalVars
@@ -42,7 +41,10 @@ def __init__(self, pattern, replacement,comment,caseSensitive=True,type=ENTRY_TY
4241
self.type=type
4342

4443
def sub(self, text):
45-
replacement=self.replacement
44+
if self.type == ENTRY_TYPE_REGEXP:
45+
replacement = self.replacement
46+
else:
47+
replacement = re.escape(self.replacement)
4648
return self.compiled.sub(replacement, text)
4749

4850
class SpeechDict(list):

0 commit comments

Comments
 (0)