aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2010-12-14 19:34:33 +0100
committerOlivier Tilloy <olivier@tilloy.net>2010-12-14 19:34:33 +0100
commitae52309c6eb60da7ea4e5a014f407b29aca14074 (patch)
tree8dfa2dc921270ddc09467091154d948adafb5135
parentc66c2afdf44a7dc73dfa646c491efe05ee813e89 (diff)
downloadpyexiv2-ae52309c6eb60da7ea4e5a014f407b29aca14074.tar.gz
Best effort, do not fail just because the original encoding of the tag cannot encode the new value.
-rw-r--r--src/pyexiv2/exif.py29
1 files changed, 17 insertions, 12 deletions
diff --git a/src/pyexiv2/exif.py b/src/pyexiv2/exif.py
index 71473a1..ed9eb4d 100644
--- a/src/pyexiv2/exif.py
+++ b/src/pyexiv2/exif.py
@@ -367,24 +367,29 @@ class ExifTag(ListenerInterface):
raise ExifValueError(value, self.type)
elif self.type == 'Comment':
- if self.raw_value is not None and \
+ if value is not None and self.raw_value is not None and \
self.raw_value.startswith('charset='):
charset, val = self.raw_value.split(' ', 1)
charset = charset.split('=')[1].strip('"')
encoding = self._match_encoding(charset)
- val = value.encode(encoding, 'replace')
- return 'charset="%s" %s' % (charset, val)
- else:
- # No encoding defined.
- if isinstance(value, unicode):
- try:
- return value.encode('utf-8')
- except UnicodeEncodeError:
- raise ExifValueError(value, self.type)
- elif isinstance(value, str):
- return value
+ try:
+ val = value.encode(encoding, 'replace')
+ except UnicodeDecodeError:
+ # Best effort, do not fail just because the original
+ # encoding of the tag cannot encode the new value.
+ pass
else:
+ return 'charset="%s" %s' % (charset, val)
+
+ if isinstance(value, unicode):
+ try:
+ return value.encode('utf-8')
+ except UnicodeEncodeError:
raise ExifValueError(value, self.type)
+ elif isinstance(value, str):
+ return value
+ else:
+ raise ExifValueError(value, self.type)
elif self.type == 'Short':
if isinstance(value, int) and value >= 0: