bpo-42426: IDLE: Fix reporting offset of the RE error in searchengine (GH-23447)

(cherry picked from commit 453bc1da20)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2020-11-21 21:24:11 -08:00 committed by GitHub
parent 6e665424d6
commit 28b40d7a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 9 deletions

View File

@ -3,6 +3,8 @@ Released on 2020-12-??
======================================
bpo-42426: Fix reporting offset of the RE error in searchengine.
bpo-42416: Get docstrings for IDLE calltips more often
by using inspect.getdoc.

View File

@ -175,11 +175,13 @@ class SearchEngineTest(unittest.TestCase):
engine.setpat('')
Equal(engine.getprog(), None)
Equal(Mbox.showerror.message,
'Error: Empty regular expression')
engine.setpat('+')
engine.revar.set(1)
Equal(engine.getprog(), None)
self.assertEqual(Mbox.showerror.message,
'Error: nothing to repeat at position 0\nPattern: +')
Equal(Mbox.showerror.message,
'Error: nothing to repeat\nPattern: +\nOffset: 0')
def test_report_error(self):
showerror = Mbox.showerror

View File

@ -84,20 +84,17 @@ class SearchEngine:
flags = flags | re.IGNORECASE
try:
prog = re.compile(pat, flags)
except re.error as what:
args = what.args
msg = args[0]
col = args[1] if len(args) >= 2 else -1
self.report_error(pat, msg, col)
except re.error as e:
self.report_error(pat, e.msg, e.pos)
return None
return prog
def report_error(self, pat, msg, col=-1):
def report_error(self, pat, msg, col=None):
# Derived class could override this with something fancier
msg = "Error: " + str(msg)
if pat:
msg = msg + "\nPattern: " + str(pat)
if col >= 0:
if col is not None:
msg = msg + "\nOffset: " + str(col)
tkMessageBox.showerror("Regular expression error",
msg, master=self.root)

View File

@ -0,0 +1 @@
Fix reporting offset of the RE error in searchengine.