GDAL

gdalpython.h
1 /******************************************************************************
2 *
3 * Project: GDAL Core
4 * Purpose: Python interface
5 * Author: Even Rouault, <even dot rouault at spatialys dot com>
6 *
7 ******************************************************************************
8 * Copyright (c) 2017-2019, Even Rouault, <even dot rouault at spatialys dot
9 *com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ****************************************************************************/
29
30 #ifndef GDALPYTHON_H_INCLUDED
31 #define GDALPYTHON_H_INCLUDED
32
33 #include "cpl_string.h"
34 #include <cwchar>
35
36 bool GDALPythonInitialize();
37
38 void GDALPythonFinalize();
39
41
42 // Subset of Python API defined as function pointers
43 // Only use the below function pointers if GDALPythonInitialize() succeeds
44 namespace GDALPy
45 {
46 typedef struct _object PyObject;
47 typedef size_t Py_ssize_t;
48
49 extern int (*Py_IsInitialized)(void);
50 extern void (*Py_SetProgramName)(const wchar_t *);
51 extern void (*Py_SetPythonHome)(const wchar_t *);
52 extern PyObject *(*PyObject_Type)(PyObject *);
53 extern int (*PyObject_IsInstance)(PyObject *, PyObject *);
54 extern PyObject *(*PyTuple_New)(size_t);
55 extern PyObject *(*PyBool_FromLong)(long);
56 extern PyObject *(*PyLong_FromLong)(long);
57 extern long (*PyLong_AsLong)(PyObject *);
58 extern PyObject *(*PyLong_FromLongLong)(GIntBig);
59 extern GIntBig (*PyLong_AsLongLong)(PyObject *);
60 extern PyObject *(*PyFloat_FromDouble)(double);
61 extern double (*PyFloat_AsDouble)(PyObject *);
62 extern PyObject *(*PyObject_Call)(PyObject *, PyObject *, PyObject *);
63 extern PyObject *(*PyObject_GetIter)(PyObject *);
64 extern PyObject *(*PyIter_Next)(PyObject *);
65 extern void (*Py_IncRef)(PyObject *);
66 extern void (*Py_DecRef)(PyObject *);
67 extern PyObject *(*PyErr_Occurred)(void);
68 extern void (*PyErr_Print)(void);
69
70 extern PyObject *(*Py_CompileString)(const char *, const char *, int);
71 extern PyObject *(*PyImport_ExecCodeModule)(const char *, PyObject *);
72 extern int (*PyObject_HasAttrString)(PyObject *, const char *);
73 extern PyObject *(*PyObject_GetAttrString)(PyObject *, const char *);
74 extern int (*PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
75 extern int (*PyTuple_SetItem)(PyObject *, size_t, PyObject *);
76 extern void (*PyObject_Print)(PyObject *, FILE *, int);
77
78 extern Py_ssize_t (*PyBytes_Size)(PyObject *);
79 extern const char *(*PyBytes_AsString)(PyObject *);
80 extern PyObject *(*PyBytes_FromStringAndSize)(const void *, size_t);
81
82 extern PyObject *(*PyUnicode_FromString)(const char *);
83 extern PyObject *(*PyUnicode_AsUTF8String)(PyObject *);
84 extern PyObject *(*PyImport_ImportModule)(const char *);
85 extern int (*PyCallable_Check)(PyObject *);
86 extern PyObject *(*PyDict_New)(void);
87 extern int (*PyDict_SetItemString)(PyObject *p, const char *key, PyObject *val);
88 extern int (*PyDict_Next)(PyObject *p, size_t *, PyObject **, PyObject **);
89 extern PyObject *(*PyDict_GetItemString)(PyObject *p, const char *key);
90 extern PyObject *(*PyList_New)(Py_ssize_t);
91 extern int (*PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
92 extern int (*PyArg_ParseTuple)(PyObject *, const char *, ...);
93
94 extern int (*PySequence_Check)(PyObject *o);
95 extern Py_ssize_t (*PySequence_Size)(PyObject *o);
96 extern PyObject *(*PySequence_GetItem)(PyObject *o, Py_ssize_t i);
97
98 extern void (*PyErr_Fetch)(PyObject **poPyType, PyObject **poPyValue,
99 PyObject **poPyTraceback);
100 extern void (*PyErr_Clear)(void);
101 extern const char *(*Py_GetVersion)(void);
102
103 typedef struct
104 {
105 // cppcheck-suppress unusedStructMember
106 char big_enough[256];
107 } Py_buffer;
108 extern int (*PyBuffer_FillInfo)(Py_buffer *view, PyObject *obj, void *buf,
109 size_t len, int readonly, int infoflags);
110 extern PyObject *(*PyMemoryView_FromBuffer)(Py_buffer *view);
111
112 typedef PyObject *(*PyCFunction)(PyObject *, PyObject *, PyObject *);
113
114 typedef struct PyMethodDef PyMethodDef;
115 struct PyMethodDef
116 {
117 const char *name;
118 PyCFunction function;
119 int flags;
120 const char *help;
121 };
122 extern PyObject *(*PyModule_Create2)(struct PyModuleDef *, int);
123
124 #define PYTHON_API_VERSION 1013
125
126 /* Flag passed to newmethodobject */
127 #define METH_VARARGS 0x0001
128 #define METH_KEYWORDS 0x0002
129
130 #define _PyObject_HEAD_EXTRA
131
132 struct _object
133 {
134 _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt;
135 void /*struct _typeobject*/ *ob_type;
136 };
137
138 #define PyObject_HEAD PyObject ob_base;
139
140 #define _PyObject_EXTRA_INIT
141
142 #define PyObject_HEAD_INIT(type) {_PyObject_EXTRA_INIT 1, type},
143
144 #define PyModuleDef_HEAD_INIT \
145 { \
146 PyObject_HEAD_INIT(nullptr) nullptr, /* m_init */ \
147 0, /* m_index */ \
148 nullptr, /* m_copy */ \
149 }
150
151 typedef struct PyModuleDef_Base
152 {
153 PyObject_HEAD PyObject *(*m_init)(void);
154 Py_ssize_t m_index;
155 PyObject *m_copy;
156 } PyModuleDef_Base;
157
158 typedef void *traverseproc;
159 typedef void *inquiry;
160 typedef void *freefunc;
161
162 typedef struct PyModuleDef
163 {
164 PyModuleDef_Base m_base;
165 const char *m_name;
166 const char *m_doc;
167 Py_ssize_t m_size;
168 const PyMethodDef *m_methods;
169 struct PyModuleDef_Slot *m_slots;
170 traverseproc m_traverse;
171 inquiry m_clear;
172 freefunc m_free;
173 } PyModuleDef;
174
175 #define Py_file_input 257
176
177 typedef int PyGILState_STATE;
178 class GIL_Holder
179 {
180 bool m_bExclusiveLock;
181 PyGILState_STATE m_eState = 0;
182
183 public:
184 explicit GIL_Holder(bool bExclusiveLock);
185 virtual ~GIL_Holder();
186 };
187
188 CPLString GetString(PyObject *obj, bool bEmitError = true);
189 CPLString GetPyExceptionString();
190 bool ErrOccurredEmitCPLError();
191
192 } // namespace GDALPy
193
195
196 #endif
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:311
cpl_string.h
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:235