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

Skip to content

Commit 4043001

Browse files
committed
Make str/str8 comparisons return True/False for !=/==.
Code that has been returning str8 becomes much more apparent thanks to this (e.g., struct module returning str8 for all string-related formats or sqlite3 passing in str8 instances when converting objects that had a __conform__ method). One also has to watch out in C code when making a key from char * using PyString in the C code but a str instance in Python code as that will not longer compare equal. Once str8 gains a constructor like the current bytes type then test_modulefinder needs a cleanup as the fix is a little messy in that file. Thanks goes to Thomas Lee for writing the patch for the change giving an initial run-down of why most of the tests were failing.
1 parent 6464d47 commit 4043001

11 files changed

Lines changed: 169 additions & 205 deletions

File tree

Lib/modulefinder.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
# remain compatible with Python < 2.3
1717
READ_MODE = "r"
1818

19-
LOAD_CONST = chr(dis.opname.index('LOAD_CONST'))
20-
IMPORT_NAME = chr(dis.opname.index('IMPORT_NAME'))
21-
STORE_NAME = chr(dis.opname.index('STORE_NAME'))
22-
STORE_GLOBAL = chr(dis.opname.index('STORE_GLOBAL'))
19+
# XXX Clean up once str8's cstor matches bytes.
20+
LOAD_CONST = str8(chr(dis.opname.index('LOAD_CONST')))
21+
IMPORT_NAME = str8(chr(dis.opname.index('IMPORT_NAME')))
22+
STORE_NAME = str8(chr(dis.opname.index('STORE_NAME')))
23+
STORE_GLOBAL = str8(chr(dis.opname.index('STORE_GLOBAL')))
2324
STORE_OPS = [STORE_NAME, STORE_GLOBAL]
24-
HAVE_ARGUMENT = chr(dis.HAVE_ARGUMENT)
25+
HAVE_ARGUMENT = str8(chr(dis.HAVE_ARGUMENT))
2526

