thebigmunch / audio-metadata

A library for reading and, in the future, writing audio metadata. https://audio-metadata.readthedocs.io/
https://forum.thebigmunch.me
MIT License
57 stars 11 forks source link

InvalidHeader exception hangs instead of processing #9

Closed kristiankiralyfp closed 5 years ago

kristiankiralyfp commented 5 years ago

I find that when I try to catch InvalidHeader exceptions on audio_metadata.load() the code will not execute in the 'except audio_metadata.exceptions.InvalidHeader:' block. Even a 'print("test")' does not execute if I catch the exception. My only options are to not catch the exception and have the code crash or to catch it and then raise it again to have the code crash.

I have a loop that iterates over a list of files to grab metadata from each. The first step is trying to get the metadata, and I handle exceptions right away before the rest of the loop code is executed.

The below prints "Test" and then crashes with the exception:

except audio_metadata.exceptions.InvalidHeader:
    print("Test")
    raise

The below does not print "Test" and hangs:

except audio_metadata.exceptions.InvalidHeader:
    print("Test")
    continue

In case it helps, this is the way my loop is set up:

for f in filenames:

    try:
        metadata = audio_metadata.load(f)
    except audio_metadata.exceptions.UnsupportedFormat:
        print("Error - Unsupported format. Skipping file")
        continue
    except audio_metadata.exceptions.InvalidHeader:
        print("Error - Invalid header. Skipping file")
        continue
    <process metadata>

Sorry, I'm pretty new to adding issues to GitHub and can't figure out how to show indentation, but it is all properly indented in code.

Thank you, thebigmunch, for fixing my formatting!

kristiankiralyfp commented 5 years ago

@thebigmunch if it helps, I can send the song that raises the exception in case it's specific to this file, but it's too large to fit in an attachment to this comment (26.4 MB)

thebigmunch commented 5 years ago

The below prints "Test" and then crashes with the exception:

The snippet doesn't seem to show the traceback of the exception, so doesn't do much to help : P

I can send the song that raises the exception in case it's specific to this file, but it's too large to fit in an attachment to this comment (26.4 MB)

Sure, that'd be great, too.

Also, I have the exceptions at the top level of the module, so you can do: except audio_metadata.InvalidHeader to save some typing unless you are being super explicit.

kristiankiralyfp commented 5 years ago

Thanks for the tip on syntactic sugar! I'll keep that in mind.

As for the traceback:

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    metadata = audio_metadata.load(f)
  File "/usr/local/lib/python3.7/site-packages/audio_metadata/api.py", line 119, in load
    return parser_cls.load(fileobj)
  File "/usr/local/lib/python3.7/site-packages/audio_metadata/formats/wav.py", line 91, in load
    raise InvalidHeader("Valid WAVE header not found.")
audio_metadata.exceptions.InvalidHeader: Valid WAVE header not found.

Here's a link to the song: https://we.tl/t-eIeI77TWXg

kristiankiralyfp commented 5 years ago

Though the exception is not the issue, I expect it to cause an exception. I just want to be able to catch it and continue execution!

thebigmunch commented 5 years ago

Yeah, you're looking for some basic Python syntax. You should be doing the <metadata processing> bit in an else clause to your try. That way it only runs if the try part of it doesn't throw an exception.

thebigmunch commented 5 years ago

More info can be found in the Python exceptions docs.

Good luck.

kristiankiralyfp commented 5 years ago

Right you are! Thanks for the help :)