Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise exception when open with write mode in call stack #140

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import fnmatch
import os
from pathlib import Path, PosixPath, PurePosixPath, WindowsPath
import inspect
import traceback
from typing import Any, IO, Iterable, Optional, TYPE_CHECKING, Union
from urllib.parse import urlparse
from warnings import warn

from .exceptions import (
BuiltInOpenWriteError,
ClientMismatchError,
CloudPathFileExistsError,
CloudPathIsADirectoryError,
Expand Down Expand Up @@ -205,6 +208,19 @@ def __eq__(self, other: Any) -> bool:
return isinstance(other, type(self)) and str(self) == str(other)

def __fspath__(self):
WRITE_MODES = {"r+", "w", "w+", "a", "a+", "rb+", "wb", "wb+", "ab", "ab+"}
for frame, _ in traceback.walk_stack(None):
if "open" in frame.f_code.co_names:
code_context = inspect.getframeinfo(frame).code_context
if "open(" in code_context[0]:
for mode in WRITE_MODES:
if f'"{mode}"' in code_context[0] or f"'{mode}'" in code_context[0]:
raise BuiltInOpenWriteError(
"Detected the use of built-in open function with a cloud path and write mode. "
"Cloud paths do not support the open function in write mode; "
"please use the .open() method instead."
)

if self.is_file():
self._refresh_cache(force_overwrite_from_cloud=False)
return str(self._local)
Expand Down
4 changes: 4 additions & 0 deletions cloudpathlib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class AnyPathTypeError(CloudPathException, TypeError):
pass


class BuiltInOpenWriteError(CloudPathException):
pass


class ClientMismatchError(CloudPathException, ValueError):
pass

Expand Down
29 changes: 28 additions & 1 deletion tests/test_cloudpath_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import pytest

from cloudpathlib.exceptions import CloudPathIsADirectoryError, DirectoryNotEmptyError
from cloudpathlib.exceptions import (
BuiltInOpenWriteError,
CloudPathIsADirectoryError,
DirectoryNotEmptyError,
)


def test_file_discovery(rig):
Expand Down Expand Up @@ -113,3 +117,26 @@ def test_os_open(rig):
p = rig.create_cloud_path("dir_0/file0_0.txt")
with open(p, "r") as f:
assert f.readable()

with pytest.raises(BuiltInOpenWriteError):
with open(p, "w") as f:
pass

with pytest.raises(BuiltInOpenWriteError):
with open(p, "wb") as f:
pass

with pytest.raises(BuiltInOpenWriteError):
with open(p, "a") as f:
pass

with pytest.raises(BuiltInOpenWriteError):
with open(p, "r+") as f:
pass

# with pytest.raises(BuiltInOpenWriteError):
# with open(
# p,
# "w",
# ) as f:
# pass
Copy link
Member Author

@jayqi jayqi Apr 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case fails because the test frame's code_context looks like this:

['        with open(\n']

Not sure what to do about it.