2627
# Modulefinder does a good job at simulating Python's, but it can not
2728
# handle __path__ modifications packages make at runtime. Therefore there
@@ -367,7 +368,7 @@ def scan_opcodes_25(self, co,
367368
consts = co.co_consts
368369
LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME
369370
while code:
370-
c = chr(code[0])
371+
c = str8(chr(code[0]))
371372
if c in STORE_OPS:
372373
oparg, = unpack('<H', code[1:3])
373374
yield "store", (names[oparg],)

Lib/sqlite3/test/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def CheckUnicodeExecute(self):
7676
class DeclTypesTests(unittest.TestCase):
7777
class Foo:
7878
def __init__(self, _val):
79+
if isinstance(_val, str8):
80+
# sqlite3 always calls __init__ with a str8 created from a
81+
# UTF-8 string when __conform__ was used to store the object.
82+
_val = _val.decode('utf8')
7983
self.val = _val
8084

8185
def __cmp__(self, other):

Lib/test/test_compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_leading_newlines(self):
157157
s256 = "".join(["\n"] * 256 + ["spam"])
158158
co = compile(s256, 'fn', 'exec')
159159
self.assertEqual(co.co_firstlineno, 257)
160-
self.assertEqual(co.co_lnotab, '')
160+
self.assertEqual(co.co_lnotab, str8(''))
161161

162162
def test_literals_with_leading_zeroes(self):
163163
for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",

Lib/test/test_format.py

Lines changed: 122 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -39,163 +39,158 @@ def testformat(formatstr, args, output=None):
3939
if verbose:
4040
print('yes')
4141

42-
def testboth(formatstr, *args):
43-
testformat(str8(formatstr), *args)
44-
testformat(formatstr, *args)
42+
testformat("%.1d", (1,), "1")
43+
testformat("%.*d", (sys.maxint,1)) # expect overflow
44+
testformat("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
45+
testformat("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
46+
testformat("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
4547

46-
47-
testboth("%.1d", (1,), "1")
48-
testboth("%.*d", (sys.maxint,1)) # expect overflow
49-
testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
50-
testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
51-
testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
52-
53-
testboth("%f", (1.0,), "1.000000")
48+
testformat("%f", (1.0,), "1.000000")
5449
# these are trying to test the limits of the internal magic-number-length
5550
# formatting buffer, if that number changes then these tests are less
5651
# effective
57-
testboth("%#.*g", (109, -1.e+49/3.))
58-
testboth("%#.*g", (110, -1.e+49/3.))
59-
testboth("%#.*g", (110, -1.e+100/3.))
52+
testformat("%#.*g", (109, -1.e+49/3.))
53+
testformat("%#.*g", (110, -1.e+49/3.))
54+
testformat("%#.*g", (110, -1.e+100/3.))
6055

6156
# test some ridiculously large precision, expect overflow
62-
testboth('%12.*f', (123456, 1.0))
57+
testformat('%12.*f', (123456, 1.0))
6358

6459
# check for internal overflow validation on length of precision
6560
overflowrequired = 1
66-
testboth("%#.*g", (110, -1.e+100/3.))
67-
testboth("%#.*G", (110, -1.e+100/3.))
68-
testboth("%#.*f", (110, -1.e+100/3.))
69-
testboth("%#.*F", (110, -1.e+100/3.))
61+
testformat("%#.*g", (110, -1.e+100/3.))
62+
testformat("%#.*G", (110, -1.e+100/3.))
63+
testformat("%#.*f", (110, -1.e+100/3.))
64+
testformat("%#.*F", (110, -1.e+100/3.))
7065
overflowrequired = 0
7166

7267
# Formatting of long integers. Overflow is not ok
7368
overflowok = 0
74-
testboth("%x", 10, "a")
75-
testboth("%x", 100000000000, "174876e800")
76-
testboth("%o", 10, "12")
77-
testboth("%o", 100000000000, "1351035564000")
78-
testboth("%d", 10, "10")
79-
testboth("%d", 100000000000, "100000000000")
69+
testformat("%x", 10, "a")
70+
testformat("%x", 100000000000, "174876e800")
71+
testformat("%o", 10, "12")
72+
testformat("%o", 100000000000, "1351035564000")
73+
testformat("%d", 10, "10")
74+
testformat("%d", 100000000000, "100000000000")
8075

8176
big = 123456789012345678901234567890
82-
testboth("%d", big, "123456789012345678901234567890")
83-
testboth("%d", -big, "-123456789012345678901234567890")
84-
testboth("%5d", -big, "-123456789012345678901234567890")
85-
testboth("%31d", -big, "-123456789012345678901234567890")
86-
testboth("%32d", -big, " -123456789012345678901234567890")
87-
testboth("%-32d", -big, "-123456789012345678901234567890 ")
88-
testboth("%032d", -big, "-0123456789012345678901234567890")
89-
testboth("%-032d", -big, "-123456789012345678901234567890 ")
90-
testboth("%034d", -big, "-000123456789012345678901234567890")
91-
testboth("%034d", big, "0000123456789012345678901234567890")
92-
testboth("%0+34d", big, "+000123456789012345678901234567890")
93-
testboth("%+34d", big, " +123456789012345678901234567890")
94-
testboth("%34d", big, " 123456789012345678901234567890")
95-
testboth("%.2d", big, "123456789012345678901234567890")
96-
testboth("%.30d", big, "123456789012345678901234567890")
97-
testboth("%.31d", big, "0123456789012345678901234567890")
98-
testboth("%32.31d", big, " 0123456789012345678901234567890")
77+
testformat("%d", big, "123456789012345678901234567890")
78+
testformat("%d", -big, "-123456789012345678901234567890")
79+
testformat("%5d", -big, "-123456789012345678901234567890")
80+
testformat("%31d", -big, "-123456789012345678901234567890")
81+
testformat("%32d", -big, " -123456789012345678901234567890")
82+
testformat("%-32d", -big, "-123456789012345678901234567890 ")
83+
testformat("%032d", -big, "-0123456789012345678901234567890")
84+
testformat("%-032d", -big, "-123456789012345678901234567890 ")
85+
testformat("%034d", -big, "-000123456789012345678901234567890")
86+
testformat("%034d", big, "0000123456789012345678901234567890")
87+
testformat("%0+34d", big, "+000123456789012345678901234567890")
88+
testformat("%+34d", big, " +123456789012345678901234567890")
89+
testformat("%34d", big, " 123456789012345678901234567890")
90+
testformat("%.2d", big, "123456789012345678901234567890")
91+
testformat("%.30d", big, "123456789012345678901234567890")
92+
testformat("%.31d", big, "0123456789012345678901234567890")
93+
testformat("%32.31d", big, " 0123456789012345678901234567890")
9994

10095
big = 0x1234567890abcdef12345 # 21 hex digits
101-
testboth("%x", big, "1234567890abcdef12345")
102-
testboth("%x", -big, "-1234567890abcdef12345")
103-
testboth("%5x", -big, "-1234567890abcdef12345")
104-
testboth("%22x", -big, "-1234567890abcdef12345")
105-
testboth("%23x", -big, " -1234567890abcdef12345")
106-
testboth("%-23x", -big, "-1234567890abcdef12345 ")
107-
testboth("%023x", -big, "-01234567890abcdef12345")
108-
testboth("%-023x", -big, "-1234567890abcdef12345 ")
109-
testboth("%025x", -big, "-0001234567890abcdef12345")
110-
testboth("%025x", big, "00001234567890abcdef12345")
111-
testboth("%0+25x", big, "+0001234567890abcdef12345")
112-
testboth("%+25x", big, " +1234567890abcdef12345")
113-
testboth("%25x", big, " 1234567890abcdef12345")
114-
testboth("%.2x", big, "1234567890abcdef12345")
115-
testboth("%.21x", big, "1234567890abcdef12345")
116-
testboth("%.22x", big, "01234567890abcdef12345")
117-
testboth("%23.22x", big, " 01234567890abcdef12345")
118-
testboth("%-23.22x", big, "01234567890abcdef12345 ")
119-
testboth("%X", big, "1234567890ABCDEF12345")
120-
testboth("%#X", big, "0X1234567890ABCDEF12345")
121-
testboth("%#x", big, "0x1234567890abcdef12345")
122-
testboth("%#x", -big, "-0x1234567890abcdef12345")
123-
testboth("%#.23x", -big, "-0x001234567890abcdef12345")
124-
testboth("%#+.23x", big, "+0x001234567890abcdef12345")
125-
testboth("%# .23x", big, " 0x001234567890abcdef12345")
126-
testboth("%#+.23X", big, "+0X001234567890ABCDEF12345")
127-
testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345")
128-
testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
129-
testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
130-
testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
96+
testformat("%x", big, "1234567890abcdef12345")
97+
testformat("%x", -big, "-1234567890abcdef12345")
98+
testformat("%5x", -big, "-1234567890abcdef12345")
99+
testformat("%22x", -big, "-1234567890abcdef12345")
100+
testformat("%23x", -big, " -1234567890abcdef12345")
101+
testformat("%-23x", -big, "-1234567890abcdef12345 ")
102+
testformat("%023x", -big, "-01234567890abcdef12345")
103+
testformat("%-023x", -big, "-1234567890abcdef12345 ")
104+
testformat("%025x", -big, "-0001234567890abcdef12345")
105+
testformat("%025x", big, "00001234567890abcdef12345")
106+
testformat("%0+25x", big, "+0001234567890abcdef12345")
107+
testformat("%+25x", big, " +1234567890abcdef12345")
108+
testformat("%25x", big, " 1234567890abcdef12345")
109+
testformat("%.2x", big, "1234567890abcdef12345")
110+
testformat("%.21x", big, "1234567890abcdef12345")
111+
testformat("%.22x", big, "01234567890abcdef12345")
112+
testformat("%23.22x", big, " 01234567890abcdef12345")
113+
testformat("%-23.22x", big, "01234567890abcdef12345 ")
114+
testformat("%X", big, "1234567890ABCDEF12345")
115+
testformat("%#X", big, "0X1234567890ABCDEF12345")
116+
testformat("%#x", big, "0x1234567890abcdef12345")
117+
testformat("%#x", -big, "-0x1234567890abcdef12345")
118+
testformat("%#.23x", -big, "-0x001234567890abcdef12345")
119+
testformat("%#+.23x", big, "+0x001234567890abcdef12345")
120+
testformat("%# .23x", big, " 0x001234567890abcdef12345")
121+
testformat("%#+.23X", big, "+0X001234567890ABCDEF12345")
122+
testformat("%#-+.23X", big, "+0X001234567890ABCDEF12345")
123+
testformat("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
124+
testformat("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
125+
testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
131126
# next one gets two leading zeroes from precision, and another from the
132127
# 0 flag and the width
133-
testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
128+
testformat("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
134129
# same, except no 0 flag
135-
testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
130+
testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
136131

137132
big = 0o12345670123456701234567012345670 # 32 octal digits
138-
testboth("%o", big, "12345670123456701234567012345670")
139-
testboth("%o", -big, "-12345670123456701234567012345670")
140-
testboth("%5o", -big, "-12345670123456701234567012345670")
141-
testboth("%33o", -big, "-12345670123456701234567012345670")
142-
testboth("%34o", -big, " -12345670123456701234567012345670")
143-
testboth("%-34o", -big, "-12345670123456701234567012345670 ")
144-
testboth("%034o", -big, "-012345670123456701234567012345670")
145-
testboth("%-034o", -big, "-12345670123456701234567012345670 ")
146-
testboth("%036o", -big, "-00012345670123456701234567012345670")
147-
testboth("%036o", big, "000012345670123456701234567012345670")
148-
testboth("%0+36o", big, "+00012345670123456701234567012345670")
149-
testboth("%+36o", big, " +12345670123456701234567012345670")
150-
testboth("%36o", big, " 12345670123456701234567012345670")
151-
testboth("%.2o", big, "12345670123456701234567012345670")
152-
testboth("%.32o", big, "12345670123456701234567012345670")
153-
testboth("%.33o", big, "012345670123456701234567012345670")
154-
testboth("%34.33o", big, " 012345670123456701234567012345670")
155-
testboth("%-34.33o", big, "012345670123456701234567012345670 ")
156-
testboth("%o", big, "12345670123456701234567012345670")
157-
testboth("%#o", big, "0o12345670123456701234567012345670")
158-
testboth("%#o", -big, "-0o12345670123456701234567012345670")
159-
testboth("%#.34o", -big, "-0o0012345670123456701234567012345670")
160-
testboth("%#+.34o", big, "+0o0012345670123456701234567012345670")
161-
testboth("%# .34o", big, " 0o0012345670123456701234567012345670")
162-
testboth("%#-+.34o", big, "+0o0012345670123456701234567012345670")
163-
testboth("%#-+39.34o", big, "+0o0012345670123456701234567012345670 ")
164-
testboth("%#+39.34o", big, " +0o0012345670123456701234567012345670")
133+
testformat("%o", big, "12345670123456701234567012345670")
134+
testformat("%o", -big, "-12345670123456701234567012345670")
135+
testformat("%5o", -big, "-12345670123456701234567012345670")
136+
testformat("%33o", -big, "-12345670123456701234567012345670")
137+
testformat("%34o", -big, " -12345670123456701234567012345670")
138+
testformat("%-34o", -big, "-12345670123456701234567012345670 ")
139+
testformat("%034o", -big, "-012345670123456701234567012345670")
140+
testformat("%-034o", -big, "-12345670123456701234567012345670 ")
141+
testformat("%036o", -big, "-00012345670123456701234567012345670")
142+
testformat("%036o", big, "000012345670123456701234567012345670")
143+
testformat("%0+36o", big, "+00012345670123456701234567012345670")
144+
testformat("%+36o", big, " +12345670123456701234567012345670")
145+
testformat("%36o", big, " 12345670123456701234567012345670")
146+
testformat("%.2o", big, "12345670123456701234567012345670")
147+
testformat("%.32o", big, "12345670123456701234567012345670")
148+
testformat("%.33o", big, "012345670123456701234567012345670")
149+
testformat("%34.33o", big, " 012345670123456701234567012345670")
150+
testformat("%-34.33o", big, "012345670123456701234567012345670 ")
151+
testformat("%o", big, "12345670123456701234567012345670")
152+
testformat("%#o", big, "0o12345670123456701234567012345670")
153+
testformat("%#o", -big, "-0o12345670123456701234567012345670")
154+
testformat("%#.34o", -big, "-0o0012345670123456701234567012345670")
155+
testformat("%#+.34o", big, "+0o0012345670123456701234567012345670")
156+
testformat("%# .34o", big, " 0o0012345670123456701234567012345670")
157+
testformat("%#-+.34o", big, "+0o0012345670123456701234567012345670")
158+
testformat("%#-+39.34o", big, "+0o0012345670123456701234567012345670 ")
159+
testformat("%#+39.34o", big, " +0o0012345670123456701234567012345670")
165160
# next one gets one leading zero from precision
166-
testboth("%.33o", big, "012345670123456701234567012345670")
161+
testformat("%.33o", big, "012345670123456701234567012345670")
167162
# one leading zero from precision
168-
testboth("%#.33o", big, "0o012345670123456701234567012345670")
163+
testformat("%#.33o", big, "0o012345670123456701234567012345670")
169164
# leading zero vanishes
170-
testboth("%#.32o", big, "0o12345670123456701234567012345670")
165+
testformat("%#.32o", big, "0o12345670123456701234567012345670")
171166
# one leading zero from precision, and another from '0' flag & width
172-
testboth("%034.33o", big, "0012345670123456701234567012345670")
167+
testformat("%034.33o", big, "0012345670123456701234567012345670")
173168
# max width includes base marker; padding zeroes come after marker
174-
testboth("%0#38.33o", big, "0o000012345670123456701234567012345670")
169+
testformat("%0#38.33o", big, "0o000012345670123456701234567012345670")
175170
# padding spaces come before marker
176-
testboth("%#36.33o", big, " 0o012345670123456701234567012345670")
171+
testformat("%#36.33o", big, " 0o012345670123456701234567012345670")
177172

178173
# Some small ints, in both Python int and long flavors).
179-
testboth("%d", 42, "42")
180-
testboth("%d", -42, "-42")
181-
testboth("%#x", 1, "0x1")
182-
testboth("%#X", 1, "0X1")
183-
testboth("%#o", 1, "0o1")
184-
testboth("%#o", 1, "0o1")
185-
testboth("%#o", 0, "0o0")
186-
testboth("%#o", 0, "0o0")
187-
testboth("%o", 0, "0")
188-
testboth("%d", 0, "0")
189-
testboth("%#x", 0, "0x0")
190-
testboth("%#X", 0, "0X0")
191-
192-
testboth("%x", 0x42, "42")
193-
testboth("%x", -0x42, "-42")
194-
195-
testboth("%o", 0o42, "42")
196-
testboth("%o", -0o42, "-42")
197-
testboth("%o", 0o42, "42")
198-
testboth("%o", -0o42, "-42")
174+
testformat("%d", 42, "42")
175+
testformat("%d", -42, "-42")
176+
testformat("%#x", 1, "0x1")
177+
testformat("%#X", 1, "0X1")
178+
testformat("%#o", 1, "0o1")
179+
testformat("%#o", 1, "0o1")
180+
testformat("%#o", 0, "0o0")
181+
testformat("%#o", 0, "0o0")
182+
testformat("%o", 0, "0")
183+
testformat("%d", 0, "0")
184+
testformat("%#x", 0, "0x0")
185+
testformat("%#X", 0, "0X0")
186+
187+
testformat("%x", 0x42, "42")
188+
testformat("%x", -0x42, "-42")
189+
190+
testformat("%o", 0o42, "42")
191+
testformat("%o", -0o42, "-42")
192+
testformat("%o", 0o42, "42")
193+
testformat("%o", -0o42, "-42")
199194

200195
# Test exception for unknown format characters
201196
if verbose:

Lib/test/test_str.py

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,59 +36,19 @@ class Foo1:
3636
def __str__(self):
3737
return "foo"
3838

39-
class Foo2(object):
40-
def __str__(self):
41-
return "foo"
42-
43-
class Foo3(object):
44-
def __str__(self):
45-
return "foo"
46-
47-
class Foo4(str8):
48-
def __str__(self):
49-
return "foo"
50-
51-
class Foo5(str):
52-
def __unicode__(self):
53-
return "foo"
54-
55-
class Foo6(str8):
56-
def __str__(self):
57-
return "foos"
58-
59-
def __unicode__(self):
60-
return "foou"
61-
6239
class Foo7(str):
6340
def __str__(self):
6441
return "foos"
65-
def __unicode__(self):
66-
return "foou"
6742

6843
class Foo8(str):
6944
def __new__(cls, content=""):
7045
return str.__new__(cls, 2*content)
7146
def __str__(self):
7247
return self
7348

74-
class Foo9(str8):
75-
def __str__(self):
76-
return "string"
77-
def __unicode__(self):
78-
return "not unicode"
79-
8049
self.assertEqual(str(Foo1()), "foo")
81-
self.assertEqual(str(Foo2()), "foo")
82-
self.assertEqual(str(Foo3()), "foo")
83-
self.assertEqual(str(Foo4("bar")), "foo")
84-
self.assertEqual(str(Foo5("bar")), "foo")
85-
self.assertEqual(str8(Foo6("bar")), "foos")
86-
self.assertEqual(str(Foo6("bar")), "foou")
87-
self.assertEqual(str8(Foo7("bar")), "foos")
88-
self.assertEqual(str(Foo7("bar")), "foou")
50+
self.assertEqual(str(Foo7("bar")), "foos")
8951
self.assertEqual(str(Foo8("foo")), "foofoo")
90-
self.assertEqual(str8(Foo9("foo")), "string")
91-
self.assertEqual(str(Foo9("foo")), "not unicode")
9252

9353
def test_expandtabs_overflows_gracefully(self):
9454
# This test only affects 32-bit platforms because expandtabs can only take

0 commit comments

Comments
 (0)