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 int *(*PyBytes_AsStringAndSize)(PyObject *, char **, Py_ssize_t *);
81 extern PyObject *(*PyBytes_FromObject)(PyObject *);
82 extern PyObject *(*PyBytes_FromStringAndSize)(const void *, size_t);
83
84 extern PyObject *(*PyUnicode_FromString)(const char *);
85 extern PyObject *(*PyUnicode_AsUTF8String)(PyObject *);
86 extern PyObject *(*PyImport_ImportModule)(const char *);
87 extern int (*PyCallable_Check)(PyObject *);
88 extern PyObject *(*PyDict_New)(void);
89 extern int (*PyDict_SetItemString)(PyObject *p, const char *key, PyObject *val);
90 extern int (*PyDict_Next)(PyObject *p, size_t *, PyObject **, PyObject **);
91 extern PyObject *(*PyDict_GetItemString)(PyObject *p, const char *key);
92 extern PyObject *(*PyList_New)(Py_ssize_t);
93 extern int (*PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
94 extern int (*PyArg_ParseTuple)(PyObject *, const char *, ...);
95
96 extern int (*PySequence_Check)(PyObject *o);
97 extern Py_ssize_t (*PySequence_Size)(PyObject *o);
98 extern PyObject *(*PySequence_GetItem)(PyObject *o, Py_ssize_t i);
99
100 extern void (*PyErr_Fetch)(PyObject **poPyType, PyObject **poPyValue,
101 PyObject **poPyTraceback);
102 extern void (*PyErr_Clear)(void);
103 extern const char *(*Py_GetVersion)(void);
104
105 typedef struct
106 {
107 // cppcheck-suppress unusedStructMember
108 char big_enough[256];
109 } Py_buffer;
110 extern int (*PyBuffer_FillInfo)(Py_buffer *view, PyObject *obj, void *buf,
111 size_t len, int readonly, int infoflags);
112 extern PyObject *(*PyMemoryView_FromBuffer)(Py_buffer *view);
113
114 typedef PyObject *(*PyCFunction)(PyObject *, PyObject *, PyObject *);
115
116 typedef struct PyMethodDef PyMethodDef;
117 struct PyMethodDef
118 {
119 const char *name;
120 PyCFunction function;
121 int flags;
122 const char *help;
123 };
124 extern PyObject *(*PyModule_Create2)(struct PyModuleDef *, int);
125
126 #define PYTHON_API_VERSION 1013
127
128 /* Flag passed to newmethodobject */
129 #define METH_VARARGS 0x0001
130 #define METH_KEYWORDS 0x0002
131
132 #define _PyObject_HEAD_EXTRA
133
134 struct _object
135 {
136 _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt;
137 void /*struct _typeobject*/ *ob_type;
138 };
139
140 #define PyObject_HEAD PyObject ob_base;
141
142 #define _PyObject_EXTRA_INIT
143
144 #define PyObject_HEAD_INIT(type) {_PyObject_EXTRA_INIT 1, type},
145
146 #define PyModuleDef_HEAD_INIT \
147 { \
148 PyObject_HEAD_INIT(nullptr) nullptr, /* m_init */ \
149 0, /* m_index */ \
150 nullptr, /* m_copy */ \
151 }
152
153 typedef struct PyModuleDef_Base
154 {
155 PyObject_HEAD PyObject *(*m_init)(void);
156 Py_ssize_t m_index;
157 PyObject *m_copy;
158 } PyModuleDef_Base;
159
160 typedef void *traverseproc;
161 typedef void *inquiry;
162 typedef void *freefunc;
163
164 typedef struct PyModuleDef
165 {
166 PyModuleDef_Base m_base;
167 const char *m_name;
168 const char *m_doc;
169 Py_ssize_t m_size;
170 const PyMethodDef *m_methods;
171 struct PyModuleDef_Slot *m_slots;
172 traverseproc m_traverse;
173 inquiry m_clear;
174 freefunc m_free;
175 } PyModuleDef;
176
177 #define Py_file_input 257
178
179 typedef int PyGILState_STATE;
180 class GIL_Holder
181 {
182 bool m_bExclusiveLock;
183 PyGILState_STATE m_eState = 0;
184
185 public:
186 explicit GIL_Holder(bool bExclusiveLock);
187 virtual ~GIL_Holder();
188 };
189
190 CPLString GetString(PyObject *obj, bool bEmitError = true);
191 CPLString GetPyExceptionString();
192 bool ErrOccurredEmitCPLError();
193
194 } // namespace GDALPy
195
197
198 #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:226