-
Notifications
You must be signed in to change notification settings - Fork 432
/
Environment.cpp
355 lines (298 loc) · 9.97 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/******************************************************************************
* 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.
****************************************************************************/
#ifndef _WIN32
#include <dlfcn.h>
#endif
#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
// See ticket #1010. This function runs when libplang is loaded. It makes
// sure python symbols can be found by extention module .so's since on some
// platforms (notably Ubuntu), they aren't linked with libpython even though
// they depend on it. If a platform doesn't support
// __attribute__ ((constructor)) this does nothing. We'll have to deal with
// those as they come up.
#ifndef _WIN32
__attribute__ ((constructor))
static void loadPython()
{
::dlopen(PDAL_PYTHON_LIBRARY, RTLD_LAZY | RTLD_GLOBAL);
}
#endif
// 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();
};
if (!Py_IsInitialized())
{
PyImport_AppendInittab(const_cast<char*>("redirector"), redirector_init);
Py_Initialize();
} else
{
m_redirector.init();
PyObject* added = PyImport_AddModule("redirector");
if (!added)
throw pdal_error("unable to add redirector module!");
}
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.");
tracebackDictionary = PyModule_GetDict(tracebackModule);
if (!tracebackDictionary)
throw pdal::pdal_error("Unable to load traceback dictionary.");
tracebackFunction =
PyDict_GetItemString(tracebackDictionary, "format_exception");
if (!tracebackFunction)
throw pdal::pdal_error("Unable to find traceback function.");
if (!PyCallable_Check(tracebackFunction))
throw pdal::pdal_error("Invalid traceback function.");
// 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
Py_ssize_t n = PyList_Size(output);
for (Py_ssize_t i = 0; i < n; i++)
{
PyObject* l = PyList_GetItem(output, i);
if (!l)
throw pdal::pdal_error("unable to get list item in getTraceback");
PyObject* r = PyObject_Repr(l);
if (!r)
throw pdal::pdal_error("unable to get repr in getTraceback");
#if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
char *d = PyUnicode_AsUTF8AndSize(r, &size);
#else
char *d = PyString_AsString(r);
#endif
mssg << d;
}
// clean up
Py_XDECREF(args);
Py_XDECREF(output);
}
else if (value != NULL)
{
PyObject* r = PyObject_Repr(value);
if (!r)
throw pdal::pdal_error("couldn't make string representation of traceback value");
#if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
char *d = PyUnicode_AsUTF8AndSize(r, &size);
#else
char *d = PyString_AsString(r);
#endif
mssg << d;
}
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 = PyList_New(0);
if (children.size())
{
for (MetadataNode& child : children)
PyList_Append(submeta, fromMetadata(child));
}
PyObject *data = PyDict_New();
PyDict_SetItemString(data, "name", PyUnicode_FromString(name.data()));
PyDict_SetItemString(data, "value", PyUnicode_FromString(value.data()));
PyDict_SetItemString(data, "type", PyUnicode_FromString(type.data()));
PyDict_SetItemString(data, "description", PyUnicode_FromString(description.data()));
PyDict_SetItemString(data, "children", submeta);
return data;
}
std::string readPythonString(PyObject* dict, const std::string& key)
{
std::stringstream ss;
PyObject* o = PyDict_GetItemString(dict, key.c_str());
if (!o)
{
std::stringstream oss;
oss << "Unable to get dictionary item '" << key << "'";
throw pdal_error(oss.str());
}
PyObject* r = PyObject_Str(o);
if (!r)
throw pdal::pdal_error("unable to get repr in readPythonString");
#if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
char *d = PyUnicode_AsUTF8AndSize(r, &size);
#else
char *d = PyString_AsString(r);
#endif
ss << d;
return ss.str();
}
void addMetadata(PyObject *dict, MetadataNode m)
{
if (! dict)
{
return;
}
if (!PyDict_Check(dict) )
throw pdal::pdal_error("'metadata' member must be a dictionary!");
std::string name = readPythonString(dict, "name");
std::string value = readPythonString(dict, "value");
std::string type = readPythonString(dict, "type");
if (type.empty())
type = Metadata::inferType(value);
std::string description = readPythonString(dict, "description");
PyObject *submeta = PyDict_GetItemString(dict, "children");
if (submeta)
{
if (!PyList_Check(submeta))
throw pdal::pdal_error("'children' metadata member must be a list!");
for (Py_ssize_t i = 0; i < PyList_Size(submeta); ++i)
{
PyObject* p = PyList_GetItem(submeta, i);
addMetadata(p, m);
}
MetadataNode child = m.addWithType(name, value, type, description);
}
}
int Environment::getPythonDataType(Dimension::Type t)
{
using namespace Dimension;
switch (t)
{
case Type::Float:
return NPY_FLOAT;
case Type::Double:
return NPY_DOUBLE;
case Type::Signed8:
return NPY_BYTE;
case Type::Signed16:
return NPY_SHORT;
case Type::Signed32:
return NPY_INT;
case Type::Signed64:
return NPY_LONGLONG;
case Type::Unsigned8:
return NPY_UBYTE;
case Type::Unsigned16:
return NPY_USHORT;
case Type::Unsigned32:
return NPY_UINT;
case Type::Unsigned64:
return NPY_ULONGLONG;
default:
return -1;
}
assert(0);
return -1;
}
} // namespace plang
} // namespace pdal