[Pytest-commit] commit/py: hpk42: implement text/bytes variants for local paths

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Sep 3 15:41:45 CEST 2014


1 new commit in py:

https://bitbucket.org/hpk42/py/commits/ca27187c337d/
Changeset:   ca27187c337d
User:        hpk42
Date:        2014-09-03 15:41:35
Summary:     implement text/bytes variants for local paths
Affected #:  4 files

diff -r 764221efcef5bbc004e0e7f68ee5230dfa7168dc -r ca27187c337d018186012ceb340d178a8adb893b CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,9 +1,13 @@
-1.4.24
+1.4.24.dev
 ==================================================
 
 - Fix retrieving source when an else: line has an other statement on
   the same line.
 
+- add localpath read_text/write_text/read_bytes/write_bytes methods
+  as shortcuts and clearer bytes/text interfaces for read/write.
+  Adapted from a PR from Paul Moore.
+
 
 1.4.23
 ==================================================

diff -r 764221efcef5bbc004e0e7f68ee5230dfa7168dc -r ca27187c337d018186012ceb340d178a8adb893b py/_path/common.py
--- a/py/_path/common.py
+++ b/py/_path/common.py
@@ -117,31 +117,19 @@
 
     def read_binary(self):
         """ read and return a bytestring from reading the path. """
-        f = self.open('rb')
-        try:
+        with self.open('rb') as f:
             return f.read()
-        finally:
-            f.close()
 
-    def read_text(self, encoding=None):
+    def read_text(self, encoding="utf8"):
         """ read and return a Unicode string from reading the path. """
-        f = self.open('r', encoding=encoding)
-        try:
+        with self.open("r", encoding=encoding) as f:
             return f.read()
-        finally:
-            f.close()
+
 
     def read(self, mode='r'):
         """ read and return a bytestring from reading the path. """
-        if sys.version_info < (2,3):
-            for x in 'u', 'U':
-                if x in mode:
-                    mode = mode.replace(x, '')
-        f = self.open(mode)
-        try:
+        with self.open(mode) as f:
             return f.read()
-        finally:
-            f.close()
 
     def readlines(self, cr=1):
         """ read and return a list of lines from the path. if cr is False, the

diff -r 764221efcef5bbc004e0e7f68ee5230dfa7168dc -r ca27187c337d018186012ceb340d178a8adb893b py/_path/local.py
--- a/py/_path/local.py
+++ b/py/_path/local.py
@@ -442,23 +442,17 @@
         """
         if ensure:
             self.dirpath().ensure(dir=1)
-        f = self.open('wb')
-        try:
+        with self.open('wb') as f:
             f.write(data)
-        finally:
-            f.close()
 
-    def write_text(self, data, ensure=False, encoding=None):
-        """ write binary data into path.   If ensure is True create
-        missing parent directories.
+    def write_text(self, data, encoding="utf8", ensure=False):
+        """ write text data into path using the specified encoding.
+        If ensure is True create missing parent directories.
         """
         if ensure:
             self.dirpath().ensure(dir=1)
-        f = self.open('w', encoding=encoding)
-        try:
+        with self.open('w', encoding=encoding) as f:
             f.write(data)
-        finally:
-            f.close()
 
     def write(self, data, mode='w', ensure=False):
         """ write data into path.   If ensure is True create

diff -r 764221efcef5bbc004e0e7f68ee5230dfa7168dc -r ca27187c337d018186012ceb340d178a8adb893b testing/path/test_local.py
--- a/testing/path/test_local.py
+++ b/testing/path/test_local.py
@@ -808,7 +808,10 @@
         part_utf8 = part.encode("utf8")
         x.write_binary(part_utf8)
         assert x.read_binary() == part_utf8
-        assert x.read_text(encoding="utf8") == part
+        s = x.read_text(encoding="utf8")
+        assert s == part
+        assert py.builtin._istext(s)
+
     def test_read_textwrite(self, tmpdir):
         x = tmpdir.join("hello")
         part = py.builtin._totext("hällo", "utf8")
@@ -816,9 +819,12 @@
         x.write_text(part, encoding="utf8")
         assert x.read_binary() == part_utf8
         assert x.read_text(encoding="utf8") == part
+
     def test_default_encoding(self, tmpdir):
         x = tmpdir.join("hello")
         # Can't use UTF8 as the default encoding (ASCII) doesn't support it
         part = py.builtin._totext("hello", "ascii")
         x.write_text(part)
-        assert x.read_text() == part
+        s = x.read_text()
+        assert s == part
+        assert type(s) == type(part)

Repository URL: https://bitbucket.org/hpk42/py/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the pytest-commit mailing list