Skip to content

Commit 1ebee37

Browse files
authored
bpo-33610: validate non-negative integer inputs in IDLE's config (GH-14822)
1 parent 4e16a4a commit 1ebee37

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

Lib/idlelib/configdialog.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
Refer to comments in EditorWindow autoindent code for details.
1010
1111
"""
12+
import re
13+
1214
from tkinter import (Toplevel, Listbox, Text, Scale, Canvas,
1315
StringVar, BooleanVar, IntVar, TRUE, FALSE,
1416
TOP, BOTTOM, RIGHT, LEFT, SOLID, GROOVE,
@@ -1764,9 +1766,18 @@ class GenPage(Frame):
17641766

17651767
def __init__(self, master):
17661768
super().__init__(master)
1769+
1770+
self.init_validators()
17671771
self.create_page_general()
17681772
self.load_general_cfg()
17691773

1774+
def init_validators(self):
1775+
digits_or_empty_re = re.compile(r'[0-9]*')
1776+
def is_digits_or_empty(s):
1777+
"Return 's is blank or contains only digits'"
1778+
return digits_or_empty_re.fullmatch(s) is not None
1779+
self.digits_only = (self.register(is_digits_or_empty), '%P',)
1780+
17701781
def create_page_general(self):
17711782
"""Return frame of widgets for General tab.
17721783
@@ -1883,16 +1894,23 @@ def create_page_general(self):
18831894
frame_win_size, text='Initial Window Size (in characters)')
18841895
win_width_title = Label(frame_win_size, text='Width')
18851896
self.win_width_int = Entry(
1886-
frame_win_size, textvariable=self.win_width, width=3)
1897+
frame_win_size, textvariable=self.win_width, width=3,
1898+
validatecommand=self.digits_only, validate='key',
1899+
)
18871900
win_height_title = Label(frame_win_size, text='Height')
18881901
self.win_height_int = Entry(
1889-
frame_win_size, textvariable=self.win_height, width=3)
1902+
frame_win_size, textvariable=self.win_height, width=3,
1903+
validatecommand=self.digits_only, validate='key',
1904+
)
18901905

18911906
frame_autocomplete = Frame(frame_window, borderwidth=0,)
18921907
auto_wait_title = Label(frame_autocomplete,
18931908
text='Completions Popup Wait (milliseconds)')
18941909
self.auto_wait_int = Entry(frame_autocomplete, width=6,
1895-
textvariable=self.autocomplete_wait)
1910+
textvariable=self.autocomplete_wait,
1911+
validatecommand=self.digits_only,
1912+
validate='key',
1913+
)
18961914

18971915
frame_paren1 = Frame(frame_window, borderwidth=0)
18981916
paren_style_title = Label(frame_paren1, text='Paren Match Style')
@@ -1922,20 +1940,26 @@ def create_page_general(self):
19221940
format_width_title = Label(frame_format,
19231941
text='Format Paragraph Max Width')
19241942
self.format_width_int = Entry(
1925-
frame_format, textvariable=self.format_width, width=4)
1943+
frame_format, textvariable=self.format_width, width=4,
1944+
validatecommand=self.digits_only, validate='key',
1945+
)
19261946

19271947
frame_context = Frame(frame_editor, borderwidth=0)
19281948
context_title = Label(frame_context, text='Max Context Lines :')
19291949
self.context_int = Entry(
1930-
frame_context, textvariable=self.context_lines, width=3)
1950+
frame_context, textvariable=self.context_lines, width=3,
1951+
validatecommand=self.digits_only, validate='key',
1952+
)
19311953

19321954
# Frame_shell.
19331955
frame_auto_squeeze_min_lines = Frame(frame_shell, borderwidth=0)
19341956
auto_squeeze_min_lines_title = Label(frame_auto_squeeze_min_lines,
19351957
text='Auto-Squeeze Min. Lines:')
19361958
self.auto_squeeze_min_lines_int = Entry(
1937-
frame_auto_squeeze_min_lines, width=4,
1938-
textvariable=self.auto_squeeze_min_lines)
1959+
frame_auto_squeeze_min_lines, width=4,
1960+
textvariable=self.auto_squeeze_min_lines,
1961+
validatecommand=self.digits_only, validate='key',
1962+
)
19391963

19401964
# frame_help.
19411965
frame_helplist = Frame(frame_help)

0 commit comments

Comments
 (0)