Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
d2730841
Commit
d2730841
authored
5 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
add context manager to SelfClosingGenerator
parent
4ca1fc47
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+1
-0
1 addition, 0 deletions
CHANGELOG.md
satella/__init__.py
+1
-1
1 addition, 1 deletion
satella/__init__.py
satella/coding/iterators.py
+9
-0
9 additions, 0 deletions
satella/coding/iterators.py
tests/test_coding/test_iterators.py
+15
-0
15 additions, 0 deletions
tests/test_coding/test_iterators.py
with
26 additions
and
1 deletion
CHANGELOG.md
+
1
−
0
View file @
d2730841
# v2.7.38
*
added context manager to SelfClosingGenerator
This diff is collapsed.
Click to expand it.
satella/__init__.py
+
1
−
1
View file @
d2730841
__version__
=
'
2.7.38_a
1
'
__version__
=
'
2.7.38_a
2
'
This diff is collapsed.
Click to expand it.
satella/coding/iterators.py
+
9
−
0
View file @
d2730841
...
...
@@ -23,6 +23,8 @@ class SelfClosingGenerator:
This will additionally exhaust the generator upon deallocation of the generator.
You can feed it with either generators, or generator-functions, it will behave correctly each time.
You can also use it as a context manager, to decouple finalizing the generator from the GC collection:
"""
__slots__
=
(
'
generator
'
,
'
stopped
'
)
...
...
@@ -46,6 +48,13 @@ class SelfClosingGenerator:
self
.
stopped
=
True
raise
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
close
()
return
False
def
close
(
self
):
if
not
self
.
stopped
:
try
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_coding/test_iterators.py
+
15
−
0
View file @
d2730841
...
...
@@ -26,3 +26,18 @@ class TestIterators(unittest.TestCase):
break
self
.
assertTrue
(
a
[
'
done
'
])
def
test_self_closing_generator_function
(
self
):
a
=
{
'
done
'
:
False
}
def
generator
():
for
i
in
range
(
5
):
yield
i
a
[
'
done
'
]
=
True
with
SelfClosingGenerator
(
generator
)()
as
gen
:
for
b
in
gen
:
if
b
==
2
:
break
self
.
assertTrue
(
a
[
'
done
'
])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment