Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_tcl.py

    r2 r391  
    22
    33import unittest
     4import sys
    45import os
     6import _testcapi
    57from test import test_support
     8from subprocess import Popen, PIPE
     9
     10# Skip this test if the _tkinter module wasn't built.
     11_tkinter = test_support.import_module('_tkinter')
     12
    613from Tkinter import Tcl
    714from _tkinter import TclError
     15
     16tcl_version = _tkinter.TCL_VERSION.split('.')
     17try:
     18    for i in range(len(tcl_version)):
     19        tcl_version[i] = int(tcl_version[i])
     20except ValueError:
     21    pass
     22tcl_version = tuple(tcl_version)
     23
     24
     25class TkinterTest(unittest.TestCase):
     26
     27    def testFlattenLen(self):
     28        # flatten(<object with no length>)
     29        self.assertRaises(TypeError, _tkinter._flatten, True)
     30
    831
    932class TclTest(unittest.TestCase):
     
    116139        self.assertRaises(TclError,tcl.eval,'package require DNE')
    117140
    118     def testLoadTk(self):
    119         import os
    120         if 'DISPLAY' not in os.environ:
    121             # skipping test of clean upgradeability
     141    def testLoadWithUNC(self):
     142        import sys
     143        if sys.platform != 'win32':
    122144            return
    123         tcl = Tcl()
    124         self.assertRaises(TclError,tcl.winfo_geometry)
    125         tcl.loadtk()
    126         self.assertEqual('1x1+0+0', tcl.winfo_geometry())
    127         tcl.destroy()
    128 
    129     def testLoadTkFailure(self):
    130         import os
    131         old_display = None
    132         import sys
    133         if sys.platform.startswith(('win', 'darwin', 'cygwin')):
    134             return  # no failure possible on windows?
    135         if 'DISPLAY' in os.environ:
    136             old_display = os.environ['DISPLAY']
    137             del os.environ['DISPLAY']
    138             # on some platforms, deleting environment variables
    139             # doesn't actually carry through to the process level
    140             # because they don't support unsetenv
    141             # If that's the case, abort.
    142             display = os.popen('echo $DISPLAY').read().strip()
    143             if display:
    144                 return
    145         try:
    146             tcl = Tcl()
    147             self.assertRaises(TclError, tcl.winfo_geometry)
    148             self.assertRaises(TclError, tcl.loadtk)
    149         finally:
    150             if old_display is not None:
    151                 os.environ['DISPLAY'] = old_display
     145
     146        # Build a UNC path from the regular path.
     147        # Something like
     148        #   \\%COMPUTERNAME%\c$\python27\python.exe
     149
     150        fullname = os.path.abspath(sys.executable)
     151        if fullname[1] != ':':
     152            return
     153        unc_name = r'\\%s\%s$\%s' % (os.environ['COMPUTERNAME'],
     154                                    fullname[0],
     155                                    fullname[3:])
     156
     157        with test_support.EnvironmentVarGuard() as env:
     158            env.unset("TCL_LIBRARY")
     159            cmd = '%s -c "import Tkinter; print Tkinter"' % (unc_name,)
     160
     161            p = Popen(cmd, stdout=PIPE, stderr=PIPE)
     162            out_data, err_data = p.communicate()
     163
     164            msg = '\n\n'.join(['"Tkinter.py" not in output',
     165                               'Command:', cmd,
     166                               'stdout:', out_data,
     167                               'stderr:', err_data])
     168
     169            self.assertIn('Tkinter.py', out_data, msg)
     170
     171            self.assertEqual(p.wait(), 0, 'Non-zero exit code')
     172
     173
     174    def test_passing_values(self):
     175        def passValue(value):
     176            return self.interp.call('set', '_', value)
     177        self.assertEqual(passValue(True), True)
     178        self.assertEqual(passValue(False), False)
     179        self.assertEqual(passValue('string'), 'string')
     180        self.assertEqual(passValue('string\u20ac'), 'string\u20ac')
     181        self.assertEqual(passValue(u'string'), u'string')
     182        self.assertEqual(passValue(u'string\u20ac'), u'string\u20ac')
     183        for i in (0, 1, -1, int(2**31-1), int(-2**31)):
     184            self.assertEqual(passValue(i), i)
     185        for f in (0.0, 1.0, -1.0, 1//3, 1/3.0,
     186                  sys.float_info.min, sys.float_info.max,
     187                  -sys.float_info.min, -sys.float_info.max):
     188            self.assertEqual(passValue(f), f)
     189        for f in float('nan'), float('inf'), -float('inf'):
     190            if f != f: # NaN
     191                self.assertNotEqual(passValue(f), f)
     192            else:
     193                self.assertEqual(passValue(f), f)
     194        self.assertEqual(passValue((1, '2', (3.4,))), (1, '2', (3.4,)))
     195
     196    def test_splitlist(self):
     197        splitlist = self.interp.tk.splitlist
     198        call = self.interp.tk.call
     199        self.assertRaises(TypeError, splitlist)
     200        self.assertRaises(TypeError, splitlist, 'a', 'b')
     201        self.assertRaises(TypeError, splitlist, 2)
     202        testcases = [
     203            ('2', ('2',)),
     204            ('', ()),
     205            ('{}', ('',)),
     206            ('""', ('',)),
     207            ('a\n b\t\r c\n ', ('a', 'b', 'c')),
     208            (u'a\n b\t\r c\n ', ('a', 'b', 'c')),
     209            ('a \xe2\x82\xac', ('a', '\xe2\x82\xac')),
     210            (u'a \u20ac', ('a', '\xe2\x82\xac')),
     211            ('a {b c}', ('a', 'b c')),
     212            (r'a b\ c', ('a', 'b c')),
     213            (('a', 'b c'), ('a', 'b c')),
     214            ('a 2', ('a', '2')),
     215            (('a', 2), ('a', 2)),
     216            ('a 3.4', ('a', '3.4')),
     217            (('a', 3.4), ('a', 3.4)),
     218            ((), ()),
     219            (call('list', 1, '2', (3.4,)), (1, '2', (3.4,))),
     220        ]
     221        if tcl_version >= (8, 5):
     222            testcases += [
     223                (call('dict', 'create', 1, u'\u20ac', '\xe2\x82\xac', (3.4,)),
     224                        (1, u'\u20ac', u'\u20ac', (3.4,))),
     225            ]
     226        for arg, res in testcases:
     227            self.assertEqual(splitlist(arg), res)
     228        self.assertRaises(TclError, splitlist, '{')
     229
     230    def test_split(self):
     231        split = self.interp.tk.split
     232        call = self.interp.tk.call
     233        self.assertRaises(TypeError, split)
     234        self.assertRaises(TypeError, split, 'a', 'b')
     235        self.assertRaises(TypeError, split, 2)
     236        testcases = [
     237            ('2', '2'),
     238            ('', ''),
     239            ('{}', ''),
     240            ('""', ''),
     241            ('{', '{'),
     242            ('a\n b\t\r c\n ', ('a', 'b', 'c')),
     243            (u'a\n b\t\r c\n ', ('a', 'b', 'c')),
     244            ('a \xe2\x82\xac', ('a', '\xe2\x82\xac')),
     245            (u'a \u20ac', ('a', '\xe2\x82\xac')),
     246            ('a {b c}', ('a', ('b', 'c'))),
     247            (r'a b\ c', ('a', ('b', 'c'))),
     248            (('a', 'b c'), ('a', ('b', 'c'))),
     249            (('a', u'b c'), ('a', ('b', 'c'))),
     250            ('a 2', ('a', '2')),
     251            (('a', 2), ('a', 2)),
     252            ('a 3.4', ('a', '3.4')),
     253            (('a', 3.4), ('a', 3.4)),
     254            (('a', (2, 3.4)), ('a', (2, 3.4))),
     255            ((), ()),
     256            (call('list', 1, '2', (3.4,)), (1, '2', (3.4,))),
     257        ]
     258        if tcl_version >= (8, 5):
     259            testcases += [
     260                (call('dict', 'create', 12, u'\u20ac', '\xe2\x82\xac', (3.4,)),
     261                        (12, u'\u20ac', u'\u20ac', (3.4,))),
     262            ]
     263        for arg, res in testcases:
     264            self.assertEqual(split(arg), res)
     265
     266
     267class BigmemTclTest(unittest.TestCase):
     268
     269    def setUp(self):
     270        self.interp = Tcl()
     271
     272    @unittest.skipUnless(_testcapi.INT_MAX < _testcapi.PY_SSIZE_T_MAX,
     273                         "needs UINT_MAX < SIZE_MAX")
     274    @test_support.precisionbigmemtest(size=_testcapi.INT_MAX + 1, memuse=5,
     275                                      dry_run=False)
     276    def test_huge_string(self, size):
     277        value = ' ' * size
     278        self.assertRaises(OverflowError, self.interp.call, 'set', '_', value)
     279
    152280
    153281def test_main():
    154     test_support.run_unittest(TclTest)
     282    test_support.run_unittest(TclTest, TkinterTest, BigmemTclTest)
    155283
    156284if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.