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
36bool GDALPythonInitialize();
37
38void GDALPythonFinalize();
39
41
42// Subset of Python API defined as function pointers
43// Only use the below function pointers if GDALPythonInitialize() succeeds
44namespace GDALPy
45{
46typedef struct _object PyObject;
47typedef size_t Py_ssize_t;
48
49extern int (*Py_IsInitialized)(void);
50extern void (*Py_SetProgramName)(const wchar_t *);
51extern void (*Py_SetPythonHome)(const wchar_t *);
52extern PyObject *(*PyObject_Type)(PyObject *);
53extern int (*PyObject_IsInstance)(PyObject *, PyObject *);
54extern PyObject *(*PyTuple_New)(size_t);
55extern PyObject *(*PyBool_FromLong)(long);
56extern PyObject *(*PyLong_FromLong)(long);
57extern long (*PyLong_AsLong)(PyObject *);
58extern PyObject *(*PyLong_FromLongLong)(GIntBig);
59extern GIntBig (*PyLong_AsLongLong)(PyObject *);
60extern PyObject *(*PyFloat_FromDouble)(double);
61extern double (*PyFloat_AsDouble)(PyObject *);
62extern PyObject *(*PyObject_Call)(PyObject *, PyObject *, PyObject *);
63extern PyObject *(*PyObject_GetIter)(PyObject *);
64extern PyObject *(*PyIter_Next)(PyObject *);
65extern void (*Py_IncRef)(PyObject *);
66extern void (*Py_DecRef)(PyObject *);
67extern PyObject *(*PyErr_Occurred)(void);
68extern void (*PyErr_Print)(void);
69
70extern PyObject *(*Py_CompileString)(const char *, const char *, int);
71extern PyObject *(*PyImport_ExecCodeModule)(const char *, PyObject *);
72extern int (*PyObject_HasAttrString)(PyObject *, const char *);
73extern PyObject *(*PyObject_GetAttrString)(PyObject *, const char *);
74extern int (*PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
75extern int (*PyTuple_SetItem)(PyObject *, size_t, PyObject *);
76extern void (*PyObject_Print)(PyObject *, FILE *, int);
77
78extern Py_ssize_t (*PyBytes_Size)(PyObject *);
79extern const char *(*PyBytes_AsString)(PyObject *);
80extern int *(*PyBytes_AsStringAndSize)(PyObject *, char **, Py_ssize_t *);
81extern PyObject *(*PyBytes_FromObject)(PyObject *);
82extern PyObject *(*PyBytes_FromStringAndSize)(const void *, size_t);
83
84extern PyObject *(*PyUnicode_FromString)(const char *);
85extern PyObject *(*PyUnicode_AsUTF8String)(PyObject *);
86extern PyObject *(*PyImport_ImportModule)(const char *);
87extern int (*PyCallable_Check)(PyObject *);
88extern PyObject *(*PyDict_New)(void);
89extern int (*PyDict_SetItemString)(PyObject *p, const char *key, PyObject *val);
90extern int (*PyDict_Next)(PyObject *p, size_t *, PyObject **, PyObject **);
91extern PyObject *(*PyDict_GetItemString)(PyObject *p, const char *key);
92extern PyObject *(*PyList_New)(Py_ssize_t);
93extern int (*PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
94extern int (*PyArg_ParseTuple)(PyObject *, const char *, ...);
95
96extern int (*PySequence_Check)(PyObject *o);
97extern Py_ssize_t (*PySequence_Size)(PyObject *o);
98extern PyObject *(*PySequence_GetItem)(PyObject *o, Py_ssize_t i);
99
100extern void (*PyErr_Fetch)(PyObject **poPyType, PyObject **poPyValue,
101 PyObject **poPyTraceback);
102extern void (*PyErr_Clear)(void);
103extern const char *(*Py_GetVersion)(void);
104
105typedef struct
106{
107 // cppcheck-suppress unusedStructMember
108 char big_enough[256];
109} Py_buffer;
110
111extern int (*PyBuffer_FillInfo)(Py_buffer *view, PyObject *obj, void *buf,
112 size_t len, int readonly, int infoflags);
113extern PyObject *(*PyMemoryView_FromBuffer)(Py_buffer *view);
114
115typedef PyObject *(*PyCFunction)(PyObject *, PyObject *, PyObject *);
116
117typedef struct PyMethodDef PyMethodDef;
118
119struct PyMethodDef
120{
121 const char *name;
122 PyCFunction function;
123 int flags;
124 const char *help;
125};
126
127extern 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
137struct _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
156typedef 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
163typedef void *traverseproc;
164typedef void *inquiry;
165typedef void *freefunc;
166
167typedef 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
182typedef int PyGILState_STATE;
183
184class 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();
196bool 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.