@@ -2275,6 +2275,123 @@ def __str__(self):
2275
2275
self .assertEqual ("%s" % s , '__str__ overridden' )
2276
2276
self .assertEqual ("{}" .format (s ), '__str__ overridden' )
2277
2277
2278
+ def test_subclass_add (self ):
2279
+ class S (str ):
2280
+ def __add__ (self , o ):
2281
+ return "3"
2282
+ self .assertEqual (S ("4" ) + S ("5" ), "3" )
2283
+ class S (str ):
2284
+ def __iadd__ (self , o ):
2285
+ return "3"
2286
+ s = S ("1" )
2287
+ s += "4"
2288
+ self .assertEqual (s , "3" )
2289
+
2290
+ def test_getnewargs (self ):
2291
+ text = 'abc'
2292
+ args = text .__getnewargs__ ()
2293
+ self .assertIsNot (args [0 ], text )
2294
+ self .assertEqual (args [0 ], text )
2295
+ self .assertEqual (len (args ), 1 )
2296
+
2297
+ def test_resize (self ):
2298
+ for length in range (1 , 100 , 7 ):
2299
+ # generate a fresh string (refcount=1)
2300
+ text = 'a' * length + 'b'
2301
+
2302
+ with support .check_warnings (('unicode_internal codec has been '
2303
+ 'deprecated' , DeprecationWarning )):
2304
+ # fill wstr internal field
2305
+ abc = text .encode ('unicode_internal' )
2306
+ self .assertEqual (abc .decode ('unicode_internal' ), text )
2307
+
2308
+ # resize text: wstr field must be cleared and then recomputed
2309
+ text += 'c'
2310
+ abcdef = text .encode ('unicode_internal' )
2311
+ self .assertNotEqual (abc , abcdef )
2312
+ self .assertEqual (abcdef .decode ('unicode_internal' ), text )
2313
+
2314
+ def test_compare (self ):
2315
+ # Issue #17615
2316
+ N = 10
2317
+ ascii = 'a' * N
2318
+ ascii2 = 'z' * N
2319
+ latin = '\x80 ' * N
2320
+ latin2 = '\xff ' * N
2321
+ bmp = '\u0100 ' * N
2322
+ bmp2 = '\uffff ' * N
2323
+ astral = '\U00100000 ' * N
2324
+ astral2 = '\U0010ffff ' * N
2325
+ strings = (
2326
+ ascii , ascii2 ,
2327
+ latin , latin2 ,
2328
+ bmp , bmp2 ,
2329
+ astral , astral2 )
2330
+ for text1 , text2 in itertools .combinations (strings , 2 ):
2331
+ equal = (text1 is text2 )
2332
+ self .assertEqual (text1 == text2 , equal )
2333
+ self .assertEqual (text1 != text2 , not equal )
2334
+
2335
+ if equal :
2336
+ self .assertTrue (text1 <= text2 )
2337
+ self .assertTrue (text1 >= text2 )
2338
+
2339
+ # text1 is text2: duplicate strings to skip the "str1 == str2"
2340
+ # optimization in unicode_compare_eq() and really compare
2341
+ # character per character
2342
+ copy1 = duplicate_string (text1 )
2343
+ copy2 = duplicate_string (text2 )
2344
+ self .assertIsNot (copy1 , copy2 )
2345
+
2346
+ self .assertTrue (copy1 == copy2 )
2347
+ self .assertFalse (copy1 != copy2 )
2348
+
2349
+ self .assertTrue (copy1 <= copy2 )
2350
+ self .assertTrue (copy2 >= copy2 )
2351
+
2352
+ self .assertTrue (ascii < ascii2 )
2353
+ self .assertTrue (ascii < latin )
2354
+ self .assertTrue (ascii < bmp )
2355
+ self .assertTrue (ascii < astral )
2356
+ self .assertFalse (ascii >= ascii2 )
2357
+ self .assertFalse (ascii >= latin )
2358
+ self .assertFalse (ascii >= bmp )
2359
+ self .assertFalse (ascii >= astral )
2360
+
2361
+ self .assertFalse (latin < ascii )
2362
+ self .assertTrue (latin < latin2 )
2363
+ self .assertTrue (latin < bmp )
2364
+ self .assertTrue (latin < astral )
2365
+ self .assertTrue (latin >= ascii )
2366
+ self .assertFalse (latin >= latin2 )
2367
+ self .assertFalse (latin >= bmp )
2368
+ self .assertFalse (latin >= astral )
2369
+
2370
+ self .assertFalse (bmp < ascii )
2371
+ self .assertFalse (bmp < latin )
2372
+ self .assertTrue (bmp < bmp2 )
2373
+ self .assertTrue (bmp < astral )
2374
+ self .assertTrue (bmp >= ascii )
2375
+ self .assertTrue (bmp >= latin )
2376
+ self .assertFalse (bmp >= bmp2 )
2377
+ self .assertFalse (bmp >= astral )
2378
+
2379
+ self .assertFalse (astral < ascii )
2380
+ self .assertFalse (astral < latin )
2381
+ self .assertFalse (astral < bmp2 )
2382
+ self .assertTrue (astral < astral2 )
2383
+ self .assertTrue (astral >= ascii )
2384
+ self .assertTrue (astral >= latin )
2385
+ self .assertTrue (astral >= bmp2 )
2386
+ self .assertFalse (astral >= astral2 )
2387
+
2388
+ def test_free_after_iterating (self ):
2389
+ support .check_free_after_iterating (self , iter , str )
2390
+ support .check_free_after_iterating (self , reversed , str )
2391
+
2392
+
2393
+ class CAPITest (unittest .TestCase ):
2394
+
2278
2395
# Test PyUnicode_FromFormat()
2279
2396
def test_from_format (self ):
2280
2397
support .import_module ('ctypes' )
@@ -2570,18 +2687,6 @@ def test_aswidecharstring(self):
2570
2687
self .assertEqual (size , nchar )
2571
2688
self .assertEqual (wchar , nonbmp + '\0 ' )
2572
2689
2573
- def test_subclass_add (self ):
2574
- class S (str ):
2575
- def __add__ (self , o ):
2576
- return "3"
2577
- self .assertEqual (S ("4" ) + S ("5" ), "3" )
2578
- class S (str ):
2579
- def __iadd__ (self , o ):
2580
- return "3"
2581
- s = S ("1" )
2582
- s += "4"
2583
- self .assertEqual (s , "3" )
2584
-
2585
2690
@support .cpython_only
2586
2691
def test_encode_decimal (self ):
2587
2692
from _testcapi import unicode_encodedecimal
@@ -2610,104 +2715,6 @@ def test_transform_decimal(self):
2610
2715
self .assertEqual (transform_decimal ('123\u20ac ' ),
2611
2716
'123\u20ac ' )
2612
2717
2613
- def test_getnewargs (self ):
2614
- text = 'abc'
2615
- args = text .__getnewargs__ ()
2616
- self .assertIsNot (args [0 ], text )
2617
- self .assertEqual (args [0 ], text )
2618
- self .assertEqual (len (args ), 1 )
2619
-
2620
- def test_resize (self ):
2621
- for length in range (1 , 100 , 7 ):
2622
- # generate a fresh string (refcount=1)
2623
- text = 'a' * length + 'b'
2624
-
2625
- with support .check_warnings (('unicode_internal codec has been '
2626
- 'deprecated' , DeprecationWarning )):
2627
- # fill wstr internal field
2628
- abc = text .encode ('unicode_internal' )
2629
- self .assertEqual (abc .decode ('unicode_internal' ), text )
2630
-
2631
- # resize text: wstr field must be cleared and then recomputed
2632
- text += 'c'
2633
- abcdef = text .encode ('unicode_internal' )
2634
- self .assertNotEqual (abc , abcdef )
2635
- self .assertEqual (abcdef .decode ('unicode_internal' ), text )
2636
-
2637
- def test_compare (self ):
2638
- # Issue #17615
2639
- N = 10
2640
- ascii = 'a' * N
2641
- ascii2 = 'z' * N
2642
- latin = '\x80 ' * N
2643
- latin2 = '\xff ' * N
2644
- bmp = '\u0100 ' * N
2645
- bmp2 = '\uffff ' * N
2646
- astral = '\U00100000 ' * N
2647
- astral2 = '\U0010ffff ' * N
2648
- strings = (
2649
- ascii , ascii2 ,
2650
- latin , latin2 ,
2651
- bmp , bmp2 ,
2652
- astral , astral2 )
2653
- for text1 , text2 in itertools .combinations (strings , 2 ):
2654
- equal = (text1 is text2 )
2655
- self .assertEqual (text1 == text2 , equal )
2656
- self .assertEqual (text1 != text2 , not equal )
2657
-
2658
- if equal :
2659
- self .assertTrue (text1 <= text2 )
2660
- self .assertTrue (text1 >= text2 )
2661
-
2662
- # text1 is text2: duplicate strings to skip the "str1 == str2"
2663
- # optimization in unicode_compare_eq() and really compare
2664
- # character per character
2665
- copy1 = duplicate_string (text1 )
2666
- copy2 = duplicate_string (text2 )
2667
- self .assertIsNot (copy1 , copy2 )
2668
-
2669
- self .assertTrue (copy1 == copy2 )
2670
- self .assertFalse (copy1 != copy2 )
2671
-
2672
- self .assertTrue (copy1 <= copy2 )
2673
- self .assertTrue (copy2 >= copy2 )
2674
-
2675
- self .assertTrue (ascii < ascii2 )
2676
- self .assertTrue (ascii < latin )
2677
- self .assertTrue (ascii < bmp )
2678
- self .assertTrue (ascii < astral )
2679
- self .assertFalse (ascii >= ascii2 )
2680
- self .assertFalse (ascii >= latin )
2681
- self .assertFalse (ascii >= bmp )
2682
- self .assertFalse (ascii >= astral )
2683
-
2684
- self .assertFalse (latin < ascii )
2685
- self .assertTrue (latin < latin2 )
2686
- self .assertTrue (latin < bmp )
2687
- self .assertTrue (latin < astral )
2688
- self .assertTrue (latin >= ascii )
2689
- self .assertFalse (latin >= latin2 )
2690
- self .assertFalse (latin >= bmp )
2691
- self .assertFalse (latin >= astral )
2692
-
2693
- self .assertFalse (bmp < ascii )
2694
- self .assertFalse (bmp < latin )
2695
- self .assertTrue (bmp < bmp2 )
2696
- self .assertTrue (bmp < astral )
2697
- self .assertTrue (bmp >= ascii )
2698
- self .assertTrue (bmp >= latin )
2699
- self .assertFalse (bmp >= bmp2 )
2700
- self .assertFalse (bmp >= astral )
2701
-
2702
- self .assertFalse (astral < ascii )
2703
- self .assertFalse (astral < latin )
2704
- self .assertFalse (astral < bmp2 )
2705
- self .assertTrue (astral < astral2 )
2706
- self .assertTrue (astral >= ascii )
2707
- self .assertTrue (astral >= latin )
2708
- self .assertTrue (astral >= bmp2 )
2709
- self .assertFalse (astral >= astral2 )
2710
-
2711
2718
@support .cpython_only
2712
2719
def test_pep393_utf8_caching_bug (self ):
2713
2720
# Issue #25709: Problem with string concatenation and utf-8 cache
@@ -2725,10 +2732,6 @@ def test_pep393_utf8_caching_bug(self):
2725
2732
# Check that the second call returns the same result
2726
2733
self .assertEqual (getargs_s_hash (s ), chr (k ).encode () * (i + 1 ))
2727
2734
2728
- def test_free_after_iterating (self ):
2729
- support .check_free_after_iterating (self , iter , str )
2730
- support .check_free_after_iterating (self , reversed , str )
2731
-
2732
2735
2733
2736
class StringModuleTest (unittest .TestCase ):
2734
2737
def test_formatter_parser (self ):
0 commit comments