forked from diffpy/libdiffpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonInterface.cpp
More file actions
131 lines (109 loc) · 3.35 KB
/
PythonInterface.cpp
File metadata and controls
131 lines (109 loc) · 3.35 KB
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
/*****************************************************************************
*
* diffpy.srreal by DANSE Diffraction group
* Simon J. L. Billinge
* (c) 2009 Trustees of the Columbia University
* in the City of New York. All rights reserved.
*
* File coded by: Pavol Juhas
*
* See AUTHORS.txt for a list of people who contributed.
* See LICENSE.txt for license information.
*
******************************************************************************
*
* Common functions for interfacing with Python interpreter.
*
*****************************************************************************/
#include <diffpy/PythonInterface.hpp>
#include <iostream>
#include <csignal>
#include <map>
using namespace std;
using namespace boost;
namespace diffpy {
// Local Helpers -------------------------------------------------------------
namespace {
// Flag if Python has been launched from C++. This means there is no
// Python interpreter to handle error_already_set exceptions.
bool python_is_embedded = false;
// Execute import from python without any error handling
python::object do_python_import(const string& modname, const string& item)
{
typedef std::map<string, python::object> ObjectCache;
static ObjectCache cacheditems;
string fullname = modname + ":" + item;
ObjectCache::iterator ii = cacheditems.find(fullname);
if (ii == cacheditems.end())
{
python::object mod = python::import(modname.c_str());
python::object obj = mod.attr(item.c_str());
cacheditems[fullname] = obj;
ii = cacheditems.find(fullname);
}
return ii->second;
}
} // namespace
// Public Functions ----------------------------------------------------------
void initializePython(int py_argc, char* py_argv[])
{
if (Py_IsInitialized()) return;
if (!py_argc && !py_argv)
{
static const int initpy_argc = 1;
static char initpy_arg0[7] = "python";
static char* initpy_argv[initpy_argc] = {initpy_arg0};
py_argc = initpy_argc;
py_argv = initpy_argv;
}
Py_Initialize();
PySys_SetArgv(py_argc, py_argv);
// Make sure Python does not eat SIGINT.
python_is_embedded = true;
signal(SIGINT, SIG_DFL);
}
string getPythonErrorString()
{
string rv;
PyObject* ptype = NULL;
PyObject* pvalue = NULL;
PyObject* ptraceback = NULL;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
if (ptype == NULL) return rv;
PyObject* pemsg = PyObject_Str(pvalue);
rv = PyString_AsString(pemsg);
Py_XDECREF(pemsg);
PyErr_Restore(ptype, pvalue, ptraceback);
return rv;
}
python::object importFromPyModule(const string& modname, const string& item,
python::object fallback)
{
python::object rv;
try {
rv = do_python_import(modname, item);
}
catch (python::error_already_set e) {
PyErr_Clear();
rv = fallback;
}
return rv;
}
python::object importFromPyModule(const string& modname, const string& item)
{
python::object rv;
try {
rv = do_python_import(modname, item);
}
catch (python::error_already_set e) {
// display error message when running embedded
if (python_is_embedded)
{
cerr << getPythonErrorString() << endl;
}
throw;
}
return rv;
}
} // namespace diffpy
// End of file