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