Imported Upstream version 3.9.91
[platform/upstream/python-gobject.git] / gi / pygi-private.h
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  */
4 #ifndef __PYGI_PRIVATE_H__
5 #define __PYGI_PRIVATE_H__
6
7 #ifdef __PYGI_H__
8 #   error "Import pygi.h or pygi-private.h, but not both"
9 #endif
10
11 #ifdef HAVE_CONFIG_H
12 #   include <config.h>
13 #endif
14
15 #include <Python.h>
16
17 #include "pygi.h"
18
19 #include "pygobject-external.h"
20
21 #include "pygi-repository.h"
22 #include "pygi-info.h"
23 #include "pygi-struct.h"
24 #include "pygi-boxed.h"
25 #include "pygi-argument.h"
26 #include "pygi-type.h"
27 #include "pygi-foreign.h"
28 #include "pygi-closure.h"
29 #include "pygi-ccallback.h"
30 #include "pygi-property.h"
31 #include "pygi-signal-closure.h"
32 #include "pygi-invoke.h"
33 #include "pygi-cache.h"
34 #include "pygi-source.h"
35
36 G_BEGIN_DECLS
37 #if PY_VERSION_HEX >= 0x03000000
38
39 #define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
40     PyObject *py_error_prefix; \
41     py_error_prefix = PyUnicode_FromFormat(format, ## __VA_ARGS__); \
42     if (py_error_prefix != NULL) { \
43         PyObject *py_error_type, *py_error_value, *py_error_traceback; \
44         PyErr_Fetch(&py_error_type, &py_error_value, &py_error_traceback); \
45         if (PyUnicode_Check(py_error_value)) { \
46             PyObject *new; \
47             new = PyUnicode_Concat(py_error_prefix, py_error_value); \
48             Py_DECREF(py_error_value); \
49             if (new != NULL) { \
50                 py_error_value = new; \
51             } \
52         } \
53         PyErr_Restore(py_error_type, py_error_value, py_error_traceback); \
54         Py_DECREF(py_error_prefix); \
55     } \
56 } G_STMT_END
57
58 #else
59
60 #define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
61     PyObject *py_error_prefix; \
62     py_error_prefix = PyString_FromFormat(format, ## __VA_ARGS__); \
63     if (py_error_prefix != NULL) { \
64         PyObject *py_error_type, *py_error_value, *py_error_traceback; \
65         PyErr_Fetch(&py_error_type, &py_error_value, &py_error_traceback); \
66         if (PyString_Check(py_error_value)) { \
67             PyString_ConcatAndDel(&py_error_prefix, py_error_value); \
68             if (py_error_prefix != NULL) { \
69                 py_error_value = py_error_prefix; \
70             } \
71         } \
72         PyErr_Restore(py_error_type, py_error_value, py_error_traceback); \
73     } \
74 } G_STMT_END
75
76 #endif
77
78 /* Redefine g_array_index because we want it to return the i-th element, casted
79  * to the type t, of the array a, and not the i-th element of the array a
80  * casted to the type t. */
81 #define _g_array_index(a,t,i) \
82     *(t *)((a)->data + g_array_get_element_size(a) * (i))
83
84
85 G_END_DECLS
86
87 #endif /* __PYGI_PRIVATE_H__ */