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