Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
f50354ad
Commit
f50354ad
authored
Apr 11, 2017
by
Serhiy Storchaka
Committed by
GitHub
Apr 11, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reimplement tempfile._RandomNameSequence using a generator function. (#1075)
parent
e8a6bb4f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
25 deletions
+15
-25
Lib/tempfile.py
Lib/tempfile.py
+12
-23
Lib/test/test_tempfile.py
Lib/test/test_tempfile.py
+3
-2
No files found.
Lib/tempfile.py
View file @
f50354ad
...
...
@@ -133,32 +133,21 @@ def _sanitize_params(prefix, suffix, dir):
return
prefix
,
suffix
,
dir
,
output_type
class
_RandomNameSequence
:
"""An instance of _RandomNameSequence generates an endless
sequence of unpredictable strings which can safely be incorporated
into file names. Each string is six characters long. Multiple
threads can safely use the same instance at the same time.
_RandomNameSequence is an iterator."""
def
_RandomNameSequence
():
"""Generate an endless sequence of unpredictable strings which
can safely be incorporated into file names. Each string is 8
characters long. Multiple threads and forked processes can
safely use the same instance at the same time."""
characters
=
"abcdefghijklmnopqrstuvwxyz0123456789_"
@
property
def
rng
(
self
):
rng_pid
=
None
while
True
:
cur_pid
=
_os
.
getpid
()
if
cur_pid
!=
getattr
(
self
,
'_rng_pid'
,
None
):
self
.
_rng
=
_Random
()
self
.
_rng_pid
=
cur_pid
return
self
.
_rng
def
__iter__
(
self
):
return
self
def
__next__
(
self
):
c
=
self
.
characters
choose
=
self
.
rng
.
choice
letters
=
[
choose
(
c
)
for
dummy
in
range
(
8
)]
return
''
.
join
(
letters
)
if
cur_pid
!=
rng_pid
:
choose
=
_Random
().
choice
rng_pid
=
cur_pid
letters
=
[
choose
(
characters
)
for
dummy
in
range
(
8
)]
yield
''
.
join
(
letters
)
def
_candidate_tempdir_list
():
"""Generate a list of candidate temporary directories which
...
...
Lib/test/test_tempfile.py
View file @
f50354ad
# tempfile.py unit tests.
import
collections.abc
import
tempfile
import
errno
import
io
...
...
@@ -290,9 +291,9 @@ class TestGetCandidateNames(BaseTestCase):
"""Test the internal function _get_candidate_names."""
def
test_retval
(
self
):
# _get_candidate_names returns a
_RandomNameSequence object
# _get_candidate_names returns a
n iterator
obj
=
tempfile
.
_get_candidate_names
()
self
.
assertIsInstance
(
obj
,
tempfile
.
_RandomNameSequence
)
self
.
assertIsInstance
(
obj
,
collections
.
abc
.
Iterator
)
def
test_same_thing
(
self
):
# _get_candidate_names always returns the same object
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment