Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "local variable 'file_type' referenced before assignment", and improve performance in common exif removal case #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

1.1.4
-----

- Fixed "local variable 'file_type' referenced before assignment" if image type wasn't known.
- No longer write image back to source image file if nothing has changed for better performance.

1.1.3
-----

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



VERSION = '1.1.3'
VERSION = '1.1.4'
56 changes: 30 additions & 26 deletions piexif/_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,35 @@ def remove(src, new_file=None):
file_type = "jpeg"
elif src_data[0:4] == b"RIFF" and src_data[8:12] == b"WEBP":
file_type = "webp"

if file_type == "jpeg":
segments = split_into_segments(src_data)
exif = get_exif_seg(segments)
if exif:
new_data = src_data.replace(exif, b"")
else:
new_data = src_data
elif file_type == "webp":
try:
new_data = _webp.remove(src_data)
except ValueError:
new_data = src_data
except e:
print(e.args)
raise ValueError("Error occurred.")
file_type = None

if isinstance(new_file, io.BytesIO):
new_file.write(new_data)
new_file.seek(0)
elif new_file:
with open(new_file, "wb+") as f:
f.write(new_data)
elif output_is_file:
with open(src, "wb+") as f:
f.write(new_data)
else:
raise ValueError("Give a second argument to 'remove' to output file")
if file_type is not None:
if file_type == "jpeg":
segments = split_into_segments(src_data)
exif = get_exif_seg(segments)
if exif:
new_data = src_data.replace(exif, b"")
else:
new_data = src_data
elif file_type == "webp":
try:
new_data = _webp.remove(src_data)
except ValueError:
new_data = src_data
except e:
print(e.args)
raise ValueError("Error occurred.")

if isinstance(new_file, io.BytesIO):
new_file.write(new_data)
new_file.seek(0)
elif new_file:
with open(new_file, "wb+") as f:
f.write(new_data)
elif output_is_file:
if new_data is not src_data:
with open(src, "wb+") as f:
f.write(new_data)
else:
raise ValueError("Give a second argument to 'remove' to output file")
1 change: 1 addition & 0 deletions tests/images/not_an_image.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This tests _remove() handling of files that are not jpgs or webp
8 changes: 8 additions & 0 deletions tests/s_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,14 @@ def test_remove(self):
piexif.remove(IMAGE_DIR + filename, OUT_DIR + "rr_" + filename)
Image.open(OUT_DIR + "rr_" + filename)

def test_remove_unknown_image_type(self):
"""Does remove ignore unknown image types?"""
IMAGE_DIR = "tests/images/"
OUT_DIR = "tests/images/out/"
filename = "not_an_image.txt"
piexif.remove(IMAGE_DIR + filename, OUT_DIR + "rr_" + filename)
self.assertFalse(os.path.isfile(OUT_DIR + "rr_" + filename))

def test_insert(self):
"""Can PIL open WebP that is inserted exif?"""
IMAGE_DIR = "tests/images/"
Expand Down