From 9cc2f68e81665829f7e560683d5fe0e9f7d4c4fa Mon Sep 17 00:00:00 2001 From: Robert Escriva Date: Fri, 4 Mar 2016 06:55:23 -0600 Subject: [PATCH] Python 3 friendly-ish. --- Makefile.am | 2 +- bindings/python/macaroons.pyx | 29 +++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Makefile.am b/Makefile.am index 4d7cdb5..660fc33 100644 --- a/Makefile.am +++ b/Makefile.am @@ -30,7 +30,7 @@ AM_CPPFLAGS = $(SODIUM_CFLAGS) AM_CFLAGS = -fvisibility=hidden $(SODIUM_CFLAGS) $(WANAL_CFLAGS) AM_CXXFLAGS = -fvisibility=hidden -fvisibility-inlines-hidden $(SODIUM_CFLAGS) $(WANAL_CXXFLAGS) -AM_DISTCHECK_CONFIGURE_FLAGS = --enable-python-bindings +AM_DISTCHECK_CONFIGURE_FLAGS = --enable-python-bindings PYTHON=python2 TESTS_ENVIRONMENT = . $(abs_top_srcdir)/test/env.sh "${abs_top_srcdir}" "${abs_top_builddir}" "${VERSION}"; pyx_verbose = $(pyx_verbose_$(V)) diff --git a/bindings/python/macaroons.pyx b/bindings/python/macaroons.pyx index 241deb7..629d0fb 100644 --- a/bindings/python/macaroons.pyx +++ b/bindings/python/macaroons.pyx @@ -24,7 +24,6 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - cdef extern from "stdlib.h": void* malloc(size_t size) @@ -84,6 +83,15 @@ class MacaroonError(Exception): pass class Unauthorized(Exception): pass +cdef bytes tobytes(s): + if type(s) in (unicode, str): + return s.encode('utf8') + elif type(s) is bytes: + return s + else: + raise TypeError("a bytes/str-like object is required, not '%s'" % s.__class__.__name__) + + cdef raise_error(macaroon_returncode err): if err == MACAROON_OUT_OF_MEMORY: raise MemoryError @@ -225,8 +233,9 @@ cdef class Macaroon: raise_error(err) return DP - def add_first_party_caveat(self, bytes predicate): + def add_first_party_caveat(self, predicate): self.assert_not_null() + predicate = tobytes(predicate) cdef macarr cdef macaroon_returncode err cdef Macaroon M = Macaroon() @@ -236,7 +245,10 @@ cdef class Macaroon: raise_error(err) return M - def add_third_party_caveat(self, bytes _location, bytes _key, bytes _key_id): + def add_third_party_caveat(self, _location, _key, _key_id): + _location = tobytes(_location) + _key = tobytes(_key) + _key_id = tobytes(_key_id) cdef unsigned char* location = _location cdef size_t location_sz = len(_location) cdef unsigned char* key = _key @@ -291,13 +303,15 @@ cdef class Verifier: raise_error(err) self._funcs.append(func) - def verify(self, Macaroon M, bytes key, MS=None): + def verify(self, Macaroon M, key, MS=None): + key = tobytes(key) if self.verify_unsafe(M, key, MS): return True else: raise Unauthorized("macaroon not authorized") - def verify_unsafe(self, Macaroon M, bytes key, MS=None): + def verify_unsafe(self, Macaroon M, key, MS=None): + key = tobytes(key) cdef macaroon_returncode err cdef macaroon** discharges = NULL cdef Macaroon tmp @@ -323,7 +337,10 @@ cdef class Verifier: free(discharges) -def create(bytes _location, bytes _key, bytes _key_id): +def create(_location, _key, _key_id): + _location = tobytes(_location) + _key = tobytes(_key) + _key_id = tobytes(_key_id) cdef unsigned char* location = _location cdef size_t location_sz = len(_location) cdef unsigned char* key = _key