Skip to content

Commit 0dff252

Browse files
committed
Merge pull request #6427 from jkseppan/pdf-dict-order
MNT: Output pdf dicts in deterministic order
2 parents 927631e + 9dfb35d commit 0dff252

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/matplotlib/backends/backend_pdf.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import warnings
1919
import zlib
2020
from io import BytesIO
21+
from functools import total_ordering
2122

2223
import numpy as np
2324
from matplotlib.externals.six import unichr
@@ -182,8 +183,8 @@ def pdfRepr(obj):
182183
# represented as Name objects.
183184
elif isinstance(obj, dict):
184185
r = [b"<<"]
185-
r.extend([Name(key).pdfRepr() + b" " + pdfRepr(val)
186-
for key, val in six.iteritems(obj)])
186+
r.extend([Name(key).pdfRepr() + b" " + pdfRepr(obj[key])
187+
for key in sorted(obj)])
187188
r.append(b">>")
188189
return fill(r)
189190

@@ -243,6 +244,7 @@ def write(self, contents, file):
243244
write(b"\nendobj\n")
244245

245246

247+
@total_ordering
246248
class Name(object):
247249
"""PDF name object."""
248250
__slots__ = ('name',)
@@ -262,6 +264,15 @@ def __repr__(self):
262264
def __str__(self):
263265
return '/' + six.text_type(self.name)
264266

267+
def __eq__(self, other):
268+
return isinstance(other, Name) and self.name == other.name
269+
270+
def __lt__(self, other):
271+
return isinstance(other, Name) and self.name < other.name
272+
273+
def __hash__(self):
274+
return hash(self.name)
275+
265276
@staticmethod
266277
def hexify(match):
267278
return '#%02x' % ord(match.group())

0 commit comments

Comments
 (0)