Skip to content

Commit

Permalink
Add some error detail to 'dump'.
Browse files Browse the repository at this point in the history
  • Loading branch information
hMatoba committed Aug 28, 2015
1 parent 8065193 commit 4b6f8bc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ samples2
load_sample.py
*.pdf
*.suo
doc/_build/
doc/_build/
run_coverage.bat
5 changes: 5 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.0.2
-----

- Add some error detail to 'dump'.

1.0.1
-----

Expand Down
2 changes: 1 addition & 1 deletion piexif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from ._exif import *


VERSION = '1.0.1'
VERSION = '1.0.2'
31 changes: 24 additions & 7 deletions piexif/_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def dump(exif_dict_original):
first_ifd = exif_dict["1st"]

zeroth_set = _dict_to_bytes(zeroth_ifd, "0th", 0)
zeroth_length = (len(zeroth_set[0]) + exif_is * 12 + gps_is * 12 + 4 +
zeroth_length = (len(zeroth_set[0]) + exif_is * 12 + gps_is * 12 + 4 +
len(zeroth_set[1]))

if exif_is:
Expand Down Expand Up @@ -205,7 +205,10 @@ def _value_to_bytes(raw_value, value_type, offset):
try:
new_value = raw_value.encode("latin1") + b"\x00"
except:
new_value = raw_value + b"\x00"
try:
new_value = raw_value + b"\x00"
except TypeError:
raise ValueError("Got invalid type to convert.")
length = len(new_value)
if length > 4:
value_str = struct.pack(">I", offset)
Expand Down Expand Up @@ -244,9 +247,16 @@ def _value_to_bytes(raw_value, value_type, offset):
length = len(raw_value)
if length > 4:
value_str = struct.pack(">I", offset)
four_bytes_over = raw_value
try:
four_bytes_over = b"" + raw_value
except TypeError:
raise ValueError("Got invalid type to convert.")
else:
value_str = raw_value + b"\x00" * (4 - length)
try:
value_str = raw_value + b"\x00" * (4 - length)
except TypeError:
raise ValueError("Got invalid type to convert.")


length_str = struct.pack(">I", length)
return length_str, value_str, four_bytes_over
Expand Down Expand Up @@ -279,9 +289,16 @@ def _dict_to_bytes(ifd_dict, ifd, ifd_offset):
raw_value = (raw_value,)
offset = TIFF_HEADER_LENGTH + entries_length + ifd_offset + len(values)

length_str, value_str, four_bytes_over = _value_to_bytes(raw_value,
value_type,
offset)
try:
length_str, value_str, four_bytes_over = _value_to_bytes(raw_value,
value_type,
offset)
except ValueError:
raise ValueError(
'"dump" got wrong type of exif value.\n' +
'{0} in {1} IFD. Got as {2}.'.format(key, ifd, type(ifd_dict[key]))
)

entries += key_str + type_str + length_str + value_str
values += four_bytes_over
return (entry_header + entries, values)
28 changes: 23 additions & 5 deletions tests/s_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,25 @@ def test_dump_fail(self):
"1st":FIRST_DICT,
"thumbnail":thumb_data}
with self.assertRaises(ValueError):
exif_bytes = piexif.dump(exif_dict)
piexif.dump(exif_dict)

def test_dump_fail2(self):
exif_ifd = {ExifIFD.DateTimeOriginal: 123}
exif_dict = {"Exif":exif_ifd}
with self.assertRaises(ValueError):
piexif.dump(exif_dict)

def test_dump_fail3(self):
exif_ifd = {ExifIFD.OECF: 1}
exif_dict = {"Exif":exif_ifd}
with self.assertRaises(ValueError):
piexif.dump(exif_dict)

def test_dump_fail4(self):
exif_ifd = {ExifIFD.OECF: (1, 2, 3, 4, 5)}
exif_dict = {"Exif":exif_ifd}
with self.assertRaises(ValueError):
piexif.dump(exif_dict)

# load and dump ------
def test_dump_and_load(self):
Expand Down Expand Up @@ -249,10 +267,10 @@ def test_dump_and_load2(self):

def test_dump_and_load3(self):
ascii_v = ["a", "ab", "abc", "abcd", "abcde"]
undefined_v = [b"\x00",
b"\x00\x01",
b"\x00\x01\x02",
b"\x00\x01\x02\x03",
undefined_v = [b"\x00",
b"\x00\x01",
b"\x00\x01\x02",
b"\x00\x01\x02\x03",
b"\x00\x01\x02\x03\x04"]
byte_v = [255,
(255, 254),
Expand Down

0 comments on commit 4b6f8bc

Please sign in to comment.