From 2e79c9ab8e89e3f19ff2e8cd7a563d8d9129edf1 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Tue, 7 Jan 2020 16:00:06 +0100 Subject: [PATCH] Fixed pickle AttributeError. #6 --- benedict/dicts/keypath.py | 2 ++ tests/test_pickle.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/test_pickle.py diff --git a/benedict/dicts/keypath.py b/benedict/dicts/keypath.py index 371fde3..9196f5d 100644 --- a/benedict/dicts/keypath.py +++ b/benedict/dicts/keypath.py @@ -7,6 +7,8 @@ from six import string_types class KeypathDict(dict): + _keypath_separator = None + def __init__(self, *args, **kwargs): """ Constructs a new instance. diff --git a/tests/test_pickle.py b/tests/test_pickle.py new file mode 100644 index 0000000..15f5456 --- /dev/null +++ b/tests/test_pickle.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +from benedict import benedict + +import pickle +import unittest + + +class pickle_test_case(unittest.TestCase): + + def test_pickle(self): + + d = { + 'a': {}, + 'b': { 'x': 1 }, + 'c': [], + 'd': [0, 1], + 'e': 0.0, + 'f': '', + 'g': None, + 'h': '0' + } + + b = benedict(d, keypath_separator='/') + b_encoded = pickle.dumps(b) + # print(b_encoded) + + b_decoded = pickle.loads(b_encoded) + # print(b_decoded) + # print(b_decoded.keypath_separator) + self.assertTrue(isinstance(b_decoded, benedict)) + self.assertEqual(b_decoded.keypath_separator, b.keypath_separator) + self.assertEqual(b_decoded, b)