diff --git a/lang/python/gpgme.i b/lang/python/gpgme.i index 7c11943c..c5b5bc97 100644 --- a/lang/python/gpgme.i +++ b/lang/python/gpgme.i @@ -173,6 +173,23 @@ PyObject* object_to_gpgme_t(PyObject* input, const char* objtype, int argnum) { 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 %ignore next; %typemap(out) gpgme_sig_notation_t, gpgme_engine_info_t, gpgme_subkey_t, gpgme_key_sig_t, diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py index e822704a..dafbd9b3 100644 --- a/lang/python/pyme/core.py +++ b/lang/python/pyme/core.py @@ -391,8 +391,10 @@ class Data(GpgmeWrapper): self.new_from_fd(file) def write(self, buffer): - """Write buffer given as bytes.""" - errorcheck(pygpgme.gpgme_data_write(self.wrapped, buffer, len(buffer))) + """Write buffer given as string or bytes. + + 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): """Read at most size bytes, returned as bytes. diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am index 56ff74ce..50c78e35 100644 --- a/lang/python/tests/Makefile.am +++ b/lang/python/tests/Makefile.am @@ -19,4 +19,4 @@ TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \ PYTHONPATH=`echo $(abs_builddir)/../build/lib.*` -TESTS = t-wrapper.py +TESTS = t-wrapper.py t-data.py diff --git a/lang/python/tests/t-data.py b/lang/python/tests/t-data.py new file mode 100755 index 00000000..af2eb986 --- /dev/null +++ b/lang/python/tests/t-data.py @@ -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 . + +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