Skip to content

Commit

Permalink
Merge branch 'release-7.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenkov committed Mar 26, 2024
2 parents 2865174 + aa51ab6 commit 623ea92
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 7.0.4, 2024-03-26

* Fix wb mode with zstd compression (PR [#815](https://github.com/piskvorky/smart_open/pull/815), [@djudd](https://github.com/djudd))
* 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))

# 7.0.3, 2024-03-21

* add support for zst writing (PR [#812](https://github.com/piskvorky/smart_open/pull/812), [@mpenkov](https://github.com/mpenkov))
Expand Down
12 changes: 12 additions & 0 deletions smart_open/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# from the MIT License (MIT).
#
"""Implements the compression layer of the ``smart_open`` library."""
import io
import logging
import os.path

Expand Down Expand Up @@ -108,6 +109,17 @@ def _handle_gzip(file_obj, mode):
def _handle_zstd(file_obj, mode):
import zstandard # type: ignore
result = zstandard.open(filename=file_obj, mode=mode)
# zstandard.open returns an io.TextIOWrapper in text mode, but otherwise
# returns a raw stream reader/writer, and we need the `io` wrapper
# to make FileLikeProxy work correctly.
#
# See:
#
# https://github.com/indygreg/python-zstandard/blob/d7d81e79dbe74feb22fb73405ebfb3e20f4c4653/zstandard/__init__.py#L169-L174
if "b" in mode and "w" in mode:
result = io.BufferedWriter(result)
elif "b" in mode and "r" in mode:
result = io.BufferedReader(result)
return result


Expand Down
6 changes: 1 addition & 5 deletions smart_open/gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ def Writer(bucket,

blob_open_kwargs = {**_DEFAULT_WRITE_OPEN_KWARGS, **blob_open_kwargs}

g_bucket = client.bucket(bucket)
if not g_bucket.exists():
raise google.cloud.exceptions.NotFound(f'bucket {bucket} not found')

g_blob = g_bucket.blob(
g_blob = client.bucket(bucket).blob(
blob,
chunk_size=min_part_size,
)
Expand Down
12 changes: 12 additions & 0 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ def test_zst_write():
assert got == ["hello world\n", "this is a test\n"]


def test_zst_write_binary():
with named_temporary_file(suffix=".zst") as tmp:
with smart_open.open(tmp.name, "wb") as fout:
fout.write(b"hello world\n")
fout.write(b"this is a test\n")

with smart_open.open(tmp.name, "rb") as fin:
got = list(fin)

assert got == [b"hello world\n", b"this is a test\n"]


class ParseUriTest(unittest.TestCase):
"""
Test ParseUri class.
Expand Down
2 changes: 1 addition & 1 deletion smart_open/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '7.0.3'
__version__ = '7.0.4'


if __name__ == '__main__':
Expand Down

0 comments on commit 623ea92

Please sign in to comment.