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

Improved flexibility of XMP parsing #7274

Merged
merged 4 commits into from Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/xmp_no_prefix.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/xmp_padded.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 26 additions & 1 deletion Tests/test_file_jpeg.py
Expand Up @@ -882,7 +882,10 @@ def read(n=-1):
def test_getxmp(self):
with Image.open("Tests/images/xmp_test.jpg") as im:
if ElementTree is None:
with pytest.warns(UserWarning):
with pytest.warns(
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}
else:
xmp = im.getxmp()
Expand All @@ -905,6 +908,28 @@ def test_getxmp(self):
with Image.open("Tests/images/hopper.jpg") as im:
assert im.getxmp() == {}

def test_getxmp_no_prefix(self):
with Image.open("Tests/images/xmp_no_prefix.jpg") as im:
if ElementTree is None:
with pytest.warns(
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}
else:
assert im.getxmp() == {"xmpmeta": {"key": "value"}}

def test_getxmp_padded(self):
with Image.open("Tests/images/xmp_padded.jpg") as im:
if ElementTree is None:
with pytest.warns(
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}
else:
assert im.getxmp() == {"xmpmeta": None}

@pytest.mark.timeout(timeout=1)
def test_eof(self):
# Even though this decoder never says that it is finished
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_png.py
Expand Up @@ -665,7 +665,10 @@ def test_plte_length(self, tmp_path):
def test_getxmp(self):
with Image.open("Tests/images/color_snakes.png") as im:
if ElementTree is None:
with pytest.warns(UserWarning):
with pytest.warns(
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}
else:
xmp = im.getxmp()
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_tiff.py
Expand Up @@ -734,7 +734,10 @@ def test_discard_icc_profile(self, tmp_path):
def test_getxmp(self):
with Image.open("Tests/images/lab.tif") as im:
if ElementTree is None:
with pytest.warns(UserWarning):
with pytest.warns(
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}
else:
xmp = im.getxmp()
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_webp_metadata.py
Expand Up @@ -118,7 +118,10 @@ def test_getxmp():

with Image.open("Tests/images/flower2.webp") as im:
if ElementTree is None:
with pytest.warns(UserWarning):
with pytest.warns(
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}
else:
assert (
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Expand Up @@ -1385,7 +1385,7 @@ def getextrema(self):

def _getxmp(self, xmp_tags):
def get_name(tag):
return tag.split("}")[1]
return re.sub("^{[^}]+}", "", tag)

def get_value(element):
value = {get_name(k): v for k, v in element.attrib.items()}
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/JpegImagePlugin.py
Expand Up @@ -496,7 +496,7 @@ def getxmp(self):

for segment, content in self.applist:
if segment == "APP1":
marker, xmp_tags = content.rsplit(b"\x00", 1)
marker, xmp_tags = content.split(b"\x00")[:2]
if marker == b"http://ns.adobe.com/xap/1.0/":
return self._getxmp(xmp_tags)
return {}
Expand Down