Skip to content

Commit

Permalink
[BugFix] Check if the current user has write access (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszGuzek authored May 17, 2024
1 parent a088b87 commit b9bca11
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 1 addition & 3 deletions tensordict/memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import mmap
import os
import stat

import sys
import tempfile
Expand Down Expand Up @@ -1073,7 +1072,6 @@ def _chunk(input, chunks, dim=0):
def _is_writable(file_path):
file_path = str(file_path)
if os.path.exists(file_path):
st = os.stat(file_path)
return bool(st.st_mode & (stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH))
return os.access(file_path, os.W_OK)
# Assume that the file can be written in the directory
return True
2 changes: 2 additions & 0 deletions test/test_memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ def test_save_td_with_nested(self, tmpdir):


class TestReadWrite:
@pytest.mark.skipif(os.getuid() == 0, reason="root can write to read-only files")
def test_read_only(self, tmpdir):
tmpdir = Path(tmpdir)
file_path = tmpdir / "elt.mmap"
Expand Down Expand Up @@ -747,6 +748,7 @@ def test_read_only(self, tmpdir):
assert (mmap.reshape(-1) == torch.arange(6)).all()

@pytest.mark.skipif(not HAS_NESTED_TENSOR, reason="Nested tensor incomplete")
@pytest.mark.skipif(os.getuid() == 0, reason="root can write to read-only files")
def test_read_only_nested(self, tmpdir):
tmpdir = Path(tmpdir)
file_path = tmpdir / "elt.mmap"
Expand Down

0 comments on commit b9bca11

Please sign in to comment.