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

Skip to content

Commit 9786910

Browse files
committed
Close #12015: The tempfile module now uses a suffix of 8 random characters
instead of 6, to reduce the risk of filename collision. The entropy was reduced when uppercase letters were removed from the charset used to generate random characters.
1 parent 0c7907d commit 9786910

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

Lib/tempfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __iter__(self):
125125
def __next__(self):
126126
c = self.characters
127127
choose = self.rng.choice
128-
letters = [choose(c) for dummy in "123456"]
128+
letters = [choose(c) for dummy in range(8)]
129129
return ''.join(letters)
130130

131131
def _candidate_tempdir_list():

Lib/test/test_tempfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# Common functionality.
3636
class BaseTestCase(unittest.TestCase):
3737

38-
str_check = re.compile(r"[a-zA-Z0-9_-]{6}$")
38+
str_check = re.compile(r"^[a-z0-9_-]{8}$")
3939

4040
def setUp(self):
4141
self._warnings_manager = support.check_warnings()
@@ -62,7 +62,7 @@ def nameCheck(self, name, dir, pre, suf):
6262

6363
nbase = nbase[len(pre):len(nbase)-len(suf)]
6464
self.assertTrue(self.str_check.match(nbase),
65-
"random string '%s' does not match /^[a-zA-Z0-9_-]{6}$/"
65+
"random string '%s' does not match ^[a-z0-9_-]{8}$"
6666
% nbase)
6767

6868

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ Core and Builtins
2828
Library
2929
-------
3030

31+
- Issue #12015: The tempfile module now uses a suffix of 8 random characters
32+
instead of 6, to reduce the risk of filename collision. The entropy was
33+
reduced when uppercase letters were removed from the charset used to generate
34+
random characters.
35+
3136
- Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a
3237
piece of text to a given length.
3338

0 commit comments

Comments
 (0)