Python: Require __enter__ and __exit__ on {Input,Output}Stream#5436
Python: Require __enter__ and __exit__ on {Input,Output}Stream#5436rdblue merged 1 commit intoapache:masterfrom
__enter__ and __exit__ on {Input,Output}Stream#5436Conversation
|
I don't think that we want to do this. We discussed this when introducing the FIleIO API, but
I'm all for adding file = io.newInputFile("s3://bucket/path.parquet")
with file.create(overwrite=True) as f:
f.write(...)
with file.toInputFile().open() as f:
f.read(...)I also think that the |
|
@rdblue I also thought about that, but the Update: S3FS also has the |
|
I don't think that we should add Can we get around the implementations not supporting |
|
Let me take a stab at adding them to the streams, I think that will work quite well 👍🏻 |
This way we always properly close the streams
782ae22 to
320d97c
Compare
| TableMetadata: A table metadata instance | ||
|
|
||
| """ | ||
| return FromByteStream.table_metadata(byte_stream=input_file.open(), encoding=encoding) |
There was a problem hiding this comment.
This one caught my eye this morning; it wasn't properly closed
|
@rdblue Looks much cleaner, I'm confident that this also will work with future FileIO implementations. |
__enter__ and __exit__ on {Input,Output}Stream
| """ | ||
| return FromByteStream.table_metadata(byte_stream=input_file.open(), encoding=encoding) | ||
| with input_file.open() as input_stream: | ||
| return FromByteStream.table_metadata(byte_stream=input_stream, encoding=encoding) |
There was a problem hiding this comment.
Do we support compressed streams? I think we should make sure we can read and write gzipped metadata files.
|
Looks like PyArrow already implements this: https://github.com/apache/arrow/blob/31494860c23ff7fe38a748a09c58822378605477/python/pyarrow/io.pxi#L121-L122 I'll merge. Thanks, @Fokko! |
This way we can nicely use context managers:
turns into:
This way we don't forget to close any files. This is very easy to do and was actually happening when reading the metadata.