[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/develop] Fix libgpgme signing error due to Python2.7 buffer() interface changes.



commit 3debed96a8e04894b0064e8075ffd8c1dd4ce10b
Author: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Date:   Wed Apr 23 05:08:10 2014 +0000

    Fix libgpgme signing error due to Python2.7 buffer() interface changes.
    
    See https://mail.python.org/pipermail/python-dev/2010-October/104917.html
        https://docs.python.org/2/c-api/buffer.html
        https://github.com/SoftwareIntrospectionLab/MininGit/issues/101#issuecomment-1602514
    
    We have to test if our Python2.7 version has the new or old builtin
    buffer() interface.
---
 lib/bridgedb/crypto.py |   27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/lib/bridgedb/crypto.py b/lib/bridgedb/crypto.py
index 4ce30b2..2feb556 100644
--- a/lib/bridgedb/crypto.py
+++ b/lib/bridgedb/crypto.py
@@ -61,6 +61,24 @@ from twisted.internet import ssl
 #: The hash digest to use for HMACs.
 DIGESTMOD = hashlib.sha1
 
+# Test to see if we have the old or new style buffer() interface. Trying
+# to use an old-style buffer on Python2.7 prior to version 2.7.5 will produce:
+#
+#     TypeError: 'buffer' does not have the buffer interface
+#
+#: ``True`` if we have the new-style
+#: `buffer <https://docs.python.org/2/c-api/buffer.html>` interface.
+NEW_BUFFER_INTERFACE = False
+try:
+    io.BytesIO(buffer('test'))
+except TypeError:
+    logging.warn(
+        "This Python version is too old! "\
+        "It doesn't support new-style buffer interfaces: "\
+        "https://mail.python.org/pipermail/python-dev/2010-October/104917.html";)
+else:
+    NEW_BUFFER_INTERFACE = True
+
 
 class RSAKeyGenerationError(Exception):
     """Raised when there was an error creating an RSA keypair."""
@@ -395,8 +413,13 @@ def gpgSignMessage(gpgmeCtx, messageString, mode=None):
     if not mode:
         mode = gpgme.SIG_MODE_CLEAR
 
-    msgFile = io.BytesIO(buffer(messageString))
-    sigFile = io.BytesIO()
+    if NEW_BUFFER_INTERFACE:
+        msgFile = io.BytesIO(buffer(messageString))
+        sigFile = io.BytesIO()
+    else:
+        msgFile = io.StringIO(unicode(messageString))
+        sigFile = io.StringIO()
+
     sigList = gpgmeCtx.sign(msgFile, sigFile, mode)
     sigFile.seek(0)
     signature = sigFile.read()



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits