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
111 extern int (*PyBuffer_FillInfo)(Py_buffer *view, PyObject *obj, void *buf,
112 size_t len, int readonly, int infoflags);
113 extern PyObject *(*PyMemoryView_FromBuffer)(Py_buffer *view);
114
115 typedef PyObject *(*PyCFunction)(PyObject *, PyObject *, PyObject *);
116
117 typedef struct PyMethodDef PyMethodDef;
118
119 struct PyMethodDef
120{
121 const char *name;
122 PyCFunction function;
123 int flags;
124 const char *help;
125};
126
127 extern PyObject *(*PyModule_Create2)(struct PyModuleDef *, int);
128
129#define PYTHON_API_VERSION 1013
130
131/* Flag passed to newmethodobject */
132#define METH_VARARGS 0x0001
133#define METH_KEYWORDS 0x0002
134
135#define _PyObject_HEAD_EXTRA
136
137 struct _object
138{
139 _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt;
140 void /*struct _typeobject*/ *ob_type;
141};
142
143#define PyObject_HEAD PyObject ob_base;
144
145#define _PyObject_EXTRA_INIT
146
147#define PyObject_HEAD_INIT(type) {_PyObject_EXTRA_INIT 1, type},
148
149#define PyModuleDef_HEAD_INIT \
150 { \
151 PyObject_HEAD_INIT(nullptr) nullptr, /* m_init */ \
152 0, /* m_index */ \
153 nullptr, /* m_copy */ \
154 }
155
156 typedef struct PyModuleDef_Base
157{
158 PyObject_HEAD PyObject *(*m_init)(void);
159 Py_ssize_t m_index;
160 PyObject *m_copy;
161} PyModuleDef_Base;
162
163 typedef void *traverseproc;
164 typedef void *inquiry;
165 typedef void *freefunc;
166
167 typedef struct PyModuleDef
168{
169 PyModuleDef_Base m_base;
170 const char *m_name;
171 const char *m_doc;
172 Py_ssize_t m_size;
173 const PyMethodDef *m_methods;
174 struct PyModuleDef_Slot *m_slots;
175 traverseproc m_traverse;
176 inquiry m_clear;
177 freefunc m_free;
178} PyModuleDef;
179
180#define Py_file_input 257
181
182 typedef int PyGILState_STATE;
183
184 class GIL_Holder
185{
186 bool m_bExclusiveLock;
187 PyGILState_STATE m_eState = 0;
188
189 public:
190 explicit GIL_Holder(bool bExclusiveLock);
191 virtual ~GIL_Holder();
192};
193
194 CPLString GetString(PyObject *obj, bool bEmitError = true);
195 CPLString GetPyExceptionString();
196 bool ErrOccurredEmitCPLError();
197
198} // namespace GDALPy
199
201
202#endif
CPLString
Convenient string class based on std::string.
Definition cpl_string.h:320
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:215
cpl_string.h
Various convenience functions for working with strings and string lists.