Skip to content

Instantly share code, notes, and snippets.

@formix
Last active October 12, 2023 16:09
Show Gist options
  • Save formix/c8f7dd1b7666230c51d0cd4ff7ea9e10 to your computer and use it in GitHub Desktop.
Save formix/c8f7dd1b7666230c51d0cd4ff7ea9e10 to your computer and use it in GitHub Desktop.
Experiment with python3 nested file streams to test if closing the tip stream will actually close the internal and the root streams.
"""
Experiment with python3 nested file streams to test if closing the tip stream will actually close
the internal and the root streams.
"""
import io
import gzip
FILE_PATH = "experiments/data/nested_stream_close_experiment.txt.gz"
print("# EXPERIMENT: Check if the tip stream closes the internal and the root streams.")
# pylint: disable=locally-disabled, consider-using-with
root_stream = open(FILE_PATH, "rb")
internal_stream = gzip.GzipFile(fileobj = root_stream, mode = "r")
tip_stream = io.TextIOWrapper(internal_stream)
for _ in tip_stream:
pass
tip_stream.close()
if internal_stream.closed:
print("# RESULT: Internal stream closed")
else:
print("# RESULT: Internal stream not closed")
internal_stream.close()
if root_stream.closed:
print("# RESULT: Root stream closed")
else:
print("# RESULT: Root stream not closed")
root_stream.close()
### Output ###
# EXPERIMENT: Check if the tip stream closes the internal and the root streams.
# RESULT: Internal stream closed
# RESULT: Root stream not closed
### Conclusion ###
# After some research, passing in a fileobj to a GzipFile is documented to not close the internal
# stream since the internal stream could be used for other purpose later in the code. Thus when
# an intermediary GzipFile is involved, the referenced fileobj should be explicitly closed or
# inside a "with" statement.
#
# See https://docs.python.org/3/library/gzip.html for details.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment