From 5de48bdd195b14426c772b4c61290aef7f33e3ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walter=20D=C3=B6rwald?= Date: Mon, 11 Jun 2007 21:38:39 +0000 Subject: 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. --- Lib/distutils/command/bdist_wininst.py | 9 ++---- Lib/idlelib/PyParse.py | 19 +++++------- Lib/lib-tk/Tkinter.py | 6 +--- Lib/test/test_cfgparser.py | 9 ++---- Lib/test/test_descr.py | 57 ++++++++++++++-------------------- Lib/test/test_iter.py | 2 +- Lib/test/test_pprint.py | 12 ++----- Lib/test/test_set.py | 16 +++++----- Lib/xml/dom/minicompat.py | 7 +---- 9 files changed, 48 insertions(+), 89 deletions(-) diff --git a/Lib/distutils/command/bdist_wininst.py b/Lib/distutils/command/bdist_wininst.py index 5074a1a..55d5d7e 100644 --- a/Lib/distutils/command/bdist_wininst.py +++ b/Lib/distutils/command/bdist_wininst.py @@ -246,13 +246,8 @@ class bdist_wininst (Command): file.write(bitmapdata) # Convert cfgdata from unicode to ascii, mbcs encoded - try: - str - except NameError: - pass - else: - if isinstance(cfgdata, str): - cfgdata = cfgdata.encode("mbcs") + if isinstance(cfgdata, str): + cfgdata = cfgdata.encode("mbcs") # Append the pre-install script cfgdata = cfgdata + "\0" diff --git a/Lib/idlelib/PyParse.py b/Lib/idlelib/PyParse.py index 463ad36..ea3cc27 100644 --- a/Lib/idlelib/PyParse.py +++ b/Lib/idlelib/PyParse.py @@ -104,33 +104,28 @@ for ch in "\"'\\\n#": _tran = ''.join(_tran) del ch -try: - UnicodeType = type(str("")) -except NameError: - UnicodeType = None - class Parser: def __init__(self, indentwidth, tabwidth): self.indentwidth = indentwidth self.tabwidth = tabwidth - def set_str(self, str): - assert len(str) == 0 or str[-1] == '\n' - if type(str) is UnicodeType: + def set_str(self, s): + assert len(s) == 0 or s[-1] == '\n' + if isinstance(s, str): # The parse functions have no idea what to do with Unicode, so # replace all Unicode characters with "x". This is "safe" # so long as the only characters germane to parsing the structure # of Python are 7-bit ASCII. It's *necessary* because Unicode # strings don't have a .translate() method that supports # deletechars. - uniphooey = str + uniphooey = s str = [] - push = str.append + push = s.append for raw in map(ord, uniphooey): push(raw < 127 and chr(raw) or "x") - str = "".join(str) - self.str = str + s = "".join(s) + self.str = s self.study_level = 0 # Return index of a good place to begin parsing, as close to the diff --git a/Lib/lib-tk/Tkinter.py b/Lib/lib-tk/Tkinter.py index 6b360fe..1426357 100644 --- a/Lib/lib-tk/Tkinter.py +++ b/Lib/lib-tk/Tkinter.py @@ -3734,11 +3734,7 @@ def _test(): root = Tk() text = "This is Tcl/Tk version %s" % TclVersion if TclVersion >= 8.1: - try: - text = text + str("\nThis should be a cedilla: \347", - "iso-8859-1") - except NameError: - pass # no unicode support + text += "\nThis should be a cedilla: \xe7" label = Label(root, text=text) label.pack() test = Button(root, text="Click me!", diff --git a/Lib/test/test_cfgparser.py b/Lib/test/test_cfgparser.py index 360998e..076de4a 100644 --- a/Lib/test/test_cfgparser.py +++ b/Lib/test/test_cfgparser.py @@ -247,13 +247,8 @@ class TestCaseBase(unittest.TestCase): cf.set("sect", "option1", mystr("splat")) cf.set("sect", "option2", "splat") cf.set("sect", "option2", mystr("splat")) - try: - str - except NameError: - pass - else: - cf.set("sect", "option1", str("splat")) - cf.set("sect", "option2", str("splat")) + cf.set("sect", "option1", "splat") + cf.set("sect", "option2", "splat") def test_read_returns_file_list(self): file1 = test_support.findfile("cfgparser.1") diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index fca0061..bcbd096 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -265,7 +265,7 @@ def test_dir(): del junk # 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) # Test dir on custom classes. Since these have object as a @@ -1117,34 +1117,29 @@ def slots(): vereq(c.abc, 5) # 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: - str - except NameError: + class C(object): + __slots__ = [chr(128)] + except (TypeError, UnicodeEncodeError): pass else: - # Test a single unicode string is not expanded as a sequence. - 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" + raise TestFailed, "[unichr(128)] slots not caught" # Test leaks class Counted(object): @@ -2693,14 +2688,8 @@ def setclass(): __slots__ = ["a", "b"] class H(object): __slots__ = ["b", "a"] - try: - str - except NameError: - class I(object): - __slots__ = ["a", "b"] - else: - class I(object): - __slots__ = [str("a"), str("b")] + class I(object): + __slots__ = ["a", "b"] class J(object): __slots__ = ["c", "b"] class K(object): diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index 42d4e3e..f7712db 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -526,7 +526,7 @@ class TestCase(unittest.TestCase): # and pass that on to unicode.join(). try: 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: f.close() try: diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index f7f6ac6..8698907 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -2,12 +2,6 @@ import pprint import test.test_support import unittest -try: - uni = str -except NameError: - def uni(x): - return x - # list, tuple and dict subclasses that do or don't overwrite __repr__ class list2(list): pass @@ -41,7 +35,7 @@ class QueryTestCase(unittest.TestCase): # Verify .isrecursive() and .isreadable() w/o recursion verify = self.assert_ 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): # module-level convenience functions verify(not pprint.isrecursive(safe), @@ -114,12 +108,12 @@ class QueryTestCase(unittest.TestCase): # multiple lines. For that reason, dicts with more than one element # aren't tested here. 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(), [], list2(), list3(), {}, dict2(), dict3(), 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}, tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), [3,4], list2([3,4]), list3([3,4]), list3(range(100)), diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index e411025..ee570f0 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -72,7 +72,7 @@ class TestJointOps(unittest.TestCase): self.assertEqual(type(u), self.thetype) self.assertRaises(PassThru, self.s.union, check_pass_thru()) 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('efgfe')), set('abcefg')) self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc')) @@ -96,7 +96,7 @@ class TestJointOps(unittest.TestCase): self.assertEqual(self.s, self.thetype(self.word)) self.assertEqual(type(i), self.thetype) 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('efgfe')), set('')) self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc')) @@ -121,7 +121,7 @@ class TestJointOps(unittest.TestCase): self.assertEqual(type(i), self.thetype) self.assertRaises(PassThru, self.s.difference, check_pass_thru()) 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('efgfe')), set('abc')) self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a')) @@ -146,7 +146,7 @@ class TestJointOps(unittest.TestCase): self.assertEqual(type(i), self.thetype) self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru()) 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('efgfe')), set('abcefg')) self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a')) @@ -390,7 +390,7 @@ class TestSet(TestJointOps): self.assertRaises(PassThru, self.s.update, check_pass_thru()) self.assertRaises(TypeError, self.s.update, [[]]) 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') self.assertEqual(s.update(C(p)), None) self.assertEqual(s, set(q)) @@ -411,7 +411,7 @@ class TestSet(TestJointOps): self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru()) self.assertRaises(TypeError, self.s.intersection_update, [[]]) 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') self.assertEqual(s.intersection_update(C(p)), None) self.assertEqual(s, set(q)) @@ -436,7 +436,7 @@ class TestSet(TestJointOps): self.assertRaises(TypeError, self.s.difference_update, [[]]) self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) 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') self.assertEqual(s.difference_update(C(p)), None) self.assertEqual(s, set(q)) @@ -460,7 +460,7 @@ class TestSet(TestJointOps): self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru()) self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) 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') self.assertEqual(s.symmetric_difference_update(C(p)), None) self.assertEqual(s, set(q)) diff --git a/Lib/xml/dom/minicompat.py b/Lib/xml/dom/minicompat.py index e2a2bca..c0a797e 100644 --- a/Lib/xml/dom/minicompat.py +++ b/Lib/xml/dom/minicompat.py @@ -40,12 +40,7 @@ __all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"] import xml.dom -try: - str -except NameError: - StringTypes = type(''), -else: - StringTypes = type(''), type(str('')) +StringTypes = (str,) class NodeList(list): -- cgit v0.12