|
| 1 | +import platform |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture(autouse=True) |
| 7 | +def _disable_adlfs_memoryview_optimization_on_pypy( |
| 8 | + monkeypatch: pytest.MonkeyPatch, |
| 9 | +) -> None: |
| 10 | + if platform.python_implementation() != "PyPy": |
| 11 | + return |
| 12 | + |
| 13 | + # Remove this whole fixture when either: |
| 14 | + # - PyPy exposes an iterable `memoryview` compatible with Azure SDK request-body |
| 15 | + # checks, or |
| 16 | + # - adlfs updates its own support check / stops using `memoryview` for uploads. |
| 17 | + # |
| 18 | + # This is intentionally a hard failure: it forces us to re-evaluate whether this |
| 19 | + # workaround is still needed. |
| 20 | + if hasattr(memoryview(b""), "__iter__"): |
| 21 | + pytest.fail( |
| 22 | + ( |
| 23 | + "PyPy memoryview now appears iterable; the adlfs/Azure workaround in " |
| 24 | + "tests/conftest.py is likely obsolete and should be removed." |
| 25 | + ), |
| 26 | + ) |
| 27 | + |
| 28 | + try: |
| 29 | + import adlfs.spec |
| 30 | + except Exception: # noqa: BLE001 |
| 31 | + return |
| 32 | + |
| 33 | + azure_blob_file = getattr(adlfs.spec, "AzureBlobFile", None) |
| 34 | + if azure_blob_file is None: |
| 35 | + return |
| 36 | + |
| 37 | + # If adlfs itself no longer claims memoryview support, then this workaround should |
| 38 | + # be unnecessary and must be removed. |
| 39 | + try: |
| 40 | + if not azure_blob_file._sdk_supports_memoryview_for_writes(None): # noqa: SLF001 |
| 41 | + pytest.fail( |
| 42 | + ( |
| 43 | + "adlfs no longer reports memoryview support for writes; the PyPy " |
| 44 | + "workaround in tests/conftest.py should be removed." |
| 45 | + ), |
| 46 | + ) |
| 47 | + except Exception: # noqa: BLE001 |
| 48 | + # If this API shape changes, we also want a visible signal rather than |
| 49 | + # silently masking. |
| 50 | + pytest.fail( |
| 51 | + "adlfs AzureBlobFile._sdk_supports_memoryview_for_writes API changed; " |
| 52 | + "re-evaluate/remove the PyPy workaround in tests/conftest.py.", |
| 53 | + ) |
| 54 | + |
| 55 | + monkeypatch.setattr( |
| 56 | + azure_blob_file, |
| 57 | + "_sdk_supports_memoryview_for_writes", |
| 58 | + lambda *_args, **_kwargs: False, |
| 59 | + raising=False, |
| 60 | + ) |
0 commit comments