diff options
author | Justus Winter <[email protected]> | 2016-09-13 08:44:14 +0000 |
---|---|---|
committer | Justus Winter <[email protected]> | 2016-09-13 11:29:43 +0000 |
commit | 70a3be27a509a1b5ea7372bee93d83c5019427ff (patch) | |
tree | 26424006293c90a51baec2276c633850fa2f539b /lang/python/helpers.c | |
parent | python: Fix types and error handling. (diff) | |
download | gpgme-70a3be27a509a1b5ea7372bee93d83c5019427ff.tar.gz gpgme-70a3be27a509a1b5ea7372bee93d83c5019427ff.zip |
python: Handle slight differences between Python 2 and 3.
* lang/python/helpers.c (pyDataWriteCb): Handle Python integers being
returned on Python 2.
(pyDataSeekCb): Likewise.
* lang/python/pyme/core.py (Data.__init__): Fix testing for string
argument.
(Data.new_from_filepart): Likewise.
* lang/python/pyme/util.py (is_a_string): New function.
* lang/python/tests/t-encrypt-large.py (read_cb): Force evaluation of
generator.
* lang/python/tests/t-idiomatic.py: Partly skip test on Python 2.
* lang/python/tests/t-verify.py (check_result): Here, the difference
between 2 and 3 really matters. We cannot change the char *
conversion in Python 2 without breaking all existing applications, and
using bytestrings in Python 3 would be very inconvenient.
Signed-off-by: Justus Winter <[email protected]>
Diffstat (limited to 'lang/python/helpers.c')
-rw-r--r-- | lang/python/helpers.c | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/lang/python/helpers.c b/lang/python/helpers.c index 5b13fee0..bc8aed40 100644 --- a/lang/python/helpers.c +++ b/lang/python/helpers.c @@ -833,17 +833,21 @@ static ssize_t pyDataWriteCb(void *hook, const void *buffer, size_t size) goto leave; } - if (! PyLong_Check(retval)) { +#if PY_MAJOR_VERSION < 3 + if (PyInt_Check(retval)) + result = PyInt_AsSsize_t(retval); + else +#endif + if (PyLong_Check(retval)) + result = PyLong_AsSsize_t(retval); + else { PyErr_Format(PyExc_TypeError, - "expected int from read callback, got %s", + "expected int from write callback, got %s", retval->ob_type->tp_name); _pyme_stash_callback_exception(self); result = -1; - goto leave; } - result = PyLong_AsSsize_t(retval); - leave: Py_XDECREF(retval); return result; @@ -894,21 +898,25 @@ static off_t pyDataSeekCb(void *hook, off_t offset, int whence) goto leave; } - if (! PyLong_Check(retval)) { +#if PY_MAJOR_VERSION < 3 + if (PyInt_Check(retval)) + result = PyInt_AsLong(retval); + else +#endif + if (PyLong_Check(retval)) +#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 + result = PyLong_AsLongLong(retval); +#else + result = PyLong_AsLong(retval); +#endif + else { PyErr_Format(PyExc_TypeError, - "expected int from read callback, got %s", + "expected int from seek callback, got %s", retval->ob_type->tp_name); _pyme_stash_callback_exception(self); result = -1; - goto leave; } -#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 - result = PyLong_AsLongLong(retval); -#else - result = PyLong_AsLong(retval); -#endif - leave: Py_XDECREF(retval); return result; |