File: hashers.py

package info (click to toggle)
python-django-imagekit 6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 688 kB
  • sloc: python: 2,044; makefile: 133; sh: 6
file content (39 lines) | stat: -rw-r--r-- 961 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sys
from copy import copy
from hashlib import md5
from io import BytesIO
from pickle import DICT, MARK, _Pickler


class CanonicalizingPickler(_Pickler):
    dispatch = copy(_Pickler.dispatch)

    def save_set(self, obj):
        rv = obj.__reduce_ex__(0)
        rv = (rv[0], (sorted(rv[1][0]),), rv[2])
        self.save_reduce(obj=obj, *rv)

    dispatch[set] = save_set

    if sys.version_info[:2] >= (3, 14):
        def save_dict(self, obj):
            write = self.write
            write(MARK + DICT)

            self.memoize(obj)
            self._batch_setitems(sorted(obj.items()), obj)
    else:
        def save_dict(self, obj):
            write = self.write
            write(MARK + DICT)

            self.memoize(obj)
            self._batch_setitems(sorted(obj.items()))

    dispatch[dict] = save_dict


def pickle(obj):
    file = BytesIO()
    CanonicalizingPickler(file, 0).dump(obj)
    return md5(file.getvalue()).hexdigest()