From 70a3be27a509a1b5ea7372bee93d83c5019427ff Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 13 Sep 2016 10:44:14 +0200 Subject: 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 --- lang/python/helpers.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'lang/python/helpers.c') 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; -- cgit v1.2.3