Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1317e14

Browse files
Issue #20368: The null character now correctly passed from Tcl to Python.
Improved error handling in variables-related commands.
1 parent fc05525 commit 1317e14

4 files changed

Lines changed: 175 additions & 78 deletions

File tree

Lib/test/test_tcl.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def testEval(self):
5555
tcl.eval('set a 1')
5656
self.assertEqual(tcl.eval('set a'),'1')
5757

58+
def test_eval_null_in_result(self):
59+
tcl = self.interp
60+
self.assertEqual(tcl.eval('set a "a\\0b"'), 'a\x00b')
61+
5862
def testEvalException(self):
5963
tcl = self.interp
6064
self.assertRaises(TclError,tcl.eval,'set a')
@@ -127,20 +131,29 @@ def testUnsetVarException(self):
127131

128132
def testEvalFile(self):
129133
tcl = self.interp
130-
filename = "testEvalFile.tcl"
131-
fd = open(filename,'w')
132-
script = """set a 1
133-
set b 2
134-
set c [ expr $a + $b ]
135-
"""
136-
fd.write(script)
137-
fd.close()
138-
tcl.evalfile(filename)
139-
os.remove(filename)
134+
with open(support.TESTFN, 'w') as f:
135+
self.addCleanup(support.unlink, support.TESTFN)
136+
f.write("""set a 1
137+
set b 2
138+
set c [ expr $a + $b ]
139+
""")
140+
tcl.evalfile(support.TESTFN)
140141
self.assertEqual(tcl.eval('set a'),'1')
141142
self.assertEqual(tcl.eval('set b'),'2')
142143
self.assertEqual(tcl.eval('set c'),'3')
143144

145+
def test_evalfile_null_in_result(self):
146+
tcl = self.interp
147+
with open(support.TESTFN, 'w') as f:
148+
self.addCleanup(support.unlink, support.TESTFN)
149+
f.write("""
150+
set a "a\0b"
151+
set b "a\\0b"
152+
""")
153+
tcl.evalfile(support.TESTFN)
154+
self.assertEqual(tcl.eval('set a'), 'a\x00b')
155+
self.assertEqual(tcl.eval('set b'), 'a\x00b')
156+
144157
def testEvalFileException(self):
145158
tcl = self.interp
146159
filename = "doesnotexists"
@@ -209,6 +222,7 @@ def check(expr, expected):
209222
check('"abc"', 'abc')
210223
check('"a\xbd\u20ac"', 'a\xbd\u20ac')
211224
check(r'"a\xbd\u20ac"', 'a\xbd\u20ac')
225+
check(r'"a\0b"', 'a\x00b')
212226

213227
def test_exprdouble(self):
214228
tcl = self.interp
@@ -320,6 +334,11 @@ def passValue(value):
320334
self.assertEqual(passValue(False), False if self.wantobjects else '0')
321335
self.assertEqual(passValue('string'), 'string')
322336
self.assertEqual(passValue('string\u20ac'), 'string\u20ac')
337+
self.assertEqual(passValue('str\x00ing'), 'str\x00ing')
338+
self.assertEqual(passValue('str\x00ing\xbd'), 'str\x00ing\xbd')
339+
self.assertEqual(passValue('str\x00ing\u20ac'), 'str\x00ing\u20ac')
340+
self.assertEqual(passValue(b'str\x00ing'), 'str\x00ing')
341+
self.assertEqual(passValue(b'str\xc0\x80ing'), 'str\x00ing')
323342
for i in (0, 1, -1, 2**31-1, -2**31):
324343
self.assertEqual(passValue(i), i if self.wantobjects else str(i))
325344
for f in (0.0, 1.0, -1.0, 1/3,
@@ -368,6 +387,13 @@ def nan_eq(actual, expected):
368387
check('string', 'string')
369388
check('string\xbd', 'string\xbd')
370389
check('string\u20ac', 'string\u20ac')
390+
check(b'string', 'string')
391+
check(b'string\xe2\x82\xac', 'string\u20ac')
392+
check('str\x00ing', 'str\x00ing')
393+
check('str\x00ing\xbd', 'str\x00ing\xbd')
394+
check('str\x00ing\u20ac', 'str\x00ing\u20ac')
395+
check(b'str\xc0\x80ing', 'str\x00ing')
396+
check(b'str\xc0\x80ing\xe2\x82\xac', 'str\x00ing\u20ac')
371397
for i in (0, 1, -1, 2**31-1, -2**31):
372398
check(i, str(i))
373399
for f in (0.0, 1.0, -1.0):
@@ -396,6 +422,7 @@ def test_splitlist(self):
396422
(b'a\n b\t\r c\n ', ('a', 'b', 'c')),
397423
('a \u20ac', ('a', '\u20ac')),
398424
(b'a \xe2\x82\xac', ('a', '\u20ac')),
425+
(b'a\xc0\x80b c\xc0\x80d', ('a\x00b', 'c\x00d')),
399426
('a {b c}', ('a', 'b c')),
400427
(r'a b\ c', ('a', 'b c')),
401428
(('a', 'b c'), ('a', 'b c')),
@@ -438,6 +465,9 @@ def test_split(self):
438465
(b'a\n b\t\r c\n ', ('a', 'b', 'c')),
439466
('a \u20ac', ('a', '\u20ac')),
440467
(b'a \xe2\x82\xac', ('a', '\u20ac')),
468+
(b'a\xc0\x80b', 'a\x00b'),
469+
(b'a\xc0\x80b c\xc0\x80d', ('a\x00b', 'c\x00d')),
470+
(b'{a\xc0\x80b c\xc0\x80d', '{a\x00b c\x00d'),
441471
('a {b c}', ('a', ('b', 'c'))),
442472
(r'a b\ c', ('a', ('b', 'c'))),
443473
(('a', b'b c'), ('a', ('b', 'c'))),

Lib/tkinter/test/test_tkinter/test_variables.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ def test_invalid_name(self):
6868
with self.assertRaises(TypeError):
6969
Variable(self.root, name=123)
7070

71+
def test_null_in_name(self):
72+
with self.assertRaises(ValueError):
73+
Variable(self.root, name='var\x00name')
74+
with self.assertRaises(ValueError):
75+
self.root.globalsetvar('var\x00name', "value")
76+
with self.assertRaises(ValueError):
77+
self.root.globalsetvar(b'var\x00name', "value")
78+
with self.assertRaises(ValueError):
79+
self.root.setvar('var\x00name', "value")
80+
with self.assertRaises(ValueError):
81+
self.root.setvar(b'var\x00name', "value")
82+
7183
def test_initialize(self):
7284
v = Var()
7385
self.assertFalse(v.side_effect)
@@ -87,6 +99,12 @@ def test_get(self):
8799
self.root.globalsetvar("name", "value")
88100
self.assertEqual("value", v.get())
89101

102+
def test_get_null(self):
103+
v = StringVar(self.root, "abc\x00def", "name")
104+
self.assertEqual("abc\x00def", v.get())
105+
self.root.globalsetvar("name", "val\x00ue")
106+
self.assertEqual("val\x00ue", v.get())
107+
90108

91109
class TestIntVar(TestBase):
92110

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Core and Builtins
4545
Library
4646
-------
4747

48+
- Issue #20368: The null character now correctly passed from Tcl to Python.
49+
Improved error handling in variables-related commands.
50+
4851
- Issue #20435: Fix _pyio.StringIO.getvalue() to take into account newline
4952
translation settings.
5053

0 commit comments

Comments
 (0)