diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-12-14 19:34:33 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-12-14 19:34:33 +0100 |
commit | ae52309c6eb60da7ea4e5a014f407b29aca14074 (patch) | |
tree | 8dfa2dc921270ddc09467091154d948adafb5135 | |
parent | c66c2afdf44a7dc73dfa646c491efe05ee813e89 (diff) | |
download | pyexiv2-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.py | 29 |
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: |