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