8000 Merge pull request #17935 from tacaswell/mnt_better_warning_on_pdfmet… · matplotlib/matplotlib@1dcfce6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1dcfce6

Browse files
authored
Merge pull request #17935 from tacaswell/mnt_better_warning_on_pdfmetadata
MNT: improve error messages on bad pdf metadata input
2 parents 5b835ee + c964cfb commit 1dcfce6

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,18 @@ def _create_pdf_info_dict(backend, metadata):
184184

185185
def is_string_like(x):
186186
return isinstance(x, str)
187+
is_string_like.text_for_warning = "an instance of str"
187188

188189
def is_date(x):
189190
return isinstance(x, datetime)
191+
is_date.text_for_warning = "an instance of datetime.datetime"
190192

191193
def check_trapped(x):
192194
if isinstance(x, Name):
193195
return x.name in (b'True', b'False', b'Unknown')
194196
else:
195197
return x in ('True', 'False', 'Unknown')
198+
check_trapped.text_for_warning = 'one of {"True", "False", "Unknown"}'
196199

197200
keywords = {
198201
'Title': is_string_like,
@@ -207,9 +210,12 @@ def check_trapped(x):
207210
}
208211
for k in info:
209212
if k not in keywords:
210-
cbook._warn_external(f'Unknown infodict keyword: {k}')
213+
cbook._warn_external(f'Unknown infodict keyword: {k!r}. '
214+
f'Must be one of {set(keywords)!r}.')
211215
elif not keywords[k](info[k]):
212-
cbook._warn_external(f'Bad value for infodict keyword {k}')
216+
cbook._warn_external(f'Bad value for infodict keyword {k}. '
217+
f'Got {info[k]!r} which is not '
218+
f'{keywords[k].text_for_warning}.')
213219
if 'Trapped' in info:
214220
info['Trapped'] = Name(info['Trapped'])
215221

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ def test_savefig_metadata(monkeypatch):
161161
}
162162

163163

164+
def test_invalid_metadata():
165+
fig, ax = plt.subplots()
166+
167+
with pytest.warns(UserWarning,
168+
match="Unknown infodict keyword: 'foobar'."):
169+
fig.savefig(io.BytesIO(), format='pdf', metadata={'foobar': 'invalid'})
170+
171+
with pytest.warns(UserWarning,
172+
match='not an instance of datetime.datetime.'):
173+
fig.savefig(io.BytesIO(), format='pdf',
174+
metadata={'ModDate': '1968-08-01'})
175+
176+
with pytest.warns(UserWarning,
177+
match='not one of {"True", "False", "Unknown"}'):
178+
fig.savefig(io.BytesIO(), format='pdf', metadata={'Trapped': 'foo'})
179+
180+
with pytest.warns(UserWarning, match='not an instance of str.'):
181+
fig.savefig(io.BytesIO(), format='pdf', metadata={'Title': 1234})
182+
183+
164184
def test_multipage_metadata(monkeypatch):
165185
pikepdf = pytest.importorskip('pikepdf')
166186
monkeypatch.setenv('SOURCE_DATE_EPOCH', '0')

0 commit comments

Comments
 (0)
0