diff -r cf70f030a744 Lib/lib-tk/Tkinter.py --- a/Lib/lib-tk/Tkinter.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/lib-tk/Tkinter.py Mon Jun 23 22:12:16 2014 +0300 @@ -43,6 +43,16 @@ from Tkconstants import * import re +try: + _decode = _unicode = unicode +except NameError: + # If Python is built without Unicode support, the unicode type + # will not exist. Fake one. + class _unicode(object): + pass + def _decode(s, encoding): + return s + wantobjects = 1 TkVersion = float(_tkinter.TK_VERSION) @@ -77,8 +87,8 @@ value = '{%s}' % _join(value) else: if isinstance(value, str): - value = unicode(value, 'utf-8') - elif not isinstance(value, unicode): + value = _decode(value, 'utf-8') + elif not isinstance(value, _unicode): value = str(value) if not value: value = '{}' diff -r cf70f030a744 Lib/lib-tk/test/test_ttk/test_functions.py --- a/Lib/lib-tk/test/test_ttk/test_functions.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/lib-tk/test/test_ttk/test_functions.py Mon Jun 23 22:12:16 2014 +0300 @@ -2,6 +2,7 @@ import sys import unittest import ttk +from test.test_support import have_unicode, requires_unicode class MockTkApp: @@ -20,8 +21,12 @@ def __init__(self, val): self.val = val - def __str__(self): - return unicode(self.val) + if have_unicode: + def __str__(self): + return unicode(self.val) + else: + def __str__(self): + return str(self.val) class MockStateSpec(object): @@ -433,20 +438,20 @@ def test_convert_stringval(self): - tests = ( - (0, 0), ('09', 9), ('a', 'a'), (u'áÚ', u'áÚ'), ([], '[]'), - (None, 'None') - ) + tests = [(0, 0), ('09', 9), ('a', 'a'), ([], '[]'), (None, 'None')] + if have_unicode: + tests.append((u'áÚ', u'áÚ')) for orig, expected in tests: self.assertEqual(ttk._convert_stringval(orig), expected) - if sys.getdefaultencoding() == 'ascii': + if have_unicode and sys.getdefaultencoding() == 'ascii': self.assertRaises(UnicodeDecodeError, ttk._convert_stringval, 'á') class TclObjsToPyTest(unittest.TestCase): + @requires_unicode def test_unicode(self): adict = {'opt': u'välúè'} self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': u'välúè'}) diff -r cf70f030a744 Lib/lib-tk/ttk.py --- a/Lib/lib-tk/ttk.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/lib-tk/ttk.py Mon Jun 23 22:12:16 2014 +0300 @@ -31,6 +31,11 @@ # Verify if Tk is new enough to not need the Tile package _REQUIRE_TILE = True if Tkinter.TkVersion < 8.5 else False +try: + _str = unicode +except NameError: + _str = str + def _load_tile(master): if _REQUIRE_TILE: import os @@ -320,7 +325,7 @@ def _convert_stringval(value): """Converts a value to, hopefully, a more appropriate Python object.""" - value = unicode(value) + value = _str(value) try: value = int(value) except (ValueError, TypeError): diff -r cf70f030a744 Lib/test/test_tcl.py --- a/Lib/test/test_tcl.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/test/test_tcl.py Mon Jun 23 22:12:16 2014 +0300 @@ -9,7 +9,6 @@ from Tkinter import Tcl from _tkinter import TclError - try: from _testcapi import INT_MAX, PY_SSIZE_T_MAX except ImportError: @@ -396,15 +395,23 @@ self.assertEqual(passValue(False), False if self.wantobjects else '0') self.assertEqual(passValue('string'), 'string') self.assertEqual(passValue('string\xbd'), 'string\xbd') - self.assertEqual(passValue('string\xe2\x82\xac'), u'string\u20ac') - self.assertEqual(passValue(u'string'), u'string') - self.assertEqual(passValue(u'string\xbd'), u'string\xbd') - self.assertEqual(passValue(u'string\u20ac'), u'string\u20ac') + self.assertEqual(passValue('string\xe2\x82\xac'), + u'string' + unichr(0x20ac) if test_support.have_unicode else + u'string\xe2\x82\xac') + if test_support.have_unicode: + self.assertEqual(passValue(u'string'), u'string') + self.assertEqual(passValue(u'string\xbd'), u'string\xbd') + self.assertEqual(passValue(u'string' + unichr(0x20ac)), + u'string' + unichr(0x20ac)) 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') + self.assertEqual(passValue('str\xc0\x80ing'), + 'str\x00ing' if test_support.have_unicode else + 'str\xc0\x80ing') + if test_support.have_unicode: + 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' + unichr(0x20ac)), + u'str\x00ing' + unichr(0x20ac)) for i in (0, 1, -1, int(2**31-1), int(-2**31)): self.assertEqual(passValue(i), i if self.wantobjects else str(i)) for f in (0.0, 1.0, -1.0, 1//3, 1/3.0, @@ -440,9 +447,9 @@ del result[:] r = self.interp.call('testfunc', value) self.assertEqual(len(result), 1) - self.assertIsInstance(result[0], (str, unicode)) + self.assertIsInstance(result[0], basestring) eq(result[0], expected2) - self.assertIsInstance(r, (str, unicode)) + self.assertIsInstance(r, basestring) eq(r, expected2) def float_eq(actual, expected): expected = float(expected) @@ -456,15 +463,25 @@ check(False, '0') check('string', 'string') check('string\xbd', 'string\xbd') - 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') + check('string\xe2\x82\xac', 'string\xe2\x82\xac', + u'string' + unichr(0x20ac) if test_support.have_unicode else + 'string\xe2\x82\xac') + if test_support.have_unicode: + check(u'string', u'string') + check(u'string\xbd', 'string\xc2\xbd', u'string\xbd') + check(u'string' + unichr(0x20ac), + 'string\xe2\x82\xac', u'string' + unichr(0x20ac)) + check('str\xc0\x80ing', 'str\xc0\x80ing', + u'str\x00ing' if test_support.have_unicode else + 'str\xc0\x80ing') + check('str\xc0\x80ing\xe2\x82\xac', 'str\xc0\x80ing\xe2\x82\xac', + u'str\x00ing' + unichr(0x20ac) if test_support.have_unicode else + 'str\xc0\x80ing\xe2\x82\xac') + if test_support.have_unicode: + 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' + unichr(0x20ac), 'str\xc0\x80ing\xe2\x82\xac', + u'str\x00ing' + unichr(0x20ac)) for i in (0, 1, -1, 2**31-1, -2**31): check(i, str(i)) for f in (0.0, 1.0, -1.0): @@ -490,9 +507,7 @@ ('{}', ('',)), ('""', ('',)), ('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')), ('a\xc0\x80b c\xc0\x80d', ('a\xc0\x80b', 'c\xc0\x80d')), ('a {b c}', ('a', 'b c')), (r'a b\ c', ('a', 'b c')), @@ -506,18 +521,23 @@ (1, '2', (3.4,)) if self.wantobjects else ('1', '2', '3.4')), ] - if tcl_version >= (8, 5): - 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,)) + if test_support.have_unicode: testcases += [ - (call('dict', 'create', 12, u'\u20ac', '\xe2\x82\xac', (3.4,)), - expected), + (u'a\n b\t\r c\n ', ('a', 'b', 'c')), + (u'a ' + unichr(0x20ac), ('a', '\xe2\x82\xac')), ] + if tcl_version >= (8, 5): + 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', unichr(0x20ac), unichr(0x20ac), '3.4') + else: + expected = (12, unichr(0x20ac), unichr(0x20ac), (3.4,)) + testcases += [ + (call('dict', 'create', 12, unichr(0x20ac), '\xe2\x82\xac', (3.4,)), + expected), + ] for arg, res in testcases: self.assertEqual(splitlist(arg), res) self.assertRaises(TclError, splitlist, '{') @@ -535,9 +555,7 @@ ('""', ''), ('{', '{'), ('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')), ('a\xc0\x80b', 'a\xc0\x80b'), ('a\xc0\x80b c\xc0\x80d', ('a\xc0\x80b', 'c\xc0\x80d')), ('a {b c}', ('a', ('b', 'c'))), @@ -554,22 +572,30 @@ (1, '2', (3.4,)) if self.wantobjects else ('1', '2', '3.4')), ] - if tcl_version >= (8, 5): - 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,)) + if test_support.have_unicode: testcases += [ - (call('dict', 'create', 12, u'\u20ac', '\xe2\x82\xac', (3.4,)), - expected), + (u'a\n b\t\r c\n ', ('a', 'b', 'c')), + (u'a ' + unichr(0x20ac), ('a', '\xe2\x82\xac')), ] + if tcl_version >= (8, 5): + 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', unichr(0x20ac), unichr(0x20ac), '3.4') + else: + expected = (12, unichr(0x20ac), unichr(0x20ac), (3.4,)) + testcases += [ + (call('dict', 'create', 12, unichr(0x20ac), '\xe2\x82\xac', (3.4,)), + expected), + ] for arg, res in testcases: self.assertEqual(split(arg), res) -character_size = 4 if sys.maxunicode > 0xFFFF else 2 +if test_support.have_unicode: + character_size = 4 if sys.maxunicode > 0xFFFF else 2 +else: + character_size = 1 class BigmemTclTest(unittest.TestCase): @@ -583,6 +609,7 @@ value = ' ' * size self.assertRaises(OverflowError, self.interp.call, 'set', '_', value) + @test_support.requires_unicode @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") @@ -601,6 +628,7 @@ value = '1' + ' ' * size self.check_huge_string_builtins(value) + @test_support.requires_unicode @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") diff -r cf70f030a744 Modules/_tkinter.c --- a/Modules/_tkinter.c Wed Jun 18 23:07:46 2014 -0400 +++ b/Modules/_tkinter.c Mon Jun 23 22:12:16 2014 +0300 @@ -617,6 +617,7 @@ return Split(PyString_AsString(arg)); /* Fall through, returning arg. */ } +#ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { int argc; char **argv; @@ -644,6 +645,7 @@ Py_DECREF(s); /* Fall through, returning arg. */ } +#endif Py_INCREF(arg); return arg; }