c7bc1d585e6db0adcf1a35612573fa4491f39ce7
[platform/upstream/pygobject2.git] / gi / pygi-util.h
1 #ifndef __PYGI_UTIL_H__
2 #define __PYGI_UTIL_H__
3
4 #include <Python.h>
5 #include <glib.h>
6 #include "pygobject-internal.h"
7 #include <pyglib-python-compat.h>
8
9 G_BEGIN_DECLS
10
11 PyObject * pyg_integer_richcompare(PyObject *v, PyObject *w, int op);
12
13 #if PY_VERSION_HEX >= 0x03000000
14
15 #define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
16     PyObject *py_error_prefix; \
17     py_error_prefix = PyUnicode_FromFormat(format, ## __VA_ARGS__); \
18     if (py_error_prefix != NULL) { \
19         PyObject *py_error_type, *py_error_value, *py_error_traceback; \
20         PyErr_Fetch(&py_error_type, &py_error_value, &py_error_traceback); \
21         if (PyUnicode_Check(py_error_value)) { \
22             PyObject *new; \
23             new = PyUnicode_Concat(py_error_prefix, py_error_value); \
24             Py_DECREF(py_error_value); \
25             if (new != NULL) { \
26                 py_error_value = new; \
27             } \
28         } \
29         PyErr_Restore(py_error_type, py_error_value, py_error_traceback); \
30         Py_DECREF(py_error_prefix); \
31     } \
32 } G_STMT_END
33
34 #else
35
36 #define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
37     PyObject *py_error_prefix; \
38     py_error_prefix = PyString_FromFormat(format, ## __VA_ARGS__); \
39     if (py_error_prefix != NULL) { \
40         PyObject *py_error_type, *py_error_value, *py_error_traceback; \
41         PyErr_Fetch(&py_error_type, &py_error_value, &py_error_traceback); \
42         if (PyString_Check(py_error_value)) { \
43             PyString_ConcatAndDel(&py_error_prefix, py_error_value); \
44             if (py_error_prefix != NULL) { \
45                 py_error_value = py_error_prefix; \
46             } \
47         } \
48         PyErr_Restore(py_error_type, py_error_value, py_error_traceback); \
49     } \
50 } G_STMT_END
51
52 #endif
53
54 G_END_DECLS
55
56 #endif /* __PYGI_UTIL_H__ */