Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5de48bd

Browse files
committed
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.
1 parent 80bfb72 commit 5de48bd

9 files changed

Lines changed: 48 additions & 89 deletions

File tree

Lib/distutils/command/bdist_wininst.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,8 @@ def create_exe (self, arcname, fullname, bitmap=None):
246246
file.write(bitmapdata)
247247

248248
# Convert cfgdata from unicode to ascii, mbcs encoded
249-
try:
250-
str
251-
except NameError:
252-
pass
253-
else:
254-
if isinstance(cfgdata, str):
255-
cfgdata = cfgdata.encode("mbcs")
249+
if isinstance(cfgdata, str):
250+
cfgdata = cfgdata.encode("mbcs")
256251

257252
# Append the pre-install script
258253
cfgdata = cfgdata + "\0"

Lib/idlelib/PyParse.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,33 +104,28 @@ def dump(*stuff):
104104
_tran = ''.join(_tran)
105105
del ch
106106

107-
try:
108-
UnicodeType = type(str(""))
109-
except NameError:
110-
UnicodeType = None
111-
112107
class Parser:
113108

114109
def __init__(self, indentwidth, tabwidth):
115110
self.indentwidth = indentwidth
116111
self.tabwidth = tabwidth
117112

118-
def set_str(self, str):
119-
assert len(str) == 0 or str[-1] == '\n'
120-
if type(str) is UnicodeType:
113+
def set_str(self, s):
114+
assert len(s) == 0 or s[-1] == '\n'
115+
if isinstance(s, str):
121116
# The parse functions have no idea what to do with Unicode, so
122117
# replace all Unicode characters with "x". This is "safe"
123118
# so long as the only characters germane to parsing the structure
124119
# of Python are 7-bit ASCII. It's *necessary* because Unicode
125120
# strings don't have a .translate() method that supports
126121
# deletechars.
127-
uniphooey = str
122+
uniphooey = s
128123
str = []
129-
push = str.append
124+
push = s.append
130125
for raw in map(ord, uniphooey):
131126
push(raw < 127 and chr(raw) or "x")
132-
str = "".join(str)
133-
self.str = str
127+
s = "".join(s)
128+
self.str = s
134129
self.study_level = 0
135130

136131
# Return index of a good place to begin parsing, as close to the

