aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/pyexiv2/metadata.py5
-rwxr-xr-xtest/TestsRunner.py2
-rw-r--r--test/encoding.py98
3 files changed, 102 insertions, 3 deletions
diff --git a/src/pyexiv2/metadata.py b/src/pyexiv2/metadata.py
index 15b32f3..018f3b3 100644
--- a/src/pyexiv2/metadata.py
+++ b/src/pyexiv2/metadata.py
@@ -29,6 +29,7 @@ Provide the ImageMetadata class.
"""
import os
+import sys
from errno import ENOENT
import libexiv2python
@@ -54,9 +55,7 @@ class ImageMetadata(object):
:param filename: path to an image file
:type filename: string
"""
- self.filename = filename
- if isinstance(filename, unicode):
- self.filename = filename.encode('utf-8')
+ self.filename = filename.encode(sys.getfilesystemencoding())
self._image = None
self._keys = {'exif': None, 'iptc': None, 'xmp': None}
self._tags = {'exif': {}, 'iptc': {}, 'xmp': {}}
diff --git a/test/TestsRunner.py b/test/TestsRunner.py
index d10b5e6..273c678 100755
--- a/test/TestsRunner.py
+++ b/test/TestsRunner.py
@@ -41,6 +41,7 @@ from exif import TestExifTag
from iptc import TestIptcTag
from xmp import TestXmpTag
from metadata import TestImageMetadata
+from encoding import TestEncodings
if __name__ == '__main__':
@@ -59,5 +60,6 @@ if __name__ == '__main__':
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestIptcTag))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestXmpTag))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestImageMetadata))
+ suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestEncodings))
# Run the test suite
unittest.TextTestRunner(verbosity=2).run(suite)
diff --git a/test/encoding.py b/test/encoding.py
new file mode 100644
index 0000000..d3f96d9
--- /dev/null
+++ b/test/encoding.py
@@ -0,0 +1,98 @@
+# -*- coding: utf-8 -*-
+
+# ******************************************************************************
+#
+# Copyright (C) 2010 Olivier Tilloy <olivier@tilloy.net>
+#
+# This file is part of the pyexiv2 distribution.
+#
+# pyexiv2 is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# pyexiv2 is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with pyexiv2; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
+#
+# Author: Olivier Tilloy <olivier@tilloy.net>
+#
+# ******************************************************************************
+
+import unittest
+import os
+import sys
+import binascii
+from tempfile import gettempdir
+
+from pyexiv2.metadata import ImageMetadata
+
+
+_HEXDATA = """
+ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48
+00 48 00 00 ff e1 00 36 45 78 69 66 00 00 49 49
+2a 00 08 00 00 00 01 00 32 01 02 00 14 00 00 00
+1a 00 00 00 00 00 00 00 32 30 31 30 3a 30 33 3a
+31 38 20 31 33 3a 33 39 3a 35 38 00 ff db 00 43
+00 05 03 04 04 04 03 05 04 04 04 05 05 05 06 07
+0c 08 07 07 07 07 0f 0b 0b 09 0c 11 0f 12 12 11
+0f 11 11 13 16 1c 17 13 14 1a 15 11 11 18 21 18
+1a 1d 1d 1f 1f 1f 13 17 22 24 22 1e 24 1c 1e 1f
+1e ff db 00 43 01 05 05 05 07 06 07 0e 08 08 0e
+1e 14 11 14 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e
+1e 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e 1e
+1e 1e 1e 1e 1e 1e ff c0 00 11 08 00 01 00 01 03
+01 22 00 02 11 01 03 11 01 ff c4 00 15 00 01 01
+00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08
+ff c4 00 14 10 01 00 00 00 00 00 00 00 00 00 00
+00 00 00 00 00 00 ff c4 00 14 01 01 00 00 00 00
+00 00 00 00 00 00 00 00 00 00 00 00 ff c4 00 14
+11 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+00 00 ff da 00 0c 03 01 00 02 11 03 11 00 3f 00
+b2 c0 07 ff d9
+"""
+
+_BINDATA = binascii.unhexlify(''.join(_HEXDATA.split()))
+
+
+class TestEncodings(unittest.TestCase):
+
+ def setUp(self):
+ self._cwd = os.getcwd()
+ os.chdir(gettempdir())
+
+ def tearDown(self):
+ os.chdir(self._cwd)
+
+ def _create_file(self, filename):
+ try:
+ os.remove(filename)
+ except OSError:
+ pass
+ fd = open(filename, 'wb')
+ fd.write(_BINDATA)
+ fd.close()
+
+ def _test_filename(self, filename):
+ self._create_file(filename)
+ m = ImageMetadata(filename)
+ m.read()
+ os.remove(filename)
+
+ def test_ascii_str(self):
+ self._test_filename('test.jpg')
+
+ def test_ascii_unicode(self):
+ self._test_filename(u'test.jpg')
+
+ def test_nonascii_unicode(self):
+ self._test_filename(u'tést.jpg')
+
+ def test_nonascii_unicode_escaped(self):
+ self._test_filename(u't\xe9st.jpg')
+