From a27fb730687a3351d35388a34a29f65408d1724e Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Tue, 10 Mar 2020 13:50:02 +0100
Subject: [PATCH] wip

---
 rfc3161/__init__.py |  6 +++---
 rfc3161/api.py      | 10 +++++-----
 setup.py            |  5 +++--
 tests/test_api.py   |  2 +-
 tox.ini             |  5 +++++
 5 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/rfc3161/__init__.py b/rfc3161/__init__.py
index 63a2917..318d17c 100644
--- a/rfc3161/__init__.py
+++ b/rfc3161/__init__.py
@@ -1,5 +1,5 @@
-from types import *
-from constants import *
-from api import *
+from .types import *
+from .constants import *
+from .api import *
 
 VERSION = '1.0.6'
diff --git a/rfc3161/api.py b/rfc3161/api.py
index 5116d22..5d819b5 100644
--- a/rfc3161/api.py
+++ b/rfc3161/api.py
@@ -8,7 +8,7 @@ from pyasn1_modules import rfc2459
 from pyasn1.type import univ
 from pyasn1.error import PyAsn1Error
 import M2Crypto.X509 as X509
-import socket
+import six
 
 import rfc3161
 
@@ -49,7 +49,7 @@ def get_timestamp(tst):
             raise ValueError("extra data after tst")
         genTime = tstinfo.getComponentByName('genTime')
         return dateutil.parser.parse(str(genTime))
-    except PyAsn1Error, e:
+    except PyAsn1Error as e:
         raise ValueError('not a valid TimeStampToken', e)
 
 def check_timestamp(tst, certificate, data=None, digest=None, hashname=None, nonce=None):
@@ -167,7 +167,7 @@ class RemoteTimestamper(object):
         message_imprint.setComponentByPosition(0, algorithm_identifier)
         hashobj = hashlib.new(self.hashname)
         if data:
-            hashobj.update(data)
+            hashobj.update(six.ensure_binary(data))
             digest = hashobj.digest()
         elif digest:
             assert len(digest) == hashobj.digest_size, 'digest length is wrong'
@@ -183,12 +183,12 @@ class RemoteTimestamper(object):
         binary_request = encoder.encode(request)
         headers = { 'Content-Type': 'application/timestamp-query' }
         if self.username != None:
-            base64string = base64.standard_b64encode('%s:%s' % (self.username, self.password))
+            base64string = base64.standard_b64encode(six.ensure_binary('%s:%s' % (self.username, self.password)))
             headers['Authorization'] = "Basic %s" % base64string
         try:
             response = requests.post(self.url, data=binary_request,
                     timeout=self.timeout, headers=headers)
-        except request.RequestException, e:
+        except request.RequestException as e:
             raise TimestampingError('Unable to send the request to %r' % self.url, e)
         tst_response, substrate = decoder.decode(response.content, asn1Spec=rfc3161.TimeStampResp())
         if substrate:
diff --git a/setup.py b/setup.py
index 47d5278..8c0dc01 100755
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@ setup(name='rfc3161',
       license='MIT',
       url='https://dev.entrouvert.org/projects/python-rfc3161',
       description='Python implementation of the RFC3161 specification, using pyasn1',
-      long_description=file('README').read(),
+      long_description=open('README').read(),
       author='Benjamin Dauvergne',
       author_email='bdauvergne@entrouvert.com',
       packages=['rfc3161'],
@@ -15,4 +15,5 @@ setup(name='rfc3161',
           'python-dateutil',
           'pyasn1_modules',
           'requests',
-          'M2Crypto'])
+          'M2Crypto',
+          'six'])
diff --git a/tests/test_api.py b/tests/test_api.py
index f04bf06..0a6e65f 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -8,7 +8,7 @@ import rfc3161
 
 def default_test(tsa_server, certificate, username=None, password=None, data='xx', nonce=None, **kwargs):
     kwargs.update({
-            'certificate': file(certificate).read()
+            'certificate': open(certificate, 'rb').read()
     })
     if username and password:
         kwargs.update({
diff --git a/tox.ini b/tox.ini
index 935b510..6fed20a 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,3 +1,8 @@
+[tox]
+toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/python-rfc3161/{env:BRANCH_NAME:}
+envlist = py2,py3
+
+
 [testenv]
 deps = 
   pytest-cov
-- 
2.20.1