Lib/lib-tk/Tkinter.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,11 +3734,7 @@ def _test():
37343734
root = Tk()
37353735
text = "This is Tcl/Tk version %s" % TclVersion
37363736
if TclVersion >= 8.1:
3737-
try:
3738-
text = text + str("\nThis should be a cedilla: \347",
3739-
"iso-8859-1")
3740-
except NameError:
3741-
pass # no unicode support
3737+
text += "\nThis should be a cedilla: \xe7"
37423738
label = Label(root, text=text)
37433739
label.pack()
37443740
test = Button(root, text="Click me!",

Lib/test/test_cfgparser.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,8 @@ class mystr(str):
247247
cf.set("sect", "option1", mystr("splat"))
248248
cf.set("sect", "option2", "splat")
249249
cf.set("sect", "option2", mystr("splat"))
250-
try:
251-
str
252-
except NameError:
253-
pass
254-
else:
255-
cf.set("sect", "option1", str("splat"))
256-
cf.set("sect", "option2", str("splat"))
250+
cf.set("sect", "option1", "splat")
251+
cf.set("sect", "option2", "splat")
257252

258253
def test_read_returns_file_list(self):
259254
file1 = test_support.findfile("cfgparser.1")

Lib/test/test_descr.py

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def test_dir():
265265
del junk
266266

267267
# Just make sure these don't blow up!
268-
for arg in 2, 2, 2j, 2e0, [2], "2", "2", (2,), {2:2}, type, test_dir:
268+
for arg in 2, 2, 2j, 2e0, [2], "2", b"2", (2,), {2:2}, type, test_dir:
269269
dir(arg)
270270

271271
# Test dir on custom classes. Since these have object as a
@@ -1117,34 +1117,29 @@ class C(object):
11171117
vereq(c.abc, 5)
11181118

11191119
# Test unicode slot names
1120+
# Test a single unicode string is not expanded as a sequence.
1121+
class C(object):
1122+
__slots__ = "abc"
1123+
c = C()
1124+
c.abc = 5
1125+
vereq(c.abc, 5)
1126+
1127+
# _unicode_to_string used to modify slots in certain circumstances
1128+
slots = ("foo", "bar")
1129+
class C(object):
1130+
__slots__ = slots
1131+
x = C()
1132+
x.foo = 5
1133+
vereq(x.foo, 5)
1134+
veris(type(slots[0]), str)
1135+
# this used to leak references
11201136
try:
1121-
str
1122-
except NameError:
1137+
class C(object):
1138+
__slots__ = [chr(128)]
1139+
except (TypeError, UnicodeEncodeError):
11231140
pass
11241141
else:
1125-
# Test a single unicode string is not expanded as a sequence.
1126-
class C(object):
1127-
__slots__ = str("abc")
1128-
c = C()
1129-
c.abc = 5
1130-
vereq(c.abc, 5)
1131-
1132-
# _unicode_to_string used to modify slots in certain circumstances
1133-
slots = (str("foo"), str("bar"))
1134-
class C(object):
1135-
__slots__ = slots
1136-
x = C()
1137-
x.foo = 5
1138-
vereq(x.foo, 5)
1139-
veris(type(slots[0]), str)
1140-
# this used to leak references
1141-
try:
1142-
class C(object):
1143-
__slots__ = [chr(128)]
1144-
except (TypeError, UnicodeEncodeError):
1145-
pass
1146-
else:
1147-
raise TestFailed, "[unichr(128)] slots not caught"
1142+
raise TestFailed, "[unichr(128)] slots not caught"
11481143

11491144
# Test leaks
11501145
class Counted(object):
@@ -2693,14 +2688,8 @@ class G(object):
26932688
__slots__ = ["a", "b"]
26942689
class H(object):
26952690
__slots__ = ["b", "a"]
2696-
try:
2697-
str
2698-
except NameError:
2699-
class I(object):
2700-
__slots__ = ["a", "b"]
2701-
else:
2702-
class I(object):
2703-
__slots__ = [str("a"), str("b")]
2691+
class I(object):
2692+
__slots__ = ["a", "b"]
27042693
class J(object):
27052694
__slots__ = ["c", "b"]
27062695
class K(object):

Lib/test/test_iter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def __next__(self):
526526
# and pass that on to unicode.join().
527527
try:
528528
got = " - ".join(OhPhooey(f))
529-
self.assertEqual(got, str("a\n - b\n - fooled you! - c\n"))
529+
self.assertEqual(got, "a\n - b\n - fooled you! - c\n")
530530
finally:
531531
f.close()
532532
try:

Lib/test/test_pprint.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
import test.test_support
33
import unittest
44

5-
try:
6-
uni = str
7-
except NameError:
8-
def uni(x):
9-
return x
10-
115
# list, tuple and dict subclasses that do or don't overwrite __repr__
126
class list2(list):
137
pass
@@ -41,7 +35,7 @@ def test_basic(self):
4135
# Verify .isrecursive() and .isreadable() w/o recursion
4236
verify = self.assert_
4337
pp = pprint.PrettyPrinter()
44-
for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
38+
for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda",
4539
self.a, self.b):
4640
# module-level convenience functions
4741
verify(not pprint.isrecursive(safe),
@@ -114,12 +108,12 @@ def test_same_as_repr(self):
114108
# multiple lines. For that reason, dicts with more than one element
115109
# aren't tested here.
116110
verify = self.assert_
117-
for simple in (0, 0, 0+0j, 0.0, "", uni(""),
111+
for simple in (0, 0, 0+0j, 0.0, "", b"",
118112
(), tuple2(), tuple3(),
119113
[], list2(), list3(),
120114
{}, dict2(), dict3(),
121115
verify, pprint,
122-
-6, -6, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
116+
-6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
123117
(1,2), [3,4], {5: 6, 7: 8},
124118
tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
125119
[3,4], list2([3,4]), list3([3,4]), list3(range(100)),

Lib/test/test_set.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_union(self):
7272
self.assertEqual(type(u), self.thetype)
7373
self.assertRaises(PassThru, self.s.union, check_pass_thru())
7474
self.assertRaises(TypeError, self.s.union, [[]])
75-
for C in set, frozenset, dict.fromkeys, str, str, list, tuple:
75+
for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
7676
self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd'))
7777
self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg'))
7878
self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc'))
@@ -96,7 +96,7 @@ def test_intersection(self):
9696
self.assertEqual(self.s, self.thetype(self.word))
9797
self.assertEqual(type(i), self.thetype)
9898
self.assertRaises(PassThru, self.s.intersection, check_pass_thru())
99-
for C in set, frozenset, dict.fromkeys, str, str, list, tuple:
99+
for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
100100
self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc'))
101101
self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set(''))
102102
self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
@@ -121,7 +121,7 @@ def test_difference(self):
121121
self.assertEqual(type(i), self.thetype)
122122
self.assertRaises(PassThru, self.s.difference, check_pass_thru())
123123
self.assertRaises(TypeError, self.s.difference, [[]])
124-
for C in set, frozenset, dict.fromkeys, str, str, list, tuple:
124+
for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
125125
self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab'))
126126
self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc'))
127127
self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a'))
@@ -146,7 +146,7 @@ def test_symmetric_difference(self):
146146
self.assertEqual(type(i), self.thetype)
147147
self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru())
148148
self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
149-
for C in set, frozenset, dict.fromkeys, str, str, list, tuple:
149+
for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
150150
self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd'))
151151
self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg'))
152152
self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a'))
@@ -390,7 +390,7 @@ def test_update(self):
390390
self.assertRaises(PassThru, self.s.update, check_pass_thru())
391391
self.assertRaises(TypeError, self.s.update, [[]])
392392
for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')):
393-
for C in set, frozenset, dict.fromkeys, str, str, list, tuple:
393+
for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
394394
s = self.thetype('abcba')
395395
self.assertEqual(s.update(C(p)), None)
396396
self.assertEqual(s, set(q))
@@ -411,7 +411,7 @@ def test_intersection_update(self):
411411
self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru())
412412
self.assertRaises(TypeError, self.s.intersection_update, [[]])
413413
for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')):
414-
for C in set, frozenset, dict.fromkeys, str, str, list, tuple:
414+
for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
415415
s = self.thetype('abcba')
416416
self.assertEqual(s.intersection_update(C(p)), None)
417417
self.assertEqual(s, set(q))
@@ -436,7 +436,7 @@ def test_difference_update(self):
436436
self.assertRaises(TypeError, self.s.difference_update, [[]])
437437
self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
438438
for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')):
439-
for C in set, frozenset, dict.fromkeys, str, str, list, tuple:
439+
for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
440440
s = self.thetype('abcba')
441441
self.assertEqual(s.difference_update(C(p)), None)
442442
self.assertEqual(s, set(q))
@@ -460,7 +460,7 @@ def test_symmetric_difference_update(self):
460460
self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru())
461461
self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
462462
for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')):
463-
for C in set, frozenset, dict.fromkeys, str, str, list, tuple:
463+
for C in set, frozenset, dict.fromkeys, str, str8, list, tuple:
464464
s = self.thetype('abcba')
465465
self.assertEqual(s.symmetric_difference_update(C(p)), None)
466466
self.assertEqual(s, set(q))

Lib/xml/dom/minicompat.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@
4040

4141
import xml.dom
4242

43-
try:
44-
str
45-
except NameError:
46-
StringTypes = type(''),
47-
else:
48-
StringTypes = type(''), type(str(''))
43+
StringTypes = (str,)
4944

5045

5146
class NodeList(list):

0 commit comments

Comments
 (0)