ยปCore Development>Code coverage>Lib/test/test_tcl.py

Python code coverage for Lib/test/test_tcl.py

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