1 | n/a | import unittest |
---|
2 | n/a | import re |
---|
3 | n/a | import subprocess |
---|
4 | n/a | import sys |
---|
5 | n/a | import os |
---|
6 | n/a | from test import support |
---|
7 | n/a | |
---|
8 | n/a | # Skip this test if the _tkinter module wasn't built. |
---|
9 | n/a | _tkinter = support.import_module('_tkinter') |
---|
10 | n/a | |
---|
11 | n/a | import tkinter |
---|
12 | n/a | from tkinter import Tcl |
---|
13 | n/a | from _tkinter import TclError |
---|
14 | n/a | |
---|
15 | n/a | try: |
---|
16 | n/a | from _testcapi import INT_MAX, PY_SSIZE_T_MAX |
---|
17 | n/a | except ImportError: |
---|
18 | n/a | INT_MAX = PY_SSIZE_T_MAX = sys.maxsize |
---|
19 | n/a | |
---|
20 | n/a | tcl_version = tuple(map(int, _tkinter.TCL_VERSION.split('.'))) |
---|
21 | n/a | |
---|
22 | n/a | _tk_patchlevel = None |
---|
23 | n/a | def get_tk_patchlevel(): |
---|
24 | n/a | global _tk_patchlevel |
---|
25 | n/a | if _tk_patchlevel is None: |
---|
26 | n/a | tcl = Tcl() |
---|
27 | n/a | patchlevel = tcl.call('info', 'patchlevel') |
---|
28 | n/a | m = re.fullmatch(r'(\d+)\.(\d+)([ab.])(\d+)', patchlevel) |
---|
29 | n/a | major, minor, releaselevel, serial = m.groups() |
---|
30 | n/a | major, minor, serial = int(major), int(minor), int(serial) |
---|
31 | n/a | releaselevel = {'a': 'alpha', 'b': 'beta', '.': 'final'}[releaselevel] |
---|
32 | n/a | if releaselevel == 'final': |
---|
33 | n/a | _tk_patchlevel = major, minor, serial, releaselevel, 0 |
---|
34 | n/a | else: |
---|
35 | n/a | _tk_patchlevel = major, minor, 0, releaselevel, serial |
---|
36 | n/a | return _tk_patchlevel |
---|
37 | n/a | |
---|
38 | n/a | |
---|
39 | n/a | class TkinterTest(unittest.TestCase): |
---|
40 | n/a | |
---|
41 | n/a | def testFlattenLen(self): |
---|
42 | n/a | # flatten(<object with no length>) |
---|
43 | n/a | self.assertRaises(TypeError, _tkinter._flatten, True) |
---|
44 | n/a | |
---|
45 | n/a | |
---|
46 | n/a | class TclTest(unittest.TestCase): |
---|
47 | n/a | |
---|
48 | n/a | def setUp(self): |
---|
49 | n/a | self.interp = Tcl() |
---|
50 | n/a | self.wantobjects = self.interp.tk.wantobjects() |
---|
51 | n/a | |
---|
52 | n/a | def testEval(self): |
---|
53 | n/a | tcl = self.interp |
---|
54 | n/a | tcl.eval('set a 1') |
---|
55 | n/a | self.assertEqual(tcl.eval('set a'),'1') |
---|
56 | n/a | |
---|
57 | n/a | def test_eval_null_in_result(self): |
---|
58 | n/a | tcl = self.interp |
---|
59 | n/a | self.assertEqual(tcl.eval('set a "a\\0b"'), 'a\x00b') |
---|
60 | n/a | |
---|
61 | n/a | def testEvalException(self): |
---|
62 | n/a | tcl = self.interp |
---|
63 | n/a | self.assertRaises(TclError,tcl.eval,'set a') |
---|
64 | n/a | |
---|
65 | n/a | def testEvalException2(self): |
---|
66 | n/a | tcl = self.interp |
---|
67 | n/a | self.assertRaises(TclError,tcl.eval,'this is wrong') |
---|
68 | n/a | |
---|
69 | n/a | def testCall(self): |
---|
70 | n/a | tcl = self.interp |
---|
71 | n/a | tcl.call('set','a','1') |
---|
72 | n/a | self.assertEqual(tcl.call('set','a'),'1') |
---|
73 | n/a | |
---|
74 | n/a | def testCallException(self): |
---|
75 | n/a | tcl = self.interp |
---|
76 | n/a | self.assertRaises(TclError,tcl.call,'set','a') |
---|
77 | n/a | |
---|
78 | n/a | def testCallException2(self): |
---|
79 | n/a | tcl = self.interp |
---|
80 | n/a | self.assertRaises(TclError,tcl.call,'this','is','wrong') |
---|
81 | n/a | |
---|
82 | n/a | def testSetVar(self): |
---|
83 | n/a | tcl = self.interp |
---|
84 | n/a | tcl.setvar('a','1') |
---|
85 | n/a | self.assertEqual(tcl.eval('set a'),'1') |
---|
86 | n/a | |
---|
87 | n/a | def testSetVarArray(self): |
---|
88 | n/a | tcl = self.interp |
---|
89 | n/a | tcl.setvar('a(1)','1') |
---|
90 | n/a | self.assertEqual(tcl.eval('set a(1)'),'1') |
---|
91 | n/a | |
---|
92 | n/a | def testGetVar(self): |
---|
93 | n/a | tcl = self.interp |
---|
94 | n/a | tcl.eval('set a 1') |
---|
95 | n/a | self.assertEqual(tcl.getvar('a'),'1') |
---|
96 | n/a | |
---|
97 | n/a | def testGetVarArray(self): |
---|
98 | n/a | tcl = self.interp |
---|
99 | n/a | tcl.eval('set a(1) 1') |
---|
100 | n/a | self.assertEqual(tcl.getvar('a(1)'),'1') |
---|
101 | n/a | |
---|
102 | n/a | def testGetVarException(self): |
---|
103 | n/a | tcl = self.interp |
---|
104 | n/a | self.assertRaises(TclError,tcl.getvar,'a') |
---|
105 | n/a | |
---|
106 | n/a | def testGetVarArrayException(self): |
---|
107 | n/a | tcl = self.interp |
---|
108 | n/a | self.assertRaises(TclError,tcl.getvar,'a(1)') |
---|
109 | n/a | |
---|
110 | n/a | def testUnsetVar(self): |
---|
111 | n/a | tcl = self.interp |
---|
112 | n/a | tcl.setvar('a',1) |
---|
113 | n/a | self.assertEqual(tcl.eval('info exists a'),'1') |
---|
114 | n/a | tcl.unsetvar('a') |
---|
115 | n/a | self.assertEqual(tcl.eval('info exists a'),'0') |
---|
116 | n/a | |
---|
117 | n/a | def testUnsetVarArray(self): |
---|
118 | n/a | tcl = self.interp |
---|
119 | n/a | tcl.setvar('a(1)',1) |
---|
120 | n/a | tcl.setvar('a(2)',2) |
---|
121 | n/a | self.assertEqual(tcl.eval('info exists a(1)'),'1') |
---|
122 | n/a | self.assertEqual(tcl.eval('info exists a(2)'),'1') |
---|
123 | n/a | tcl.unsetvar('a(1)') |
---|
124 | n/a | self.assertEqual(tcl.eval('info exists a(1)'),'0') |
---|
125 | n/a | self.assertEqual(tcl.eval('info exists a(2)'),'1') |
---|
126 | n/a | |
---|
127 | n/a | def testUnsetVarException(self): |
---|
128 | n/a | tcl = self.interp |
---|
129 | n/a | self.assertRaises(TclError,tcl.unsetvar,'a') |
---|
130 | n/a | |
---|
131 | n/a | def get_integers(self): |
---|
132 | n/a | integers = (0, 1, -1, 2**31-1, -2**31, 2**31, -2**31-1, 2**63-1, -2**63) |
---|
133 | n/a | # bignum was added in Tcl 8.5, but its support is able only since 8.5.8 |
---|
134 | n/a | if (get_tk_patchlevel() >= (8, 6, 0, 'final') or |
---|
135 | n/a | (8, 5, 8) <= get_tk_patchlevel() < (8, 6)): |
---|
136 | n/a | integers += (2**63, -2**63-1, 2**1000, -2**1000) |
---|
137 | n/a | return integers |
---|
138 | n/a | |
---|
139 | n/a | def test_getint(self): |
---|
140 | n/a | tcl = self.interp.tk |
---|
141 | n/a | for i in self.get_integers(): |
---|
142 | n/a | self.assertEqual(tcl.getint(' %d ' % i), i) |
---|
143 | n/a | if tcl_version >= (8, 5): |
---|
144 | n/a | self.assertEqual(tcl.getint(' %#o ' % i), i) |
---|
145 | n/a | self.assertEqual(tcl.getint((' %#o ' % i).replace('o', '')), i) |
---|
146 | n/a | self.assertEqual(tcl.getint(' %#x ' % i), i) |
---|
147 | n/a | if tcl_version < (8, 5): # bignum was added in Tcl 8.5 |
---|
148 | n/a | self.assertRaises(TclError, tcl.getint, str(2**1000)) |
---|
149 | n/a | self.assertEqual(tcl.getint(42), 42) |
---|
150 | n/a | self.assertRaises(TypeError, tcl.getint) |
---|
151 | n/a | self.assertRaises(TypeError, tcl.getint, '42', '10') |
---|
152 | n/a | self.assertRaises(TypeError, tcl.getint, b'42') |
---|
153 | n/a | self.assertRaises(TypeError, tcl.getint, 42.0) |
---|
154 | n/a | self.assertRaises(TclError, tcl.getint, 'a') |
---|
155 | n/a | self.assertRaises((TypeError, ValueError, TclError), |
---|
156 | n/a | tcl.getint, '42\0') |
---|
157 | n/a | self.assertRaises((UnicodeEncodeError, ValueError, TclError), |
---|
158 | n/a | tcl.getint, '42\ud800') |
---|
159 | n/a | |
---|
160 | n/a | def test_getdouble(self): |
---|
161 | n/a | tcl = self.interp.tk |
---|
162 | n/a | self.assertEqual(tcl.getdouble(' 42 '), 42.0) |
---|
163 | n/a | self.assertEqual(tcl.getdouble(' 42.5 '), 42.5) |
---|
164 | n/a | self.assertEqual(tcl.getdouble(42.5), 42.5) |
---|
165 | n/a | self.assertEqual(tcl.getdouble(42), 42.0) |
---|
166 | n/a | self.assertRaises(TypeError, tcl.getdouble) |
---|
167 | n/a | self.assertRaises(TypeError, tcl.getdouble, '42.5', '10') |
---|
168 | n/a | self.assertRaises(TypeError, tcl.getdouble, b'42.5') |
---|
169 | n/a | self.assertRaises(TclError, tcl.getdouble, 'a') |
---|
170 | n/a | self.assertRaises((TypeError, ValueError, TclError), |
---|
171 | n/a | tcl.getdouble, '42.5\0') |
---|
172 | n/a | self.assertRaises((UnicodeEncodeError, ValueError, TclError), |
---|
173 | n/a | tcl.getdouble, '42.5\ud800') |
---|
174 | n/a | |
---|
175 | n/a | def test_getboolean(self): |
---|
176 | n/a | tcl = self.interp.tk |
---|
177 | n/a | self.assertIs(tcl.getboolean('on'), True) |
---|
178 | n/a | self.assertIs(tcl.getboolean('1'), True) |
---|
179 | n/a | self.assertIs(tcl.getboolean(42), True) |
---|
180 | n/a | self.assertIs(tcl.getboolean(0), False) |
---|
181 | n/a | self.assertRaises(TypeError, tcl.getboolean) |
---|
182 | n/a | self.assertRaises(TypeError, tcl.getboolean, 'on', '1') |
---|
183 | n/a | self.assertRaises(TypeError, tcl.getboolean, b'on') |
---|
184 | n/a | self.assertRaises(TypeError, tcl.getboolean, 1.0) |
---|
185 | n/a | self.assertRaises(TclError, tcl.getboolean, 'a') |
---|
186 | n/a | self.assertRaises((TypeError, ValueError, TclError), |
---|
187 | n/a | tcl.getboolean, 'on\0') |
---|
188 | n/a | self.assertRaises((UnicodeEncodeError, ValueError, TclError), |
---|
189 | n/a | tcl.getboolean, 'on\ud800') |
---|
190 | n/a | |
---|
191 | n/a | def testEvalFile(self): |
---|
192 | n/a | tcl = self.interp |
---|
193 | n/a | with open(support.TESTFN, 'w') as f: |
---|
194 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
195 | n/a | f.write("""set a 1 |
---|
196 | n/a | set b 2 |
---|
197 | n/a | set c [ expr $a + $b ] |
---|
198 | n/a | """) |
---|
199 | n/a | tcl.evalfile(support.TESTFN) |
---|
200 | n/a | self.assertEqual(tcl.eval('set a'),'1') |
---|
201 | n/a | self.assertEqual(tcl.eval('set b'),'2') |
---|
202 | n/a | self.assertEqual(tcl.eval('set c'),'3') |
---|
203 | n/a | |
---|
204 | n/a | def test_evalfile_null_in_result(self): |
---|
205 | n/a | tcl = self.interp |
---|
206 | n/a | with open(support.TESTFN, 'w') as f: |
---|
207 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
208 | n/a | f.write(""" |
---|
209 | n/a | set a "a\0b" |
---|
210 | n/a | set b "a\\0b" |
---|
211 | n/a | """) |
---|
212 | n/a | tcl.evalfile(support.TESTFN) |
---|
213 | n/a | self.assertEqual(tcl.eval('set a'), 'a\x00b') |
---|
214 | n/a | self.assertEqual(tcl.eval('set b'), 'a\x00b') |
---|
215 | n/a | |
---|
216 | n/a | def testEvalFileException(self): |
---|
217 | n/a | tcl = self.interp |
---|
218 | n/a | filename = "doesnotexists" |
---|
219 | n/a | try: |
---|
220 | n/a | os.remove(filename) |
---|
221 | n/a | except Exception as e: |
---|
222 | n/a | pass |
---|
223 | n/a | self.assertRaises(TclError,tcl.evalfile,filename) |
---|
224 | n/a | |
---|
225 | n/a | def testPackageRequireException(self): |
---|
226 | n/a | tcl = self.interp |
---|
227 | n/a | self.assertRaises(TclError,tcl.eval,'package require DNE') |
---|
228 | n/a | |
---|
229 | n/a | @unittest.skipUnless(sys.platform == 'win32', 'Requires Windows') |
---|
230 | n/a | def testLoadWithUNC(self): |
---|
231 | n/a | # Build a UNC path from the regular path. |
---|
232 | n/a | # Something like |
---|
233 | n/a | # \\%COMPUTERNAME%\c$\python27\python.exe |
---|
234 | n/a | |
---|
235 | n/a | fullname = os.path.abspath(sys.executable) |
---|
236 | n/a | if fullname[1] != ':': |
---|
237 | n/a | raise unittest.SkipTest('Absolute path should have drive part') |
---|
238 | n/a | unc_name = r'\\%s\%s$\%s' % (os.environ['COMPUTERNAME'], |
---|
239 | n/a | fullname[0], |
---|
240 | n/a | fullname[3:]) |
---|
241 | n/a | if not os.path.exists(unc_name): |
---|
242 | n/a | raise unittest.SkipTest('Cannot connect to UNC Path') |
---|
243 | n/a | |
---|
244 | n/a | with support.EnvironmentVarGuard() as env: |
---|
245 | n/a | env.unset("TCL_LIBRARY") |
---|
246 | n/a | stdout = subprocess.check_output( |
---|
247 | n/a | [unc_name, '-c', 'import tkinter; print(tkinter)']) |
---|
248 | n/a | |
---|
249 | n/a | self.assertIn(b'tkinter', stdout) |
---|
250 | n/a | |
---|
251 | n/a | def test_exprstring(self): |
---|
252 | n/a | tcl = self.interp |
---|
253 | n/a | tcl.call('set', 'a', 3) |
---|
254 | n/a | tcl.call('set', 'b', 6) |
---|
255 | n/a | def check(expr, expected): |
---|
256 | n/a | result = tcl.exprstring(expr) |
---|
257 | n/a | self.assertEqual(result, expected) |
---|
258 | n/a | self.assertIsInstance(result, str) |
---|
259 | n/a | |
---|
260 | n/a | self.assertRaises(TypeError, tcl.exprstring) |
---|
261 | n/a | self.assertRaises(TypeError, tcl.exprstring, '8.2', '+6') |
---|
262 | n/a | self.assertRaises(TypeError, tcl.exprstring, b'8.2 + 6') |
---|
263 | n/a | self.assertRaises(TclError, tcl.exprstring, 'spam') |
---|
264 | n/a | check('', '0') |
---|
265 | n/a | check('8.2 + 6', '14.2') |
---|
266 | n/a | check('3.1 + $a', '6.1') |
---|
267 | n/a | check('2 + "$a.$b"', '5.6') |
---|
268 | n/a | check('4*[llength "6 2"]', '8') |
---|
269 | n/a | check('{word one} < "word $a"', '0') |
---|
270 | n/a | check('4*2 < 7', '0') |
---|
271 | n/a | check('hypot($a, 4)', '5.0') |
---|
272 | n/a | check('5 / 4', '1') |
---|
273 | n/a | check('5 / 4.0', '1.25') |
---|
274 | n/a | check('5 / ( [string length "abcd"] + 0.0 )', '1.25') |
---|
275 | n/a | check('20.0/5.0', '4.0') |
---|
276 | n/a | check('"0x03" > "2"', '1') |
---|
277 | n/a | check('[string length "a\xbd\u20ac"]', '3') |
---|
278 | n/a | check(r'[string length "a\xbd\u20ac"]', '3') |
---|
279 | n/a | check('"abc"', 'abc') |
---|
280 | n/a | check('"a\xbd\u20ac"', 'a\xbd\u20ac') |
---|
281 | n/a | check(r'"a\xbd\u20ac"', 'a\xbd\u20ac') |
---|
282 | n/a | check(r'"a\0b"', 'a\x00b') |
---|
283 | n/a | if tcl_version >= (8, 5): # bignum was added in Tcl 8.5 |
---|
284 | n/a | check('2**64', str(2**64)) |
---|
285 | n/a | |
---|
286 | n/a | def test_exprdouble(self): |
---|
287 | n/a | tcl = self.interp |
---|
288 | n/a | tcl.call('set', 'a', 3) |
---|
289 | n/a | tcl.call('set', 'b', 6) |
---|
290 | n/a | def check(expr, expected): |
---|
291 | n/a | result = tcl.exprdouble(expr) |
---|
292 | n/a | self.assertEqual(result, expected) |
---|
293 | n/a | self.assertIsInstance(result, float) |
---|
294 | n/a | |
---|
295 | n/a | self.assertRaises(TypeError, tcl.exprdouble) |
---|
296 | n/a | self.assertRaises(TypeError, tcl.exprdouble, '8.2', '+6') |
---|
297 | n/a | self.assertRaises(TypeError, tcl.exprdouble, b'8.2 + 6') |
---|
298 | n/a | self.assertRaises(TclError, tcl.exprdouble, 'spam') |
---|
299 | n/a | check('', 0.0) |
---|
300 | n/a | check('8.2 + 6', 14.2) |
---|
301 | n/a | check('3.1 + $a', 6.1) |
---|
302 | n/a | check('2 + "$a.$b"', 5.6) |
---|
303 | n/a | check('4*[llength "6 2"]', 8.0) |
---|
304 | n/a | check('{word one} < "word $a"', 0.0) |
---|
305 | n/a | check('4*2 < 7', 0.0) |
---|
306 | n/a | check('hypot($a, 4)', 5.0) |
---|
307 | n/a | check('5 / 4', 1.0) |
---|
308 | n/a | check('5 / 4.0', 1.25) |
---|
309 | n/a | check('5 / ( [string length "abcd"] + 0.0 )', 1.25) |
---|
310 | n/a | check('20.0/5.0', 4.0) |
---|
311 | n/a | check('"0x03" > "2"', 1.0) |
---|
312 | n/a | check('[string length "a\xbd\u20ac"]', 3.0) |
---|
313 | n/a | check(r'[string length "a\xbd\u20ac"]', 3.0) |
---|
314 | n/a | self.assertRaises(TclError, tcl.exprdouble, '"abc"') |
---|
315 | n/a | if tcl_version >= (8, 5): # bignum was added in Tcl 8.5 |
---|
316 | n/a | check('2**64', float(2**64)) |
---|
317 | n/a | |
---|
318 | n/a | def test_exprlong(self): |
---|
319 | n/a | tcl = self.interp |
---|
320 | n/a | tcl.call('set', 'a', 3) |
---|
321 | n/a | tcl.call('set', 'b', 6) |
---|
322 | n/a | def check(expr, expected): |
---|
323 | n/a | result = tcl.exprlong(expr) |
---|
324 | n/a | self.assertEqual(result, expected) |
---|
325 | n/a | self.assertIsInstance(result, int) |
---|
326 | n/a | |
---|
327 | n/a | self.assertRaises(TypeError, tcl.exprlong) |
---|
328 | n/a | self.assertRaises(TypeError, tcl.exprlong, '8.2', '+6') |
---|
329 | n/a | self.assertRaises(TypeError, tcl.exprlong, b'8.2 + 6') |
---|
330 | n/a | self.assertRaises(TclError, tcl.exprlong, 'spam') |
---|
331 | n/a | check('', 0) |
---|
332 | n/a | check('8.2 + 6', 14) |
---|
333 | n/a | check('3.1 + $a', 6) |
---|
334 | n/a | check('2 + "$a.$b"', 5) |
---|
335 | n/a | check('4*[llength "6 2"]', 8) |
---|
336 | n/a | check('{word one} < "word $a"', 0) |
---|
337 | n/a | check('4*2 < 7', 0) |
---|
338 | n/a | check('hypot($a, 4)', 5) |
---|
339 | n/a | check('5 / 4', 1) |
---|
340 | n/a | check('5 / 4.0', 1) |
---|
341 | n/a | check('5 / ( [string length "abcd"] + 0.0 )', 1) |
---|
342 | n/a | check('20.0/5.0', 4) |
---|
343 | n/a | check('"0x03" > "2"', 1) |
---|
344 | n/a | check('[string length "a\xbd\u20ac"]', 3) |
---|
345 | n/a | check(r'[string length "a\xbd\u20ac"]', 3) |
---|
346 | n/a | self.assertRaises(TclError, tcl.exprlong, '"abc"') |
---|
347 | n/a | if tcl_version >= (8, 5): # bignum was added in Tcl 8.5 |
---|
348 | n/a | self.assertRaises(TclError, tcl.exprlong, '2**64') |
---|
349 | n/a | |
---|
350 | n/a | def test_exprboolean(self): |
---|
351 | n/a | tcl = self.interp |
---|
352 | n/a | tcl.call('set', 'a', 3) |
---|
353 | n/a | tcl.call('set', 'b', 6) |
---|
354 | n/a | def check(expr, expected): |
---|
355 | n/a | result = tcl.exprboolean(expr) |
---|
356 | n/a | self.assertEqual(result, expected) |
---|
357 | n/a | self.assertIsInstance(result, int) |
---|
358 | n/a | self.assertNotIsInstance(result, bool) |
---|
359 | n/a | |
---|
360 | n/a | self.assertRaises(TypeError, tcl.exprboolean) |
---|
361 | n/a | self.assertRaises(TypeError, tcl.exprboolean, '8.2', '+6') |
---|
362 | n/a | self.assertRaises(TypeError, tcl.exprboolean, b'8.2 + 6') |
---|
363 | n/a | self.assertRaises(TclError, tcl.exprboolean, 'spam') |
---|
364 | n/a | check('', False) |
---|
365 | n/a | for value in ('0', 'false', 'no', 'off'): |
---|
366 | n/a | check(value, False) |
---|
367 | n/a | check('"%s"' % value, False) |
---|
368 | n/a | check('{%s}' % value, False) |
---|
369 | n/a | for value in ('1', 'true', 'yes', 'on'): |
---|
370 | n/a | check(value, True) |
---|
371 | n/a | check('"%s"' % value, True) |
---|
372 | n/a | check('{%s}' % value, True) |
---|
373 | n/a | check('8.2 + 6', True) |
---|
374 | n/a | check('3.1 + $a', True) |
---|
375 | n/a | check('2 + "$a.$b"', True) |
---|
376 | n/a | check('4*[llength "6 2"]', True) |
---|
377 | n/a | check('{word one} < "word $a"', False) |
---|
378 | n/a | check('4*2 < 7', False) |
---|
379 | n/a | check('hypot($a, 4)', True) |
---|
380 | n/a | check('5 / 4', True) |
---|
381 | n/a | check('5 / 4.0', True) |
---|
382 | n/a | check('5 / ( [string length "abcd"] + 0.0 )', True) |
---|
383 | n/a | check('20.0/5.0', True) |
---|
384 | n/a | check('"0x03" > "2"', True) |
---|
385 | n/a | check('[string length "a\xbd\u20ac"]', True) |
---|
386 | n/a | check(r'[string length "a\xbd\u20ac"]', True) |
---|
387 | n/a | self.assertRaises(TclError, tcl.exprboolean, '"abc"') |
---|
388 | n/a | if tcl_version >= (8, 5): # bignum was added in Tcl 8.5 |
---|
389 | n/a | check('2**64', True) |
---|
390 | n/a | |
---|
391 | n/a | @unittest.skipUnless(tcl_version >= (8, 5), 'requires Tcl version >= 8.5') |
---|
392 | n/a | def test_booleans(self): |
---|
393 | n/a | tcl = self.interp |
---|
394 | n/a | def check(expr, expected): |
---|
395 | n/a | result = tcl.call('expr', expr) |
---|
396 | n/a | if tcl.wantobjects(): |
---|
397 | n/a | self.assertEqual(result, expected) |
---|
398 | n/a | self.assertIsInstance(result, int) |
---|
399 | n/a | else: |
---|
400 | n/a | self.assertIn(result, (expr, str(int(expected)))) |
---|
401 | n/a | self.assertIsInstance(result, str) |
---|
402 | n/a | check('true', True) |
---|
403 | n/a | check('yes', True) |
---|
404 | n/a | check('on', True) |
---|
405 | n/a | check('false', False) |
---|
406 | n/a | check('no', False) |
---|
407 | n/a | check('off', False) |
---|
408 | n/a | check('1 < 2', True) |
---|
409 | n/a | check('1 > 2', False) |
---|
410 | n/a | |
---|
411 | n/a | def test_expr_bignum(self): |
---|
412 | n/a | tcl = self.interp |
---|
413 | n/a | for i in self.get_integers(): |
---|
414 | n/a | result = tcl.call('expr', str(i)) |
---|
415 | n/a | if self.wantobjects: |
---|
416 | n/a | self.assertEqual(result, i) |
---|
417 | n/a | self.assertIsInstance(result, int) |
---|
418 | n/a | else: |
---|
419 | n/a | self.assertEqual(result, str(i)) |
---|
420 | n/a | self.assertIsInstance(result, str) |
---|
421 | n/a | if tcl_version < (8, 5): # bignum was added in Tcl 8.5 |
---|
422 | n/a | self.assertRaises(TclError, tcl.call, 'expr', str(2**1000)) |
---|
423 | n/a | |
---|
424 | n/a | def test_passing_values(self): |
---|
425 | n/a | def passValue(value): |
---|
426 | n/a | return self.interp.call('set', '_', value) |
---|
427 | n/a | |
---|
428 | n/a | self.assertEqual(passValue(True), True if self.wantobjects else '1') |
---|
429 | n/a | self.assertEqual(passValue(False), False if self.wantobjects else '0') |
---|
430 | n/a | self.assertEqual(passValue('string'), 'string') |
---|
431 | n/a | self.assertEqual(passValue('string\u20ac'), 'string\u20ac') |
---|
432 | n/a | self.assertEqual(passValue('str\x00ing'), 'str\x00ing') |
---|
433 | n/a | self.assertEqual(passValue('str\x00ing\xbd'), 'str\x00ing\xbd') |
---|
434 | n/a | self.assertEqual(passValue('str\x00ing\u20ac'), 'str\x00ing\u20ac') |
---|
435 | n/a | self.assertEqual(passValue(b'str\x00ing'), |
---|
436 | n/a | b'str\x00ing' if self.wantobjects else 'str\x00ing') |
---|
437 | n/a | self.assertEqual(passValue(b'str\xc0\x80ing'), |
---|
438 | n/a | b'str\xc0\x80ing' if self.wantobjects else 'str\xc0\x80ing') |
---|
439 | n/a | self.assertEqual(passValue(b'str\xbding'), |
---|
440 | n/a | b'str\xbding' if self.wantobjects else 'str\xbding') |
---|
441 | n/a | for i in self.get_integers(): |
---|
442 | n/a | self.assertEqual(passValue(i), i if self.wantobjects else str(i)) |
---|
443 | n/a | if tcl_version < (8, 5): # bignum was added in Tcl 8.5 |
---|
444 | n/a | self.assertEqual(passValue(2**1000), str(2**1000)) |
---|
445 | n/a | for f in (0.0, 1.0, -1.0, 1/3, |
---|
446 | n/a | sys.float_info.min, sys.float_info.max, |
---|
447 | n/a | -sys.float_info.min, -sys.float_info.max): |
---|
448 | n/a | if self.wantobjects: |
---|
449 | n/a | self.assertEqual(passValue(f), f) |
---|
450 | n/a | else: |
---|
451 | n/a | self.assertEqual(float(passValue(f)), f) |
---|
452 | n/a | if self.wantobjects: |
---|
453 | n/a | f = passValue(float('nan')) |
---|
454 | n/a | self.assertNotEqual(f, f) |
---|
455 | n/a | self.assertEqual(passValue(float('inf')), float('inf')) |
---|
456 | n/a | self.assertEqual(passValue(-float('inf')), -float('inf')) |
---|
457 | n/a | else: |
---|
458 | n/a | self.assertEqual(float(passValue(float('inf'))), float('inf')) |
---|
459 | n/a | self.assertEqual(float(passValue(-float('inf'))), -float('inf')) |
---|
460 | n/a | # XXX NaN representation can be not parsable by float() |
---|
461 | n/a | self.assertEqual(passValue((1, '2', (3.4,))), |
---|
462 | n/a | (1, '2', (3.4,)) if self.wantobjects else '1 2 3.4') |
---|
463 | n/a | self.assertEqual(passValue(['a', ['b', 'c']]), |
---|
464 | n/a | ('a', ('b', 'c')) if self.wantobjects else 'a {b c}') |
---|
465 | n/a | |
---|
466 | n/a | def test_user_command(self): |
---|
467 | n/a | result = None |
---|
468 | n/a | def testfunc(arg): |
---|
469 | n/a | nonlocal result |
---|
470 | n/a | result = arg |
---|
471 | n/a | return arg |
---|
472 | n/a | self.interp.createcommand('testfunc', testfunc) |
---|
473 | n/a | self.addCleanup(self.interp.tk.deletecommand, 'testfunc') |
---|
474 | n/a | def check(value, expected=None, *, eq=self.assertEqual): |
---|
475 | n/a | if expected is None: |
---|
476 | n/a | expected = value |
---|
477 | n/a | nonlocal result |
---|
478 | n/a | result = None |
---|
479 | n/a | r = self.interp.call('testfunc', value) |
---|
480 | n/a | self.assertIsInstance(result, str) |
---|
481 | n/a | eq(result, expected) |
---|
482 | n/a | self.assertIsInstance(r, str) |
---|
483 | n/a | eq(r, expected) |
---|
484 | n/a | def float_eq(actual, expected): |
---|
485 | n/a | self.assertAlmostEqual(float(actual), expected, |
---|
486 | n/a | delta=abs(expected) * 1e-10) |
---|
487 | n/a | |
---|
488 | n/a | check(True, '1') |
---|
489 | n/a | check(False, '0') |
---|
490 | n/a | check('string') |
---|
491 | n/a | check('string\xbd') |
---|
492 | n/a | check('string\u20ac') |
---|
493 | n/a | check('') |
---|
494 | n/a | check(b'string', 'string') |
---|
495 | n/a | check(b'string\xe2\x82\xac', 'string\xe2\x82\xac') |
---|
496 | n/a | check(b'string\xbd', 'string\xbd') |
---|
497 | n/a | check(b'', '') |
---|
498 | n/a | check('str\x00ing') |
---|
499 | n/a | check('str\x00ing\xbd') |
---|
500 | n/a | check('str\x00ing\u20ac') |
---|
501 | n/a | check(b'str\x00ing', 'str\x00ing') |
---|
502 | n/a | check(b'str\xc0\x80ing', 'str\xc0\x80ing') |
---|
503 | n/a | check(b'str\xc0\x80ing\xe2\x82\xac', 'str\xc0\x80ing\xe2\x82\xac') |
---|
504 | n/a | for i in self.get_integers(): |
---|
505 | n/a | check(i, str(i)) |
---|
506 | n/a | if tcl_version < (8, 5): # bignum was added in Tcl 8.5 |
---|
507 | n/a | check(2**1000, str(2**1000)) |
---|
508 | n/a | for f in (0.0, 1.0, -1.0): |
---|
509 | n/a | check(f, repr(f)) |
---|
510 | n/a | for f in (1/3.0, sys.float_info.min, sys.float_info.max, |
---|
511 | n/a | -sys.float_info.min, -sys.float_info.max): |
---|
512 | n/a | check(f, eq=float_eq) |
---|
513 | n/a | check(float('inf'), eq=float_eq) |
---|
514 | n/a | check(-float('inf'), eq=float_eq) |
---|
515 | n/a | # XXX NaN representation can be not parsable by float() |
---|
516 | n/a | check((), '') |
---|
517 | n/a | check((1, (2,), (3, 4), '5 6', ()), '1 2 {3 4} {5 6} {}') |
---|
518 | n/a | check([1, [2,], [3, 4], '5 6', []], '1 2 {3 4} {5 6} {}') |
---|
519 | n/a | |
---|
520 | n/a | def test_splitlist(self): |
---|
521 | n/a | splitlist = self.interp.tk.splitlist |
---|
522 | n/a | call = self.interp.tk.call |
---|
523 | n/a | self.assertRaises(TypeError, splitlist) |
---|
524 | n/a | self.assertRaises(TypeError, splitlist, 'a', 'b') |
---|
525 | n/a | self.assertRaises(TypeError, splitlist, 2) |
---|
526 | n/a | testcases = [ |
---|
527 | n/a | ('2', ('2',)), |
---|
528 | n/a | ('', ()), |
---|
529 | n/a | ('{}', ('',)), |
---|
530 | n/a | ('""', ('',)), |
---|
531 | n/a | ('a\n b\t\r c\n ', ('a', 'b', 'c')), |
---|
532 | n/a | (b'a\n b\t\r c\n ', ('a', 'b', 'c')), |
---|
533 | n/a | ('a \u20ac', ('a', '\u20ac')), |
---|
534 | n/a | (b'a \xe2\x82\xac', ('a', '\u20ac')), |
---|
535 | n/a | (b'a\xc0\x80b c\xc0\x80d', ('a\x00b', 'c\x00d')), |
---|
536 | n/a | ('a {b c}', ('a', 'b c')), |
---|
537 | n/a | (r'a b\ c', ('a', 'b c')), |
---|
538 | n/a | (('a', 'b c'), ('a', 'b c')), |
---|
539 | n/a | ('a 2', ('a', '2')), |
---|
540 | n/a | (('a', 2), ('a', 2)), |
---|
541 | n/a | ('a 3.4', ('a', '3.4')), |
---|
542 | n/a | (('a', 3.4), ('a', 3.4)), |
---|
543 | n/a | ((), ()), |
---|
544 | n/a | ([], ()), |
---|
545 | n/a | (['a', ['b', 'c']], ('a', ['b', 'c'])), |
---|
546 | n/a | (call('list', 1, '2', (3.4,)), |
---|
547 | n/a | (1, '2', (3.4,)) if self.wantobjects else |
---|
548 | n/a | ('1', '2', '3.4')), |
---|
549 | n/a | ] |
---|
550 | n/a | tk_patchlevel = get_tk_patchlevel() |
---|
551 | n/a | if tcl_version >= (8, 5): |
---|
552 | n/a | if not self.wantobjects or tk_patchlevel < (8, 5, 5): |
---|
553 | n/a | # Before 8.5.5 dicts were converted to lists through string |
---|
554 | n/a | expected = ('12', '\u20ac', '\xe2\x82\xac', '3.4') |
---|
555 | n/a | else: |
---|
556 | n/a | expected = (12, '\u20ac', b'\xe2\x82\xac', (3.4,)) |
---|
557 | n/a | testcases += [ |
---|
558 | n/a | (call('dict', 'create', 12, '\u20ac', b'\xe2\x82\xac', (3.4,)), |
---|
559 | n/a | expected), |
---|
560 | n/a | ] |
---|
561 | n/a | dbg_info = ('want objects? %s, Tcl version: %s, Tk patchlevel: %s' |
---|
562 | n/a | % (self.wantobjects, tcl_version, tk_patchlevel)) |
---|
563 | n/a | for arg, res in testcases: |
---|
564 | n/a | self.assertEqual(splitlist(arg), res, |
---|
565 | n/a | 'arg=%a, %s' % (arg, dbg_info)) |
---|
566 | n/a | self.assertRaises(TclError, splitlist, '{') |
---|
567 | n/a | |
---|
568 | n/a | def test_split(self): |
---|
569 | n/a | split = self.interp.tk.split |
---|
570 | n/a | call = self.interp.tk.call |
---|
571 | n/a | self.assertRaises(TypeError, split) |
---|
572 | n/a | self.assertRaises(TypeError, split, 'a', 'b') |
---|
573 | n/a | self.assertRaises(TypeError, split, 2) |
---|
574 | n/a | testcases = [ |
---|
575 | n/a | ('2', '2'), |
---|
576 | n/a | ('', ''), |
---|
577 | n/a | ('{}', ''), |
---|
578 | n/a | ('""', ''), |
---|
579 | n/a | ('{', '{'), |
---|
580 | n/a | ('a\n b\t\r c\n ', ('a', 'b', 'c')), |
---|
581 | n/a | (b'a\n b\t\r c\n ', ('a', 'b', 'c')), |
---|
582 | n/a | ('a \u20ac', ('a', '\u20ac')), |
---|
583 | n/a | (b'a \xe2\x82\xac', ('a', '\u20ac')), |
---|
584 | n/a | (b'a\xc0\x80b', 'a\x00b'), |
---|
585 | n/a | (b'a\xc0\x80b c\xc0\x80d', ('a\x00b', 'c\x00d')), |
---|
586 | n/a | (b'{a\xc0\x80b c\xc0\x80d', '{a\x00b c\x00d'), |
---|
587 | n/a | ('a {b c}', ('a', ('b', 'c'))), |
---|
588 | n/a | (r'a b\ c', ('a', ('b', 'c'))), |
---|
589 | n/a | (('a', b'b c'), ('a', ('b', 'c'))), |
---|
590 | n/a | (('a', 'b c'), ('a', ('b', 'c'))), |
---|
591 | n/a | ('a 2', ('a', '2')), |
---|
592 | n/a | (('a', 2), ('a', 2)), |
---|
593 | n/a | ('a 3.4', ('a', '3.4')), |
---|
594 | n/a | (('a', 3.4), ('a', 3.4)), |
---|
595 | n/a | (('a', (2, 3.4)), ('a', (2, 3.4))), |
---|
596 | n/a | ((), ()), |
---|
597 | n/a | ([], ()), |
---|
598 | n/a | (['a', 'b c'], ('a', ('b', 'c'))), |
---|
599 | n/a | (['a', ['b', 'c']], ('a', ('b', 'c'))), |
---|
600 | n/a | (call('list', 1, '2', (3.4,)), |
---|
601 | n/a | (1, '2', (3.4,)) if self.wantobjects else |
---|
602 | n/a | ('1', '2', '3.4')), |
---|
603 | n/a | ] |
---|
604 | n/a | if tcl_version >= (8, 5): |
---|
605 | n/a | if not self.wantobjects or get_tk_patchlevel() < (8, 5, 5): |
---|
606 | n/a | # Before 8.5.5 dicts were converted to lists through string |
---|
607 | n/a | expected = ('12', '\u20ac', '\xe2\x82\xac', '3.4') |
---|
608 | n/a | else: |
---|
609 | n/a | expected = (12, '\u20ac', b'\xe2\x82\xac', (3.4,)) |
---|
610 | n/a | testcases += [ |
---|
611 | n/a | (call('dict', 'create', 12, '\u20ac', b'\xe2\x82\xac', (3.4,)), |
---|
612 | n/a | expected), |
---|
613 | n/a | ] |
---|
614 | n/a | for arg, res in testcases: |
---|
615 | n/a | self.assertEqual(split(arg), res, msg=arg) |
---|
616 | n/a | |
---|
617 | n/a | def test_splitdict(self): |
---|
618 | n/a | splitdict = tkinter._splitdict |
---|
619 | n/a | tcl = self.interp.tk |
---|
620 | n/a | |
---|
621 | n/a | arg = '-a {1 2 3} -something foo status {}' |
---|
622 | n/a | self.assertEqual(splitdict(tcl, arg, False), |
---|
623 | n/a | {'-a': '1 2 3', '-something': 'foo', 'status': ''}) |
---|
624 | n/a | self.assertEqual(splitdict(tcl, arg), |
---|
625 | n/a | {'a': '1 2 3', 'something': 'foo', 'status': ''}) |
---|
626 | n/a | |
---|
627 | n/a | arg = ('-a', (1, 2, 3), '-something', 'foo', 'status', '{}') |
---|
628 | n/a | self.assertEqual(splitdict(tcl, arg, False), |
---|
629 | n/a | {'-a': (1, 2, 3), '-something': 'foo', 'status': '{}'}) |
---|
630 | n/a | self.assertEqual(splitdict(tcl, arg), |
---|
631 | n/a | {'a': (1, 2, 3), 'something': 'foo', 'status': '{}'}) |
---|
632 | n/a | |
---|
633 | n/a | self.assertRaises(RuntimeError, splitdict, tcl, '-a b -c ') |
---|
634 | n/a | self.assertRaises(RuntimeError, splitdict, tcl, ('-a', 'b', '-c')) |
---|
635 | n/a | |
---|
636 | n/a | arg = tcl.call('list', |
---|
637 | n/a | '-a', (1, 2, 3), '-something', 'foo', 'status', ()) |
---|
638 | n/a | self.assertEqual(splitdict(tcl, arg), |
---|
639 | n/a | {'a': (1, 2, 3) if self.wantobjects else '1 2 3', |
---|
640 | n/a | 'something': 'foo', 'status': ''}) |
---|
641 | n/a | |
---|
642 | n/a | if tcl_version >= (8, 5): |
---|
643 | n/a | arg = tcl.call('dict', 'create', |
---|
644 | n/a | '-a', (1, 2, 3), '-something', 'foo', 'status', ()) |
---|
645 | n/a | if not self.wantobjects or get_tk_patchlevel() < (8, 5, 5): |
---|
646 | n/a | # Before 8.5.5 dicts were converted to lists through string |
---|
647 | n/a | expected = {'a': '1 2 3', 'something': 'foo', 'status': ''} |
---|
648 | n/a | else: |
---|
649 | n/a | expected = {'a': (1, 2, 3), 'something': 'foo', 'status': ''} |
---|
650 | n/a | self.assertEqual(splitdict(tcl, arg), expected) |
---|
651 | n/a | |
---|
652 | n/a | def test_new_tcl_obj(self): |
---|
653 | n/a | self.assertRaises(TypeError, _tkinter.Tcl_Obj) |
---|
654 | n/a | |
---|
655 | n/a | class BigmemTclTest(unittest.TestCase): |
---|
656 | n/a | |
---|
657 | n/a | def setUp(self): |
---|
658 | n/a | self.interp = Tcl() |
---|
659 | n/a | |
---|
660 | n/a | @support.cpython_only |
---|
661 | n/a | @unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX, "needs UINT_MAX < SIZE_MAX") |
---|
662 | n/a | @support.bigmemtest(size=INT_MAX + 1, memuse=5, dry_run=False) |
---|
663 | n/a | def test_huge_string_call(self, size): |
---|
664 | n/a | value = ' ' * size |
---|
665 | n/a | self.assertRaises(OverflowError, self.interp.call, 'set', '_', value) |
---|
666 | n/a | |
---|
667 | n/a | @support.cpython_only |
---|
668 | n/a | @unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX, "needs UINT_MAX < SIZE_MAX") |
---|
669 | n/a | @support.bigmemtest(size=INT_MAX + 1, memuse=9, dry_run=False) |
---|
670 | n/a | def test_huge_string_builtins(self, size): |
---|
671 | n/a | value = '1' + ' ' * size |
---|
672 | n/a | self.assertRaises(OverflowError, self.interp.tk.getint, value) |
---|
673 | n/a | self.assertRaises(OverflowError, self.interp.tk.getdouble, value) |
---|
674 | n/a | self.assertRaises(OverflowError, self.interp.tk.getboolean, value) |
---|
675 | n/a | self.assertRaises(OverflowError, self.interp.eval, value) |
---|
676 | n/a | self.assertRaises(OverflowError, self.interp.evalfile, value) |
---|
677 | n/a | self.assertRaises(OverflowError, self.interp.record, value) |
---|
678 | n/a | self.assertRaises(OverflowError, self.interp.adderrorinfo, value) |
---|
679 | n/a | self.assertRaises(OverflowError, self.interp.setvar, value, 'x', 'a') |
---|
680 | n/a | self.assertRaises(OverflowError, self.interp.setvar, 'x', value, 'a') |
---|
681 | n/a | self.assertRaises(OverflowError, self.interp.unsetvar, value) |
---|
682 | n/a | self.assertRaises(OverflowError, self.interp.unsetvar, 'x', value) |
---|
683 | n/a | self.assertRaises(OverflowError, self.interp.adderrorinfo, value) |
---|
684 | n/a | self.assertRaises(OverflowError, self.interp.exprstring, value) |
---|
685 | n/a | self.assertRaises(OverflowError, self.interp.exprlong, value) |
---|
686 | n/a | self.assertRaises(OverflowError, self.interp.exprboolean, value) |
---|
687 | n/a | self.assertRaises(OverflowError, self.interp.splitlist, value) |
---|
688 | n/a | self.assertRaises(OverflowError, self.interp.split, value) |
---|
689 | n/a | self.assertRaises(OverflowError, self.interp.createcommand, value, max) |
---|
690 | n/a | self.assertRaises(OverflowError, self.interp.deletecommand, value) |
---|
691 | n/a | |
---|
692 | n/a | |
---|
693 | n/a | def setUpModule(): |
---|
694 | n/a | if support.verbose: |
---|
695 | n/a | tcl = Tcl() |
---|
696 | n/a | print('patchlevel =', tcl.call('info', 'patchlevel')) |
---|
697 | n/a | |
---|
698 | n/a | |
---|
699 | n/a | def test_main(): |
---|
700 | n/a | support.run_unittest(TclTest, TkinterTest, BigmemTclTest) |
---|
701 | n/a | |
---|
702 | n/a | if __name__ == "__main__": |
---|
703 | n/a | test_main() |
---|