Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Output pdf dicts in deterministic order #6427

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

Merged
merged 2 commits into from
May 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import warnings
import zlib
from io import BytesIO
from functools import total_ordering

import numpy as np
from matplotlib.externals.six import unichr
Expand Down Expand Up @@ -182,8 +183,8 @@ def pdfRepr(obj):
# represented as Name objects.
elif isinstance(obj, dict):
r = [b"<<"]
r.extend([Name(key).pdfRepr() + b" " + pdfRepr(val)
for key, val in six.iteritems(obj)])
r.extend([Name(key).pdfRepr() + b" " + pdfRepr(obj[key])
for key in sorted(obj)])
r.append(b">>")
return fill(r)

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


@total_ordering
class Name(object):
"""PDF name object."""
__slots__ = ('name',)
Expand All @@ -262,6 +264,15 @@ def __repr__(self):
def __str__(self):
return '/' + six.text_type(self.name)

def __eq__(self, other):
return isinstance(other, Name) and self.name == other.name

def __lt__(self, other):
return isinstance(other, Name) and self.name < other.name

def __hash__(self):
return hash(self.name)

@staticmethod
def hexify(match):
return '#%02x' % ord(match.group())
Expand Down