Skip to content

Commit f412b22

Browse files
committed
Adapt to python3 shelve.open behavior changes
When running tests between python2 and python3, shelve.open's behavior has changed. These changes make it so that a previous run of one python version of tests will not interfere with the current run. There are still some issues with the database filename which may need to be addressed.
1 parent 39b0774 commit f412b22

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

src/saml2/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CacheError(SAMLError):
2323
class Cache(object):
2424
def __init__(self, filename=None):
2525
if filename:
26-
self._db = shelve.open(filename, writeback=True)
26+
self._db = shelve.open(filename, writeback=True, protocol=2)
2727
self._sync = True
2828
else:
2929
self._db = {}

src/saml2/ident.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class IdentDB(object):
7878
"""
7979
def __init__(self, db, domain="", name_qualifier=""):
8080
if isinstance(db, six.string_types):
81-
self.db = shelve.open(db)
81+
self.db = shelve.open(db, protocol=2)
8282
else:
8383
self.db = db
8484
self.domain = domain

src/saml2/s2repoze/plugins/sp.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ def __init__(self, rememberer_name, config, saml_client, wayf, cache,
100100
except KeyError:
101101
self.metadata = None
102102
if sid_store:
103-
self.outstanding_queries = shelve.open(sid_store, writeback=True)
103+
self.outstanding_queries = shelve.open(sid_store, writeback=True,
104+
protocol=2)
104105
else:
105106
self.outstanding_queries = {}
106107
if sid_store_cert:
107-
self.outstanding_certs = shelve.open(sid_store_cert, writeback=True)
108+
self.outstanding_certs = shelve.open(sid_store_cert, writeback=True,
109+
protocol=2)
108110
else:
109111
self.outstanding_certs = {}
110112

src/saml2/server.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010

1111
import importlib
12+
import dbm
1213
import shelve
1314
import six
1415
import threading
@@ -56,6 +57,17 @@
5657
"subject_locality": "subject_locality"
5758
}
5859

60+
def _shelve_compat(name, *args, **kwargs):
61+
try:
62+
return shelve.open(name, *args, **kwargs)
63+
except dbm.error[0]:
64+
# Python 3 whichdb needs to try .db to determine type
65+
if name.endswith('.db'):
66+
name = name.rsplit('.db', 1)[0]
67+
return shelve.open(name, *args, **kwargs)
68+
else:
69+
raise
70+
5971

6072
class Server(Entity):
6173
""" A class that does things that IdPs or AAs do """
@@ -118,12 +130,12 @@ def init_config(self, stype="idp"):
118130
if not dbspec:
119131
idb = {}
120132
elif isinstance(dbspec, six.string_types):
121-
idb = shelve.open(dbspec, writeback=True)
133+
idb = _shelve_compat(dbspec, writeback=True, protocol=2)
122134
else: # database spec is a a 2-tuple (type, address)
123135
#print(>> sys.stderr, "DBSPEC: %s" % (dbspec,))
124136
(typ, addr) = dbspec
125137
if typ == "shelve":
126-
idb = shelve.open(addr, writeback=True)
138+
idb = _shelve_compat(addr, writeback=True, protocol=2)
127139
elif typ == "memcached":
128140
import memcache
129141

0 commit comments

Comments
 (0)