summaryrefslogtreecommitdiff
path: root/gi/pygi-util.c
diff options
context:
space:
mode:
authorChristoph Reiter <reiter.christoph@gmail.com>2018-04-14 11:48:49 +0200
committerChristoph Reiter <reiter.christoph@gmail.com>2018-04-14 12:15:02 +0200
commit0520434aa75e191c1b679bbc1a738f7ee136c2e2 (patch)
tree1796b940ee82eeb4ac3edb17b2bb76596854b1fa /gi/pygi-util.c
parent7f5d8b08f7085ba1eec95f17d690d5d4b10902b8 (diff)
downloadpygobject-0520434aa75e191c1b679bbc1a738f7ee136c2e2.tar.gz
int converters: unify overflow error handling
In case functions like PyLong_AsLong() fail we bail out and just return the error set by it. In most cases this is due to overflow and results in error messages such as "too large to convert" and "too large to convert to C long". The problem here is that for the same type of error we get different error messages depending on the Python version, the architecture (long size) or the size of the value itself. To unify the error message reraise the OverflowError with our default error message in all cases.
Diffstat (limited to 'gi/pygi-util.c')
-rw-r--r--gi/pygi-util.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/gi/pygi-util.c b/gi/pygi-util.c
index ee322238..c02a53c0 100644
--- a/gi/pygi-util.c
+++ b/gi/pygi-util.c
@@ -18,6 +18,38 @@
#include "pygi-util.h"
+/**
+ * Like PyErr_Format, but supports the format syntax of
+ * PyUnicode_FromFormat also under Python 2.
+ * Note: Python 2 doesn't support %lld and %llo there.
+ */
+PyObject*
+pygi_pyerr_format (PyObject *exception, const char *format, ...)
+{
+ PyObject *text;
+ va_list argp;
+ va_start(argp, format);
+ text = PyUnicode_FromFormatV (format, argp);
+ va_end(argp);
+
+ if (text != NULL) {
+#if PY_MAJOR_VERSION < 3
+ PyObject *str;
+ str = PyUnicode_AsUTF8String (text);
+ Py_DECREF (text);
+ if (str) {
+ PyErr_SetObject (exception, str);
+ Py_DECREF (str);
+ }
+#else
+ PyErr_SetObject (exception, text);
+ Py_DECREF (text);
+#endif
+ }
+
+ return NULL;
+}
+
gboolean
pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result)
{