Skip to content

Commit 8cd240f

Browse files
authored
Merge pull request #4388 from lanzagar/pickle-protocol
[FIX] Explicitly define the protocol version for pickling
2 parents 796bd26 + 2335aa8 commit 8cd240f

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

Orange/data/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from .domain import *
77
from .storage import *
88
from .table import *
9-
from .io_base import *
109
from .io_util import *
1110
from .io import *
1211
from .filter import *

Orange/data/io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import openpyxl
2424

2525
from Orange.data import _io, Table, Domain, ContinuousVariable
26-
from Orange.data import Flags, FileFormatBase, DataTableMixin
2726
from Orange.data import Compression, open_compressed, detect_encoding, \
2827
isnastr, guess_data_type, sanitize_variable
28+
from Orange.data.io_base import FileFormatBase, Flags, DataTableMixin, PICKLE_PROTOCOL
2929

3030
from Orange.util import flatten
3131

@@ -218,7 +218,7 @@ def read(self):
218218
@classmethod
219219
def write_file(cls, filename, data):
220220
with cls.open(filename, 'wb') as f:
221-
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
221+
pickle.dump(data, f, protocol=PICKLE_PROTOCOL)
222222

223223

224224
class BasketReader(FileFormat):

Orange/data/io_base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
from Orange.data.variable import VariableMeta
2424
from Orange.util import Registry, flatten, namegen
2525

26-
__all__ = ["FileFormatBase", "Flags", "DataTableMixin"]
26+
__all__ = ["FileFormatBase", "Flags", "DataTableMixin", "PICKLE_PROTOCOL"]
27+
28+
29+
PICKLE_PROTOCOL = 4
2730

2831

2932
class Flags:
@@ -605,7 +608,7 @@ def write_file(fn):
605608
for kv in data.attributes.items()))
606609
else:
607610
with open(fn, 'wb') as f:
608-
pickle.dump(data.attributes, f, pickle.HIGHEST_PROTOCOL)
611+
pickle.dump(data.attributes, f, protocol=PICKLE_PROTOCOL)
609612

610613
if isinstance(filename, str):
611614
metafile = filename + '.metadata'

Orange/tests/test_io.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Test methods with long descriptive names can omit docstrings
22
# pylint: disable=missing-docstring
33

4-
import unittest
5-
from unittest.mock import Mock, patch
4+
import io
65
import os
7-
import tempfile
6+
import pickle
87
import shutil
9-
import io
8+
import tempfile
9+
import unittest
10+
from unittest.mock import Mock, patch
1011

1112
from Orange import data
1213

1314
from Orange.data.io import FileFormat, TabReader, CSVReader, PickleReader
15+
from Orange.data.io_base import PICKLE_PROTOCOL
1416
from Orange.data.table import get_sample_datasets_dir
1517
from Orange.data import Table, Variable
1618
from Orange.tests import test_dirname
@@ -180,3 +182,17 @@ def test_load_pickle(self):
180182
self.assertEqual(attributes_count, len(data2.domain.attributes))
181183
self.assertEqual(attributes_count, len(data3.domain.attributes))
182184
self.assertEqual(attributes_count, len(data4.domain.attributes))
185+
186+
def test_pickle_version(self):
187+
"""
188+
Orange uses a fixed PICKLE_PROTOCOL (currently set to 4)
189+
for pickling data files and possibly elsewhere for consistent
190+
behaviour across different python versions (e.g. 3.6 - 3.8).
191+
When the default protocol is increased in a future version of python
192+
we should consider increasing this constant to match it as well.
193+
"""
194+
# we should use a version that is at least as high as the default.
195+
# it could be higher for older (but supported) python versions
196+
self.assertGreaterEqual(PICKLE_PROTOCOL, pickle.DEFAULT_PROTOCOL)
197+
# we should not use a version that is not supported
198+
self.assertLessEqual(PICKLE_PROTOCOL, pickle.HIGHEST_PROTOCOL)

0 commit comments

Comments
 (0)