Skip to content

Commit 4f3d18e

Browse files
committed
move common code to utils
1 parent e2a595a commit 4f3d18e

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import google_crc32c
2+
3+
from google.cloud.storage import exceptions
4+
5+
def raise_if_no_fast_crc32c():
6+
"""Check if the C-accelerated version of google-crc32c is available.
7+
8+
If not, raise an error to prevent silent performance degradation.
9+
10+
raises google.api_core.exceptions.NotFound: If the C extension is not available.
11+
returns: True if the C extension is available.
12+
rtype: bool
13+
14+
"""
15+
if google_crc32c.implementation != "c":
16+
raise exceptions.NotFound(
17+
"The google-crc32c package is not installed with C support. "
18+
"Bidi reads require the C extension for data integrity checks."
19+
"For more information, see https://github.com/googleapis/python-crc32c."
20+
)

google/cloud/storage/_experimental/asyncio/async_appendable_object_writer.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
if you want to use these Rapid Storage APIs.
2222
2323
"""
24+
import time
2425
from typing import Optional, Union
26+
2527
from google.api_core import exceptions
26-
from google_crc32c import Checksum, implementation as crc32c_impl
28+
from google_crc32c import Checksum
29+
30+
from ._utils import raise_if_no_fast_crc32c
2731
from google.cloud import _storage_v2
2832
from google.cloud.storage._experimental.asyncio.async_grpc_client import (
2933
AsyncGrpcClient,
@@ -102,14 +106,7 @@ def __init__(
102106
:param write_handle: (Optional) An existing handle for writing the object.
103107
If provided, opening the bidi-gRPC connection will be faster.
104108
"""
105-
# Verify that the fast, C-accelerated version of crc32c is available.
106-
# If not, raise an error to prevent silent performance degradation.
107-
if crc32c_impl != "c":
108-
raise exceptions.NotFound(
109-
"The google-crc32c package is not installed with C support. "
110-
"Bidi reads require the C extension for data integrity checks."
111-
"For more information, see https://github.com/googleapis/python-crc32c."
112-
)
109+
raise_if_no_fast_crc32c()
113110
self.client = client
114111
self.bucket_name = bucket_name
115112
self.object_name = object_name

google/cloud/storage/_experimental/asyncio/async_multi_range_downloader.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
from __future__ import annotations
1616
import asyncio
17-
import google_crc32c
17+
from typing import List, Optional, Tuple
18+
1819
from google.api_core import exceptions
1920
from google_crc32c import Checksum
2021

21-
from typing import List, Optional, Tuple
22-
22+
from ._utils import raise_if_no_fast_crc32c
2323
from google.cloud.storage._experimental.asyncio.async_read_object_stream import (
2424
_AsyncReadObjectStream,
2525
)
@@ -160,14 +160,7 @@ def __init__(
160160
:param read_handle: (Optional) An existing read handle.
161161
"""
162162

163-
# Verify that the fast, C-accelerated version of crc32c is available.
164-
# If not, raise an error to prevent silent performance degradation.
165-
if google_crc32c.implementation != "c":
166-
raise exceptions.NotFound(
167-
"The google-crc32c package is not installed with C support. "
168-
"Bidi reads require the C extension for data integrity checks."
169-
"For more information, see https://github.com/googleapis/python-crc32c."
170-
)
163+
raise_if_no_fast_crc32c()
171164

172165
self.client = client
173166
self.bucket_name = bucket_name

0 commit comments

Comments
 (0)