hMatoba / Piexif

Exif manipulation with pure python script.
MIT License
367 stars 81 forks source link

Python 2.7: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode #64

Closed ndbroadbent closed 5 years ago

ndbroadbent commented 6 years ago

I'm running py.test and seeing the following warnings:

  /usr/local/lib/python2.7/site-packages/piexif/_load.py:67: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
    if data[0:2] == b"\xff\xd8":  # JPEG

tests/test_download_photos.py::DownloadPhotoTestCase::test_download_photos_and_set_exif
  /usr/local/lib/python2.7/site-packages/piexif/_insert.py:22: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
    if image[0:2] == b"\xff\xd8":

-- Docs: http://doc.pytest.org/en/latest/warnings.html
ndbroadbent commented 5 years ago

Reading up on byte literals: https://stackoverflow.com/a/6269785/304706

A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.

Does this mean that Piexif will not work on Python 2?

Just trying to figure out this unicode warning because it's making my tests a bit noisy. And also I'm starting to think that the comparisons are actually broken in Python 2, so it might not even be working.

ndbroadbent commented 5 years ago

I was able to remove the warning by changing /usr/local/lib/python2.7/site-packages/piexif/_load.py:67 to:

if data[0:2] == u"\xff\xd8":  # JPEG

(instead of b"\xff\xd8".)

But I'm actually not sure if that is the correct behavior. Maybe the type of data needs to be changed so that it is not unicode?

ndbroadbent commented 5 years ago

Oh wow, just reading through the code, I realized that data is actually the filename I'm passing in, and not the file data!

class _ExifReader(object):
    def __init__(self, data):
        if data[0:2] == b"\xff\xd8":  # JPEG

So it first checks to see if the filename string is an image, and if that fails, then it tries to open the string as a file path. So I think it's safe to say that in Python 2.7, if data is a unicode string, then it's not going to be some image data. However, a non-unicode string might be data OR a filename.

ndbroadbent commented 5 years ago

Fixed these warnings with a PR in #68

hMatoba commented 5 years ago

Thank you for sending pull request. But I'm using unittest and have no problem. https://travis-ci.org/hMatoba/Piexif/builds/402180473

ndbroadbent commented 5 years ago

Hello, yes this not an error, but python shows a warning in the console. It is happening because I am passing filename strings that have unicode characters. For example:

exif_dict = piexif.load("./相片/photo.jpg")

This works fine on Python 3 but shows a warning on Python 2.7

jdpiguet commented 5 years ago

I have the same problem with Python 2.7 (Linux Mint). It happens if the file name is a Unicode string. `jdp@amroth:~$ python Python 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information.

import piexif filepath = u'/net/gandalf/srv/photos/Camino/2017/test/20170826_111047.jpg' exif_datas = piexif.load(filepath) /usr/local/lib/python2.7/dist-packages/piexif/_load.py:67: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if data[0:2] == b"\xff\xd8": # JPEG

`