python: Improve support for edit callbacks.
* lang/python/helpers.c (pyEditCb): Stash exceptions. * lang/python/pyme/core.py (Context.op_edit): Hand in 'self'. * lang/python/tests/Makefile.am (py_tests): Add new test. * lang/python/tests/t-callbacks.py: Test edit callbacks. * lang/python/tests/t-edit.py: New file. Signed-off-by: Justus Winter <justus@gnupg.org>
This commit is contained in:
parent
283f0bdc3d
commit
09803c4a81
@ -280,15 +280,18 @@ gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
|
|||||||
PyObject *func = NULL, *dataarg = NULL, *pyargs = NULL, *retval = NULL;
|
PyObject *func = NULL, *dataarg = NULL, *pyargs = NULL, *retval = NULL;
|
||||||
PyObject *pyopaque = (PyObject *) opaque;
|
PyObject *pyopaque = (PyObject *) opaque;
|
||||||
gpgme_error_t err_status = 0;
|
gpgme_error_t err_status = 0;
|
||||||
|
PyObject *self = NULL;
|
||||||
|
|
||||||
pygpgme_exception_init();
|
pygpgme_exception_init();
|
||||||
|
|
||||||
if (PyTuple_Check(pyopaque)) {
|
assert (PyTuple_Check(pyopaque));
|
||||||
func = PyTuple_GetItem(pyopaque, 0);
|
assert (PyTuple_Size(pyopaque) == 2 || PyTuple_Size(pyopaque) == 3);
|
||||||
dataarg = PyTuple_GetItem(pyopaque, 1);
|
self = PyTuple_GetItem(pyopaque, 0);
|
||||||
|
func = PyTuple_GetItem(pyopaque, 1);
|
||||||
|
if (PyTuple_Size(pyopaque) == 3) {
|
||||||
|
dataarg = PyTuple_GetItem(pyopaque, 2);
|
||||||
pyargs = PyTuple_New(3);
|
pyargs = PyTuple_New(3);
|
||||||
} else {
|
} else {
|
||||||
func = pyopaque;
|
|
||||||
pyargs = PyTuple_New(2);
|
pyargs = PyTuple_New(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,6 +306,7 @@ gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
|
|||||||
Py_DECREF(pyargs);
|
Py_DECREF(pyargs);
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
err_status = pygpgme_exception2code();
|
err_status = pygpgme_exception2code();
|
||||||
|
pygpgme_stash_callback_exception(self);
|
||||||
} else {
|
} else {
|
||||||
if (fd>=0 && retval && PyUnicode_Check(retval)) {
|
if (fd>=0 && retval && PyUnicode_Check(retval)) {
|
||||||
const char *buffer;
|
const char *buffer;
|
||||||
|
@ -225,8 +225,15 @@ class Context(GpgmeWrapper):
|
|||||||
"""Start key editing using supplied callback function"""
|
"""Start key editing using supplied callback function"""
|
||||||
if key == None:
|
if key == None:
|
||||||
raise ValueError("op_edit: First argument cannot be None")
|
raise ValueError("op_edit: First argument cannot be None")
|
||||||
opaquedata = (func, fnc_value)
|
if fnc_value:
|
||||||
errorcheck(pygpgme.gpgme_op_edit(self.wrapped, key, opaquedata, out))
|
opaquedata = (self, func, fnc_value)
|
||||||
|
else:
|
||||||
|
opaquedata = (self, func)
|
||||||
|
|
||||||
|
result = pygpgme.gpgme_op_edit(self.wrapped, key, opaquedata, out)
|
||||||
|
if self._callback_excinfo:
|
||||||
|
pygpgme.pygpgme_raise_callback_exception(self)
|
||||||
|
errorcheck(result)
|
||||||
|
|
||||||
class Data(GpgmeWrapper):
|
class Data(GpgmeWrapper):
|
||||||
"""From the GPGME C manual:
|
"""From the GPGME C manual:
|
||||||
|
@ -38,7 +38,8 @@ py_tests = t-wrapper.py \
|
|||||||
t-sign.py \
|
t-sign.py \
|
||||||
t-signers.py \
|
t-signers.py \
|
||||||
t-decrypt.py \
|
t-decrypt.py \
|
||||||
t-export.py
|
t-export.py \
|
||||||
|
t-edit.py
|
||||||
|
|
||||||
TESTS = $(top_srcdir)/tests/gpg/initial.test \
|
TESTS = $(top_srcdir)/tests/gpg/initial.test \
|
||||||
$(py_tests) \
|
$(py_tests) \
|
||||||
|
@ -113,3 +113,36 @@ except Exception as e:
|
|||||||
assert e == myException
|
assert e == myException
|
||||||
else:
|
else:
|
||||||
assert False, "Expected an error, got none"
|
assert False, "Expected an error, got none"
|
||||||
|
|
||||||
|
|
||||||
|
# Test the edit callback.
|
||||||
|
c = core.Context()
|
||||||
|
c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
|
||||||
|
c.set_passphrase_cb(lambda *args: "abc")
|
||||||
|
sink = core.Data()
|
||||||
|
alpha = c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False)
|
||||||
|
|
||||||
|
cookie = object()
|
||||||
|
edit_cb_called = False
|
||||||
|
def edit_cb(status, args, hook):
|
||||||
|
global edit_cb_called
|
||||||
|
edit_cb_called = True
|
||||||
|
assert hook == cookie
|
||||||
|
return "quit" if args == "keyedit.prompt" else None
|
||||||
|
c.op_edit(alpha, edit_cb, cookie, sink)
|
||||||
|
assert edit_cb_called
|
||||||
|
|
||||||
|
# Test exceptions.
|
||||||
|
c = core.Context()
|
||||||
|
c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
|
||||||
|
c.set_passphrase_cb(lambda *args: "abc")
|
||||||
|
sink = core.Data()
|
||||||
|
|
||||||
|
def edit_cb(status, args):
|
||||||
|
raise myException
|
||||||
|
try:
|
||||||
|
c.op_edit(alpha, edit_cb, None, sink)
|
||||||
|
except Exception as e:
|
||||||
|
assert e == myException
|
||||||
|
else:
|
||||||
|
assert False, "Expected an error, got none"
|
||||||
|
62
lang/python/tests/t-edit.py
Executable file
62
lang/python/tests/t-edit.py
Executable file
@ -0,0 +1,62 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# Copyright (C) 2005 Igor Belyi <belyi@users.sourceforge.net>
|
||||||
|
# Copyright (C) 2016 g10 Code GmbH
|
||||||
|
#
|
||||||
|
# This file is part of GPGME.
|
||||||
|
#
|
||||||
|
# GPGME is free software; you can redistribute it and/or modify it
|
||||||
|
# under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation; either version 2 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# GPGME is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
|
||||||
|
# Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Lesser General Public
|
||||||
|
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
from pyme import core, constants
|
||||||
|
import support
|
||||||
|
|
||||||
|
class KeyEditor(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.steps = ["fpr", "expire", "1", "primary", "quit"]
|
||||||
|
self.step = 0
|
||||||
|
self.done = False
|
||||||
|
self.verbose = int(os.environ.get('verbose', 0)) > 1
|
||||||
|
|
||||||
|
def edit_fnc(self, status, args, out):
|
||||||
|
if args == "keyedit.prompt":
|
||||||
|
result = self.steps[self.step]
|
||||||
|
self.step += 1
|
||||||
|
elif args == "keyedit.save.okay":
|
||||||
|
result = "Y"
|
||||||
|
self.done = self.step == len(self.steps)
|
||||||
|
elif args == "keygen.valid":
|
||||||
|
result = "0"
|
||||||
|
else:
|
||||||
|
result = None
|
||||||
|
|
||||||
|
if self.verbose:
|
||||||
|
sys.stderr.write("Code: {}, args: {!r}, Returning: {!r}\n"
|
||||||
|
.format(status, args, result))
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
support.init_gpgme(constants.PROTOCOL_OpenPGP)
|
||||||
|
|
||||||
|
c = core.Context()
|
||||||
|
c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
|
||||||
|
c.set_passphrase_cb(lambda *args: "abc")
|
||||||
|
c.set_armor(True)
|
||||||
|
sink = core.Data()
|
||||||
|
|
||||||
|
editor = KeyEditor()
|
||||||
|
c.op_edit(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False),
|
||||||
|
editor.edit_fnc, sink, sink)
|
||||||
|
assert editor.done
|
Loading…
Reference in New Issue
Block a user