python: Fix writing to data buffers.
* lang/python/gpgme.i: Add typemap for buffers. * lang/python/pyme/core.py (Data.write): Fix function. * lang/python/tests/Makefile.am: Add new test. * lang/python/tests/t-data.py: New file. Signed-off-by: Justus Winter <justus@gnupg.org>
This commit is contained in:
parent
e64bffe030
commit
f7094d8358
@ -173,6 +173,23 @@ PyObject* object_to_gpgme_t(PyObject* input, const char* objtype, int argnum) {
|
|||||||
free($1);
|
free($1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* For gpgme_data_write, but should be universal. */
|
||||||
|
%typemap(in) (const void *buffer, size_t size) {
|
||||||
|
if ($input == Py_None)
|
||||||
|
$1 = NULL, $2 = 0;
|
||||||
|
else if (PyUnicode_Check($input))
|
||||||
|
$1 = PyUnicode_AsUTF8AndSize($input, (size_t *) &$2);
|
||||||
|
else if (PyBytes_Check($input))
|
||||||
|
PyBytes_AsStringAndSize($input, (char **) &$1, (size_t *) &$2);
|
||||||
|
else {
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"arg %d: expected str, bytes, or None, got %s",
|
||||||
|
$argnum, $input->ob_type->tp_name);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
%typemap(freearg) (const void *buffer, size_t size) "";
|
||||||
|
|
||||||
// Make types containing 'next' field to be lists
|
// Make types containing 'next' field to be lists
|
||||||
%ignore next;
|
%ignore next;
|
||||||
%typemap(out) gpgme_sig_notation_t, gpgme_engine_info_t, gpgme_subkey_t, gpgme_key_sig_t,
|
%typemap(out) gpgme_sig_notation_t, gpgme_engine_info_t, gpgme_subkey_t, gpgme_key_sig_t,
|
||||||
|
@ -391,8 +391,10 @@ class Data(GpgmeWrapper):
|
|||||||
self.new_from_fd(file)
|
self.new_from_fd(file)
|
||||||
|
|
||||||
def write(self, buffer):
|
def write(self, buffer):
|
||||||
"""Write buffer given as bytes."""
|
"""Write buffer given as string or bytes.
|
||||||
errorcheck(pygpgme.gpgme_data_write(self.wrapped, buffer, len(buffer)))
|
|
||||||
|
If a string is given, it is implicitly encoded using UTF-8."""
|
||||||
|
return pygpgme.gpgme_data_write(self.wrapped, buffer)
|
||||||
|
|
||||||
def read(self, size = -1):
|
def read(self, size = -1):
|
||||||
"""Read at most size bytes, returned as bytes.
|
"""Read at most size bytes, returned as bytes.
|
||||||
|
@ -19,4 +19,4 @@
|
|||||||
TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \
|
TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \
|
||||||
PYTHONPATH=`echo $(abs_builddir)/../build/lib.*`
|
PYTHONPATH=`echo $(abs_builddir)/../build/lib.*`
|
||||||
|
|
||||||
TESTS = t-wrapper.py
|
TESTS = t-wrapper.py t-data.py
|
||||||
|
49
lang/python/tests/t-data.py
Executable file
49
lang/python/tests/t-data.py
Executable file
@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# 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 os
|
||||||
|
|
||||||
|
from pyme import core
|
||||||
|
|
||||||
|
data = core.Data('Hello world!')
|
||||||
|
assert data.read() == b'Hello world!'
|
||||||
|
assert data.read() == b''
|
||||||
|
|
||||||
|
data.seek(0, os.SEEK_SET)
|
||||||
|
assert data.read() == b'Hello world!'
|
||||||
|
assert data.read() == b''
|
||||||
|
|
||||||
|
data = core.Data(b'Hello world!')
|
||||||
|
assert data.read() == b'Hello world!'
|
||||||
|
|
||||||
|
data = core.Data()
|
||||||
|
data.write('Hello world!')
|
||||||
|
data.seek(0, os.SEEK_SET)
|
||||||
|
assert data.read() == b'Hello world!'
|
||||||
|
|
||||||
|
data = core.Data()
|
||||||
|
data.write(b'Hello world!')
|
||||||
|
data.seek(0, os.SEEK_SET)
|
||||||
|
assert data.read() == b'Hello world!'
|
||||||
|
|
||||||
|
binjunk = bytes(range(256))
|
||||||
|
data = core.Data()
|
||||||
|
data.write(binjunk)
|
||||||
|
data.seek(0, os.SEEK_SET)
|
||||||
|
assert data.read() == binjunk
|
Loading…
Reference in New Issue
Block a user