Skip to content

Commit c001c36

Browse files
committed
add test: sink_to_textfile closes file when deleted
update docs
1 parent 31018a7 commit c001c36

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

docs/source/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Stream
2626
rate_limit
2727
scatter
2828
sink
29+
sink_to_textfile
2930
slice
3031
sliding_window
3132
starmap
@@ -84,6 +85,7 @@ Definitions
8485
.. autofunction:: partition
8586
.. autofunction:: rate_limit
8687
.. autofunction:: sink
88+
.. autofunction:: sink_to_textfile
8789
.. autofunction:: sliding_window
8890
.. autofunction:: Stream
8991
.. autofunction:: timed_window

docs/source/plugins.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Different kinds of add-ons go into different entry point groups:
6060
=========== ======================= =====================
6161
Source ``streamz.Source`` ``streamz.sources``
6262
Node ``streamz.Stream`` ``streamz.nodes``
63-
Sink ``streamz.Stream`` ``streamz.sinks``
63+
Sink ``streamz.Sink`` ``streamz.sinks``
6464
=========== ======================= =====================
6565

6666

streamz/sinks.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ def update(self, x, who=None, metadata=None):
5555

5656
@Stream.register_api()
5757
class sink_to_textfile(Sink):
58-
""" Write elements to a plain text file, one element per line. Type of elements
59-
must be ``str``.
58+
""" Write elements to a plain text file, one element per line.
59+
60+
Type of elements must be ``str``.
6061
6162
Arguments
6263
---------
6364
file: str or file-like
6465
File to write the elements to. ``str`` is treated as a file name to open.
65-
If file-like, descriptor must be open in text mode.
66-
Note that the file descriptor will be closed when sink is destroyed.
66+
If file-like, descriptor must be open in text mode. Note that the file
67+
descriptor will be closed when this sink is destroyed.
6768
end: str, optional
6869
This value will be written to the file after each element.
6970
Defaults to newline character.
@@ -74,7 +75,7 @@ class sink_to_textfile(Sink):
7475
Examples
7576
--------
7677
>>> source = Stream()
77-
>>> source.map(str).sink_to_file("test.txt")
78+
>>> source.map(str).sink_to_textfile("test.txt")
7879
>>> source.emit(0)
7980
>>> source.emit(1)
8081
>>> print(open("test.txt", "r").read())

streamz/tests/test_sinks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from time import sleep
22

3+
import pytest
34
from streamz import Stream
5+
from streamz.sinks import _global_sinks
46
from streamz.utils_test import tmpfile
57

68

@@ -43,3 +45,15 @@ def test_sink_to_textfile_named():
4345
sleep(0.01)
4446

4547
assert open(filename, "r").read() == "0\n1\n"
48+
49+
50+
def test_sink_to_textfile_closes():
51+
source = Stream()
52+
with tmpfile() as filename:
53+
sink = source.sink_to_textfile(filename)
54+
fp = sink._fp
55+
_global_sinks.remove(sink)
56+
del sink
57+
58+
with pytest.raises(ValueError): # I/O operation on closed file
59+
fp.write(".")

0 commit comments

Comments
 (0)