[Pkg-gnupg-commit] [gpgme] 113/412: python: More type conversion fixes.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Thu Sep 22 21:26:28 UTC 2016


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch master
in repository gpgme.

commit c1c893e3384a07ff864507902fbac4a7fee4d6f1
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 19 10:55:34 2016 +0200

    python: More type conversion fixes.
    
    * lang/python/helpers.c (pyPassphraseCb): Cope with 'uid_hint' being
    NULL, convert it to an Unicode object, and cope with the callback
    returning both Unicode and bytes objects.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>
---
 lang/python/helpers.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/lang/python/helpers.c b/lang/python/helpers.c
index 285b1da..447a29f 100644
--- a/lang/python/helpers.c
+++ b/lang/python/helpers.c
@@ -82,7 +82,15 @@ static gpgme_error_t pyPassphraseCb(void *hook,
     args = PyTuple_New(3);
   }
 
-  PyTuple_SetItem(args, 0, PyBytes_FromString(uid_hint));
+  if (uid_hint == NULL)
+    {
+      Py_INCREF(Py_None);
+      PyTuple_SetItem(args, 0, Py_None);
+    }
+  else
+    PyTuple_SetItem(args, 0, PyUnicode_DecodeUTF8(uid_hint, strlen (uid_hint),
+                                                  "strict"));
+
   PyTuple_SetItem(args, 1, PyBytes_FromString(passphrase_info));
   PyTuple_SetItem(args, 2, PyBool_FromLong((long)prev_was_bad));
   if (dataarg) {
@@ -98,7 +106,21 @@ static gpgme_error_t pyPassphraseCb(void *hook,
     if (!retval) {
       write(fd, "\n", 1);
     } else {
-      write(fd, PyBytes_AsString(retval), PyBytes_Size(retval));
+      char *buf;
+      size_t len;
+      if (PyBytes_Check(retval))
+        buf = PyBytes_AsString(retval), len = PyBytes_Size(retval);
+      else if (PyUnicode_Check(retval))
+        buf = PyUnicode_AsUTF8AndSize(retval, &len);
+      else
+        {
+          PyErr_Format(PyExc_TypeError,
+                       "expected str or bytes from passphrase callback, got %s",
+                       retval->ob_type->tp_name);
+          return gpg_error(GPG_ERR_GENERAL);
+        }
+
+      write(fd, buf, len);
       write(fd, "\n", 1);
       Py_DECREF(retval);
     }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gpgme.git



More information about the Pkg-gnupg-commit mailing list