summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2012-07-01 11:38:29 +0200
committerHelmut Grohne <helmut@subdivi.de>2012-07-01 11:38:29 +0200
commitb0938bb51c915ea5d888e2e88bbb62f4d1da199c (patch)
tree01fd6d2da9b8a0aebedbb1297e39c7cfd9b55d68
parent85a4d0c404c767460887eafe5e7aa2511f70bad6 (diff)
downloadwsgitools-b0938bb51c915ea5d888e2e88bbb62f4d1da199c.tar.gz
make HtdigestTokenGenerator work with py3k
Define a textopen function that returns "native strings" (in the sense of pep3333). Therefore textopen needs to decode using iso-8859-1 iff running on py3k. Additionally use a with construct to close the file being read in all circumstances.
-rw-r--r--wsgitools/digest.py25
-rw-r--r--wsgitools/internal.py5
2 files changed, 18 insertions, 12 deletions
diff --git a/wsgitools/digest.py b/wsgitools/digest.py
index 532b371..6395d02 100644
--- a/wsgitools/digest.py
+++ b/wsgitools/digest.py
@@ -19,7 +19,7 @@ import hashlib
import time
import os
-from wsgitools.internal import bytes2str, str2bytes
+from wsgitools.internal import bytes2str, str2bytes, textopen
from wsgitools.authentication import AuthenticationRequired, \
ProtocolViolation, AuthenticationMiddleware
@@ -239,18 +239,19 @@ class HtdigestTokenGenerator(AbstractTokenGenerator):
"""
assert isinstance(htdigestfile, str)
self.users = {}
- for line in file(htdigestfile):
- parts = line.rstrip("\n").split(":")
- if len(parts) != 3:
- if ignoreparseerrors:
+ with textopen(htdigestfile, "r") as htdigest:
+ for line in htdigest:
+ parts = line.rstrip("\n").split(":")
+ if len(parts) != 3:
+ if ignoreparseerrors:
+ continue
+ raise ValueError("invalid number of colons in htdigest file")
+ user, realm, token = parts
+ if realm != self.realm:
continue
- raise ValueError("invalid number of colons in htdigest file")
- user, realm, token = parts
- if realm != self.realm:
- continue
- if user in self.users and not ignoreparseerrors:
- raise ValueError("duplicate user in htdigest file")
- self.users[user] = token
+ if user in self.users and not ignoreparseerrors:
+ raise ValueError("duplicate user in htdigest file")
+ self.users[user] = token
def __call__(self, user, algo="md5"):
assert algo.lower() in ["md5", "md5-sess"]
diff --git a/wsgitools/internal.py b/wsgitools/internal.py
index c392b6a..c4f1da1 100644
--- a/wsgitools/internal.py
+++ b/wsgitools/internal.py
@@ -5,6 +5,8 @@ if bytes is str:
def str2bytes(sstr):
assert isinstance(sstr, str)
return sstr
+ def textopen(filename, mode):
+ return open(filename, mode)
else:
def bytes2str(bstr):
assert isinstance(bstr, bytes)
@@ -12,3 +14,6 @@ else:
def str2bytes(sstr):
assert isinstance(sstr, str)
return sstr.encode("iso-8859-1") # might fail, but spec says it doesn't
+ def textopen(filename, mode):
+ # We use the same encoding as for all wsgi strings here.
+ return open(filename, mode, encoding="iso-8859-1")