Skip to content

Commit

Permalink
Simplify __init__.py to not repeat itself three times.
Browse files Browse the repository at this point in the history
There are many fewer 'no cover' lines now.

Remove the call to copy_reg.constructor---it doesn't do anything anymore except check that the object is callable, there is no registry.

Also make timestamp support PURE_PYTHON.
  • Loading branch information
jamadden committed Jul 31, 2018
1 parent 91612ca commit 74b3f02
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
91 changes: 42 additions & 49 deletions persistent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,48 @@
Fall back to pure Python implementations.
"""
import os

PURE_PYTHON = os.environ.get('PURE_PYTHON')
if not PURE_PYTHON:
try:
from persistent.cPersistence import Persistent
from persistent.cPersistence import GHOST
from persistent.cPersistence import UPTODATE
from persistent.cPersistence import CHANGED
from persistent.cPersistence import STICKY
from persistent.cPersistence import simple_new
except ImportError: # pragma: no cover
from persistent.persistence import Persistent
from persistent.persistence import GHOST
from persistent.persistence import UPTODATE
from persistent.persistence import CHANGED
from persistent.persistence import STICKY
else:
from persistent._compat import copy_reg
copy_reg.constructor(simple_new)
# Make an interface declaration for Persistent, if zope.interface
# is available. Note that the Python version already does this.
try:
from zope.interface import classImplements
except ImportError: # pragma: no cover
pass
else:
from persistent.interfaces import IPersistent
classImplements(Persistent, IPersistent)
import sys

try:
from persistent.cPickleCache import PickleCache
except ImportError: # pragma: no cover
from persistent.picklecache import PickleCache
__all__ = [
'IPersistent',
'Persistent',
'GHOST',
'UPTODATE',
'CHANGED',
'STICKY',
'PickleCache',
'TimeStamp',
]
from persistent._compat import PURE_PYTHON
from persistent.interfaces import IPersistent

try:
import persistent.TimeStamp
except ImportError: # pragma: no cover
import persistent.timestamp as TimeStamp
import sys
sys.modules['persistent.TimeStamp'
] = sys.modules['persistent.timestamp']
else: # pragma: no cover
from persistent.persistence import Persistent
from persistent.persistence import GHOST
from persistent.persistence import UPTODATE
from persistent.persistence import CHANGED
from persistent.persistence import STICKY
from persistent.picklecache import PickleCache
import persistent.timestamp as TimeStamp
import sys
sys.modules['persistent.TimeStamp'] = sys.modules['persistent.timestamp']
import persistent.timestamp as TimeStamp

from persistent import persistence as pyPersistence
from persistent import picklecache as pyPickleCache

try:
from persistent import cPersistence
from persistent import cPickleCache
except ImportError: # pragma: no cover
cPersistence = None
cPickleCache = None
else:
# Make an interface declaration for Persistent
# Note that the Python version already does this.
from zope.interface import classImplements
classImplements(cPersistence.Persistent, IPersistent)


_persistence = pyPersistence if PURE_PYTHON or cPersistence is None else cPersistence
_picklecache = pyPickleCache if PURE_PYTHON or cPickleCache is None else cPickleCache

Persistent = _persistence.Persistent
GHOST = _persistence.GHOST
UPTODATE = _persistence.UPTODATE
CHANGED = _persistence.CHANGED
STICKY = _persistence.STICKY
PickleCache = _picklecache.PickleCache

sys.modules['persistent.TimeStamp'] = sys.modules['persistent.timestamp']
3 changes: 3 additions & 0 deletions persistent/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
##############################################################################

import sys
import os

PURE_PYTHON = os.environ.get('PURE_PYTHON')

if sys.version_info[0] > 2: # pragma: no cover
import copyreg as copy_reg
Expand Down
8 changes: 6 additions & 2 deletions persistent/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import struct
import sys

from persistent._compat import PURE_PYTHON

_RAWTYPE = bytes
_MAXINT = sys.maxsize

Expand Down Expand Up @@ -205,6 +207,8 @@ def __ge__(self, other):


try:
from persistent._timestamp import TimeStamp
from persistent._timestamp import TimeStamp as CTimeStamp
except ImportError: # pragma: no cover
TimeStamp = pyTimeStamp
CTimeStamp = None

TimeStamp = pyTimeStamp if PURE_PYTHON or CTimeStamp is None else CTimeStamp

0 comments on commit 74b3f02

Please sign in to comment.