summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_tcl.py185
-rw-r--r--Lib/tkinter/test/test_tkinter/test_variables.py18
-rw-r--r--Lib/tkinter/test/test_tkinter/test_widgets.py9
-rw-r--r--Lib/tkinter/test/widget_tests.py12
4 files changed, 207 insertions, 17 deletions
diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py
index 2ed85dd..7ef7995 100644
--- a/Lib/test/test_tcl.py
+++ b/Lib/test/test_tcl.py
@@ -55,6 +55,10 @@ class TclTest(unittest.TestCase):
tcl.eval('set a 1')
self.assertEqual(tcl.eval('set a'),'1')
+ def test_eval_null_in_result(self):
+ tcl = self.interp
+ self.assertEqual(tcl.eval('set a "a\\0b"'), 'a\x00b')
+
def testEvalException(self):
tcl = self.interp
self.assertRaises(TclError,tcl.eval,'set a')
@@ -127,20 +131,29 @@ class TclTest(unittest.TestCase):
def testEvalFile(self):
tcl = self.interp
- filename = "testEvalFile.tcl"
- fd = open(filename,'w')
- script = """set a 1
- set b 2
- set c [ expr $a + $b ]
- """
- fd.write(script)
- fd.close()
- tcl.evalfile(filename)
- os.remove(filename)
+ with open(support.TESTFN, 'w') as f:
+ self.addCleanup(support.unlink, support.TESTFN)
+ f.write("""set a 1
+ set b 2
+ set c [ expr $a + $b ]
+ """)
+ tcl.evalfile(support.TESTFN)
self.assertEqual(tcl.eval('set a'),'1')
self.assertEqual(tcl.eval('set b'),'2')
self.assertEqual(tcl.eval('set c'),'3')
+ def test_evalfile_null_in_result(self):
+ tcl = self.interp
+ with open(support.TESTFN, 'w') as f:
+ self.addCleanup(support.unlink, support.TESTFN)
+ f.write("""
+ set a "a\0b"
+ set b "a\\0b"
+ """)
+ tcl.evalfile(support.TESTFN)
+ self.assertEqual(tcl.eval('set a'), 'a\x00b')
+ self.assertEqual(tcl.eval('set b'), 'a\x00b')
+
def testEvalFileException(self):
tcl = self.interp
filename = "doesnotexists"
@@ -177,6 +190,142 @@ class TclTest(unittest.TestCase):
# exit code must be zero
self.assertEqual(f.close(), None)
+ def test_exprstring(self):
+ tcl = self.interp
+ tcl.call('set', 'a', 3)
+ tcl.call('set', 'b', 6)
+ def check(expr, expected):
+ result = tcl.exprstring(expr)
+ self.assertEqual(result, expected)
+ self.assertIsInstance(result, str)
+
+ self.assertRaises(TypeError, tcl.exprstring)
+ self.assertRaises(TypeError, tcl.exprstring, '8.2', '+6')
+ self.assertRaises(TypeError, tcl.exprstring, b'8.2 + 6')
+ self.assertRaises(TclError, tcl.exprstring, 'spam')
+ check('', '0')
+ check('8.2 + 6', '14.2')
+ check('2**64', str(2**64))
+ check('3.1 + $a', '6.1')
+ check('2 + "$a.$b"', '5.6')
+ check('4*[llength "6 2"]', '8')
+ check('{word one} < "word $a"', '0')
+ check('4*2 < 7', '0')
+ check('hypot($a, 4)', '5.0')
+ check('5 / 4', '1')
+ check('5 / 4.0', '1.25')
+ check('5 / ( [string length "abcd"] + 0.0 )', '1.25')
+ check('20.0/5.0', '4.0')
+ check('"0x03" > "2"', '1')
+ check('[string length "a\xbd\u20ac"]', '3')
+ check(r'[string length "a\xbd\u20ac"]', '3')
+ check('"abc"', 'abc')
+ check('"a\xbd\u20ac"', 'a\xbd\u20ac')
+ check(r'"a\xbd\u20ac"', 'a\xbd\u20ac')
+ check(r'"a\0b"', 'a\x00b')
+
+ def test_exprdouble(self):
+ tcl = self.interp
+ tcl.call('set', 'a', 3)
+ tcl.call('set', 'b', 6)
+ def check(expr, expected):
+ result = tcl.exprdouble(expr)
+ self.assertEqual(result, expected)
+ self.assertIsInstance(result, float)
+
+ self.assertRaises(TypeError, tcl.exprdouble)
+ self.assertRaises(TypeError, tcl.exprdouble, '8.2', '+6')
+ self.assertRaises(TypeError, tcl.exprdouble, b'8.2 + 6')
+ self.assertRaises(TclError, tcl.exprdouble, 'spam')
+ check('', 0.0)
+ check('8.2 + 6', 14.2)
+ check('2**64', float(2**64))
+ check('3.1 + $a', 6.1)
+ check('2 + "$a.$b"', 5.6)
+ check('4*[llength "6 2"]', 8.0)
+ check('{word one} < "word $a"', 0.0)
+ check('4*2 < 7', 0.0)
+ check('hypot($a, 4)', 5.0)
+ check('5 / 4', 1.0)
+ check('5 / 4.0', 1.25)
+ check('5 / ( [string length "abcd"] + 0.0 )', 1.25)
+ check('20.0/5.0', 4.0)
+ check('"0x03" > "2"', 1.0)
+ check('[string length "a\xbd\u20ac"]', 3.0)
+ check(r'[string length "a\xbd\u20ac"]', 3.0)
+ self.assertRaises(TclError, tcl.exprdouble, '"abc"')
+
+ def test_exprlong(self):
+ tcl = self.interp
+ tcl.call('set', 'a', 3)
+ tcl.call('set', 'b', 6)
+ def check(expr, expected):
+ result = tcl.exprlong(expr)
+ self.assertEqual(result, expected)
+ self.assertIsInstance(result, int)
+
+ self.assertRaises(TypeError, tcl.exprlong)
+ self.assertRaises(TypeError, tcl.exprlong, '8.2', '+6')
+ self.assertRaises(TypeError, tcl.exprlong, b'8.2 + 6')
+ self.assertRaises(TclError, tcl.exprlong, 'spam')
+ check('', 0)
+ check('8.2 + 6', 14)
+ self.assertRaises(TclError, tcl.exprlong, '2**64')
+ check('3.1 + $a', 6)
+ check('2 + "$a.$b"', 5)
+ check('4*[llength "6 2"]', 8)
+ check('{word one} < "word $a"', 0)
+ check('4*2 < 7', 0)
+ check('hypot($a, 4)', 5)
+ check('5 / 4', 1)
+ check('5 / 4.0', 1)
+ check('5 / ( [string length "abcd"] + 0.0 )', 1)
+ check('20.0/5.0', 4)
+ check('"0x03" > "2"', 1)
+ check('[string length "a\xbd\u20ac"]', 3)
+ check(r'[string length "a\xbd\u20ac"]', 3)
+ self.assertRaises(TclError, tcl.exprlong, '"abc"')
+
+ def test_exprboolean(self):
+ tcl = self.interp
+ tcl.call('set', 'a', 3)
+ tcl.call('set', 'b', 6)
+ def check(expr, expected):
+ result = tcl.exprboolean(expr)
+ self.assertEqual(result, expected)
+ self.assertIsInstance(result, int)
+ self.assertNotIsInstance(result, bool)
+
+ self.assertRaises(TypeError, tcl.exprboolean)
+ self.assertRaises(TypeError, tcl.exprboolean, '8.2', '+6')
+ self.assertRaises(TypeError, tcl.exprboolean, b'8.2 + 6')
+ self.assertRaises(TclError, tcl.exprboolean, 'spam')
+ check('', False)
+ for value in ('0', 'false', 'no', 'off'):
+ check(value, False)
+ check('"%s"' % value, False)
+ check('{%s}' % value, False)
+ for value in ('1', 'true', 'yes', 'on'):
+ check(value, True)
+ check('"%s"' % value, True)
+ check('{%s}' % value, True)
+ check('8.2 + 6', True)
+ check('2**64', True)
+ check('3.1 + $a', True)
+ check('2 + "$a.$b"', True)
+ check('4*[llength "6 2"]', True)
+ check('{word one} < "word $a"', False)
+ check('4*2 < 7', False)
+ check('hypot($a, 4)', True)
+ check('5 / 4', True)
+ check('5 / 4.0', True)
+ check('5 / ( [string length "abcd"] + 0.0 )', True)
+ check('20.0/5.0', True)
+ check('"0x03" > "2"', True)
+ check('[string length "a\xbd\u20ac"]', True)
+ check(r'[string length "a\xbd\u20ac"]', True)
+ self.assertRaises(TclError, tcl.exprboolean, '"abc"')
+
def test_passing_values(self):
def passValue(value):
return self.interp.call('set', '_', value)
@@ -185,6 +334,11 @@ class TclTest(unittest.TestCase):
self.assertEqual(passValue(False), False if self.wantobjects else '0')
self.assertEqual(passValue('string'), 'string')
self.assertEqual(passValue('string\u20ac'), 'string\u20ac')
+ self.assertEqual(passValue('str\x00ing'), 'str\x00ing')
+ self.assertEqual(passValue('str\x00ing\xbd'), 'str\x00ing\xbd')
+ self.assertEqual(passValue('str\x00ing\u20ac'), 'str\x00ing\u20ac')
+ self.assertEqual(passValue(b'str\x00ing'), 'str\x00ing')
+ self.assertEqual(passValue(b'str\xc0\x80ing'), 'str\x00ing')
for i in (0, 1, -1, 2**31-1, -2**31):
self.assertEqual(passValue(i), i if self.wantobjects else str(i))
for f in (0.0, 1.0, -1.0, 1/3,
@@ -233,6 +387,13 @@ class TclTest(unittest.TestCase):
check('string', 'string')
check('string\xbd', 'string\xbd')
check('string\u20ac', 'string\u20ac')
+ check(b'string', 'string')
+ check(b'string\xe2\x82\xac', 'string\u20ac')
+ check('str\x00ing', 'str\x00ing')
+ check('str\x00ing\xbd', 'str\x00ing\xbd')
+ check('str\x00ing\u20ac', 'str\x00ing\u20ac')
+ check(b'str\xc0\x80ing', 'str\x00ing')
+ check(b'str\xc0\x80ing\xe2\x82\xac', 'str\x00ing\u20ac')
for i in (0, 1, -1, 2**31-1, -2**31):
check(i, str(i))
for f in (0.0, 1.0, -1.0):
@@ -261,6 +422,7 @@ class TclTest(unittest.TestCase):
(b'a\n b\t\r c\n ', ('a', 'b', 'c')),
('a \u20ac', ('a', '\u20ac')),
(b'a \xe2\x82\xac', ('a', '\u20ac')),
+ (b'a\xc0\x80b c\xc0\x80d', ('a\x00b', 'c\x00d')),
('a {b c}', ('a', 'b c')),
(r'a b\ c', ('a', 'b c')),
(('a', 'b c'), ('a', 'b c')),
@@ -303,6 +465,9 @@ class TclTest(unittest.TestCase):
(b'a\n b\t\r c\n ', ('a', 'b', 'c')),
('a \u20ac', ('a', '\u20ac')),
(b'a \xe2\x82\xac', ('a', '\u20ac')),
+ (b'a\xc0\x80b', 'a\x00b'),
+ (b'a\xc0\x80b c\xc0\x80d', ('a\x00b', 'c\x00d')),
+ (b'{a\xc0\x80b c\xc0\x80d', '{a\x00b c\x00d'),
('a {b c}', ('a', ('b', 'c'))),
(r'a b\ c', ('a', ('b', 'c'))),
(('a', b'b c'), ('a', ('b', 'c'))),
diff --git a/Lib/tkinter/test/test_tkinter/test_variables.py b/Lib/tkinter/test/test_tkinter/test_variables.py
index fa1f898..9d910ac 100644
--- a/Lib/tkinter/test/test_tkinter/test_variables.py
+++ b/Lib/tkinter/test/test_tkinter/test_variables.py
@@ -68,6 +68,18 @@ class TestVariable(TestBase):
with self.assertRaises(TypeError):
Variable(self.root, name=123)
+ def test_null_in_name(self):
+ with self.assertRaises(ValueError):
+ Variable(self.root, name='var\x00name')
+ with self.assertRaises(ValueError):
+ self.root.globalsetvar('var\x00name', "value")
+ with self.assertRaises(ValueError):
+ self.root.globalsetvar(b'var\x00name', "value")
+ with self.assertRaises(ValueError):
+ self.root.setvar('var\x00name', "value")
+ with self.assertRaises(ValueError):
+ self.root.setvar(b'var\x00name', "value")
+
def test_initialize(self):
v = Var()
self.assertFalse(v.side_effect)
@@ -87,6 +99,12 @@ class TestStringVar(TestBase):
self.root.globalsetvar("name", "value")
self.assertEqual("value", v.get())
+ def test_get_null(self):
+ v = StringVar(self.root, "abc\x00def", "name")
+ self.assertEqual("abc\x00def", v.get())
+ self.root.globalsetvar("name", "val\x00ue")
+ self.assertEqual("val\x00ue", v.get())
+
class TestIntVar(TestBase):
diff --git a/Lib/tkinter/test/test_tkinter/test_widgets.py b/Lib/tkinter/test/test_tkinter/test_widgets.py
index 4de6531..47e281f 100644
--- a/Lib/tkinter/test/test_tkinter/test_widgets.py
+++ b/Lib/tkinter/test/test_tkinter/test_widgets.py
@@ -329,10 +329,11 @@ class EntryTest(AbstractWidgetTest, unittest.TestCase):
self.checkColorParam(widget, 'disabledbackground')
def test_insertborderwidth(self):
- widget = self.create()
- self.checkPixelsParam(widget, 'insertborderwidth', 0, 1.3, -2)
- self.checkParam(widget, 'insertborderwidth', 2, expected=1)
- self.checkParam(widget, 'insertborderwidth', '10p', expected=1)
+ widget = self.create(insertwidth=100)
+ self.checkPixelsParam(widget, 'insertborderwidth',
+ 0, 1.3, 2.6, 6, -2, '10p')
+ # insertborderwidth is bounded above by a half of insertwidth.
+ self.checkParam(widget, 'insertborderwidth', 60, expected=100//2)
def test_insertwidth(self):
widget = self.create()
diff --git a/Lib/tkinter/test/widget_tests.py b/Lib/tkinter/test/widget_tests.py
index ac194a8..a9820a7 100644
--- a/Lib/tkinter/test/widget_tests.py
+++ b/Lib/tkinter/test/widget_tests.py
@@ -6,6 +6,7 @@ import tkinter
from tkinter.ttk import setup_master, Scale
from tkinter.test.support import (tcl_version, requires_tcl, get_tk_patchlevel,
pixels_conv, tcl_obj_eq)
+import test.support
noconv = False
@@ -234,8 +235,14 @@ class StandardOptionsTests:
widget = self.create()
self.checkParam(widget, 'bitmap', 'questhead')
self.checkParam(widget, 'bitmap', 'gray50')
- self.checkInvalidParam(widget, 'bitmap', 'spam',
- errmsg='bitmap "spam" not defined')
+ filename = test.support.findfile('python.xbm', subdir='imghdrdata')
+ self.checkParam(widget, 'bitmap', '@' + filename)
+ # Cocoa Tk widgets don't detect invalid -bitmap values
+ # See https://core.tcl.tk/tk/info/31cd33dbf0
+ if not ('aqua' in self.root.tk.call('tk', 'windowingsystem') and
+ 'AppKit' in self.root.winfo_server()):
+ self.checkInvalidParam(widget, 'bitmap', 'spam',
+ errmsg='bitmap "spam" not defined')
def test_borderwidth(self):
widget = self.create()
@@ -495,7 +502,6 @@ def add_standard_options(*source_classes):
return decorator
def setUpModule():
- import test.support
if test.support.verbose:
tcl = tkinter.Tcl()
print('patchlevel =', tcl.call('info', 'patchlevel'))