-
Notifications
You must be signed in to change notification settings - Fork 432
/
Environment.cpp
277 lines (227 loc) · 8.15 KB
/
Environment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include <pdal/plang/Environment.hpp>
#include <pdal/plang/Redirector.hpp>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_ARRAY_UNIQUE_SYMBOL PDAL_ARRAY_API
#include <numpy/arrayobject.h>
#include <sstream>
#include <mutex>
#ifdef PDAL_COMPILER_MSVC
# pragma warning(disable: 4127) // conditional expression is constant
#endif
#include <Python.h>
#include <pystate.h>
#undef toupper
#undef tolower
#undef isspace
// http://www.linuxjournal.com/article/3641
// http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I
// http://stackoverflow.com/questions/6596016/python-threads-in-c
namespace pdal
{
namespace plang
{
static Environment* g_environment=0;
EnvironmentPtr Environment::get()
{
static std::once_flag flag;
auto init = []()
{
g_environment = new Environment();
};
std::call_once(flag, init);
return g_environment;
}
Environment::Environment()
{
// This awfulness is due to the import_array MACRO that returns a value
// in some cases and returns nothing at other times. Defining
// NUMPY_IMPORT_ARRAY_RETVAL to nothing makes it so we don't ever return
// a value. The function needs to be stuck in a function to deal with
// the return.
auto initNumpy = []()
{
#undef NUMPY_IMPORT_ARRAY_RETVAL
#define NUMPY_IMPORT_ARRAY_RETVAL
import_array();
};
PyImport_AppendInittab(const_cast<char*>("redirector"), redirector_init);
Py_Initialize();
initNumpy();
PyImport_ImportModule("redirector");
}
Environment::~Environment()
{
Py_Finalize();
}
void Environment::set_stdout(std::ostream* ostr)
{
m_redirector.set_stdout(ostr);
}
void Environment::reset_stdout()
{
m_redirector.reset_stdout();
}
std::string getTraceback()
{
// get exception info
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyErr_NormalizeException(&type, &value, &traceback);
std::ostringstream mssg;
if (traceback)
{
PyObject* tracebackModule;
PyObject* tracebackDictionary;
PyObject* tracebackFunction;
tracebackModule = PyImport_ImportModule("traceback");
if (!tracebackModule)
throw pdal::pdal_error("Unable to load traceback module while "
"importing numpy inside PDAL.");
tracebackDictionary = PyModule_GetDict(tracebackModule);
tracebackFunction =
PyDict_GetItemString(tracebackDictionary, "format_exception");
if (!tracebackFunction)
throw pdal::pdal_error("Unable to find traceback function while "
"importing numpy inside PDAL.");
if (!PyCallable_Check(tracebackFunction))
throw pdal::pdal_error("Invalid traceback function while importing numpy "
"inside PDAL.");
// create an argument for "format exception"
PyObject* args = PyTuple_New(3);
PyTuple_SetItem(args, 0, type);
PyTuple_SetItem(args, 1, value);
PyTuple_SetItem(args, 2, traceback);
// get a list of string describing what went wrong
PyObject* output = PyObject_CallObject(tracebackFunction, args);
// print error message
int n = PyList_Size(output);
for (int i = 0; i < n; i++)
{
#if PY_MAJOR_VERSION >= 3
PyObject* u = PyUnicode_AsUTF8String(PyList_GetItem(output, i));
const char* p = PyBytes_AsString(u);
mssg << p;
#else
mssg << PyUnicode_AsString(PyList_GetItem(output, i));
#endif
}
// clean up
Py_XDECREF(args);
Py_XDECREF(output);
}
else if (value != NULL)
{
PyObject *s = PyObject_Str(value);
#if PY_MAJOR_VERSION >= 3
// const char* text = PyUnicode_AS_DATA(s);
PyObject* u = PyUnicode_AsUTF8String(s);
const char* text = PyBytes_AsString(u);
#else
const char* text = PyString_AS_STRING(s);
#endif
Py_DECREF(s);
mssg << text;
}
else
mssg << "unknown error that we are unable to get a traceback for."
"Was it already printed/taken?";
Py_XDECREF(value);
Py_XDECREF(type);
Py_XDECREF(traceback);
return mssg.str();
}
PyObject *fromMetadata(MetadataNode m)
{
std::string name = m.name();
std::string value = m.value();
std::string type = m.type();
std::string description = m.description();
MetadataNodeList children = m.children();
PyObject *submeta = NULL;
if (children.size())
{
submeta = PyList_New(0);
for (MetadataNode& child : children)
PyList_Append(submeta, fromMetadata(child));
}
PyObject *data = PyTuple_New(5);
PyTuple_SetItem(data, 0, PyUnicode_FromString(name.data()));
PyTuple_SetItem(data, 1, PyUnicode_FromString(value.data()));
PyTuple_SetItem(data, 2, PyUnicode_FromString(type.data()));
PyTuple_SetItem(data, 3, PyUnicode_FromString(description.data()));
PyTuple_SetItem(data, 4, submeta);
return data;
}
void addMetadata(PyObject *list, MetadataNode m)
{
if (!PyList_Check(list))
return;
for (Py_ssize_t i = 0; i < PyList_Size(list); ++i)
{
PyObject *tuple = PyList_GetItem(list, i);
if (!PyTuple_Check(tuple) || PyTuple_Size(tuple) != 5)
continue;
PyObject *o;
o = PyTuple_GetItem(tuple, 0);
if (!o || !PyUnicode_Check(o))
continue;
PyObject* unicode = PyUnicode_AsASCIIString(PyObject_Str(o));
std::string name = PyBytes_AsString(unicode);
o = PyTuple_GetItem(tuple, 1);
if (!o || !PyUnicode_Check(o))
continue;
unicode = PyUnicode_AsASCIIString(PyObject_Str(o));
std::string value = PyBytes_AsString(unicode);
o = PyTuple_GetItem(tuple, 2);
if (!o || !PyUnicode_Check(o))
continue;
unicode = PyUnicode_AsASCIIString(PyObject_Str(o));
std::string type = PyBytes_AsString(unicode);
if (type.empty())
type = Metadata::inferType(value);
o = PyTuple_GetItem(tuple, 3);
if (!o || !PyUnicode_Check(o))
continue;
unicode = PyUnicode_AsASCIIString(PyObject_Str(o));
std::string description = PyBytes_AsString(unicode);
PyObject *submeta = PyTuple_GetItem(tuple, 4);
MetadataNode child = m.add(name, value, type, description);
if (submeta)
addMetadata(submeta, child);
}
}
} // namespace plang
} // namespace pdal