@@ -52,12 +52,11 @@ def __hash__(self):
52
52
self .gen .seed (arg )
53
53
54
54
for arg in [1 + 2j , tuple ('abc' ), MySeed ()]:
55
- with self .assertWarns ( DeprecationWarning ):
55
+ with self .assertRaises ( TypeError ):
56
56
self .gen .seed (arg )
57
57
58
58
for arg in [list (range (3 )), dict (one = 1 )]:
59
- with self .assertWarns (DeprecationWarning ):
60
- self .assertRaises (TypeError , self .gen .seed , arg )
59
+ self .assertRaises (TypeError , self .gen .seed , arg )
61
60
self .assertRaises (TypeError , self .gen .seed , 1 , 2 , 3 , 4 )
62
61
self .assertRaises (TypeError , type (self .gen ), [])
63
62
@@ -110,22 +109,28 @@ def test_shuffle(self):
110
109
self .assertTrue (lst != shuffled_lst )
111
110
self .assertRaises (TypeError , shuffle , (1 , 2 , 3 ))
112
111
113
- def test_shuffle_random_argument (self ):
114
- # Test random argument to shuffle.
115
- shuffle = self .gen .shuffle
116
- mock_random = unittest .mock .Mock (return_value = 0.5 )
117
- seq = bytearray (b'abcdefghijk' )
118
- with self .assertWarns (DeprecationWarning ):
119
- shuffle (seq , mock_random )
120
- mock_random .assert_called_with ()
121
-
122
112
def test_choice (self ):
123
113
choice = self .gen .choice
124
114
with self .assertRaises (IndexError ):
125
115
choice ([])
126
116
self .assertEqual (choice ([50 ]), 50 )
127
117
self .assertIn (choice ([25 , 75 ]), [25 , 75 ])
128
118
119
+ def test_choice_with_numpy (self ):
120
+ # Accommodation for NumPy arrays which have disabled __bool__().
121
+ # See: https://github.com/python/cpython/issues/100805
122
+ choice = self .gen .choice
123
+
124
+ class NA (list ):
125
+ "Simulate numpy.array() behavior"
126
+ def __bool__ (self ):
127
+ raise RuntimeError
128
+
129
+ with self .assertRaises (IndexError ):
130
+ choice (NA ([]))
131
+ self .assertEqual (choice (NA ([50 ])), 50 )
132
+ self .assertIn (choice (NA ([25 , 75 ])), [25 , 75 ])
133
+
129
134
def test_sample (self ):
130
135
# For the entire allowable range of 0 <= k <= N, validate that
131
136
# the sample is of the correct length and contains only unique items
@@ -169,7 +174,7 @@ def test_sample_on_dicts(self):
169
174
self .assertRaises (TypeError , self .gen .sample , dict .fromkeys ('abcdef' ), 2 )
170
175
171
176
def test_sample_on_sets (self ):
172
- with self .assertWarns ( DeprecationWarning ):
177
+ with self .assertRaises ( TypeError ):
173
178
population = {10 , 20 , 30 , 40 , 50 , 60 , 70 }
174
179
self .gen .sample (population , k = 5 )
175
180
@@ -391,23 +396,6 @@ def test_pickling(self):
391
396
restoredseq = [newgen .random () for i in range (10 )]
392
397
self .assertEqual (origseq , restoredseq )
393
398
394
- @test .support .cpython_only
395
- def test_bug_41052 (self ):
396
- # _random.Random should not be allowed to serialization
397
- import _random
398
- for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
399
- r = _random .Random ()
400
- self .assertRaises (TypeError , pickle .dumps , r , proto )
401
-
402
- @test .support .cpython_only
403
- def test_bug_42008 (self ):
404
- # _random.Random should call seed with first element of arg tuple
405
- import _random
406
- r1 = _random .Random ()
407
- r1 .seed (8675309 )
408
- r2 = _random .Random (8675309 )
409
- self .assertEqual (r1 .random (), r2 .random ())
410
-
411
399
# TODO: RUSTPYTHON AttributeError: 'super' object has no attribute 'getstate'
412
400
@unittest .expectedFailure
413
401
def test_bug_1727780 (self ):
@@ -445,6 +433,10 @@ def test_randbytes(self):
445
433
self .assertRaises (ValueError , self .gen .randbytes , - 1 )
446
434
self .assertRaises (TypeError , self .gen .randbytes , 1.0 )
447
435
436
+ def test_mu_sigma_default_args (self ):
437
+ self .assertIsInstance (self .gen .normalvariate (), float )
438
+ self .assertIsInstance (self .gen .gauss (), float )
439
+
448
440
449
441
try :
450
442
random .SystemRandom ().random ()
@@ -592,6 +584,25 @@ def test_randbelow_logic(self, _log=log, int=int):
592
584
self .assertTrue (2 ** k > n > 2 ** (k - 1 )) # note the stronger assertion
593
585
594
586
587
+ class TestRawMersenneTwister (unittest .TestCase ):
588
+ @test .support .cpython_only
589
+ def test_bug_41052 (self ):
590
+ # _random.Random should not be allowed to serialization
591
+ import _random
592
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
593
+ r = _random .Random ()
594
+ self .assertRaises (TypeError , pickle .dumps , r , proto )
595
+
596
+ @test .support .cpython_only
597
+ def test_bug_42008 (self ):
598
+ # _random.Random should call seed with first element of arg tuple
599
+ import _random
600
+ r1 = _random .Random ()
601
+ r1 .seed (8675309 )
602
+ r2 = _random .Random (8675309 )
603
+ self .assertEqual (r1 .random (), r2 .random ())
604
+
605
+
595
606
class MersenneTwister_TestBasicOps (TestBasicOps , unittest .TestCase ):
596
607
gen = random .Random ()
597
608
@@ -846,10 +857,6 @@ def test_randbelow_without_getrandbits(self):
846
857
maxsize + 1 , maxsize = maxsize
847
858
)
848
859
self .gen ._randbelow_without_getrandbits (5640 , maxsize = maxsize )
849
- # issue 33203: test that _randbelow returns zero on
850
- # n == 0 also in its getrandbits-independent branch.
851
- x = self .gen ._randbelow_without_getrandbits (0 , maxsize = maxsize )
852
- self .assertEqual (x , 0 )
853
860
854
861
# This might be going too far to test a single line, but because of our
855
862
# noble aim of achieving 100% test coverage we need to write a case in
@@ -1331,7 +1338,7 @@ def test__all__(self):
1331
1338
# tests validity but not completeness of the __all__ list
1332
1339
self .assertTrue (set (random .__all__ ) <= set (dir (random )))
1333
1340
1334
- @unittest . skipUnless ( hasattr ( os , "fork" ), "fork() required" )
1341
+ @test . support . requires_fork ( )
1335
1342
def test_after_fork (self ):
1336
1343
# Test the global Random instance gets reseeded in child
1337
1344
r , w = os .pipe ()
0 commit comments