File: test_pickle.py

package info (click to toggle)
python-wrapt 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,592 kB
  • sloc: python: 8,452; ansic: 2,978; makefile: 168; sh: 46
file content (35 lines) | stat: -rw-r--r-- 763 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
import unittest

import pickle

import wrapt


class CustomObjectProxy(wrapt.ObjectProxy):

    def __reduce_ex__(self, proto):
        return (list, (self.__wrapped__,))


class TestObjectPickle(unittest.TestCase):

    def test_pickle(self):
        proxy = wrapt.ObjectProxy([1])

        with self.assertRaises(NotImplementedError) as context:
            data = pickle.dumps(proxy)

        self.assertTrue(
            str(context.exception) == "object proxy must define __reduce_ex__()"
        )

    def test_pickle_proxy(self):
        proxy1 = CustomObjectProxy([1])
        pickled = pickle.dumps(proxy1)
        restored = pickle.loads(pickled)

        self.assertEqual(proxy1.__wrapped__, restored)


if __name__ == "__main__":
    unittest.main()