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