Rev 4260: Test and implements osutils.readlink(). in file:///home/vila/src/bzr/bugs/355454-unicode-warning/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Thu Apr 16 17:57:28 BST 2009
At file:///home/vila/src/bzr/bugs/355454-unicode-warning/
------------------------------------------------------------
revno: 4260
revision-id: v.ladeuil+lp at free.fr-20090416165728-lnobbei6iew38g2d
parent: v.ladeuil+lp at free.fr-20090416165006-o3xlq80o3oneb8vc
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 355454-unicode-warning
timestamp: Thu 2009-04-16 18:57:28 +0200
message:
Test and implements osutils.readlink().
* bzrlib/tests/test_osutils.py:
(TestDirReader.test_symlink): Fix typo.
(TestReadLink): Explain the need for osutils.readlink().
* bzrlib/osutils.py:
(readlink): Reliable version handling unicode paths only.
-------------- next part --------------
=== modified file 'BRANCH.TODO'
--- a/BRANCH.TODO 2009-04-16 14:13:57 +0000
+++ b/BRANCH.TODO 2009-04-16 16:57:28 +0000
@@ -3,17 +3,6 @@
#
#
-- test and code osutils.readlink around:
-
- def read_link(link):
- # The only reliable way to get the link target for python2.[456]
- # other aternative implementations either fails to reliably return
- # a unicode string or fails to encode the received unicode string
- link = link.encode(osutils._fs_enc)
- target = os.readlink(link)
- target = target.decode(osutils._fs_enc)
- return target
-
- use osutils.readlink in:
- HashCache.get_sha1 (one test failing)
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py 2009-04-16 14:13:57 +0000
+++ b/bzrlib/osutils.py 2009-04-16 16:57:28 +0000
@@ -925,6 +925,20 @@
and sys.platform not in ('cygwin', 'win32'))
+def readlink(abspath):
+ """Return a string representing the path to which the symbolic link points.
+
+ :param abspath: The link absolute unicode path.
+
+ This his guaranteed to return the symbolic link in unicode in all python
+ versions.
+ """
+ link = abspath.encode(_fs_enc)
+ target = os.readlink(link)
+ target = target.decode(_fs_enc)
+ return target
+
+
def contains_whitespace(s):
"""True if there are any whitespace characters in s."""
# string.whitespace can include '\xa0' in certain locales, because it is
=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py 2009-04-10 15:54:36 +0000
+++ b/bzrlib/tests/test_osutils.py 2009-04-16 16:57:28 +0000
@@ -1707,8 +1707,8 @@
def test_symlink(self):
self.requireFeature(tests.SymlinkFeature)
- target = u'target\n{Euro Sign}'
- link_name = u'l\n{Euro Sign}nk'
+ target = u'target\N{Euro Sign}'
+ link_name = u'l\N{Euro Sign}nk'
os.symlink(target, link_name)
target_utf8 = target.encode('UTF-8')
link_name_utf8 = link_name.encode('UTF-8')
@@ -1719,3 +1719,30 @@
)]
result = list(osutils._walkdirs_utf8('.'))
self.assertEqual(expected_dirblocks, self._filter_out(result))
+
+
+class TestReadLink(tests.TestCaseInTempDir):
+ """Exposes os.readlink() problems and the osutils solution.
+
+ The only guarantee offered by os.readlink(), starting with 2.6, is that a
+ unicode string will be returned if a unicode string is passed.
+
+ But prior python versions failed to properly encode a the passed unicode
+ string.
+ """
+
+ def setUp(self):
+ super(tests.TestCaseInTempDir, self).setUp()
+ self.link = u'l\N{Euro Sign}ink'
+ self.target = u'targe\N{Euro Sign}t'
+ os.symlink(self.target, self.link)
+
+ def test_os_readlink_link_encoding(self):
+ if sys.version_info < (2, 6):
+ self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
+ else:
+ self.assertEquals(self.target, os.readlink(self.link))
+
+ def test_os_readlink_link_decoding(self):
+ self.assertEquals(self.target.encode(osutils._fs_enc),
+ os.readlink(self.link.encode(osutils._fs_enc)))
More information about the bazaar-commits
mailing list