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 *(*PyCFunction_New)(const PyMethodDef *ml, PyObject *self);
112 extern int (*PyModule_AddObject)(PyObject *mod, const char *name,
113 PyObject *value);
114
115#define METH_VARARGS 0x0001
116#define METH_KEYWORDS 0x0002
117
118#define Py_file_input 257
119
120 typedef int PyGILState_STATE;
121
122 class GIL_Holder
123{
124 bool m_bExclusiveLock;
125 PyGILState_STATE m_eState = 0;
126
127 public:
128 explicit GIL_Holder(bool bExclusiveLock);
129 virtual ~GIL_Holder();
130};
131
132 CPLString GetString(PyObject *obj, bool bEmitError = true);
133 CPLString GetPyExceptionString();
134 bool ErrOccurredEmitCPLError();
135
136} // namespace GDALPy
137
139
140#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:205
cpl_string.h
Various convenience functions for working with strings and string lists.