File tree Expand file tree Collapse file tree 5 files changed +31
-6
lines changed Expand file tree Collapse file tree 5 files changed +31
-6
lines changed Original file line number Diff line number Diff line change 1+ # 7.0.4, 2024-03-26
2+
3+ * Fix wb mode with zstd compression (PR [ #815 ] ( https://github.com/piskvorky/smart_open/pull/815 ) , [ @djudd ] ( https://github.com/djudd ) )
4+ * Remove GCS bucket.exists call to avoid storage.buckets.get permission (PR [ #813 ] ( https://github.com/piskvorky/smart_open/pull/813 ) , [ @ddelange ] ( https://github.com/ddelange ) )
5+
16# 7.0.3, 2024-03-21
27
38* add support for zst writing (PR [ #812 ] ( https://github.com/piskvorky/smart_open/pull/812 ) , [ @mpenkov ] ( https://github.com/mpenkov ) )
Original file line number Diff line number Diff line change 66# from the MIT License (MIT).
77#
88"""Implements the compression layer of the ``smart_open`` library."""
9+ import io
910import logging
1011import os .path
1112
@@ -108,6 +109,17 @@ def _handle_gzip(file_obj, mode):
108109def _handle_zstd (file_obj , mode ):
109110 import zstandard # type: ignore
110111 result = zstandard .open (filename = file_obj , mode = mode )
112+ # zstandard.open returns an io.TextIOWrapper in text mode, but otherwise
113+ # returns a raw stream reader/writer, and we need the `io` wrapper
114+ # to make FileLikeProxy work correctly.
115+ #
116+ # See:
117+ #
118+ # https://github.com/indygreg/python-zstandard/blob/d7d81e79dbe74feb22fb73405ebfb3e20f4c4653/zstandard/__init__.py#L169-L174
119+ if "b" in mode and "w" in mode :
120+ result = io .BufferedWriter (result )
121+ elif "b" in mode and "r" in mode :
122+ result = io .BufferedReader (result )
111123 return result
112124
113125
Original file line number Diff line number Diff line change @@ -152,11 +152,7 @@ def Writer(bucket,
152152
153153 blob_open_kwargs = {** _DEFAULT_WRITE_OPEN_KWARGS , ** blob_open_kwargs }
154154
155- g_bucket = client .bucket (bucket )
156- if not g_bucket .exists ():
157- raise google .cloud .exceptions .NotFound (f'bucket { bucket } not found' )
158-
159- g_blob = g_bucket .blob (
155+ g_blob = client .bucket (bucket ).blob (
160156 blob ,
161157 chunk_size = min_part_size ,
162158 )
Original file line number Diff line number Diff line change @@ -89,6 +89,18 @@ def test_zst_write():
8989 assert got == ["hello world\n " , "this is a test\n " ]
9090
9191
92+ def test_zst_write_binary ():
93+ with named_temporary_file (suffix = ".zst" ) as tmp :
94+ with smart_open .open (tmp .name , "wb" ) as fout :
95+ fout .write (b"hello world\n " )
96+ fout .write (b"this is a test\n " )
97+
98+ with smart_open .open (tmp .name , "rb" ) as fin :
99+ got = list (fin )
100+
101+ assert got == [b"hello world\n " , b"this is a test\n " ]
102+
103+
92104class ParseUriTest (unittest .TestCase ):
93105 """
94106 Test ParseUri class.
Original file line number Diff line number Diff line change 1- __version__ = '7.0.3 '
1+ __version__ = '7.0.4 '
22
33
44if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments