Skip to content

Commit

Permalink
pythongh-108963: using random to generate unique string in sys.intern…
Browse files Browse the repository at this point in the history
… test (python#109491)
  • Loading branch information
aisk authored and Glyphack committed Jan 27, 2024
1 parent b4e318f commit 34f3941
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import locale
import operator
import os
import random
import struct
import subprocess
import sys
Expand All @@ -30,10 +31,6 @@ def requires_subinterpreters(meth):
'subinterpreters required')(meth)


# count the number of test runs, used to create unique
# strings to intern in test_intern()
INTERN_NUMRUNS = 0

DICT_KEY_STRUCT_FORMAT = 'n2BI2n'

class DisplayHookTest(unittest.TestCase):
Expand Down Expand Up @@ -696,10 +693,8 @@ def test_43581(self):
self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)

def test_intern(self):
global INTERN_NUMRUNS
INTERN_NUMRUNS += 1
self.assertRaises(TypeError, sys.intern)
s = "never interned before" + str(INTERN_NUMRUNS)
s = "never interned before" + str(random.randrange(0, 10**9))
self.assertTrue(sys.intern(s) is s)
s2 = s.swapcase().swapcase()
self.assertTrue(sys.intern(s2) is s)
Expand All @@ -717,9 +712,7 @@ def __hash__(self):

@requires_subinterpreters
def test_subinterp_intern_dynamically_allocated(self):
global INTERN_NUMRUNS
INTERN_NUMRUNS += 1
s = "never interned before" + str(INTERN_NUMRUNS)
s = "never interned before" + str(random.randrange(0, 10**9))
t = sys.intern(s)
self.assertIs(t, s)

Expand Down

0 comments on commit 34f3941

Please sign in to comment.