GDAL

- Main Page
- Related Pages
- Classes
- Files
- Download
- Issue Tracker

- File List
- File Members

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* (*PyBuffer_FromReadWriteMemory)(void*, size_t);
50 extern PyObject* (*PyObject_Type)(PyObject*);
51 extern int (*PyObject_IsInstance)(PyObject*, PyObject*);
52 extern PyObject* (*PyTuple_New)(size_t);
53 extern PyObject* (*PyBool_FromLong)(long);
54 extern PyObject* (*PyInt_FromLong)(long); // Py2 only normally, aliased on PyLong_FromLong on Py3
55 extern long (*PyInt_AsLong)(PyObject *); // Py2 only normally, aliased on PyLong_AsLong on Py3
56 extern PyObject* (*PyLong_FromLongLong)(GIntBig);
57 extern GIntBig (*PyLong_AsLongLong)(PyObject *);
58 extern PyObject* (*PyFloat_FromDouble)(double);
59 extern double (*PyFloat_AsDouble)(PyObject*);
60 extern PyObject* (*PyObject_Call)(PyObject*, PyObject*, PyObject*);
61 extern PyObject* (*PyObject_GetIter)(PyObject*);
62 extern PyObject* (*PyIter_Next)(PyObject*);
63 extern void (*Py_IncRef)(PyObject*);
64 extern void (*Py_DecRef)(PyObject*);
65 extern PyObject* (*PyErr_Occurred)(void);
66 extern void (*PyErr_Print)(void);
67
68 extern PyObject* (*Py_CompileString)(const char*, const char*, int);
69 extern PyObject* (*PyImport_ExecCodeModule)(const char*, PyObject*);
70 extern int (*PyObject_HasAttrString)(PyObject*, const char*);
71 extern PyObject* (*PyObject_GetAttrString)(PyObject*, const char*);
72 extern int (*PyObject_SetAttrString)(PyObject*, const char*, PyObject*);
73 extern int (*PyTuple_SetItem)(PyObject *, size_t, PyObject *);
74 extern void (*PyObject_Print)(PyObject*,FILE*,int);
75
76 extern Py_ssize_t (*PyBytes_Size)(PyObject *); // Py3 only normally, aliased on PyString_Size on Py2
77 extern const char* (*PyBytes_AsString)(PyObject*); // Py3 only normally, aliased on PyString_AsString on Py2
78 extern PyObject* (*PyBytes_FromStringAndSize)(const void*, size_t); // Py3 only normally, aliased on PyString_FromStringAndSize on Py2
79
80 extern PyObject* (*PyString_FromStringAndSize)(const void*, size_t); // Py2 only normally, aliased on PyBytes_FromStringAndSize on Py3
81 extern const char* (*PyString_AsString)(PyObject*); // Py2 only. Not aliased for Py3
82
83 extern PyObject* (*PyUnicode_FromString)(const char*);
84 extern PyObject* (*PyUnicode_AsUTF8String)(PyObject *);
85 extern PyObject* (*PyImport_ImportModule)(const char*);
86 extern int (*PyCallable_Check)(PyObject*);
87 extern PyObject* (*PyDict_New)(void);
88 extern int (*PyDict_SetItemString)(PyObject *p, const char *key,
89 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 extern int (*PyBuffer_FillInfo)(Py_buffer *view, PyObject *obj, void *buf,
111 size_t len, int readonly, int infoflags);
112 extern PyObject* (*PyMemoryView_FromBuffer)(Py_buffer *view);
113
114
115 typedef PyObject* (*PyCFunction)(PyObject*, PyObject*, PyObject*);
116
117 typedef struct PyMethodDef PyMethodDef;
118 struct PyMethodDef
119 {
120 const char* name;
121 PyCFunction function;
122 int flags;
123 const char* help;
124 };
125 extern PyObject* (*Py_InitModule4)(const char*, const PyMethodDef*, const char*, PyObject*, int); // Py2 only
126
127 extern PyObject * (*PyModule_Create2)(struct PyModuleDef*, int); // Py3
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 _PyObject_HEAD_EXTRA
139 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) \
148 { _PyObject_EXTRA_INIT \
149 1, type },
150
151
152 #define PyModuleDef_HEAD_INIT { \
153 PyObject_HEAD_INIT(nullptr) \
154 nullptr, /* m_init */ \
155 0, /* m_index */ \
156 nullptr, /* m_copy */ \
157 }
158
159 typedef struct PyModuleDef_Base {
160 PyObject_HEAD
161 PyObject* (*m_init)(void);
162 Py_ssize_t m_index;
163 PyObject* m_copy;
164 } PyModuleDef_Base;
165
166 typedef void* traverseproc;
167 typedef void* inquiry;
168 typedef void* freefunc;
169
170 typedef struct PyModuleDef{
171 PyModuleDef_Base m_base;
172 const char* m_name;
173 const char* m_doc;
174 Py_ssize_t m_size;
175 const PyMethodDef *m_methods;
176 struct PyModuleDef_Slot* m_slots;
177 traverseproc m_traverse;
178 inquiry m_clear;
179 freefunc m_free;
180 } PyModuleDef;
181
182 #define Py_file_input 257
183
184 typedef int PyGILState_STATE;
185 class GIL_Holder
186 {
187 bool m_bExclusiveLock;
188 PyGILState_STATE m_eState = 0;
189
190 public:
191
192 explicit GIL_Holder(bool bExclusiveLock);
193 virtual ~GIL_Holder();
194 };
195
196 CPLString GetString(PyObject* obj, bool bEmitError = true);
197 CPLString GetPyExceptionString();
198 bool ErrOccurredEmitCPLError();
199
200 } // namespace GDALPy
201
203
204 #endif
GDALPy
Definition: gdalpython.cpp:65
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:332
cpl_string.h
Various convenience functions for working with strings and string lists.
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:248