[Pkg-gnupg-commit] [gpgme] 09/132: python: Try to be more helpful when given a string to encrypt().

Daniel Kahn Gillmor dkg at fifthhorseman.net
Wed Apr 26 01:00:46 UTC 2017


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

dkg pushed a commit to branch experimental
in repository gpgme.

commit 05896c210f7fa9ce7b97784a75e49dc4489e9252
Author: Tobias Mueller <muelli at cryptobitch.de>
Date:   Fri Dec 2 23:37:27 2016 +0100

    python: Try to be more helpful when given a string to encrypt().
    
    * lang/python/helpers.c (_gpg_obj2gpgme_data_t): Extended error
    message.
    * lang/python/tests/t-encrypt.py: Test for "encode" in error message.
    --
    The motivation is to help the user when encrypting fails. I claim that
    it is not obvious to not being able to encrypt a string directly.  To
    nudge the user into encoding it to bytes, the error message is a bit
    extended.
    
    Signed-off-by: Tobias Mueller <muelli at cryptobitch.de>
---
 lang/python/helpers.c          |  6 ++++--
 lang/python/tests/t-encrypt.py | 15 +++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/lang/python/helpers.c b/lang/python/helpers.c
index 8f71a30..576767c 100644
--- a/lang/python/helpers.c
+++ b/lang/python/helpers.c
@@ -293,8 +293,10 @@ _gpg_obj2gpgme_data_t(PyObject *input, int argnum, gpgme_data_t *wrapper,
     return _gpg_obj2gpgme_t(data, "gpgme_data_t", argnum);
 
   return PyErr_Format(PyExc_TypeError,
-                      "arg %d: expected gpg.Data, file, or an object "
-                      "implementing the buffer protocol, got %s",
+                      "arg %d: expected gpg.Data, file, "
+                      "bytes (not string!), or an object "
+                      "implementing the buffer protocol. Got: %s. "
+                      "If you provided a string, try to encode() it.",
                       argnum, data->ob_type->tp_name);
 }
 
diff --git a/lang/python/tests/t-encrypt.py b/lang/python/tests/t-encrypt.py
index 0c0ca35..3cbe8f2 100755
--- a/lang/python/tests/t-encrypt.py
+++ b/lang/python/tests/t-encrypt.py
@@ -62,3 +62,18 @@ with gpg.Context(armor=True) as c:
         assert support.sign_only.endswith(e.recipients[0].fpr)
     else:
         assert False, "Expected an InvalidRecipients error, got none"
+
+
+
+    try:
+        # People might be tempted to provide strings.
+        # We should raise something useful.
+        ciphertext, _, _ = c.encrypt("Hallo Leute\n",
+                                     recipients=keys,
+                                     sign=False,
+                                     always_trust=True)
+    except TypeError as e:
+        # This test is a bit fragile, because the message
+        # may very well change. So if the behaviour will change
+        # this test can easily be deleted.
+        assert "encode" in str(e)

-- 
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