|
54 | 54 |
|
55 | 55 | from pandas.core.dtypes.common import is_file_like
|
56 | 56 |
|
57 |
| -from pandas.core import generic |
| 57 | +from pandas.core.shared_docs import _shared_docs |
58 | 58 |
|
59 | 59 | _VALID_URLS = set(uses_relative + uses_netloc + uses_params)
|
60 | 60 | _VALID_URLS.discard("")
|
@@ -248,10 +248,7 @@ def is_fsspec_url(url: FilePath | BaseBuffer) -> bool:
|
248 | 248 | )
|
249 | 249 |
|
250 | 250 |
|
251 |
| -@doc( |
252 |
| - compression_options=generic._shared_docs["compression_options"] |
253 |
| - % "filepath_or_buffer" |
254 |
| -) |
| 251 | +@doc(compression_options=_shared_docs["compression_options"] % "filepath_or_buffer") |
255 | 252 | def _get_filepath_or_buffer(
|
256 | 253 | filepath_or_buffer: FilePath | BaseBuffer,
|
257 | 254 | encoding: str = "utf-8",
|
@@ -497,10 +494,7 @@ def get_compression_method(
|
497 | 494 | return compression_method, compression_args
|
498 | 495 |
|
499 | 496 |
|
500 |
| -@doc( |
501 |
| - compression_options=generic._shared_docs["compression_options"] |
502 |
| - % "filepath_or_buffer" |
503 |
| -) |
| 497 | +@doc(compression_options=_shared_docs["compression_options"] % "filepath_or_buffer") |
504 | 498 | def infer_compression(
|
505 | 499 | filepath_or_buffer: FilePath | BaseBuffer, compression: str | None
|
506 | 500 | ) -> str | None:
|
@@ -604,7 +598,7 @@ def get_handle(
|
604 | 598 | ...
|
605 | 599 |
|
606 | 600 |
|
607 |
| -@doc(compression_options=generic._shared_docs["compression_options"] % "path_or_buf") |
| 601 | +@doc(compression_options=_shared_docs["compression_options"] % "path_or_buf") |
608 | 602 | def get_handle(
|
609 | 603 | path_or_buf: FilePath | BaseBuffer,
|
610 | 604 | mode: str,
|
@@ -1136,18 +1130,20 @@ def _is_binary_mode(handle: FilePath | BaseBuffer, mode: str) -> bool:
|
1136 | 1130 | )
|
1137 | 1131 |
|
1138 | 1132 |
|
1139 |
| -def _get_binary_io_classes() -> tuple[type, ...]: |
| 1133 | +def _get_binary_io_classes(_cache: list[type] = []) -> tuple[type, ...]: |
1140 | 1134 | """IO classes that that expect bytes"""
|
1141 |
| - binary_classes: tuple[type, ...] = (BufferedIOBase, RawIOBase) |
1142 |
| - |
1143 |
| - # python-zstandard doesn't use any of the builtin base classes; instead we |
1144 |
| - # have to use the `zstd.ZstdDecompressionReader` class for isinstance checks. |
1145 |
| - # Unfortunately `zstd.ZstdDecompressionReader` isn't exposed by python-zstandard |
1146 |
| - # so we have to get it from a `zstd.ZstdDecompressor` instance. |
1147 |
| - # See also https://github.com/indygreg/python-zstandard/pull/165. |
1148 |
| - zstd = import_optional_dependency("zstandard", errors="ignore") |
1149 |
| - if zstd is not None: |
1150 |
| - with zstd.ZstdDecompressor().stream_reader(b"") as reader: |
1151 |
| - binary_classes += (type(reader),) |
1152 |
| - |
1153 |
| - return binary_classes |
| 1135 | + if not _cache: |
| 1136 | + binary_classes: tuple[type, ...] = (BufferedIOBase, RawIOBase) |
| 1137 | + |
| 1138 | + # python-zstandard doesn't use any of the builtin base classes; instead we |
| 1139 | + # have to use the `zstd.ZstdDecompressionReader` class for isinstance checks. |
| 1140 | + # Unfortunately `zstd.ZstdDecompressionReader` isn't exposed by python-zstandard |
| 1141 | + # so we have to get it from a `zstd.ZstdDecompressor` instance. |
| 1142 | + # See also https://github.com/indygreg/python-zstandard/pull/165. |
| 1143 | + zstd = import_optional_dependency("zstandard", errors="ignore") |
| 1144 | + if zstd is not None: |
| 1145 | + with zstd.ZstdDecompressor().stream_reader(b"") as reader: |
| 1146 | + binary_classes += (type(reader),) |
| 1147 | + |
| 1148 | + _cache[:] = binary_classes |
| 1149 | + return tuple(_cache) |
0 commit comments