Skip to content

OSError possible on macOS due to large filenames  #144

@lachlankidson

Description

@lachlankidson

Bug Report

Under certain circumstances the generated filename for .bin files can exceed OS limitations on Mac systems. This is more likely in projects with nested folders in their tests (i.e. tests > unit > some_module > test_x.py) or where parametrised tests are being used.

When using parameterised tests pytest will display the values in the test summary, i.e. for an input of:

@pytest.mark.parametrize(
    ("some_input", "some_expected_value"),
    [(1, "A string"), (2, f"A very l{'o' * 200}ng string")],
)
def test_reproduction(some_input: int, some_expected_value: str) -> None:
    pass

ran with pytest test_reproduction.py -svv, the following output can be expected:

========================================================================================================== test session starts ===========================================================================================================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0 -- /Users/lachlankidson/repos/memray-repro/.venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/lachlankidson/repos/memray-repro
configfile: pyproject.toml
plugins: memray-1.7.0
collected 2 items                                                                                                                                                                                                                        

test_reproduction.py::test_reproduction[1-A string] PASSED
test_reproduction.py::test_reproduction[2-A very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string] PASSED

=========================================================================================================== 2 passed in 0.02s ============================================================================================================

This example works under some configurations of memray, but fails with an OSError if --memray-bin-path is specified due to limitations on filename length, e.g. pytest --memray --memray-bin-path memray test_reproduction.py -svv produces:

================================================================================================================ FAILURES ================================================================================================================
_ test_reproduction[2-A very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string] _

args = ()
kwargs = {'some_expected_value': 'A very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo...ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string', 'some_input': 2}

    @functools.wraps(func)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
>       with memory_reporting():

.venv/lib/python3.13/site-packages/pytest_memray/plugin.py:213: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../.local/share/uv/python/cpython-3.13.3-macos-aarch64-none/lib/python3.13/contextlib.py:141: in __enter__
    return next(self.gen)
.venv/lib/python3.13/site-packages/pytest_memray/plugin.py:190: in memory_reporting
    result_file = _build_bin_path()
.venv/lib/python3.13/site-packages/pytest_memray/plugin.py:170: in _build_bin_path
    if self._tmp_dir is None and result_file.exists():
../../.local/share/uv/python/cpython-3.13.3-macos-aarch64-none/lib/python3.13/pathlib/_abc.py:450: in exists
    self.stat(follow_symlinks=follow_symlinks)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = PosixPath('/Users/lachlankidson/repos/memray-repro/memray/f3a29c6eebd84c06a78568a0158b89cb-test_reproduction.py-test_r...ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string].bin')

    def stat(self, *, follow_symlinks=True):
        """
        Return the result of the stat() system call on this path, like
        os.stat() does.
        """
>       return os.stat(self, follow_symlinks=follow_symlinks)
E       OSError: [Errno 63] File name too long: '/Users/lachlankidson/repos/memray-repro/memray/f3a29c6eebd84c06a78568a0158b89cb-test_reproduction.py-test_reproduction[2-A very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string].bin'

../../.local/share/uv/python/cpython-3.13.3-macos-aarch64-none/lib/python3.13/pathlib/_local.py:515: OSError

============================================================================================================= MEMRAY REPORT ==============================================================================================================
Created 1 binary dumps at /Users/lachlankidson/repos/memray-repro/memray with prefix f3a29c6eebd84c06a78568a0158b89cb
======================================================================================================== short test summary info =========================================================================================================
FAILED test_reproduction.py::test_reproduction[2-A very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string] - OSError: [Errno 63] File name too long: '/Users/lachlankidson/repos/memray-repro/memray/f3a29c6eebd84c06a78568a0158b89cb-test_reproduction.py-test_reproduction[2-A very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string].bin'
====================================================================================================== 1 failed, 1 passed in 0.28s =======================================================================================================

Expected behavior/code
pytest-memray does not produce filenames that are incompatible with the host operating system.

Environment

  • OS: macOS Sequoia 15.4.1
  • Python(s): 3.13.3 (via uv / python-build-standalone)
  • memray: 1.17.2
  • pytest: 8.3.5
  • pytest-memray: 1.7.0

Possible Solution

@pytest.mark.parametrize(
    ("some_input", "some_expected_value"),
    [pytest.param(1, "A string", id="1"), pytest.param(2, f"A very l{'o' * 200}ng string", id="2")],
)
def test_reproduction(some_input: int, some_expected_value: str) -> None:
    pass

Output:

========================================================================================================== test session starts ===========================================================================================================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0 -- /Users/lachlankidson/repos/memray-repro/.venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/lachlankidson/repos/memray-repro
configfile: pyproject.toml
plugins: memray-1.7.0
collected 2 items                                                                                                                                                                                                                        

test_reproduction.py::test_reproduction[1] PASSED
test_reproduction.py::test_reproduction[2] PASSED


============================================================================================================= MEMRAY REPORT ==============================================================================================================
Created 2 binary dumps at /Users/lachlankidson/repos/memray-repro/memray with prefix 0ab4300122d24f3d958bc923cd41ecb7
=========================================================================================================== 2 passed in 0.07s ============================================================================================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions