Commit 4630d05f authored by Marius Gedminas's avatar Marius Gedminas

Extract most compat code into ZODB._compat

parent 7f4c41b3
...@@ -25,27 +25,13 @@ import sys ...@@ -25,27 +25,13 @@ import sys
from struct import pack as _structpack, unpack as _structunpack from struct import pack as _structpack, unpack as _structunpack
import zope.interface import zope.interface
from persistent.TimeStamp import TimeStamp from persistent.TimeStamp import TimeStamp
import ZODB.interfaces import ZODB.interfaces
from ZODB import POSException from ZODB import POSException
from ZODB.utils import z64, oid_repr, byte_ord, byte_chr from ZODB.utils import z64, oid_repr, byte_ord, byte_chr
from ZODB.UndoLogCompatible import UndoLogCompatible from ZODB.UndoLogCompatible import UndoLogCompatible
from ZODB._compat import pickle, py2_hasattr
try:
import cPickle as pickle
except ImportError:
# Py3
import pickle
# Py3: Python 3's `hasattr()` only swallows AttributeError.
def py2_hasattr(obj, name):
try:
getattr(obj, name)
except:
return False
return True
log = logging.getLogger("ZODB.BaseStorage") log = logging.getLogger("ZODB.BaseStorage")
......
...@@ -14,25 +14,18 @@ ...@@ -14,25 +14,18 @@
import logging import logging
import sys import sys
from pickle import PicklingError
import six import six
import zope.interface import zope.interface
from ZODB.POSException import ConflictError from ZODB.POSException import ConflictError
from ZODB.loglevels import BLATHER from ZODB.loglevels import BLATHER
from ZODB.serialize import _protocol, _Unpickler from ZODB.serialize import _protocol, _Unpickler
from ZODB._compat import BytesIO, pickle
try: # Subtle: Python 2.x has pickle.PicklingError and cPickle.PicklingError,
from cStringIO import StringIO as BytesIO # and these are unrelated classes! So we shouldn't use pickle.PicklingError,
except ImportError: # since on Python 2, ZODB._compat.pickle is cPickle.
# Py3 from pickle import PicklingError
from io import BytesIO
try:
from cPickle import Pickler
except ImportError:
# Py3
from pickle import Pickler
logger = logging.getLogger('ZODB.ConflictResolution') logger = logging.getLogger('ZODB.ConflictResolution')
...@@ -290,7 +283,7 @@ def tryToResolveConflict(self, oid, committedSerial, oldSerial, newpickle, ...@@ -290,7 +283,7 @@ def tryToResolveConflict(self, oid, committedSerial, oldSerial, newpickle,
resolved = resolve(old, committed, newstate) resolved = resolve(old, committed, newstate)
file = BytesIO() file = BytesIO()
pickler = Pickler(file, _protocol) pickler = pickle.Pickler(file, _protocol)
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
pickler.inst_persistent_id = persistent_id pickler.inst_persistent_id = persistent_id
else: else:
......
...@@ -23,6 +23,7 @@ import warnings ...@@ -23,6 +23,7 @@ import warnings
from ZODB.broken import find_global from ZODB.broken import find_global
from ZODB.utils import z64 from ZODB.utils import z64
from ZODB.Connection import Connection from ZODB.Connection import Connection
from ZODB._compat import pickle, BytesIO
import ZODB.serialize import ZODB.serialize
import transaction.weakset import transaction.weakset
...@@ -36,17 +37,6 @@ import transaction ...@@ -36,17 +37,6 @@ import transaction
from persistent.TimeStamp import TimeStamp from persistent.TimeStamp import TimeStamp
import six import six
try:
import cPickle as pickle
except ImportError:
# Py3
import pickle
try:
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
logger = logging.getLogger('ZODB.DB') logger = logging.getLogger('ZODB.DB')
......
...@@ -12,30 +12,21 @@ ...@@ -12,30 +12,21 @@
# #
############################################################################## ##############################################################################
"""Support for database export and import.""" """Support for database export and import."""
import os
from tempfile import TemporaryFile
import logging import logging
import six import os
import sys import sys
from tempfile import TemporaryFile
import six
from ZODB.blob import Blob from ZODB.blob import Blob
from ZODB.interfaces import IBlobStorage from ZODB.interfaces import IBlobStorage
from ZODB.POSException import ExportError from ZODB.POSException import ExportError
from ZODB.serialize import referencesf, _protocol from ZODB.serialize import referencesf, _protocol
from ZODB.utils import p64, u64, cp, mktemp from ZODB.utils import p64, u64, cp, mktemp
from ZODB._compat import pickle, BytesIO
try:
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
try:
from cPickle import Unpickler, Pickler
except ImportError:
# Py3
from pickle import Unpickler, Pickler
logger = logging.getLogger('ZODB.ExportImport') logger = logging.getLogger('ZODB.ExportImport')
...@@ -176,11 +167,11 @@ class ExportImport: ...@@ -176,11 +167,11 @@ class ExportImport:
blob_filename = None blob_filename = None
pfile = BytesIO(data) pfile = BytesIO(data)
unpickler = Unpickler(pfile) unpickler = pickle.Unpickler(pfile)
unpickler.persistent_load = persistent_load unpickler.persistent_load = persistent_load
newp = BytesIO() newp = BytesIO()
pickler = Pickler(newp, _protocol) pickler = pickle.Pickler(newp, _protocol)
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
pickler.inst_persistent_id = persistent_id pickler.inst_persistent_id = persistent_id
else: else:
......
...@@ -15,9 +15,23 @@ ...@@ -15,9 +15,23 @@
""" """
from __future__ import print_function from __future__ import print_function
from persistent.TimeStamp import TimeStamp import binascii
import contextlib
import errno
import logging
import os
import threading
import time
from struct import pack, unpack from struct import pack, unpack
import six
import zope.interface
from persistent.TimeStamp import TimeStamp
from zc.lockfile import LockFile from zc.lockfile import LockFile
import ZODB.blob
import ZODB.interfaces
import ZODB.utils
from ZODB.FileStorage.format import CorruptedError, CorruptedDataError from ZODB.FileStorage.format import CorruptedError, CorruptedDataError
from ZODB.FileStorage.format import FileStorageFormatter, DataHeader from ZODB.FileStorage.format import FileStorageFormatter, DataHeader
from ZODB.FileStorage.format import TRANS_HDR, TRANS_HDR_LEN from ZODB.FileStorage.format import TRANS_HDR, TRANS_HDR_LEN
...@@ -27,31 +41,7 @@ from ZODB.fsIndex import fsIndex ...@@ -27,31 +41,7 @@ from ZODB.fsIndex import fsIndex
from ZODB import BaseStorage, ConflictResolution, POSException from ZODB import BaseStorage, ConflictResolution, POSException
from ZODB.POSException import UndoError, POSKeyError, MultipleUndoErrors from ZODB.POSException import UndoError, POSKeyError, MultipleUndoErrors
from ZODB.utils import p64, u64, z64, as_bytes, as_text from ZODB.utils import p64, u64, z64, as_bytes, as_text
from ZODB._compat import pickle, decodebytes, encodebytes
import binascii
import contextlib
import errno
import logging
import os
import six
import threading
import time
import ZODB.blob
import ZODB.interfaces
import zope.interface
import ZODB.utils
try:
from cPickle import Pickler, loads
except ImportError:
# Py3
from pickle import Pickler, loads
try:
# Py3
from base64 import decodebytes, encodebytes
except ImportError:
from base64 import decodestring as decodebytes, encodestring as encodebytes
# Not all platforms have fsync # Not all platforms have fsync
...@@ -379,7 +369,7 @@ class FileStorage( ...@@ -379,7 +369,7 @@ class FileStorage(
if not self._is_read_only: if not self._is_read_only:
# Save the converted index. # Save the converted index.
f = open(index_name, 'wb') f = open(index_name, 'wb')
p = Pickler(f, 1) p = pickle.Pickler(f, 1)
info['index'] = index info['index'] = index
p.dump(info) p.dump(info)
f.close() f.close()
...@@ -1011,7 +1001,7 @@ class FileStorage( ...@@ -1011,7 +1001,7 @@ class FileStorage(
th = self._read_txn_header(h.tloc) th = self._read_txn_header(h.tloc)
if th.ext: if th.ext:
d = loads(th.ext) d = pickle.loads(th.ext)
else: else:
d = {} d = {}
...@@ -1852,7 +1842,7 @@ class FileIterator(FileStorageFormatter): ...@@ -1852,7 +1842,7 @@ class FileIterator(FileStorageFormatter):
e = {} e = {}
if h.elen: if h.elen:
try: try:
e = loads(h.ext) e = pickle.loads(h.ext)
except: except:
pass pass
...@@ -1994,7 +1984,7 @@ class UndoSearch: ...@@ -1994,7 +1984,7 @@ class UndoSearch:
e = {} e = {}
if el: if el:
try: try:
e = loads(self.file.read(el)) e = pickle.loads(self.file.read(el))
except: except:
pass pass
d = {'id': encodebytes(tid).rstrip(), d = {'id': encodebytes(tid).rstrip(),
......
##############################################################################
#
# Copyright (c) 2013 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
try:
# Python 2.x
import cPickle as pickle
IMPORT_MAPPING = {}
NAME_MAPPING = {}
except ImportError:
# Python 3.x: can't use stdlib's pickle because
# http://bugs.python.org/issue6784
## import zodbpickle as pickle
import pickle
from _compat_pickle import IMPORT_MAPPING, NAME_MAPPING
# XXX: overridable Unpickler.find_global as used in serialize.py?
# XXX: consistent spelling of inst_persistent_id/persistent_id?
# e.g. StorageTestBase and probably elsewhere
try:
# Python 2.x
# XXX: why not just import BytesIO from io?
from cStringIO import StringIO as BytesIO
except ImportError:
# Python 3.x
from io import BytesIO
try:
# Python 3.x
from base64 import decodebytes, encodebytes
except ImportError:
# Python 2.x
from base64 import decodestring as decodebytes, encodestring as encodebytes
# Python 3.x: ``hasattr()`` swallows only AttributeError.
def py2_hasattr(obj, name):
try:
getattr(obj, name)
except:
return False
return True
try:
# Py2: simply reexport the builtin
long = long
except NameError:
# Py3
long = int
...@@ -26,28 +26,19 @@ import tempfile ...@@ -26,28 +26,19 @@ import tempfile
import weakref import weakref
import zope.interface import zope.interface
import persistent
import ZODB.interfaces import ZODB.interfaces
from ZODB.interfaces import BlobError from ZODB.interfaces import BlobError
from ZODB import utils, serialize from ZODB import utils, serialize
from ZODB.POSException import POSKeyError from ZODB.POSException import POSKeyError
import persistent from ZODB._compat import BytesIO
try:
import cPickle
except ImportError:
# Py3
import pickle as cPickle
try:
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
from io import FileIO as file from io import FileIO as file
logger = logging.getLogger('ZODB.blob') logger = logging.getLogger('ZODB.blob')
BLOB_SUFFIX = ".blob" BLOB_SUFFIX = ".blob"
......
...@@ -15,18 +15,13 @@ ...@@ -15,18 +15,13 @@
""" """
import sys import sys
import persistent
import persistent
import zope.interface import zope.interface
import ZODB.interfaces import ZODB.interfaces
from ZODB._compat import pickle, IMPORT_MAPPING, NAME_MAPPING
try:
# Python 3
import _compat_pickle
except ImportError:
# Python 2
_compat_pickle = None
broken_cache = {} broken_cache = {}
...@@ -87,12 +82,7 @@ class Broken(object): ...@@ -87,12 +82,7 @@ class Broken(object):
>>> r[2] >>> r[2]
{'x': 1} {'x': 1}
>>> try: >>> a2 = pickle.loads(pickle.dumps(a, 1))
... import cPickle
... except ImportError:
... # Py3
... import pickle as cPickle
>>> a2 = cPickle.loads(cPickle.dumps(a, 1))
>>> a2 >>> a2
<broken not.there.Atall instance> <broken not.there.Atall instance>
>>> a2.__Broken_newargs__ >>> a2.__Broken_newargs__
...@@ -196,12 +186,10 @@ def find_global(modulename, globalname, ...@@ -196,12 +186,10 @@ def find_global(modulename, globalname,
>>> broken_cache.clear() >>> broken_cache.clear()
""" """
if _compat_pickle is not None: if (modulename, globalname) in NAME_MAPPING:
if (modulename, globalname) in _compat_pickle.NAME_MAPPING: modulename, globalname = NAME_MAPPING[(modulename, globalname)]
modulename, globalname = _compat_pickle.NAME_MAPPING[ if modulename in IMPORT_MAPPING:
(modulename, globalname)] modulename = IMPORT_MAPPING[modulename]
if modulename in _compat_pickle.IMPORT_MAPPING:
modulename = _compat_pickle.IMPORT_MAPPING[modulename]
# short circuit common case: # short circuit common case:
try: try:
......
...@@ -44,11 +44,7 @@ from BTrees._fsBTree import fsBucket ...@@ -44,11 +44,7 @@ from BTrees._fsBTree import fsBucket
from BTrees.OOBTree import OOBTree from BTrees.OOBTree import OOBTree
import six import six
try: from ZODB._compat import pickle
import cPickle as pickle
except ImportError:
# Py3
import pickle
# convert between numbers and six-byte strings # convert between numbers and six-byte strings
......
...@@ -71,12 +71,6 @@ import getopt ...@@ -71,12 +71,6 @@ import getopt
import time import time
from struct import unpack from struct import unpack
try:
from cPickle import loads
except ImportError:
# Py3
from pickle import loads
try: try:
import ZODB import ZODB
except ImportError: except ImportError:
...@@ -89,6 +83,7 @@ except ImportError: ...@@ -89,6 +83,7 @@ except ImportError:
import ZODB.FileStorage import ZODB.FileStorage
from ZODB.utils import u64, as_text from ZODB.utils import u64, as_text
from ZODB.FileStorage import TransactionRecord from ZODB.FileStorage import TransactionRecord
from ZODB._compat import pickle
from persistent.TimeStamp import TimeStamp from persistent.TimeStamp import TimeStamp
...@@ -149,9 +144,9 @@ def read_txn_header(f, pos, file_size, outp, ltid): ...@@ -149,9 +144,9 @@ def read_txn_header(f, pos, file_size, outp, ltid):
user = f.read(ul) user = f.read(ul)
description = f.read(dl) description = f.read(dl)
if el: if el:
try: e=loads(f.read(el)) try: e = pickle.loads(f.read(el))
except: e={} except: e = {}
else: e={} else: e = {}
result = TransactionRecord(tid, status, user, description, e, pos, tend, result = TransactionRecord(tid, status, user, description, e, pos, tend,
f, tpos) f, tpos)
......
...@@ -23,14 +23,9 @@ import struct ...@@ -23,14 +23,9 @@ import struct
from ZODB.FileStorage.format import TRANS_HDR, DATA_HDR, TRANS_HDR_LEN from ZODB.FileStorage.format import TRANS_HDR, DATA_HDR, TRANS_HDR_LEN
from ZODB.FileStorage.format import DATA_HDR_LEN from ZODB.FileStorage.format import DATA_HDR_LEN
from ZODB.utils import u64 from ZODB.utils import u64
from ZODB._compat import pickle
from persistent.TimeStamp import TimeStamp from persistent.TimeStamp import TimeStamp
try:
import cPickle
except ImportError:
# Py3
import pickle as cPickle
class TxnHeader: class TxnHeader:
"""Object representing a transaction record header. """Object representing a transaction record header.
...@@ -70,7 +65,7 @@ class TxnHeader: ...@@ -70,7 +65,7 @@ class TxnHeader:
self.descr = self._file.read(self.descr_len) self.descr = self._file.read(self.descr_len)
if self.ext_len: if self.ext_len:
self._ext = self._file.read(self.ext_len) self._ext = self._file.read(self.ext_len)
self.ext = cPickle.loads(self._ext) self.ext = pickle.loads(self._ext)
def get_data_offset(self): def get_data_offset(self):
return (self._pos + TRANS_HDR_LEN + self.user_len + self.descr_len return (self._pos + TRANS_HDR_LEN + self.user_len + self.descr_len
......
...@@ -2,15 +2,16 @@ ...@@ -2,15 +2,16 @@
# Based on a transaction analyzer by Matt Kromer. # Based on a transaction analyzer by Matt Kromer.
from __future__ import print_function from __future__ import print_function
import pickle
import sys import sys
from ZODB.FileStorage import FileStorage from ZODB.FileStorage import FileStorage
from ZODB._compat import BytesIO
# We must not use cPickle on Python 2: cPickle.Unpickler cannot be
# subclassed.
import pickle
try:
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
class FakeError(Exception): class FakeError(Exception):
def __init__(self, module, name): def __init__(self, module, name):
......
...@@ -140,18 +140,7 @@ from persistent import Persistent ...@@ -140,18 +140,7 @@ from persistent import Persistent
from persistent.wref import WeakRefMarker, WeakRef from persistent.wref import WeakRefMarker, WeakRef
from ZODB import broken from ZODB import broken
from ZODB.POSException import InvalidObjectReference from ZODB.POSException import InvalidObjectReference
from ZODB._compat import pickle, BytesIO
try:
import cPickle as pickle
except ImportError:
# Py3
import pickle
try:
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
_Unpickler = pickle.Unpickler _Unpickler = pickle.Unpickler
......
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
"""Run some tests relevant for storages that support pack().""" """Run some tests relevant for storages that support pack()."""
from __future__ import print_function from __future__ import print_function
import doctest
import sys
import time
from persistent import Persistent from persistent import Persistent
from persistent.mapping import PersistentMapping from persistent.mapping import PersistentMapping
from ZODB import DB from ZODB import DB
...@@ -22,25 +26,12 @@ from ZODB.serialize import referencesf, _Unpickler, _protocol ...@@ -22,25 +26,12 @@ from ZODB.serialize import referencesf, _Unpickler, _protocol
from ZODB.tests.MinPO import MinPO from ZODB.tests.MinPO import MinPO
from ZODB.tests.MTStorage import TestThread from ZODB.tests.MTStorage import TestThread
from ZODB.tests.StorageTestBase import snooze from ZODB.tests.StorageTestBase import snooze
import doctest from ZODB._compat import pickle, BytesIO
import sys
import time
import transaction import transaction
import ZODB.interfaces import ZODB.interfaces
import ZODB.tests.util import ZODB.tests.util
import zope.testing.setupstack import zope.testing.setupstack
try:
import cPickle as pickle
except ImportError:
# Py3
import pickle
try:
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
ZERO = b'\0'*8 ZERO = b'\0'*8
......
...@@ -25,20 +25,9 @@ import transaction ...@@ -25,20 +25,9 @@ import transaction
from ZODB.utils import u64 from ZODB.utils import u64
from ZODB.tests.MinPO import MinPO from ZODB.tests.MinPO import MinPO
from ZODB._compat import pickle, BytesIO
import ZODB.tests.util import ZODB.tests.util
try:
from cPickle import Pickler, Unpickler
except ImportError:
# Py3
from pickle import Pickler, Unpickler
try:
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
ZERO = b'\0'*8 ZERO = b'\0'*8
...@@ -61,7 +50,7 @@ def _persistent_id(obj): ...@@ -61,7 +50,7 @@ def _persistent_id(obj):
def zodb_pickle(obj): def zodb_pickle(obj):
"""Create a pickle in the format expected by ZODB.""" """Create a pickle in the format expected by ZODB."""
f = BytesIO() f = BytesIO()
p = Pickler(f, 1) p = pickle.Pickler(f, 1)
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
p.inst_persistent_id = _persistent_id p.inst_persistent_id = _persistent_id
else: else:
...@@ -87,7 +76,7 @@ def persistent_load(pid): ...@@ -87,7 +76,7 @@ def persistent_load(pid):
def zodb_unpickle(data): def zodb_unpickle(data):
"""Unpickle an object stored using the format expected by ZODB.""" """Unpickle an object stored using the format expected by ZODB."""
f = BytesIO(data) f = BytesIO(data)
u = Unpickler(f) u = pickle.Unpickler(f)
u.persistent_load = persistent_load u.persistent_load = persistent_load
klass_info = u.load() klass_info = u.load()
if isinstance(klass_info, tuple): if isinstance(klass_info, tuple):
......
...@@ -32,13 +32,7 @@ from ZODB.tests import HistoryStorage, IteratorStorage, Corruption ...@@ -32,13 +32,7 @@ from ZODB.tests import HistoryStorage, IteratorStorage, Corruption
from ZODB.tests import RevisionStorage, PersistentStorage, MTStorage from ZODB.tests import RevisionStorage, PersistentStorage, MTStorage
from ZODB.tests import ReadOnlyStorage, RecoveryStorage from ZODB.tests import ReadOnlyStorage, RecoveryStorage
from ZODB.tests.StorageTestBase import MinPO, zodb_pickle from ZODB.tests.StorageTestBase import MinPO, zodb_pickle
from ZODB._compat import pickle
try:
import cPickle
except ImportError:
# Py3
import pickle as cPickle
class FileStorageTests( class FileStorageTests(
...@@ -97,7 +91,7 @@ class FileStorageTests( ...@@ -97,7 +91,7 @@ class FileStorageTests(
newindex = dict(index) newindex = dict(index)
data['index'] = newindex data['index'] = newindex
cPickle.dump(data, open('FileStorageTests.fs.index', 'wb'), 1) pickle.dump(data, open('FileStorageTests.fs.index', 'wb'), 1)
return index return index
def check_conversion_to_fsIndex(self, read_only=False): def check_conversion_to_fsIndex(self, read_only=False):
......
...@@ -21,18 +21,14 @@ old code, developers will have a hard time testing the new code. ...@@ -21,18 +21,14 @@ old code, developers will have a hard time testing the new code.
""" """
import unittest import unittest
import sys
import transaction import transaction
from transaction import Transaction from transaction import Transaction
import ZODB import ZODB
from ZODB.MappingStorage import MappingStorage from ZODB.MappingStorage import MappingStorage
import sys from ZODB._compat import pickle
try:
import cPickle
except ImportError:
# Py3
import pickle as cPickle
try: try:
import cStringIO import cStringIO
...@@ -89,8 +85,9 @@ class PMTests(unittest.TestCase): ...@@ -89,8 +85,9 @@ class PMTests(unittest.TestCase):
# MappingStorage stores serialno + pickle in its _index. # MappingStorage stores serialno + pickle in its _index.
root_pickle = s._index['\000' * 8][8:] root_pickle = s._index['\000' * 8][8:]
# XXX not BytesIO really?
f = cStringIO.StringIO(root_pickle) f = cStringIO.StringIO(root_pickle)
u = cPickle.Unpickler(f) u = pickle.Unpickler(f)
klass_info = u.load() klass_info = u.load()
klass = find_global(*klass_info[0]) klass = find_global(*klass_info[0])
inst = klass.__new__(klass) inst = klass.__new__(klass)
......
...@@ -17,18 +17,7 @@ import unittest ...@@ -17,18 +17,7 @@ import unittest
import ZODB.tests.util import ZODB.tests.util
from ZODB import serialize from ZODB import serialize
from ZODB._compat import pickle, BytesIO
try:
import cPickle as pickle
except ImportError:
# Py3
import pickle
try:
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
class ClassWithNewargs(int): class ClassWithNewargs(int):
......
...@@ -20,12 +20,8 @@ from persistent import Persistent ...@@ -20,12 +20,8 @@ from persistent import Persistent
from zope.testing import renormalizing from zope.testing import renormalizing
from ZODB.utils import U64, p64, u64 from ZODB.utils import U64, p64, u64
from ZODB._compat import pickle, long
try:
long
except NameError:
# Py3
long = int
NUM = 100 NUM = 100
...@@ -77,11 +73,6 @@ class TestUtils(unittest.TestCase): ...@@ -77,11 +73,6 @@ class TestUtils(unittest.TestCase):
from ZODB.serialize import ObjectWriter from ZODB.serialize import ObjectWriter
from ZODB.POSException import ConflictError from ZODB.POSException import ConflictError
from ZODB.tests.MinPO import MinPO from ZODB.tests.MinPO import MinPO
try:
import cPickle as pickle
except ImportError:
# Py3
import pickle
obj = MinPO() obj = MinPO()
data = ObjectWriter().serialize(obj) data = ObjectWriter().serialize(obj)
......
...@@ -220,12 +220,8 @@ Note that we pass a file position, which gets saved with the index data. ...@@ -220,12 +220,8 @@ Note that we pass a file position, which gets saved with the index data.
If we save the data in the old format, we can still read it: If we save the data in the old format, we can still read it:
>>> try: >>> from ZODB._compat import pickle
... import cPickle >>> pickle.dump(dict(pos=42, index=index), open('old', 'wb'), 1)
... except ImportError:
... # Py3
... import pickle as cPickle
>>> cPickle.dump(dict(pos=42, index=index), open('old', 'wb'), 1)
>>> info = fsIndex.load('old') >>> info = fsIndex.load('old')
>>> info['pos'] >>> info['pos']
42 42
......
...@@ -11,29 +11,19 @@ ...@@ -11,29 +11,19 @@
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE
# #
############################################################################## ##############################################################################
import os
import struct
import sys import sys
import time import time
import struct
from struct import pack, unpack
from binascii import hexlify, unhexlify
import warnings import warnings
from binascii import hexlify, unhexlify
from struct import pack, unpack
from tempfile import mkstemp from tempfile import mkstemp
import os
try:
import cPickle as pickle
except ImportError:
# Py3
import pickle
try: from persistent.TimeStamp import TimeStamp
from cStringIO import StringIO as BytesIO
except ImportError:
# Py3
from io import BytesIO
from ZODB._compat import pickle, BytesIO
from persistent.TimeStamp import TimeStamp
__all__ = ['z64', __all__ = ['z64',
'p64', 'p64',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment