Commit 89ac7c98 authored by Walter Dörwald's avatar Walter Dörwald

Simplify various spots where: str() is called on something

that already is a string or the existence of the str class
is checked or a check is done for str twice. These all stem
from the initial unicode->str replacement.
parent c2bc3eee
...@@ -246,13 +246,8 @@ class bdist_wininst (Command): ...@@ -246,13 +246,8 @@ class bdist_wininst (Command):
file.write(bitmapdata) file.write(bitmapdata)
# Convert cfgdata from unicode to ascii, mbcs encoded # Convert cfgdata from unicode to ascii, mbcs encoded
try: if isinstance(cfgdata, str):
str cfgdata = cfgdata.encode("mbcs")
except NameError:
pass
else:
if isinstance(cfgdata, str):
cfgdata = cfgdata.encode("mbcs")
# Append the pre-install script # Append the pre-install script
cfgdata = cfgdata + "\0" cfgdata = cfgdata + "\0"
......
...@@ -104,33 +104,28 @@ for ch in "\"'\\\n#": ...@@ -104,33 +104,28 @@ for ch in "\"'\\\n#":
_tran = ''.join(_tran) _tran = ''.join(_tran)
del ch del ch
try:
UnicodeType = type(str(""))
except NameError:
UnicodeType = None
class Parser: class Parser:
def __init__(self, indentwidth, tabwidth): def __init__(self, indentwidth, tabwidth):
self.indentwidth = indentwidth self.indentwidth = indentwidth
self.tabwidth = tabwidth self.tabwidth = tabwidth
def set_str(self, str): def set_str(self, s):
assert len(str) == 0 or str[-1] == '\n' assert len(s) == 0 or s[-1] == '\n'
if type(str) is UnicodeType: if isinstance(s, str):
# The parse functions have no idea what to do with Unicode, so # The parse functions have no idea what to do with Unicode, so
# replace all Unicode characters with "x". This is "safe" # replace all Unicode characters with "x". This is "safe"
# so long as the only characters germane to parsing the structure # so long as the only characters germane to parsing the structure
# of Python are 7-bit ASCII. It's *necessary* because Unicode # of Python are 7-bit ASCII. It's *necessary* because Unicode
# strings don't have a .translate() method that supports # strings don't have a .translate() method that supports
# deletechars. # deletechars.
uniphooey = str uniphooey = s
str = [] str = []
push = str.append push = s.append
for raw in map(ord, uniphooey): for raw in map(ord, uniphooey):
push(raw < 127 and chr(raw) or "x") push(raw < 127 and chr(raw) or "x")
str = "".join(str) s = "".join(s)
self.str = str self.str = s
self.study_level = 0 self.study_level = 0
# Return index of a good place to begin parsing, as close to the # Return index of a good place to begin parsing, as close to the
......
...@@ -3734,11 +3734,7 @@ def _test(): ...@@ -3734,11 +3734,7 @@ def _test():
root = Tk() root = Tk()
text = "This is Tcl/Tk version %s" % TclVersion text = "This is Tcl/Tk version %s" % TclVersion
if TclVersion >= 8.1: if TclVersion >= 8.1:
try: text += "\nThis should be a cedilla: \xe7"
text = text + str("\nThis should be a cedilla: \347",
"iso-8859-1")
except NameError:
pass # no unicode support
label = Label(root, text=text) label = Label(root, text=text)
label.pack() label.pack()
test = Button(root, text="Click me!", test = Button(root, text="Click me!",
......
...@@ -247,13 +247,8 @@ class TestCaseBase(unittest.TestCase): ...@@ -247,13 +247,8 @@ class TestCaseBase(unittest.TestCase):
cf.set("sect", "option1", mystr("splat")) cf.set("sect", "option1", mystr("splat"))
cf.set("sect", "option2", "splat") cf.set("sect", "option2", "splat")
cf.set("sect", "option2", mystr("splat")) cf.set("sect", "option2", mystr("splat"))
try: cf.set("sect", "option1", "splat")
str cf.set("sect", "option2", "splat")
except NameError:
pass
else:
cf.set("sect", "option1", str("splat"))
cf.set("sect", "option2", str("splat"))
def test_read_returns_file_list(self): def test_read_returns_file_list(self):
file1 = test_support.findfile("cfgparser.1") file1 = test_support.findfile("cfgparser.1")
......
...@@ -265,7 +265,7 @@ def test_dir(): ...@@ -265,7 +265,7 @@ def test_dir():
del junk del junk
# Just make sure these don't blow up! # Just make sure these don't blow up!
for arg in 2, 2, 2j, 2e0, [2], "2", "2", (2,), {2:2}, type, test_dir: for arg in 2, 2, 2j, 2e0, [2], "2", b"2", (2,), {2:2}, type, test_dir:
dir(arg) dir(arg)
# Test dir on custom classes. Since these have object as a # Test dir on custom classes. Since these have object as a
...@@ -1117,34 +1117,29 @@ def slots(): ...@@ -1117,34 +1117,29 @@ def slots():
vereq(c.abc, 5) vereq(c.abc, 5)
# Test unicode slot names # Test unicode slot names
# Test a single unicode string is not expanded as a sequence.
class C(object):
__slots__ = "abc"
c = C()
c.abc = 5
vereq(c.abc, 5)
# _unicode_to_string used to modify slots in certain circumstances
slots = ("foo", "bar")
class C(object):
__slots__ = slots
x = C()
x.foo = 5
vereq(x.foo, 5)
veris(type(slots[0]), str)
# this used to leak references
try: try:
str class C(object):
except NameError: __slots__ = [chr(128)]
except (TypeError, UnicodeEncodeError):
pass pass
else: else:
# Test a single unicode string is not expanded as a sequence. raise TestFailed, "[unichr(128)] slots not caught"
class C(object):
__slots__ = str("abc")
c = C()
c.abc = 5
vereq(c.abc, 5)
# _unicode_to_string used to modify slots in certain circumstances
slots = (str("foo"), str("bar"))
class C(object):
__slots__ = slots
x = C()
x.foo = 5
vereq(x.foo, 5)
veris(type(slots[0]), str)
# this used to leak references
try:
class C(object):
__slots__ = [chr(128)]
except (TypeError, UnicodeEncodeError):
pass
else:
raise TestFailed, "[unichr(128)] slots not caught"
# Test leaks # Test leaks
class Counted(object): class Counted(object):
...@@ -2693,14 +2688,8 @@ def setclass(): ...@@ -2693,14 +2688,8 @@ def setclass():
__slots__ = ["a", "b"] __slots__ = ["a", "b"]
class H(object): class H(object):
__slots__ = ["b", "a"] __slots__ = ["b", "a"]
try: class I(object):
str __slots__ = ["a", "b"]
except NameError:
class I(object):
__slots__ = ["a", "b"]
else:
class I(object):
__slots__ = [str("a"), str("b")]
class J(object): class J(object):
__slots__ = ["c", "b"] __slots__ = ["c", "b"]
class K(object): class K(object):
......
...@@ -526,7 +526,7 @@ class TestCase(unittest.TestCase): ...@@ -526,7 +526,7 @@ class TestCase(unittest.TestCase):
# and pass that on to unicode.join(). # and pass that on to unicode.join().
try: try:
got = " - ".join(OhPhooey(f)) got = " - ".join(OhPhooey(f))
self.assertEqual(got, str("a\n - b\n - fooled you! - c\n")) self.assertEqual(got, "a\n - b\n - fooled you! - c\n")
finally: finally:
f.close() f.close()
try: try:
......
...@@ -2,12 +2,6 @@ import pprint ...@@ -2,12 +2,6 @@ import pprint
import test.test_support import test.test_support
import unittest import unittest
try:
uni = str
except NameError:
def uni(x):
return x
# list, tuple and dict subclasses that do or don't overwrite __repr__ # list, tuple and dict subclasses that do or don't overwrite __repr__
class list2(list): class list2(list):
pass pass
...@@ -41,7 +35,7 @@ class QueryTestCase(unittest.TestCase): ...@@ -41,7 +35,7 @@ class QueryTestCase(unittest.TestCase):
# Verify .isrecursive() and .isreadable() w/o recursion # Verify .isrecursive() and .isreadable() w/o recursion
verify = self.assert_ verify = self.assert_
pp = pprint.PrettyPrinter() pp = pprint.PrettyPrinter()
for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"), for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda",
self.a, self.b): self.a, self.b):
# module-level convenience functions # module-level convenience functions
verify(not pprint.isrecursive(safe), verify(not pprint.isrecursive(safe),
...@@ -114,12 +108,12 @@ class QueryTestCase(unittest.TestCase): ...@@ -114,12 +108,12 @@ class QueryTestCase(unittest.TestCase):
# multiple lines. For that reason, dicts with more than one element # multiple lines. For that reason, dicts with more than one element
# aren't tested here. # aren't tested here.
verify = self.assert_ verify = self.assert_
for simple in (0, 0, 0+0j, 0.0, "", uni(""), for simple in (0, 0, 0+0j, 0.0, "", b"",
(), tuple2(), tuple3(), (), tuple2(), tuple3(),
[], list2(), list3(), [], list2(), list3(),
{}, dict2(), dict3(), {}, dict2(), dict3(),
verify, pprint, verify, pprint,
-6, -6, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6}, -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
(1,2), [3,4], {5: 6, 7: 8}, (1,2), [3,4], {5: 6, 7: 8},
tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
[3,4], list2([3,4]), list3([3,4]), list3(range(100)), [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
......
...@@ -72,7 +72,7 @@ class TestJointOps(unittest.TestCase): ...@@ -72,7 +72,7 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(type(u), self.thetype) self.assertEqual(type(u), self.thetype)
self.assertRaises(PassThru, self.s.union, check_pass_thru()) self.assertRaises(PassThru, self.s.union, check_pass_thru())
self.assertRaises(TypeError, self.s.union, [[]]) self.assertRaises(TypeError, self.s.union, [[]])
for C in set, frozenset, dict.fromkeys, str, str, list, tuple: for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd')) self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd'))
self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg')) self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg'))
self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc')) self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc'))
...@@ -96,7 +96,7 @@ class TestJointOps(unittest.TestCase): ...@@ -96,7 +96,7 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(self.s, self.thetype(self.word)) self.assertEqual(self.s, self.thetype(self.word))
self.assertEqual(type(i), self.thetype) self.assertEqual(type(i), self.thetype)
self.assertRaises(PassThru, self.s.intersection, check_pass_thru()) self.assertRaises(PassThru, self.s.intersection, check_pass_thru())
for C in set, frozenset, dict.fromkeys, str, str, list, tuple: for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc')) self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc'))
self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set('')) self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set(''))
self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc')) self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
...@@ -121,7 +121,7 @@ class TestJointOps(unittest.TestCase): ...@@ -121,7 +121,7 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(type(i), self.thetype) self.assertEqual(type(i), self.thetype)
self.assertRaises(PassThru, self.s.difference, check_pass_thru()) self.assertRaises(PassThru, self.s.difference, check_pass_thru())
self.assertRaises(TypeError, self.s.difference, [[]]) self.assertRaises(TypeError, self.s.difference, [[]])
for C in set, frozenset, dict.fromkeys, str, str, list, tuple: for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab')) self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab'))
self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc')) self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc'))
self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a')) self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a'))
...@@ -146,7 +146,7 @@ class TestJointOps(unittest.TestCase): ...@@ -146,7 +146,7 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(type(i), self.thetype) self.assertEqual(type(i), self.thetype)
self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru()) self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru())
self.assertRaises(TypeError, self.s.symmetric_difference, [[]]) self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
for C in set, frozenset, dict.fromkeys, str, str, list, tuple: for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd')) self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd'))
self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg')) self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg'))
self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a')) self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a'))
...@@ -390,7 +390,7 @@ class TestSet(TestJointOps): ...@@ -390,7 +390,7 @@ class TestSet(TestJointOps):
self.assertRaises(PassThru, self.s.update, check_pass_thru()) self.assertRaises(PassThru, self.s.update, check_pass_thru())
self.assertRaises(TypeError, self.s.update, [[]]) self.assertRaises(TypeError, self.s.update, [[]])
for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')): for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')):
for C in set, frozenset, dict.fromkeys, str, str, list, tuple: for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
s = self.thetype('abcba') s = self.thetype('abcba')
self.assertEqual(s.update(C(p)), None) self.assertEqual(s.update(C(p)), None)
self.assertEqual(s, set(q)) self.assertEqual(s, set(q))
...@@ -411,7 +411,7 @@ class TestSet(TestJointOps): ...@@ -411,7 +411,7 @@ class TestSet(TestJointOps):
self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru()) self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru())
self.assertRaises(TypeError, self.s.intersection_update, [[]]) self.assertRaises(TypeError, self.s.intersection_update, [[]])
for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')): for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')):
for C in set, frozenset, dict.fromkeys, str, str, list, tuple: for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
s = self.thetype('abcba') s = self.thetype('abcba')
self.assertEqual(s.intersection_update(C(p)), None) self.assertEqual(s.intersection_update(C(p)), None)
self.assertEqual(s, set(q)) self.assertEqual(s, set(q))
...@@ -436,7 +436,7 @@ class TestSet(TestJointOps): ...@@ -436,7 +436,7 @@ class TestSet(TestJointOps):
self.assertRaises(TypeError, self.s.difference_update, [[]]) self.assertRaises(TypeError, self.s.difference_update, [[]])
self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')): for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')):
for C in set, frozenset, dict.fromkeys, str, str, list, tuple: for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
s = self.thetype('abcba') s = self.thetype('abcba')
self.assertEqual(s.difference_update(C(p)), None) self.assertEqual(s.difference_update(C(p)), None)
self.assertEqual(s, set(q)) self.assertEqual(s, set(q))
...@@ -460,7 +460,7 @@ class TestSet(TestJointOps): ...@@ -460,7 +460,7 @@ class TestSet(TestJointOps):
self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru()) self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru())
self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')): for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')):
for C in set, frozenset, dict.fromkeys, str, str, list, tuple: for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
s = self.thetype('abcba') s = self.thetype('abcba')
self.assertEqual(s.symmetric_difference_update(C(p)), None) self.assertEqual(s.symmetric_difference_update(C(p)), None)
self.assertEqual(s, set(q)) self.assertEqual(s, set(q))
......
...@@ -40,12 +40,7 @@ __all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"] ...@@ -40,12 +40,7 @@ __all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]
import xml.dom import xml.dom
try: StringTypes = (str,)
str
except NameError:
StringTypes = type(''),
else:
StringTypes = type(''), type(str(''))
class NodeList(list): class NodeList(list):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment