| 1 |
|
| 2 |
import sys |
| 3 |
import pytest |
| 4 |
from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class TestString(BaseTestPyPyC): |
| 10 |
|
| 11 |
def test_python3_missing_bchr(self): |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
def main(n): |
| 16 |
i = 0 |
| 17 |
result = b'' |
| 18 |
while i < n: |
| 19 |
c = bytes([i]) |
| 20 |
result += c |
| 21 |
i += 1 |
| 22 |
return i |
| 23 |
log = self.run(main, [255]) |
| 24 |
assert log.result == 255 |
| 25 |
loop, = log.loops_by_filename(self.filepath) |
| 26 |
assert loop.match(""" |
| 27 |
# nothing left like allocating a list object or doing any |
| 28 |
# residual call |
| 29 |
i49 = int_lt(i38, i26) |
| 30 |
guard_true(i49, descr=...) |
| 31 |
guard_not_invalidated(descr=...) |
| 32 |
i51 = int_lt(i38, 256) |
| 33 |
guard_true(i51, descr=...) |
| 34 |
i53 = int_add(i38, 1) |
| 35 |
--TICK-- |
| 36 |
i58 = strlen(p46) |
| 37 |
i60 = int_add(i58, 1) |
| 38 |
p61 = newstr(i60) |
| 39 |
copystrcontent(p46, p61, 0, 0, i58) |
| 40 |
strsetitem(p61, i58, i38) |
| 41 |
p62 = newstr(1) |
| 42 |
strsetitem(p62, 0, i38) |
| 43 |
jump(..., descr=...) |
| 44 |
""") |
| 45 |
|
| 46 |
def test_lookup_default_encoding(self): |
| 47 |
def main(n): |
| 48 |
i = 0 |
| 49 |
letters = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 50 |
uletters = u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 51 |
while i < n: |
| 52 |
c = bytes([letters[i % len(uletters)]]) |
| 53 |
i += (c.decode() == uletters[i % len(uletters)]) |
| 54 |
return i |
| 55 |
|
| 56 |
log = self.run(main, [300], import_site=True) |
| 57 |
assert log.result == 300 |
| 58 |
loop, = log.loops_by_filename(self.filepath) |
| 59 |
assert loop.match(""" |
| 60 |
i88 = int_lt(i83, i36) |
| 61 |
guard_true(i88, descr=...) |
| 62 |
guard_not_invalidated(descr=...) |
| 63 |
i92 = int_eq(i83, %d) |
| 64 |
i94 = call_i(ConstClass(ll_int_py_mod__Signed_Signed), i83, i46, descr=<Calli . ii EF=0 OS=14>) |
| 65 |
i97 = int_ge(i94, i53) |
| 66 |
guard_false(i97, descr=...) |
| 67 |
i98 = strgetitem(p52, i94) |
| 68 |
p103 = newstr(1) |
| 69 |
strsetitem(p103, 0, i98) |
| 70 |
i95 = call_i(ConstClass(_check_utf8), p103, 0, 0, -1, descr=<Calli 8 riii EF=4>) |
| 71 |
guard_no_exception(descr=...) |
| 72 |
i98 = int_ge(i95, 0) |
| 73 |
guard_true(i98, descr=...) |
| 74 |
i99 = int_ge(i94, i46) |
| 75 |
guard_false(i99, descr=...) |
| 76 |
i115 = int_add(i94, 1) |
| 77 |
i116 = int_gt(i115, i71) |
| 78 |
guard_false(i116, descr=...) |
| 79 |
|
| 80 |
i104 = call_i(ConstClass(_ll_4_str_eq_slice_char__rpy_stringPtr_Signed_Signed_Char), p65, i94, 1, i98, descr=<Calli 8 riii EF=0 OS=27>) |
| 81 |
guard_true(i104, descr=...) |
| 82 |
i124 = int_add(i83, 1) |
| 83 |
--TICK-- |
| 84 |
jump(..., descr=...) |
| 85 |
""" % (-sys.maxint-1,)) |
| 86 |
|
| 87 |
def test_int_base_16(self): |
| 88 |
def main(n): |
| 89 |
i = 1 |
| 90 |
while i < n: |
| 91 |
digits = '0123456789' |
| 92 |
i += int(digits[i % len(digits)], 16) |
| 93 |
return i |
| 94 |
|
| 95 |
log = self.run(main, [1100], import_site=True) |
| 96 |
assert log.result == main(1100) |
| 97 |
loop, = log.loops_by_filename(self.filepath) |
| 98 |
if sys.maxint > 2**32: |
| 99 |
args = (63, -3689348814741910323, 3) |
| 100 |
else: |
| 101 |
args = (31, -858993459, 3) |
| 102 |
assert loop.match(""" |
| 103 |
i11 = int_lt(i6, i7) |
| 104 |
guard_true(i11, descr=...) |
| 105 |
guard_not_invalidated(descr=...) |
| 106 |
i13 = int_eq(i6, %d) # value provided below |
| 107 |
|
| 108 |
# "mod 10" block: |
| 109 |
i79 = int_rshift(i6, %d) |
| 110 |
i80 = int_xor(i6, i79) |
| 111 |
i82 = uint_mul_high(i80, %d) |
| 112 |
i84 = uint_rshift(i82, %d) |
| 113 |
i85 = int_xor(i84, i79) |
| 114 |
i87 = int_mul(i85, 10) |
| 115 |
i19 = int_sub(i6, i87) |
| 116 |
|
| 117 |
i85 = int_add(i19, 1) # not used |
| 118 |
p25 = newstr(1) |
| 119 |
i23 = strgetitem(ConstPtr(ptr92), i19) |
| 120 |
strsetitem(p25, 0, i23) |
| 121 |
i107 = call_i(ConstClass(string_to_int), p25, 16, 1, 1, 0, 1, descr=<Calli . riiiii EF=4>) |
| 122 |
guard_no_exception(descr=...) |
| 123 |
i95 = int_add_ovf(i6, i107) |
| 124 |
guard_no_overflow(descr=...) |
| 125 |
--TICK-- |
| 126 |
jump(..., descr=...) |
| 127 |
""" % ((-sys.maxint-1,)+args)) |
| 128 |
|
| 129 |
def test_str_mod(self): |
| 130 |
def main(n): |
| 131 |
s = 0 |
| 132 |
while n > 0: |
| 133 |
s += len('%d %d' % (n, n)) |
| 134 |
n -= 1 |
| 135 |
return s |
| 136 |
|
| 137 |
log = self.run(main, [1000]) |
| 138 |
assert log.result == main(1000) |
| 139 |
loop, = log.loops_by_filename(self.filepath) |
| 140 |
assert loop.match(""" |
| 141 |
i79 = int_gt(i74, 0) |
| 142 |
guard_true(i79, descr=...) |
| 143 |
guard_not_invalidated(descr=...) |
| 144 |
p80 = call_r(ConstClass(ll_int2dec__Signed), i74, descr=<Callr . i EF=3>) |
| 145 |
guard_no_exception(descr=...) |
| 146 |
i85 = strlen(p80) |
| 147 |
p86 = new(descr=<SizeDescr .+>) |
| 148 |
p88 = newstr(23) |
| 149 |
{{{ |
| 150 |
setfield_gc(p86, 0, descr=<FieldS stringbuilder.current_pos .+>) |
| 151 |
setfield_gc(p86, p88, descr=<FieldP stringbuilder.current_buf .+>) |
| 152 |
setfield_gc(p86, 23, descr=<FieldS stringbuilder.current_end .+>) |
| 153 |
setfield_gc(p86, 23, descr=<FieldS stringbuilder.total_size .+>) |
| 154 |
}}} |
| 155 |
call_n(ConstClass(ll_append_res0__stringbuilderPtr_rpy_stringPtr), p86, p80, descr=<Callv 0 rr EF=5>) |
| 156 |
guard_no_exception(descr=...) |
| 157 |
i89 = getfield_gc_i(p86, descr=<FieldS stringbuilder.current_pos .+>) |
| 158 |
i90 = getfield_gc_i(p86, descr=<FieldS stringbuilder.current_end .+>) |
| 159 |
i91 = int_eq(i89, i90) |
| 160 |
cond_call(i91, ConstClass(ll_grow_by__stringbuilderPtr_Signed), p86, 1, descr=<Callv 0 ri EF=5>) |
| 161 |
guard_no_exception(descr=...) |
| 162 |
i92 = getfield_gc_i(p86, descr=<FieldS stringbuilder.current_pos .+>) |
| 163 |
i93 = int_add(i92, 1) |
| 164 |
p94 = getfield_gc_r(p86, descr=<FieldP stringbuilder.current_buf .+>) |
| 165 |
strsetitem(p94, i92, 32) |
| 166 |
setfield_gc(p86, i93, descr=<FieldS stringbuilder.current_pos .+>) |
| 167 |
call_n(ConstClass(ll_append_res0__stringbuilderPtr_rpy_stringPtr), p86, p80, descr=<Callv 0 rr EF=5>) |
| 168 |
guard_no_exception(descr=...) |
| 169 |
p95 = call_r(..., descr=<Callr . r EF=5>) # ll_build |
| 170 |
guard_no_exception(descr=...) |
| 171 |
i96 = call_i(ConstClass(codepoints_in_utf8), p95, 0, _, descr=<Calli . rii EF=4>) |
| 172 |
guard_no_exception(descr=...) |
| 173 |
i969 = int_lt(i96, 0) |
| 174 |
guard_false(i969, descr=...) |
| 175 |
i97 = int_add_ovf(i71, i96) |
| 176 |
guard_no_overflow(descr=...) |
| 177 |
i98 = int_sub(i74, 1) |
| 178 |
--TICK-- |
| 179 |
jump(..., descr=...) |
| 180 |
""") |
| 181 |
|
| 182 |
def test_remove_duplicate_method_calls_bytes(self): |
| 183 |
def main(n): |
| 184 |
lst = [] |
| 185 |
for i in range(n): |
| 186 |
s = b'Hello %d' % i |
| 187 |
t = s.lower() |
| 188 |
u = s.lower() |
| 189 |
lst.append(t) |
| 190 |
lst.append(u) |
| 191 |
return len(b','.join(lst)) |
| 192 |
log = self.run(main, [1000]) |
| 193 |
assert log.result == main(1000) |
| 194 |
loops = log.loops_by_filename(self.filepath) |
| 195 |
loop, = loops |
| 196 |
assert loop.match_by_id('callone', ''' |
| 197 |
p114 = call_r(ConstClass(ll_lower__rpy_stringPtr), p113, descr=<Callr . r EF=3>) |
| 198 |
guard_no_exception(descr=...) |
| 199 |
''') |
| 200 |
assert loop.match_by_id('calltwo', '') |
| 201 |
|
| 202 |
def test_remove_duplicate_method_calls_unicode(self): |
| 203 |
def main(n): |
| 204 |
lst = [] |
| 205 |
for i in range(n): |
| 206 |
s = u'H\xe4llo %d' % i |
| 207 |
t = s.lower() |
| 208 |
u = s.lower() |
| 209 |
lst.append(t) |
| 210 |
lst.append(u) |
| 211 |
return len(u','.join(lst)) |
| 212 |
log = self.run(main, [1000]) |
| 213 |
assert log.result == main(1000) |
| 214 |
loops = log.loops_by_filename(self.filepath) |
| 215 |
loop, = loops |
| 216 |
assert loop.match_by_id('callone', ''' |
| 217 |
i136 = strlen(p131) |
| 218 |
i137 = int_eq(i135, i136) |
| 219 |
guard_false(i137, descr=...) |
| 220 |
p139 = call_r(ConstClass(_lower_unicode), p131, descr=...) |
| 221 |
guard_no_exception(descr=...) |
| 222 |
guard_nonnull(p139, descr=...) |
| 223 |
''') |
| 224 |
assert loop.match_by_id('calltwo', '') |
| 225 |
|
| 226 |
@pytest.mark.xfail |
| 227 |
def test_move_method_call_out_of_loop(self): |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
def main(n): |
| 234 |
lst = [] |
| 235 |
s = 'Hello %d' % n |
| 236 |
for i in range(n): |
| 237 |
t = s.lower() |
| 238 |
lst.append(t) |
| 239 |
return len(','.join(lst)) |
| 240 |
log = self.run(main, [1000]) |
| 241 |
assert log.result == main(1000) |
| 242 |
loops = log.loops_by_filename(self.filepath) |
| 243 |
loop, = loops |
| 244 |
assert loop.match_by_id('callone', '') |
| 245 |
|
| 246 |
def test_lookup_codec(self): |
| 247 |
log = self.run(""" |
| 248 |
import codecs |
| 249 |
|
| 250 |
def main(n): |
| 251 |
for i in range(n): |
| 252 |
codecs.lookup('utf8') |
| 253 |
return i |
| 254 |
""", [1000]) |
| 255 |
loop, = log.loops_by_filename(self.filepath) |
| 256 |
assert loop.match(""" |
| 257 |
i45 = int_lt(i43, i26) |
| 258 |
guard_true(i45, descr=...) |
| 259 |
i46 = int_add(i43, 1) |
| 260 |
setfield_gc(p15, i46, descr=<FieldS pypy.module.__builtin__.functional.W_IntRangeIterator.inst_current 8>) |
| 261 |
guard_not_invalidated(descr=...) |
| 262 |
--TICK-- |
| 263 |
jump(..., descr=...) |
| 264 |
""") |
| 265 |
|
| 266 |
def test_decode_ascii(self): |
| 267 |
log = self.run(""" |
| 268 |
def main(n): |
| 269 |
for i in range(n): |
| 270 |
(b"x" * (i & 15)).decode('ascii') |
| 271 |
return i |
| 272 |
""", [1000]) |
| 273 |
loop, = log.loops_by_filename(self.filepath) |
| 274 |
assert loop.match(""" |
| 275 |
i49 = int_lt(i47, i24) |
| 276 |
guard_true(i49, descr=...) |
| 277 |
i50 = int_add(i47, 1) |
| 278 |
i53 = int_and(i47, 15) |
| 279 |
setfield_gc(p15, i50, descr=<FieldS pypy.module.__builtin__.functional.W_IntRangeIterator.inst_current 8>) |
| 280 |
i55 = int_le(i53, 0) |
| 281 |
guard_false(i55, descr=...) |
| 282 |
i56 = int_eq(i53, 1) |
| 283 |
guard_false(i56, descr=...) |
| 284 |
p80 = call_r(ConstClass(ll_char_mul__Char_Signed), 120, i53, descr=<Callr . ii EF=3>) |
| 285 |
guard_no_exception(descr=...) |
| 286 |
guard_not_invalidated(descr=...) |
| 287 |
i59 = call_i(ConstClass(first_non_ascii_char), p80, descr=<Calli . r EF=4>) |
| 288 |
guard_no_exception(descr=...) |
| 289 |
i61 = int_lt(i59, 0) |
| 290 |
guard_true(i61, descr=...) |
| 291 |
i62 = strlen(p80) |
| 292 |
--TICK-- |
| 293 |
jump(..., descr=...) |
| 294 |
""") |
| 295 |
|
| 296 |
def test_unicode_indexing_makes_no_bridges(self): |
| 297 |
log = self.run(r""" |
| 298 |
u = 'abä👨👩👧👦 ' * 1000 |
| 299 |
def main(): |
| 300 |
for j in range(10): |
| 301 |
for i in range(len(u)): |
| 302 |
u[i] # ID: index0 |
| 303 |
""", []) |
| 304 |
loop, = log.loops_by_id("index0") |
| 305 |
ops = loop.ops_by_id("index0") |
| 306 |
for op in ops: |
| 307 |
assert op.bridge is None |
| 308 |
|
| 309 |
def test_unicode_indexing_small_constant_indices(self): |
| 310 |
log = self.run(""" |
| 311 |
l = [u"abä", u"cdä", u"äü", u"éé", u"–—¿"] * 1000 |
| 312 |
def main(n): |
| 313 |
global s |
| 314 |
for u in l: |
| 315 |
s = u[0] + u[1] + u[-1] # ID: index |
| 316 |
len(u) |
| 317 |
return len(s) |
| 318 |
""", [1000]) |
| 319 |
loop, = log.loops_by_filename(self.filepath) |
| 320 |
assert loop.match_by_id('index', ''' |
| 321 |
i77 = getfield_gc_i(p73, descr=<FieldS pypy.objspace.std.unicodeobject.W_UnicodeObject.inst__length .*>) |
| 322 |
p78 = getfield_gc_r(p73, descr=<FieldP pypy.objspace.std.unicodeobject.W_UnicodeObject.inst__utf8 .* pure>) |
| 323 |
i79 = strlen(p78) |
| 324 |
i80 = int_eq(i77, i79) |
| 325 |
guard_false(i80, descr=...) # check not ascii |
| 326 |
i82 = int_ge(0, i77) |
| 327 |
guard_false(i82, descr=...) |
| 328 |
i85 = call_i(ConstClass(next_codepoint_pos_dont_look_inside), p78, 0, descr=...) |
| 329 |
i86 = int_gt(i85, i79) |
| 330 |
guard_false(i86, descr=...) |
| 331 |
i88 = int_ge(1, i77) |
| 332 |
guard_false(i88, descr=...) |
| 333 |
i90 = call_i(ConstClass(next_codepoint_pos_dont_look_inside), p78, i85, descr=...) |
| 334 |
i91 = int_gt(i90, i79) |
| 335 |
guard_false(i91, descr=...) |
| 336 |
i92 = int_sub(i90, i85) |
| 337 |
i94 = int_add(-1, i77) |
| 338 |
i96 = call_i(ConstClass(prev_codepoint_pos_dont_look_inside), p78, i79, descr=...) |
| 339 |
i97 = int_sub(i79, i96) |
| 340 |
guard_not_invalidated(descr=...) |
| 341 |
''') |
| 342 |
|
| 343 |
def test_unicode_slicing_small_constant_indices(self): |
| 344 |
log = self.run(""" |
| 345 |
def main(n): |
| 346 |
u = 'abä👨👩👧👦 ' * 1000 |
| 347 |
global s |
| 348 |
count = 0 |
| 349 |
while u: |
| 350 |
u = u[1:] # ID: index |
| 351 |
count += 1 |
| 352 |
return count |
| 353 |
""", [1000]) |
| 354 |
loop, = log.loops_by_filename(self.filepath) |
| 355 |
assert loop.match_by_id('index', ''' |
| 356 |
i51 = int_ge(1, i30) |
| 357 |
guard_false(i51, descr=...) |
| 358 |
i53 = int_sub(i30, 1) |
| 359 |
i55 = int_sub(i30, 2) |
| 360 |
i56 = strlen(p48) |
| 361 |
i57 = int_eq(i30, i56) |
| 362 |
guard_false(i57, descr=...) |
| 363 |
i60 = call_i(ConstClass(next_codepoint_pos_dont_look_inside), p48, 0, descr=...) |
| 364 |
i61 = int_sub(i56, i60) |
| 365 |
''') |
| 366 |
|
| 367 |
def test_decode_encode(self): |
| 368 |
log = self.run(r""" |
| 369 |
def main(n): |
| 370 |
global s |
| 371 |
u = b'ab\xc3\xa4\xf0\x9f\x91\xa9\xe2\x80\x8d\xf0\x9f\x91\xa9\xe2\x80\x8d\xf0\x9f\x91\xa7\xe2\x80\x8d\xf0\x9f\x91\xa6'.decode("utf-8") |
| 372 |
count = 0 |
| 373 |
for i in range(n): |
| 374 |
b = (u + str(i)).encode("utf-8") |
| 375 |
u = b.decode("utf-8") # ID: decode |
| 376 |
count += 1 |
| 377 |
return count |
| 378 |
""", [10000]) |
| 379 |
loop, = log.loops_by_filename(self.filepath) |
| 380 |
|
| 381 |
|
| 382 |
assert loop.match_by_id('decode', ''' |
| 383 |
i1 = int_ge(i0, 0) |
| 384 |
guard_true(i1, descr=...) |
| 385 |
''') |
| 386 |
|
| 387 |
def test_find(self): |
| 388 |
log = self.run(""" |
| 389 |
def main(n): |
| 390 |
global s |
| 391 |
b = b'abaaac' * 10000 |
| 392 |
count = 0 |
| 393 |
start = 0 |
| 394 |
while 1: |
| 395 |
start = b.find(b'a', start) |
| 396 |
if start < 0: |
| 397 |
break |
| 398 |
b[start] # ID: index |
| 399 |
count += 1 |
| 400 |
start += 1 |
| 401 |
return count |
| 402 |
""", [10000]) |
| 403 |
loop, = log.loops_by_filename(self.filepath) |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
assert loop.match_by_id('index', ''' |
| 408 |
i2 = strgetitem(p1, i1) |
| 409 |
''') |
| 410 |
|
| 411 |
def test_strip_doesnt_escape_bytes(self): |
| 412 |
log = self.run(""" |
| 413 |
def main(n): |
| 414 |
l = [bytes(x) for x in range(10000)] |
| 415 |
res = 0 |
| 416 |
for data in l: |
| 417 |
res += len(data.strip(b'1')) # ID: striparg |
| 418 |
for data in l: |
| 419 |
res += len(data.strip()) # ID: stripnone |
| 420 |
return res |
| 421 |
""", [10000]) |
| 422 |
_, loop1, loop2 = log.loops_by_filename(self.filepath) |
| 423 |
opnames = log.opnames(loop1.ops_by_id('striparg')) |
| 424 |
assert "new_with_vtable" not in opnames |
| 425 |
assert "call_may_force_r" not in opnames |
| 426 |
assert opnames.count("call_i") == 2 |
| 427 |
opnames = log.opnames(loop2.ops_by_id('stripnone')) |
| 428 |
assert "new_with_vtable" not in opnames |
| 429 |
assert "call_may_force_r" not in opnames |
| 430 |
assert opnames.count("call_i") == 2 |
| 431 |
|
| 432 |
def test_unicode_strip_doesnt_escape_uniobject(self): |
| 433 |
log = self.run(""" |
| 434 |
def main(n): |
| 435 |
uni = bytes(bytearray([0xc3, 0xa4])).decode("utf-8") |
| 436 |
l = [str(x) + uni + str(x) for x in range(10000)] |
| 437 |
res = 0 |
| 438 |
for data in l: |
| 439 |
res += len(data.strip(u'1')) # ID: stripnone |
| 440 |
for data in l: |
| 441 |
res += len(data.strip()) # ID: striparg |
| 442 |
return res |
| 443 |
""", [10000]) |
| 444 |
_, loop1, loop2 = log.loops_by_filename(self.filepath) |
| 445 |
opnames = log.opnames(loop1.ops_by_id('stripnone')) |
| 446 |
assert "new_with_vtable" not in opnames |
| 447 |
assert "call_may_force_r" not in opnames |
| 448 |
assert "call_r" in opnames |
| 449 |
opnames = log.opnames(loop2.ops_by_id('striparg')) |
| 450 |
assert "new_with_vtable" not in opnames |
| 451 |
assert "call_may_force_r" not in opnames |
| 452 |
assert "call_r" in opnames |
| 453 |
|
| 454 |
def test_unicode_strip_doesnt_escape_uniobject_ascii(self): |
| 455 |
log = self.run(""" |
| 456 |
def main(n): |
| 457 |
l = [str(x) for x in range(10000)] |
| 458 |
res = 0 |
| 459 |
for data in l: |
| 460 |
res += len(data.strip(u'1')) # ID: stripnone |
| 461 |
for data in l: |
| 462 |
res += len(data.strip()) # ID: striparg |
| 463 |
return res |
| 464 |
""", [10000]) |
| 465 |
_, loop1, loop2 = log.loops_by_filename(self.filepath) |
| 466 |
opnames = log.opnames(loop1.ops_by_id('stripnone')) |
| 467 |
assert "new_with_vtable" not in opnames |
| 468 |
assert "call_may_force_r" not in opnames |
| 469 |
assert "call_r" not in opnames |
| 470 |
assert opnames.count("call_i") == 2 |
| 471 |
opnames = log.opnames(loop2.ops_by_id('striparg')) |
| 472 |
assert "new_with_vtable" not in opnames |
| 473 |
assert "call_may_force_r" not in opnames |
| 474 |
assert "call_r" not in opnames |
| 475 |
assert opnames.count("call_i") == 2 |
| 476 |
|
| 477 |
def test_unicode_splitlines_doesnt_allocate_uniobject(self): |
| 478 |
log = self.run(""" |
| 479 |
def main(n): |
| 480 |
res = 0 |
| 481 |
for i in range(n): |
| 482 |
data = str(i) + "\\n" |
| 483 |
res += len(data.splitlines()) # ID: splitlines |
| 484 |
return res |
| 485 |
""", [10000]) |
| 486 |
loop, = log.loops_by_filename(self.filepath) |
| 487 |
opnames = log.opnames(loop.ops_by_id('splitlines')) |
| 488 |
assert "new_with_vtable" not in opnames |
| 489 |
|
| 490 |
def test_textiowrapper_write_is_inlined(self): |
| 491 |
log = self.run(""" |
| 492 |
def main(n): |
| 493 |
import io |
| 494 |
tw = io.TextIOWrapper(io.BytesIO()) |
| 495 |
for i in range(n): |
| 496 |
tw.write('a') |
| 497 |
""", [10000]) |
| 498 |
loop1 = log.loops_by_filename(self.filepath) |
| 499 |
|
| 500 |
assert any(c.code and c.code.name == 'encode' for c in loop1[0].chunks) |