Skip to content

Commit

Permalink
fix: content streams could not be opened
Browse files Browse the repository at this point in the history
  • Loading branch information
matfax committed Oct 15, 2019
1 parent 0921994 commit 77c5a74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mutapath/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__EXCLUDE_FROM_WRAPPING = ["__dir__", "__eq__", "__format__", "__repr__", "__str__", "__sizeof__", "__init__",
"__post_init__", "__getattribute__", "__delattr__", "__setattr__", "__getattr__",
"__exit__", "__fspath__", "'_Path__wrap_attribute'", "__wrap_decorator", "_op_context",
"__hash__", "_norm"]
"__hash__", "__enter__", "_norm", "open"]

__MUTABLE_FUNCTIONS = {"rename", "renames", "copy", "copy2", "copyfile", "copymode", "copystat", "copytree", "move",
"basename", "abspath", "join", "joinpath", "normpath", "relpath", "realpath", "relpathto"}
Expand Down
13 changes: 11 additions & 2 deletions mutapath/immutapath.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import io
import os
import pathlib
from contextlib import contextmanager
Expand Down Expand Up @@ -111,10 +112,10 @@ def __rdiv__(self, other):
__rtruediv__ = __rdiv__

def __enter__(self):
return self._contained.__enter__()
return Path(self._contained.__enter__())

def __exit__(self, *_):
return self._contained.__exit__()
self._contained.__exit__()

def __fspath__(self):
return self._contained.__fspath__()
Expand Down Expand Up @@ -357,6 +358,14 @@ def owner(self):
"""
return self._contained.owner

def open(self, *args, **kwargs):
"""
Open a file and return a stream to its content.
seealso:: :func:`io.open`
"""
return io.open(str(self), *args, **kwargs)

@contextmanager
def mutate(self):
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_with_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def test_wrapped_generator(self):
finally:
self._clean()

def test_open(self):
try:
test_file = self._gen_start_path()
expected = "test"
with test_file.open("w") as w:
w.write(expected)
actual = test_file.text()
self.assertEqual(expected, actual)
self.assertIsInstance(test_file, Path)
finally:
self._clean()

def test_size(self):
try:
test_file = self._gen_start_path()
Expand Down

0 comments on commit 77c5a74

Please sign in to comment.