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
56eccc75
Commit
56eccc75
authored
5 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
enabled `SelfClosingGenerator` to accept generator-generating functions as well
parent
cccda1d4
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
+3
-0
3 additions, 0 deletions
CHANGELOG.md
satella/__init__.py
+1
-1
1 addition, 1 deletion
satella/__init__.py
satella/coding/iterators.py
+8
-0
8 additions, 0 deletions
satella/coding/iterators.py
tests/test_coding/test_iterators.py
+16
-2
16 additions, 2 deletions
tests/test_coding/test_iterators.py
with
28 additions
and
3 deletions
CHANGELOG.md
+
3
−
0
View file @
56eccc75
# v2.7.34
*
enabled
`SelfClosingGenerator`
to accept generator-generating functions as well
This diff is collapsed.
Click to expand it.
satella/__init__.py
+
1
−
1
View file @
56eccc75
__version__
=
'
2.7.34
_a1
'
__version__
=
'
2.7.34
'
This diff is collapsed.
Click to expand it.
satella/coding/iterators.py
+
8
−
0
View file @
56eccc75
...
...
@@ -11,6 +11,8 @@ class SelfClosingGenerator:
This will allow generators to complete that don
'
t provide a .close() method.
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.
"""
__slots__
=
(
'
generator
'
,
'
stopped
'
)
...
...
@@ -21,6 +23,12 @@ class SelfClosingGenerator:
def
__iter__
(
self
):
return
self
.
generator
def
__call__
(
self
,
*
args
,
**
kwargs
):
return
SelfClosingGenerator
(
self
.
generator
(
*
args
,
**
kwargs
))
def
send
(
self
,
obj
:
tp
.
Any
):
self
.
generator
.
send
(
obj
)
def
__next__
(
self
):
try
:
return
next
(
self
.
generator
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_coding/test_iterators.py
+
16
−
2
View file @
56eccc75
...
...
@@ -13,9 +13,8 @@ class TestIterators(unittest.TestCase):
g
=
hint_with_length
(
generator
(),
1000
)
self
.
assertEqual
(
g
.
__length_hint__
(),
1000
)
@unittest.skipUnless
(
sys
.
implementation
.
name
==
'
cpython
'
,
'
Not CPython
'
)
@unittest.skipUnless
(
sys
.
implementation
.
name
==
'
cpython
'
,
'
Not CPython
, this needs deterministic GC
'
)
def
test_self_closing_generator
(
self
):
a
=
{
'
done
'
:
False
}
def
generator
():
...
...
@@ -28,3 +27,18 @@ class TestIterators(unittest.TestCase):
break
self
.
assertTrue
(
a
[
'
done
'
])
@unittest.skipUnless
(
sys
.
implementation
.
name
==
'
cpython
'
,
'
Not CPython, this needs deterministic GC
'
)
def
test_self_closing_generator_function
(
self
):
a
=
{
'
done
'
:
False
}
def
generator
():
for
i
in
range
(
5
):
yield
i
a
[
'
done
'
]
=
True
for
i
in
SelfClosingGenerator
(
generator
)():
if
i
==
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