summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2016-10-23 13:27:58 +0700
committerGitHub <noreply@github.com>2016-10-23 13:27:58 +0700
commit3efe0766069a45bc4c9ff68671350e5c37b1cddf (patch)
tree3965abf5a6cbfca7b49b8fc63ea90c22c524f8e2
parentee147e3803def88aeca7986cbe51a36c9a386e36 (diff)
parent5900d74fd8f3bd6697df8ca080cda02ff37371e2 (diff)
downloadxattr-3efe0766069a45bc4c9ff68671350e5c37b1cddf.tar.gz
Merge pull request #50 from nyov/master
Fix Python3 bytes handling in xattr/tool.py
-rw-r--r--xattr/tests/test_xattr.py25
-rwxr-xr-xxattr/tool.py15
2 files changed, 25 insertions, 15 deletions
diff --git a/xattr/tests/test_xattr.py b/xattr/tests/test_xattr.py
index 548f735..c63a240 100644
--- a/xattr/tests/test_xattr.py
+++ b/xattr/tests/test_xattr.py
@@ -8,6 +8,11 @@ import xattr
class BaseTestXattr(object):
+ # TESTDIR for temporary files usually defaults to "/tmp",
+ # which may not have XATTR support (e.g. tmpfs);
+ # manual override here.
+ TESTDIR = None
+
def test_attr(self):
x = xattr.xattr(self.tempfile)
@@ -66,15 +71,17 @@ class BaseTestXattr(object):
self.assertEqual(str(e), msg)
def test_symlink_attrs(self):
- # Solaris doesn't support extended attributes on symlinks
- if sys.platform == 'sunos5':
- return
symlinkPath = self.tempfilename + '.link'
os.symlink(self.tempfilename, symlinkPath)
try:
symlink = xattr.xattr(symlinkPath, options=xattr.XATTR_NOFOLLOW)
realfile = xattr.xattr(self.tempfilename)
- symlink['user.islink'] = b'true'
+ try:
+ symlink['user.islink'] = b'true'
+ except IOError:
+ # Solaris, Linux don't support extended attributes on symlinks
+ raise unittest.SkipTest("XATTRs on symlink not allowed"
+ " on filesystem/platform")
self.assertEqual(dict(realfile), {})
self.assertEqual(symlink['user.islink'], b'true')
finally:
@@ -83,7 +90,7 @@ class BaseTestXattr(object):
class TestFile(TestCase, BaseTestXattr):
def setUp(self):
- self.tempfile = NamedTemporaryFile()
+ self.tempfile = NamedTemporaryFile(dir=self.TESTDIR)
self.tempfilename = self.tempfile.name
def tearDown(self):
@@ -92,7 +99,7 @@ class TestFile(TestCase, BaseTestXattr):
class TestDir(TestCase, BaseTestXattr):
def setUp(self):
- self.tempfile = mkdtemp()
+ self.tempfile = mkdtemp(dir=self.TESTDIR)
self.tempfilename = self.tempfile
def tearDown(self):
@@ -107,7 +114,9 @@ except AttributeError:
else:
class TestFileWithSurrogates(TestFile):
def setUp(self):
- if sys.platform != 'linux':
+ if sys.platform not in ('linux', 'linux2'):
raise unittest.SkipTest('Files with invalid encoded names are only supported under linux')
- self.tempfile = NamedTemporaryFile(prefix=b'invalid-\xe9'.decode('utf8','surrogateescape'))
+ if sys.version_info[0] < 3:
+ raise unittest.SkipTest('Test is only available on Python3') # surrogateescape not avail in py2
+ self.tempfile = NamedTemporaryFile(prefix=b'invalid-\xe9'.decode('utf8','surrogateescape'), dir=self.TESTDIR)
self.tempfilename = self.tempfile.name
diff --git a/xattr/tool.py b/xattr/tool.py
index 573261f..5cdecbd 100755
--- a/xattr/tool.py
+++ b/xattr/tool.py
@@ -3,7 +3,7 @@
##
# Copyright (c) 2007 Apple Inc.
#
-# This is the MIT license. This software may also be distributed under the
+# This is the MIT license. This software may also be distributed under the
# same terms as Python (the PSF license).
#
# Permission is hereby granted, free of charge, to any person obtaining a
@@ -35,6 +35,10 @@ import zlib
import xattr
+class NullsInString(Exception):
+ """Nulls in string."""
+
+
def usage(e=None):
if e:
print(e)
@@ -62,10 +66,6 @@ def usage(e=None):
sys.exit(0)
-class NullsInString(Exception):
- """Nulls in string."""
-
-
_FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
@@ -127,7 +127,7 @@ def main():
if write:
if not args:
usage("No attr_value")
- attr_value = args.pop(0)
+ attr_value = args.pop(0).encode('utf-8')
if len(args) > 1:
multiple_files = True
@@ -185,13 +185,14 @@ def main():
attr_value = decompress(attrs[attr_name])
except zlib.error:
attr_value = attrs[attr_name]
+ attr_value = attr_value.decode('utf-8')
except KeyError:
onError("%sNo such xattr: %s" % (file_prefix, attr_name))
continue
if long_format:
try:
- if attr_value.find('\0') >= 0:
+ if '\0' in attr_value:
raise NullsInString
print("".join((file_prefix, "%s: " % (attr_name,), attr_value)))
except (UnicodeDecodeError, NullsInString):