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

Fixes sigal aborting if image contains malformed metadata #355

Merged
merged 4 commits into from Dec 18, 2018

Conversation

Projects
None yet
2 participants
@tudacs
Copy link
Contributor

tudacs commented Dec 17, 2018

Currently sigal (or better: Whatever reads the metadata) stops working if it encounters malformed Metadata in a picture. This fix prevents the metadata-reading functions from issueing fatal errors by catching the error, returning empty iptc-data and logging the affected file to the output.

@codecov

This comment has been minimized.

Copy link

codecov bot commented Dec 17, 2018

Codecov Report

Merging #355 into master will decrease coverage by 0.09%.
The diff coverage is 75%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #355     +/-   ##
=========================================
- Coverage   87.47%   87.37%   -0.1%     
=========================================
  Files          19       19             
  Lines        1429     1434      +5     
=========================================
+ Hits         1250     1253      +3     
- Misses        179      181      +2
Impacted Files Coverage Δ
sigal/image.py 91.09% <75%> (-0.84%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1c0b8d9...a65a3de. Read the comment docs.

@saimn

This comment has been minimized.

Copy link
Owner

saimn commented Dec 17, 2018

Do you have an example of the traceback raised without this ? I'm surprised by the SyntaxError.

@tudacs

This comment has been minimized.

Copy link
Contributor Author

tudacs commented Dec 18, 2018

That's the setup:

 $ pip list
Package    Version
---------- -------
blinker    1.4
Click      7.0
Jinja2     2.10
Markdown   3.0.1
MarkupSafe 1.1.0
pilkit     2.0
Pillow     5.3.0
pip        18.1
setuptools 40.6.3
sigal      1.4.1
wheel      0.32.3

... and the Traceback:

$ sigal build
Collecting albums -Traceback (most recent call last):
  File "sigal/bin/sigal", line 11, in <module>
    sys.exit(main())
  File "sigal/local/lib/python2.7/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "sigal/local/lib/python2.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "sigal/local/lib/python2.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "sigal/local/lib/python2.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "sigal/local/lib/python2.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "sigal/local/lib/python2.7/site-packages/sigal/__init__.py", line 141, in build
    gal = Gallery(settings, ncpu=ncpu)
  File "sigal/local/lib/python2.7/site-packages/sigal/gallery.py", line 582, in __init__
    album = Album(relpath, settings, dirs, files, self)
  File "sigal/local/lib/python2.7/site-packages/sigal/gallery.py", line 277, in __init__
    media = Image(f, self.path, settings)
  File "sigal/local/lib/python2.7/site-packages/sigal/gallery.py", line 86, in __init__
    self._get_metadata()
  File "sigal/local/lib/python2.7/site-packages/sigal/gallery.py", line 178, in _get_metadata
    iptc_data = get_iptc_data(self.src_path)
  File "sigal/local/lib/python2.7/site-packages/sigal/image.py", line 247, in get_iptc_data
    raw_iptc = IptcImagePlugin.getiptcinfo(img)
  File "sigal/local/lib/python2.7/site-packages/PIL/IptcImagePlugin.py", line 251, in getiptcinfo
    im._open()
  File "sigal/local/lib/python2.7/site-packages/PIL/IptcImagePlugin.py", line 91, in _open
    tag, size = self.field()
  File "sigal/local/lib/python2.7/site-packages/PIL/IptcImagePlugin.py", line 71, in field
    raise SyntaxError("invalid IPTC/NAA file")
SyntaxError: invalid IPTC/NAA file

$

I have shortened the paths a bit to just show the part inside the virtualenv. The image causing PIL to fail is not mentioned.

@saimn

This comment has been minimized.

Copy link
Owner

saimn commented Dec 18, 2018

Ok thanks, SyntaxError should not be used for this, could you add a comment in the except to explain where it comes from ? With a link to https://github.com/python-pillow/Pillow/blob/9dd0348be2751beb2c617e32ff9985aa2f92ae5f/src/PIL/IptcImagePlugin.py#L71.

if raw_iptc and (2, 120) in raw_iptc:
iptc_data["description"] = raw_iptc[(2, 120)].decode('utf-8')
except SyntaxError:
print("IPTC Error in %s\n"%filename)

This comment has been minimized.

@saimn

saimn Dec 18, 2018

Owner

Please use logging instead of a print (see below in the file for examples).

This comment has been minimized.

@tudacs

tudacs Dec 18, 2018

Author Contributor

Thanks for the hint! :) (and/or sorry for not looking for coding standards in sigal first...)

tudacs added some commits Dec 18, 2018

Shortens try/sxcept block to keep code cleaner
... and adds a comment where the SyntaxError may come from.

@saimn saimn merged commit 714eeae into saimn:master Dec 18, 2018

1 of 3 checks passed

codecov/patch 75% of diff hit (target 87.47%)
Details
codecov/project 87.37% (-0.1%) compared to 1c0b8d9
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@saimn

This comment has been minimized.

Copy link
Owner

saimn commented Dec 18, 2018

Thanks @tudacs !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment