|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 | # pylint: disable=protected-access,unused-argument,redefined-outer-name |
| 18 | +import gc |
18 | 19 | import logging |
19 | 20 | import os |
| 21 | +import pickle |
20 | 22 | import sys |
21 | 23 | import tempfile |
22 | 24 | import uuid |
23 | 25 | import warnings |
| 26 | +import weakref |
24 | 27 | from collections.abc import Iterator |
| 28 | +from contextlib import contextmanager |
25 | 29 | from datetime import date, datetime, timezone |
26 | 30 | from pathlib import Path |
27 | 31 | from typing import Any |
@@ -3307,6 +3311,42 @@ def test_pyarrow_file_io_fs_by_scheme_cache() -> None: |
3307 | 3311 | assert pyarrow_file_io.fs_by_scheme.cache_info().hits == 2 # type: ignore |
3308 | 3312 |
|
3309 | 3313 |
|
| 3314 | +@contextmanager |
| 3315 | +def _cycle_collector_disabled() -> Iterator[None]: |
| 3316 | + was_enabled = gc.isenabled() |
| 3317 | + gc.disable() |
| 3318 | + try: |
| 3319 | + yield |
| 3320 | + finally: |
| 3321 | + if was_enabled: |
| 3322 | + gc.enable() |
| 3323 | + |
| 3324 | + |
| 3325 | +def test_pyarrow_file_io_freed_by_refcounting() -> None: |
| 3326 | + with _cycle_collector_disabled(): |
| 3327 | + file_io = PyArrowFileIO() |
| 3328 | + file_io.fs_by_scheme("file", None) |
| 3329 | + file_io_ref = weakref.ref(file_io) |
| 3330 | + del file_io |
| 3331 | + |
| 3332 | + assert file_io_ref() is None |
| 3333 | + |
| 3334 | + |
| 3335 | +def test_pyarrow_file_io_pickle_round_trip_keeps_cache_and_refcounting() -> None: |
| 3336 | + file_io = PyArrowFileIO() |
| 3337 | + file_io.fs_by_scheme("file", None) |
| 3338 | + |
| 3339 | + restored = pickle.loads(pickle.dumps(file_io)) |
| 3340 | + assert isinstance(restored.fs_by_scheme("file", None), LocalFileSystem) |
| 3341 | + assert restored.fs_by_scheme.cache_info().currsize == 1 |
| 3342 | + |
| 3343 | + with _cycle_collector_disabled(): |
| 3344 | + restored_ref = weakref.ref(restored) |
| 3345 | + del restored |
| 3346 | + |
| 3347 | + assert restored_ref() is None |
| 3348 | + |
| 3349 | + |
3310 | 3350 | def test_pyarrow_io_new_input_multi_region(caplog: Any) -> None: |
3311 | 3351 | # It's better to set up multi-region minio servers for an integration test once `endpoint_url` argument |
3312 | 3352 | # becomes available for `resolve_s3_region` |
|
0 commit comments