2004-02-18 01:59:53 -04:00
|
|
|
import unittest
|
2013-02-07 09:37:53 -04:00
|
|
|
import sys
|
2004-02-18 01:59:53 -04:00
|
|
|
import os
|
2004-02-18 22:37:29 -04:00
|
|
|
from test import test_support
|
2013-05-07 03:34:49 -03:00
|
|
|
from subprocess import Popen, PIPE
|
2009-03-31 15:32:17 -03:00
|
|
|
|
|
|
|
# Skip this test if the _tkinter module wasn't built.
|
|
|
|
_tkinter = test_support.import_module('_tkinter')
|
|
|
|
|
2009-02-06 22:20:29 -04:00
|
|
|
from Tkinter import Tcl
|
2004-02-18 01:59:53 -04:00
|
|
|
from _tkinter import TclError
|
|
|
|
|
2014-02-07 04:06:05 -04:00
|
|
|
try:
|
|
|
|
from _testcapi import INT_MAX, PY_SSIZE_T_MAX
|
|
|
|
except ImportError:
|
|
|
|
INT_MAX = PY_SSIZE_T_MAX = sys.maxsize
|
|
|
|
|
2013-09-08 14:32:56 -03:00
|
|
|
tcl_version = _tkinter.TCL_VERSION.split('.')
|
|
|
|
try:
|
|
|
|
for i in range(len(tcl_version)):
|
|
|
|
tcl_version[i] = int(tcl_version[i])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
tcl_version = tuple(tcl_version)
|
|
|
|
|
2014-02-02 17:04:06 -04:00
|
|
|
_tk_patchlevel = None
|
|
|
|
def get_tk_patchlevel():
|
|
|
|
global _tk_patchlevel
|
|
|
|
if _tk_patchlevel is None:
|
|
|
|
tcl = Tcl()
|
|
|
|
patchlevel = []
|
|
|
|
for x in tcl.call('info', 'patchlevel').split('.'):
|
|
|
|
try:
|
|
|
|
x = int(x, 10)
|
|
|
|
except ValueError:
|
|
|
|
x = -1
|
|
|
|
patchlevel.append(x)
|
|
|
|
_tk_patchlevel = tuple(patchlevel)
|
|
|
|
return _tk_patchlevel
|
|
|
|
|
2009-01-29 22:24:39 -04:00
|
|
|
|
|
|
|
class TkinterTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def testFlattenLen(self):
|
|
|
|
# flatten(<object with no length>)
|
|
|
|
self.assertRaises(TypeError, _tkinter._flatten, True)
|
|
|
|
|
|
|
|
|
2004-02-18 01:59:53 -04:00
|
|
|
class TclTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.interp = Tcl()
|
2013-12-25 11:28:50 -04:00
|
|
|
self.wantobjects = self.interp.tk.wantobjects()
|
2004-02-18 01:59:53 -04:00
|
|
|
|
|
|
|
def testEval(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.eval('set a 1')
|
|
|
|
self.assertEqual(tcl.eval('set a'),'1')
|
|
|
|
|
|
|
|
def testEvalException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.eval,'set a')
|
|
|
|
|
|
|
|
def testEvalException2(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.eval,'this is wrong')
|
|
|
|
|
|
|
|
def testCall(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.call('set','a','1')
|
|
|
|
self.assertEqual(tcl.call('set','a'),'1')
|
|
|
|
|
|
|
|
def testCallException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.call,'set','a')
|
|
|
|
|
|
|
|
def testCallException2(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.call,'this','is','wrong')
|
|
|
|
|
|
|
|
def testSetVar(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.setvar('a','1')
|
|
|
|
self.assertEqual(tcl.eval('set a'),'1')
|
|
|
|
|
|
|
|
def testSetVarArray(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.setvar('a(1)','1')
|
|
|
|
self.assertEqual(tcl.eval('set a(1)'),'1')
|
|
|
|
|
|
|
|
def testGetVar(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.eval('set a 1')
|
|
|
|
self.assertEqual(tcl.getvar('a'),'1')
|
|
|
|
|
|
|
|
def testGetVarArray(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.eval('set a(1) 1')
|
|
|
|
self.assertEqual(tcl.getvar('a(1)'),'1')
|
|
|
|
|
|
|
|
def testGetVarException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.getvar,'a')
|
|
|
|
|
|
|
|
def testGetVarArrayException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.getvar,'a(1)')
|
|
|
|
|
|
|
|
def testUnsetVar(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.setvar('a',1)
|
|
|
|
self.assertEqual(tcl.eval('info exists a'),'1')
|
|
|
|
tcl.unsetvar('a')
|
|
|
|
self.assertEqual(tcl.eval('info exists a'),'0')
|
|
|
|
|
|
|
|
def testUnsetVarArray(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.setvar('a(1)',1)
|
|
|
|
tcl.setvar('a(2)',2)
|
|
|
|
self.assertEqual(tcl.eval('info exists a(1)'),'1')
|
|
|
|
self.assertEqual(tcl.eval('info exists a(2)'),'1')
|
|
|
|
tcl.unsetvar('a(1)')
|
|
|
|
self.assertEqual(tcl.eval('info exists a(1)'),'0')
|
|
|
|
self.assertEqual(tcl.eval('info exists a(2)'),'1')
|
|
|
|
|
|
|
|
def testUnsetVarException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.unsetvar,'a')
|
2004-07-08 01:22:35 -03:00
|
|
|
|
2014-05-30 08:07:20 -03:00
|
|
|
def test_getint(self):
|
|
|
|
tcl = self.interp.tk
|
|
|
|
self.assertEqual(tcl.getint(' 42 '), 42)
|
|
|
|
self.assertEqual(tcl.getint(42), 42)
|
|
|
|
self.assertRaises(TypeError, tcl.getint)
|
|
|
|
self.assertRaises(TypeError, tcl.getint, '42', '10')
|
|
|
|
self.assertRaises(TypeError, tcl.getint, 42.0)
|
|
|
|
self.assertRaises(TclError, tcl.getint, 'a')
|
|
|
|
self.assertRaises((TypeError, ValueError, TclError),
|
|
|
|
tcl.getint, '42\0')
|
|
|
|
if test_support.have_unicode:
|
|
|
|
self.assertEqual(tcl.getint(unicode('42')), 42)
|
|
|
|
self.assertRaises((UnicodeEncodeError, ValueError, TclError),
|
|
|
|
tcl.getint, '42' + unichr(0xd800))
|
|
|
|
|
|
|
|
def test_getdouble(self):
|
|
|
|
tcl = self.interp.tk
|
|
|
|
self.assertEqual(tcl.getdouble(' 42 '), 42.0)
|
|
|
|
self.assertEqual(tcl.getdouble(' 42.5 '), 42.5)
|
|
|
|
self.assertEqual(tcl.getdouble(42.5), 42.5)
|
|
|
|
self.assertRaises(TypeError, tcl.getdouble)
|
|
|
|
self.assertRaises(TypeError, tcl.getdouble, '42.5', '10')
|
|
|
|
self.assertRaises(TypeError, tcl.getdouble, 42)
|
|
|
|
self.assertRaises(TclError, tcl.getdouble, 'a')
|
|
|
|
self.assertRaises((TypeError, ValueError, TclError),
|
|
|
|
tcl.getdouble, '42.5\0')
|
|
|
|
if test_support.have_unicode:
|
|
|
|
self.assertEqual(tcl.getdouble(unicode('42.5')), 42.5)
|
|
|
|
self.assertRaises((UnicodeEncodeError, ValueError, TclError),
|
|
|
|
tcl.getdouble, '42.5' + unichr(0xd800))
|
|
|
|
|
|
|
|
def test_getboolean(self):
|
|
|
|
tcl = self.interp.tk
|
|
|
|
self.assertIs(tcl.getboolean('on'), True)
|
|
|
|
self.assertIs(tcl.getboolean('1'), True)
|
|
|
|
self.assertEqual(tcl.getboolean(42), 42)
|
|
|
|
self.assertRaises(TypeError, tcl.getboolean)
|
|
|
|
self.assertRaises(TypeError, tcl.getboolean, 'on', '1')
|
|
|
|
self.assertRaises(TypeError, tcl.getboolean, 1.0)
|
|
|
|
self.assertRaises(TclError, tcl.getboolean, 'a')
|
|
|
|
self.assertRaises((TypeError, ValueError, TclError),
|
|
|
|
tcl.getboolean, 'on\0')
|
|
|
|
if test_support.have_unicode:
|
|
|
|
self.assertIs(tcl.getboolean(unicode('on')), True)
|
|
|
|
self.assertRaises((UnicodeEncodeError, ValueError, TclError),
|
|
|
|
tcl.getboolean, 'on' + unichr(0xd800))
|
|
|
|
|
2004-02-18 01:59:53 -04:00
|
|
|
def testEvalFile(self):
|
|
|
|
tcl = self.interp
|
|
|
|
filename = "testEvalFile.tcl"
|
|
|
|
fd = open(filename,'w')
|
|
|
|
script = """set a 1
|
|
|
|
set b 2
|
|
|
|
set c [ expr $a + $b ]
|
|
|
|
"""
|
|
|
|
fd.write(script)
|
|
|
|
fd.close()
|
|
|
|
tcl.evalfile(filename)
|
2004-02-29 11:37:50 -04:00
|
|
|
os.remove(filename)
|
2004-02-18 01:59:53 -04:00
|
|
|
self.assertEqual(tcl.eval('set a'),'1')
|
|
|
|
self.assertEqual(tcl.eval('set b'),'2')
|
|
|
|
self.assertEqual(tcl.eval('set c'),'3')
|
|
|
|
|
2014-02-03 15:23:46 -04:00
|
|
|
def test_evalfile_null_in_result(self):
|
|
|
|
tcl = self.interp
|
|
|
|
with open(test_support.TESTFN, 'wb') as f:
|
|
|
|
self.addCleanup(test_support.unlink, test_support.TESTFN)
|
|
|
|
f.write("""
|
|
|
|
set a "a\0b"
|
|
|
|
set b "a\\0b"
|
|
|
|
""")
|
|
|
|
tcl.evalfile(test_support.TESTFN)
|
|
|
|
self.assertEqual(tcl.eval('set a'), 'a\xc0\x80b')
|
|
|
|
self.assertEqual(tcl.eval('set b'), 'a\xc0\x80b')
|
|
|
|
|
2004-02-18 01:59:53 -04:00
|
|
|
def testEvalFileException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
filename = "doesnotexists"
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except Exception,e:
|
|
|
|
pass
|
|
|
|
self.assertRaises(TclError,tcl.evalfile,filename)
|
|
|
|
|
|
|
|
def testPackageRequireException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.eval,'package require DNE')
|
|
|
|
|
2013-11-04 00:51:25 -04:00
|
|
|
@unittest.skipUnless(sys.platform == 'win32', "only applies to Windows")
|
2010-06-04 16:39:07 -03:00
|
|
|
def testLoadWithUNC(self):
|
|
|
|
# Build a UNC path from the regular path.
|
|
|
|
# Something like
|
|
|
|
# \\%COMPUTERNAME%\c$\python27\python.exe
|
|
|
|
|
|
|
|
fullname = os.path.abspath(sys.executable)
|
|
|
|
if fullname[1] != ':':
|
2013-11-04 00:51:25 -04:00
|
|
|
self.skipTest('unusable path: %r' % fullname)
|
2010-06-04 16:39:07 -03:00
|
|
|
unc_name = r'\\%s\%s$\%s' % (os.environ['COMPUTERNAME'],
|
|
|
|
fullname[0],
|
|
|
|
fullname[3:])
|
|
|
|
|
|
|
|
with test_support.EnvironmentVarGuard() as env:
|
|
|
|
env.unset("TCL_LIBRARY")
|
2013-05-07 03:34:49 -03:00
|
|
|
cmd = '%s -c "import Tkinter; print Tkinter"' % (unc_name,)
|
|
|
|
|
2013-11-04 00:51:25 -04:00
|
|
|
try:
|
|
|
|
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
|
|
|
|
except WindowsError as e:
|
|
|
|
if e.winerror == 5:
|
|
|
|
self.skipTest('Not permitted to start the child process')
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2013-05-07 03:34:49 -03:00
|
|
|
out_data, err_data = p.communicate()
|
|
|
|
|
|
|
|
msg = '\n\n'.join(['"Tkinter.py" not in output',
|
|
|
|
'Command:', cmd,
|
|
|
|
'stdout:', out_data,
|
|
|
|
'stderr:', err_data])
|
|
|
|
|
|
|
|
self.assertIn('Tkinter.py', out_data, msg)
|
|
|
|
|
|
|
|
self.assertEqual(p.wait(), 0, 'Non-zero exit code')
|
2010-06-04 16:39:07 -03:00
|
|
|
|
|
|
|
|
2014-02-03 14:41:04 -04:00
|
|
|
def test_exprstring(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.call('set', 'a', 3)
|
|
|
|
tcl.call('set', 'b', 6)
|
|
|
|
def check(expr, expected):
|
|
|
|
result = tcl.exprstring(expr)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
self.assertIsInstance(result, str)
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, tcl.exprstring)
|
|
|
|
self.assertRaises(TypeError, tcl.exprstring, '8.2', '+6')
|
|
|
|
self.assertRaises(TclError, tcl.exprstring, 'spam')
|
|
|
|
check('', '0')
|
|
|
|
check('8.2 + 6', '14.2')
|
|
|
|
check('3.1 + $a', '6.1')
|
|
|
|
check('2 + "$a.$b"', '5.6')
|
|
|
|
check('4*[llength "6 2"]', '8')
|
|
|
|
check('{word one} < "word $a"', '0')
|
|
|
|
check('4*2 < 7', '0')
|
|
|
|
check('hypot($a, 4)', '5.0')
|
|
|
|
check('5 / 4', '1')
|
|
|
|
check('5 / 4.0', '1.25')
|
|
|
|
check('5 / ( [string length "abcd"] + 0.0 )', '1.25')
|
|
|
|
check('20.0/5.0', '4.0')
|
|
|
|
check('"0x03" > "2"', '1')
|
|
|
|
check('[string length "a\xc2\xbd\xe2\x82\xac"]', '3')
|
|
|
|
check(r'[string length "a\xbd\u20ac"]', '3')
|
|
|
|
check('"abc"', 'abc')
|
|
|
|
check('"a\xc2\xbd\xe2\x82\xac"', 'a\xc2\xbd\xe2\x82\xac')
|
|
|
|
check(r'"a\xbd\u20ac"', 'a\xc2\xbd\xe2\x82\xac')
|
2014-02-03 15:23:46 -04:00
|
|
|
check(r'"a\0b"', 'a\xc0\x80b')
|
2014-02-03 16:30:22 -04:00
|
|
|
if tcl_version >= (8, 5):
|
|
|
|
check('2**64', str(2**64))
|
2014-02-03 14:41:04 -04:00
|
|
|
|
|
|
|
def test_exprdouble(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.call('set', 'a', 3)
|
|
|
|
tcl.call('set', 'b', 6)
|
|
|
|
def check(expr, expected):
|
|
|
|
result = tcl.exprdouble(expr)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
self.assertIsInstance(result, float)
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, tcl.exprdouble)
|
|
|
|
self.assertRaises(TypeError, tcl.exprdouble, '8.2', '+6')
|
|
|
|
self.assertRaises(TclError, tcl.exprdouble, 'spam')
|
|
|
|
check('', 0.0)
|
|
|
|
check('8.2 + 6', 14.2)
|
|
|
|
check('3.1 + $a', 6.1)
|
|
|
|
check('2 + "$a.$b"', 5.6)
|
|
|
|
check('4*[llength "6 2"]', 8.0)
|
|
|
|
check('{word one} < "word $a"', 0.0)
|
|
|
|
check('4*2 < 7', 0.0)
|
|
|
|
check('hypot($a, 4)', 5.0)
|
|
|
|
check('5 / 4', 1.0)
|
|
|
|
check('5 / 4.0', 1.25)
|
|
|
|
check('5 / ( [string length "abcd"] + 0.0 )', 1.25)
|
|
|
|
check('20.0/5.0', 4.0)
|
|
|
|
check('"0x03" > "2"', 1.0)
|
|
|
|
check('[string length "a\xc2\xbd\xe2\x82\xac"]', 3.0)
|
|
|
|
check(r'[string length "a\xbd\u20ac"]', 3.0)
|
|
|
|
self.assertRaises(TclError, tcl.exprdouble, '"abc"')
|
2014-02-03 16:30:22 -04:00
|
|
|
if tcl_version >= (8, 5):
|
|
|
|
check('2**64', float(2**64))
|
2014-02-03 14:41:04 -04:00
|
|
|
|
|
|
|
def test_exprlong(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.call('set', 'a', 3)
|
|
|
|
tcl.call('set', 'b', 6)
|
|
|
|
def check(expr, expected):
|
|
|
|
result = tcl.exprlong(expr)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
self.assertIsInstance(result, int)
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, tcl.exprlong)
|
|
|
|
self.assertRaises(TypeError, tcl.exprlong, '8.2', '+6')
|
|
|
|
self.assertRaises(TclError, tcl.exprlong, 'spam')
|
|
|
|
check('', 0)
|
|
|
|
check('8.2 + 6', 14)
|
|
|
|
check('3.1 + $a', 6)
|
|
|
|
check('2 + "$a.$b"', 5)
|
|
|
|
check('4*[llength "6 2"]', 8)
|
|
|
|
check('{word one} < "word $a"', 0)
|
|
|
|
check('4*2 < 7', 0)
|
|
|
|
check('hypot($a, 4)', 5)
|
|
|
|
check('5 / 4', 1)
|
|
|
|
check('5 / 4.0', 1)
|
|
|
|
check('5 / ( [string length "abcd"] + 0.0 )', 1)
|
|
|
|
check('20.0/5.0', 4)
|
|
|
|
check('"0x03" > "2"', 1)
|
|
|
|
check('[string length "a\xc2\xbd\xe2\x82\xac"]', 3)
|
|
|
|
check(r'[string length "a\xbd\u20ac"]', 3)
|
|
|
|
self.assertRaises(TclError, tcl.exprlong, '"abc"')
|
2014-02-03 16:30:22 -04:00
|
|
|
if tcl_version >= (8, 5):
|
|
|
|
self.assertRaises(TclError, tcl.exprlong, '2**64')
|
2014-02-03 14:41:04 -04:00
|
|
|
|
|
|
|
def test_exprboolean(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.call('set', 'a', 3)
|
|
|
|
tcl.call('set', 'b', 6)
|
|
|
|
def check(expr, expected):
|
|
|
|
result = tcl.exprboolean(expr)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
self.assertIsInstance(result, int)
|
|
|
|
self.assertNotIsInstance(result, bool)
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, tcl.exprboolean)
|
|
|
|
self.assertRaises(TypeError, tcl.exprboolean, '8.2', '+6')
|
|
|
|
self.assertRaises(TclError, tcl.exprboolean, 'spam')
|
|
|
|
check('', False)
|
|
|
|
for value in ('0', 'false', 'no', 'off'):
|
|
|
|
check(value, False)
|
|
|
|
check('"%s"' % value, False)
|
|
|
|
check('{%s}' % value, False)
|
|
|
|
for value in ('1', 'true', 'yes', 'on'):
|
|
|
|
check(value, True)
|
|
|
|
check('"%s"' % value, True)
|
|
|
|
check('{%s}' % value, True)
|
|
|
|
check('8.2 + 6', True)
|
|
|
|
check('3.1 + $a', True)
|
|
|
|
check('2 + "$a.$b"', True)
|
|
|
|
check('4*[llength "6 2"]', True)
|
|
|
|
check('{word one} < "word $a"', False)
|
|
|
|
check('4*2 < 7', False)
|
|
|
|
check('hypot($a, 4)', True)
|
|
|
|
check('5 / 4', True)
|
|
|
|
check('5 / 4.0', True)
|
|
|
|
check('5 / ( [string length "abcd"] + 0.0 )', True)
|
|
|
|
check('20.0/5.0', True)
|
|
|
|
check('"0x03" > "2"', True)
|
|
|
|
check('[string length "a\xc2\xbd\xe2\x82\xac"]', True)
|
|
|
|
check(r'[string length "a\xbd\u20ac"]', True)
|
|
|
|
self.assertRaises(TclError, tcl.exprboolean, '"abc"')
|
2014-02-03 16:30:22 -04:00
|
|
|
if tcl_version >= (8, 5):
|
|
|
|
check('2**64', True)
|
2014-02-03 14:41:04 -04:00
|
|
|
|
2013-02-07 09:37:53 -04:00
|
|
|
def test_passing_values(self):
|
|
|
|
def passValue(value):
|
|
|
|
return self.interp.call('set', '_', value)
|
2013-12-25 11:28:50 -04:00
|
|
|
|
|
|
|
self.assertEqual(passValue(True), True if self.wantobjects else '1')
|
|
|
|
self.assertEqual(passValue(False), False if self.wantobjects else '0')
|
2014-02-03 15:23:46 -04:00
|
|
|
self.assertEqual(passValue('string'), 'string')
|
|
|
|
self.assertEqual(passValue('string\xbd'), 'string\xbd')
|
|
|
|
self.assertEqual(passValue('string\xe2\x82\xac'), u'string\u20ac')
|
2013-02-07 09:37:53 -04:00
|
|
|
self.assertEqual(passValue(u'string'), u'string')
|
2014-02-03 15:23:46 -04:00
|
|
|
self.assertEqual(passValue(u'string\xbd'), u'string\xbd')
|
2013-02-07 09:37:53 -04:00
|
|
|
self.assertEqual(passValue(u'string\u20ac'), u'string\u20ac')
|
2014-02-03 15:23:46 -04:00
|
|
|
self.assertEqual(passValue('str\x00ing'), 'str\x00ing')
|
|
|
|
self.assertEqual(passValue('str\xc0\x80ing'), 'str\x00ing')
|
|
|
|
self.assertEqual(passValue(u'str\x00ing'), u'str\x00ing')
|
|
|
|
self.assertEqual(passValue(u'str\x00ing\xbd'), u'str\x00ing\xbd')
|
|
|
|
self.assertEqual(passValue(u'str\x00ing\u20ac'), u'str\x00ing\u20ac')
|
2013-02-07 09:37:53 -04:00
|
|
|
for i in (0, 1, -1, int(2**31-1), int(-2**31)):
|
2013-12-25 11:28:50 -04:00
|
|
|
self.assertEqual(passValue(i), i if self.wantobjects else str(i))
|
2013-02-23 02:19:00 -04:00
|
|
|
for f in (0.0, 1.0, -1.0, 1//3, 1/3.0,
|
2013-02-07 09:37:53 -04:00
|
|
|
sys.float_info.min, sys.float_info.max,
|
|
|
|
-sys.float_info.min, -sys.float_info.max):
|
2013-12-25 11:28:50 -04:00
|
|
|
if self.wantobjects:
|
2013-02-07 09:37:53 -04:00
|
|
|
self.assertEqual(passValue(f), f)
|
2013-12-25 11:28:50 -04:00
|
|
|
else:
|
|
|
|
self.assertEqual(float(passValue(f)), f)
|
|
|
|
if self.wantobjects:
|
|
|
|
f = passValue(float('nan'))
|
|
|
|
self.assertNotEqual(f, f)
|
|
|
|
self.assertEqual(passValue(float('inf')), float('inf'))
|
|
|
|
self.assertEqual(passValue(-float('inf')), -float('inf'))
|
|
|
|
else:
|
|
|
|
f = float(passValue(float('nan')))
|
|
|
|
self.assertNotEqual(f, f)
|
|
|
|
self.assertEqual(float(passValue(float('inf'))), float('inf'))
|
|
|
|
self.assertEqual(float(passValue(-float('inf'))), -float('inf'))
|
|
|
|
self.assertEqual(passValue((1, '2', (3.4,))),
|
|
|
|
(1, '2', (3.4,)) if self.wantobjects else '1 2 3.4')
|
2010-06-04 16:39:07 -03:00
|
|
|
|
2014-01-23 03:42:46 -04:00
|
|
|
def test_user_command(self):
|
|
|
|
result = []
|
|
|
|
def testfunc(arg):
|
|
|
|
result.append(arg)
|
|
|
|
return arg
|
|
|
|
self.interp.createcommand('testfunc', testfunc)
|
2014-02-23 14:39:06 -04:00
|
|
|
self.addCleanup(self.interp.tk.deletecommand, 'testfunc')
|
2014-02-03 15:23:46 -04:00
|
|
|
def check(value, expected, expected2=None, eq=self.assertEqual):
|
|
|
|
if expected2 is None:
|
|
|
|
expected2 = expected
|
2014-01-23 03:42:46 -04:00
|
|
|
del result[:]
|
2014-01-23 05:03:02 -04:00
|
|
|
r = self.interp.call('testfunc', value)
|
|
|
|
self.assertEqual(len(result), 1)
|
2014-02-03 15:23:46 -04:00
|
|
|
self.assertIsInstance(result[0], (str, unicode))
|
|
|
|
eq(result[0], expected2)
|
|
|
|
self.assertIsInstance(r, (str, unicode))
|
|
|
|
eq(r, expected2)
|
2014-01-23 08:38:44 -04:00
|
|
|
def float_eq(actual, expected):
|
2014-01-23 10:08:35 -04:00
|
|
|
expected = float(expected)
|
|
|
|
self.assertAlmostEqual(float(actual), expected,
|
|
|
|
delta=abs(expected) * 1e-10)
|
2014-01-23 08:38:44 -04:00
|
|
|
def nan_eq(actual, expected):
|
|
|
|
actual = float(actual)
|
|
|
|
self.assertNotEqual(actual, actual)
|
2014-01-23 03:42:46 -04:00
|
|
|
|
|
|
|
check(True, '1')
|
|
|
|
check(False, '0')
|
|
|
|
check('string', 'string')
|
|
|
|
check('string\xbd', 'string\xbd')
|
2014-02-03 15:23:46 -04:00
|
|
|
check('string\xe2\x82\xac', 'string\xe2\x82\xac', u'string\u20ac')
|
|
|
|
check(u'string', u'string')
|
|
|
|
check(u'string\xbd', 'string\xc2\xbd', u'string\xbd')
|
|
|
|
check(u'string\u20ac', 'string\xe2\x82\xac', u'string\u20ac')
|
|
|
|
check('str\xc0\x80ing', 'str\xc0\x80ing', u'str\x00ing')
|
|
|
|
check('str\xc0\x80ing\xe2\x82\xac', 'str\xc0\x80ing\xe2\x82\xac', u'str\x00ing\u20ac')
|
|
|
|
check(u'str\x00ing', 'str\xc0\x80ing', u'str\x00ing')
|
|
|
|
check(u'str\x00ing\xbd', 'str\xc0\x80ing\xc2\xbd', u'str\x00ing\xbd')
|
|
|
|
check(u'str\x00ing\u20ac', 'str\xc0\x80ing\xe2\x82\xac', u'str\x00ing\u20ac')
|
2014-01-23 03:42:46 -04:00
|
|
|
for i in (0, 1, -1, 2**31-1, -2**31):
|
|
|
|
check(i, str(i))
|
2014-01-23 08:38:44 -04:00
|
|
|
for f in (0.0, 1.0, -1.0):
|
|
|
|
check(f, repr(f))
|
|
|
|
for f in (1/3.0, sys.float_info.min, sys.float_info.max,
|
2014-01-23 03:42:46 -04:00
|
|
|
-sys.float_info.min, -sys.float_info.max):
|
2014-01-23 08:38:44 -04:00
|
|
|
check(f, f, eq=float_eq)
|
|
|
|
check(float('inf'), 'Inf', eq=float_eq)
|
|
|
|
check(-float('inf'), '-Inf', eq=float_eq)
|
|
|
|
check(float('nan'), 'NaN', eq=nan_eq)
|
2014-01-23 03:42:46 -04:00
|
|
|
check((), '')
|
|
|
|
check((1, (2,), (3, 4), '5 6', ()), '1 2 {3 4} {5 6} {}')
|
|
|
|
|
2013-07-11 14:32:48 -03:00
|
|
|
def test_splitlist(self):
|
|
|
|
splitlist = self.interp.tk.splitlist
|
|
|
|
call = self.interp.tk.call
|
|
|
|
self.assertRaises(TypeError, splitlist)
|
|
|
|
self.assertRaises(TypeError, splitlist, 'a', 'b')
|
|
|
|
self.assertRaises(TypeError, splitlist, 2)
|
|
|
|
testcases = [
|
|
|
|
('2', ('2',)),
|
|
|
|
('', ()),
|
|
|
|
('{}', ('',)),
|
|
|
|
('""', ('',)),
|
|
|
|
('a\n b\t\r c\n ', ('a', 'b', 'c')),
|
|
|
|
(u'a\n b\t\r c\n ', ('a', 'b', 'c')),
|
|
|
|
('a \xe2\x82\xac', ('a', '\xe2\x82\xac')),
|
|
|
|
(u'a \u20ac', ('a', '\xe2\x82\xac')),
|
2014-02-03 15:23:46 -04:00
|
|
|
('a\xc0\x80b c\xc0\x80d', ('a\xc0\x80b', 'c\xc0\x80d')),
|
2013-07-11 14:32:48 -03:00
|
|
|
('a {b c}', ('a', 'b c')),
|
|
|
|
(r'a b\ c', ('a', 'b c')),
|
|
|
|
(('a', 'b c'), ('a', 'b c')),
|
|
|
|
('a 2', ('a', '2')),
|
|
|
|
(('a', 2), ('a', 2)),
|
|
|
|
('a 3.4', ('a', '3.4')),
|
|
|
|
(('a', 3.4), ('a', 3.4)),
|
|
|
|
((), ()),
|
2013-12-25 11:28:50 -04:00
|
|
|
(call('list', 1, '2', (3.4,)),
|
|
|
|
(1, '2', (3.4,)) if self.wantobjects else
|
|
|
|
('1', '2', '3.4')),
|
2013-07-11 14:32:48 -03:00
|
|
|
]
|
2013-09-08 14:32:56 -03:00
|
|
|
if tcl_version >= (8, 5):
|
2014-02-02 17:04:06 -04:00
|
|
|
if not self.wantobjects:
|
|
|
|
expected = ('12', '\xe2\x82\xac', '\xe2\x82\xac', '3.4')
|
|
|
|
elif get_tk_patchlevel() < (8, 5, 5):
|
|
|
|
# Before 8.5.5 dicts were converted to lists through string
|
|
|
|
expected = ('12', u'\u20ac', u'\u20ac', '3.4')
|
|
|
|
else:
|
|
|
|
expected = (12, u'\u20ac', u'\u20ac', (3.4,))
|
2013-09-08 14:32:56 -03:00
|
|
|
testcases += [
|
2014-02-02 17:04:06 -04:00
|
|
|
(call('dict', 'create', 12, u'\u20ac', '\xe2\x82\xac', (3.4,)),
|
|
|
|
expected),
|
2013-09-08 14:32:56 -03:00
|
|
|
]
|
2013-07-11 14:32:48 -03:00
|
|
|
for arg, res in testcases:
|
|
|
|
self.assertEqual(splitlist(arg), res)
|
|
|
|
self.assertRaises(TclError, splitlist, '{')
|
|
|
|
|
|
|
|
def test_split(self):
|
|
|
|
split = self.interp.tk.split
|
|
|
|
call = self.interp.tk.call
|
|
|
|
self.assertRaises(TypeError, split)
|
|
|
|
self.assertRaises(TypeError, split, 'a', 'b')
|
|
|
|
self.assertRaises(TypeError, split, 2)
|
|
|
|
testcases = [
|
|
|
|
('2', '2'),
|
|
|
|
('', ''),
|
|
|
|
('{}', ''),
|
|
|
|
('""', ''),
|
|
|
|
('{', '{'),
|
|
|
|
('a\n b\t\r c\n ', ('a', 'b', 'c')),
|
|
|
|
(u'a\n b\t\r c\n ', ('a', 'b', 'c')),
|
|
|
|
('a \xe2\x82\xac', ('a', '\xe2\x82\xac')),
|
|
|
|
(u'a \u20ac', ('a', '\xe2\x82\xac')),
|
2014-02-03 15:23:46 -04:00
|
|
|
('a\xc0\x80b', 'a\xc0\x80b'),
|
|
|
|
('a\xc0\x80b c\xc0\x80d', ('a\xc0\x80b', 'c\xc0\x80d')),
|
2013-07-11 14:32:48 -03:00
|
|
|
('a {b c}', ('a', ('b', 'c'))),
|
|
|
|
(r'a b\ c', ('a', ('b', 'c'))),
|
|
|
|
(('a', 'b c'), ('a', ('b', 'c'))),
|
|
|
|
(('a', u'b c'), ('a', ('b', 'c'))),
|
|
|
|
('a 2', ('a', '2')),
|
|
|
|
(('a', 2), ('a', 2)),
|
|
|
|
('a 3.4', ('a', '3.4')),
|
|
|
|
(('a', 3.4), ('a', 3.4)),
|
|
|
|
(('a', (2, 3.4)), ('a', (2, 3.4))),
|
|
|
|
((), ()),
|
2013-12-25 11:28:50 -04:00
|
|
|
(call('list', 1, '2', (3.4,)),
|
|
|
|
(1, '2', (3.4,)) if self.wantobjects else
|
|
|
|
('1', '2', '3.4')),
|
2013-07-11 14:32:48 -03:00
|
|
|
]
|
2013-09-08 14:32:56 -03:00
|
|
|
if tcl_version >= (8, 5):
|
2014-02-02 17:04:06 -04:00
|
|
|
if not self.wantobjects:
|
|
|
|
expected = ('12', '\xe2\x82\xac', '\xe2\x82\xac', '3.4')
|
|
|
|
elif get_tk_patchlevel() < (8, 5, 5):
|
|
|
|
# Before 8.5.5 dicts were converted to lists through string
|
|
|
|
expected = ('12', u'\u20ac', u'\u20ac', '3.4')
|
|
|
|
else:
|
|
|
|
expected = (12, u'\u20ac', u'\u20ac', (3.4,))
|
2013-09-08 14:32:56 -03:00
|
|
|
testcases += [
|
|
|
|
(call('dict', 'create', 12, u'\u20ac', '\xe2\x82\xac', (3.4,)),
|
2014-02-02 17:04:06 -04:00
|
|
|
expected),
|
2013-09-08 14:32:56 -03:00
|
|
|
]
|
2013-07-11 14:32:48 -03:00
|
|
|
for arg, res in testcases:
|
|
|
|
self.assertEqual(split(arg), res)
|
|
|
|
|
2014-05-30 08:23:52 -03:00
|
|
|
character_size = 4 if sys.maxunicode > 0xFFFF else 2
|
2004-02-18 01:59:53 -04:00
|
|
|
|
2013-08-21 15:46:12 -03:00
|
|
|
class BigmemTclTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.interp = Tcl()
|
|
|
|
|
2014-02-07 04:06:05 -04:00
|
|
|
@test_support.cpython_only
|
|
|
|
@unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX, "needs UINT_MAX < SIZE_MAX")
|
|
|
|
@test_support.precisionbigmemtest(size=INT_MAX + 1, memuse=5, dry_run=False)
|
2014-05-30 08:23:52 -03:00
|
|
|
def test_huge_string_call(self, size):
|
2013-08-21 15:46:12 -03:00
|
|
|
value = ' ' * size
|
|
|
|
self.assertRaises(OverflowError, self.interp.call, 'set', '_', value)
|
|
|
|
|
2014-05-30 08:23:52 -03:00
|
|
|
@test_support.cpython_only
|
|
|
|
@unittest.skipUnless(test_support.have_unicode, 'requires unicode support')
|
|
|
|
@unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX, "needs UINT_MAX < SIZE_MAX")
|
|
|
|
@test_support.precisionbigmemtest(size=INT_MAX + 1,
|
|
|
|
memuse=2*character_size + 2,
|
|
|
|
dry_run=False)
|
|
|
|
def test_huge_unicode_call(self, size):
|
|
|
|
value = unicode(' ') * size
|
|
|
|
self.assertRaises(OverflowError, self.interp.call, 'set', '_', value)
|
|
|
|
|
|
|
|
|
|
|
|
@test_support.cpython_only
|
|
|
|
@unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX, "needs UINT_MAX < SIZE_MAX")
|
|
|
|
@test_support.precisionbigmemtest(size=INT_MAX + 1, memuse=9, dry_run=False)
|
|
|
|
def test_huge_string_builtins(self, size):
|
|
|
|
value = '1' + ' ' * size
|
|
|
|
self.check_huge_string_builtins(value)
|
|
|
|
|
|
|
|
@test_support.cpython_only
|
|
|
|
@unittest.skipUnless(test_support.have_unicode, 'requires unicode support')
|
|
|
|
@unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX, "needs UINT_MAX < SIZE_MAX")
|
|
|
|
@test_support.precisionbigmemtest(size=INT_MAX + 1,
|
|
|
|
memuse=2*character_size + 7,
|
|
|
|
dry_run=False)
|
|
|
|
def test_huge_unicode_builtins(self, size):
|
|
|
|
value = unicode('1' + ' ' * size)
|
|
|
|
self.check_huge_string_builtins(value)
|
|
|
|
|
|
|
|
def check_huge_string_builtins(self, value):
|
|
|
|
self.assertRaises(OverflowError, self.interp.tk.getint, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.tk.getdouble, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.tk.getboolean, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.eval, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.evalfile, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.record, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.adderrorinfo, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.setvar, value, 'x', 'a')
|
|
|
|
self.assertRaises(OverflowError, self.interp.setvar, 'x', value, 'a')
|
|
|
|
self.assertRaises(OverflowError, self.interp.unsetvar, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.unsetvar, 'x', value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.adderrorinfo, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.exprstring, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.exprlong, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.exprboolean, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.splitlist, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.split, value)
|
|
|
|
self.assertRaises(OverflowError, self.interp.createcommand, value, max)
|
|
|
|
self.assertRaises(OverflowError, self.interp.deletecommand, value)
|
|
|
|
|
2013-08-21 15:46:12 -03:00
|
|
|
|
2013-11-20 11:44:38 -04:00
|
|
|
def setUpModule():
|
|
|
|
if test_support.verbose:
|
|
|
|
tcl = Tcl()
|
|
|
|
print 'patchlevel =', tcl.call('info', 'patchlevel')
|
|
|
|
|
|
|
|
|
2004-02-18 22:37:29 -04:00
|
|
|
def test_main():
|
2013-08-21 15:46:12 -03:00
|
|
|
test_support.run_unittest(TclTest, TkinterTest, BigmemTclTest)
|
2004-02-18 01:59:53 -04:00
|
|
|
|
2004-02-18 22:37:29 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|