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
34 bool GDALPythonInitialize();
35
36 void GDALPythonFinalize();
37
39
40 // Subset of Python API defined as function pointers
41 // Only use the below function pointers if GDALPythonInitialize() succeeds
42 namespace GDALPy
43 {
44 typedef struct _object PyObject;
45 typedef size_t Py_ssize_t;
46
47 extern int (*Py_IsInitialized)(void);
48 extern void (*Py_SetProgramName)(const char*);
49 extern PyObject* (*PyObject_Type)(PyObject*);
50 extern int (*PyObject_IsInstance)(PyObject*, PyObject*);
51 extern PyObject* (*PyTuple_New)(size_t);
52 extern PyObject* (*PyBool_FromLong)(long);
53 extern PyObject* (*PyLong_FromLong)(long);
54 extern long (*PyLong_AsLong)(PyObject *);
55 extern PyObject* (*PyLong_FromLongLong)(GIntBig);
56 extern GIntBig (*PyLong_AsLongLong)(PyObject *);
57 extern PyObject* (*PyFloat_FromDouble)(double);
58 extern double (*PyFloat_AsDouble)(PyObject*);
59 extern PyObject* (*PyObject_Call)(PyObject*, PyObject*, PyObject*);
60 extern PyObject* (*PyObject_GetIter)(PyObject*);
61 extern PyObject* (*PyIter_Next)(PyObject*);
62 extern void (*Py_IncRef)(PyObject*);
63 extern void (*Py_DecRef)(PyObject*);
64 extern PyObject* (*PyErr_Occurred)(void);
65 extern void (*PyErr_Print)(void);
66
67 extern PyObject* (*Py_CompileString)(const char*, const char*, int);
68 extern PyObject* (*PyImport_ExecCodeModule)(const char*, PyObject*);
69 extern int (*PyObject_HasAttrString)(PyObject*, const char*);
70 extern PyObject* (*PyObject_GetAttrString)(PyObject*, const char*);
71 extern int (*PyObject_SetAttrString)(PyObject*, const char*, PyObject*);
72 extern int (*PyTuple_SetItem)(PyObject *, size_t, PyObject *);
73 extern void (*PyObject_Print)(PyObject*,FILE*,int);
74
75 extern Py_ssize_t (*PyBytes_Size)(PyObject *);
76 extern const char* (*PyBytes_AsString)(PyObject*);
77 extern PyObject* (*PyBytes_FromStringAndSize)(const void*, size_t);
78
79 extern PyObject* (*PyUnicode_FromString)(const char*);
80 extern PyObject* (*PyUnicode_AsUTF8String)(PyObject *);
81 extern PyObject* (*PyImport_ImportModule)(const char*);
82 extern int (*PyCallable_Check)(PyObject*);
83 extern PyObject* (*PyDict_New)(void);
84 extern int (*PyDict_SetItemString)(PyObject *p, const char *key,
85 PyObject *val);
86 extern int (*PyDict_Next)(PyObject *p, size_t *, PyObject **, PyObject **);
87 extern PyObject* (*PyDict_GetItemString)(PyObject *p, const char *key);
88 extern PyObject* (*PyList_New)(Py_ssize_t);
89 extern int (*PyList_SetItem)(PyObject *, Py_ssize_t , PyObject *);
90 extern int (*PyArg_ParseTuple)(PyObject *, const char *, ...);
91
92 extern int (*PySequence_Check)(PyObject *o);
93 extern Py_ssize_t (*PySequence_Size)(PyObject *o);
94 extern PyObject* (*PySequence_GetItem)(PyObject *o, Py_ssize_t i);
95
96 extern void (*PyErr_Fetch)(PyObject **poPyType, PyObject **poPyValue,
97 PyObject **poPyTraceback);
98 extern void (*PyErr_Clear)(void);
99 extern const char* (*Py_GetVersion)(void);
100
101 typedef struct
102 {
103 //cppcheck-suppress unusedStructMember
104 char big_enough[256];
105 } Py_buffer;
106 extern int (*PyBuffer_FillInfo)(Py_buffer *view, PyObject *obj, void *buf,
107 size_t len, int readonly, int infoflags);
108 extern PyObject* (*PyMemoryView_FromBuffer)(Py_buffer *view);
109
110
111 typedef PyObject* (*PyCFunction)(PyObject*, PyObject*, PyObject*);
112
113 typedef struct PyMethodDef PyMethodDef;
114 struct PyMethodDef
115 {
116 const char* name;
117 PyCFunction function;
118 int flags;
119 const char* help;
120 };
121 extern PyObject * (*PyModule_Create2)(struct PyModuleDef*, int);
122
123 #define PYTHON_API_VERSION 1013
124
125 /* Flag passed to newmethodobject */
126 #define METH_VARARGS 0x0001
127 #define METH_KEYWORDS 0x0002
128
129 #define _PyObject_HEAD_EXTRA
130
131 struct _object {
132 _PyObject_HEAD_EXTRA
133 Py_ssize_t ob_refcnt;
134 void /*struct _typeobject*/ *ob_type;
135 };
136
137 #define PyObject_HEAD PyObject ob_base;
138
139 #define _PyObject_EXTRA_INIT
140
141 #define PyObject_HEAD_INIT(type) \
142 { _PyObject_EXTRA_INIT \
143 1, type },
144
145
146 #define PyModuleDef_HEAD_INIT { \
147 PyObject_HEAD_INIT(nullptr) \
148 nullptr, /* m_init */ \
149 0, /* m_index */ \
150 nullptr, /* m_copy */ \
151 }
152
153 typedef struct PyModuleDef_Base {
154 PyObject_HEAD
155 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 PyModuleDef_Base m_base;
166 const char* m_name;
167 const char* m_doc;
168 Py_ssize_t m_size;
169 const PyMethodDef *m_methods;
170 struct PyModuleDef_Slot* m_slots;
171 traverseproc m_traverse;
172 inquiry m_clear;
173 freefunc m_free;
174 } PyModuleDef;
175
176 #define Py_file_input 257
177
178 typedef int PyGILState_STATE;
179 class GIL_Holder
180 {
181 bool m_bExclusiveLock;
182 PyGILState_STATE m_eState = 0;
183
184 public:
185
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:333
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:248
cpl_string.h
Various convenience functions for working with strings and string lists.