-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPyExtensionManager.cpp
More file actions
252 lines (210 loc) · 6.8 KB
/
PyExtensionManager.cpp
File metadata and controls
252 lines (210 loc) · 6.8 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
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
/* -*- c-basic-offset: 8 indent-tabs-mode: t -*- */
/*
* Vampy : This plugin is a wrapper around the Vamp plugin API.
* It allows for writing Vamp plugins in Python.
* Centre for Digital Music, Queen Mary University of London.
* Copyright (C) 2008-2009 Gyorgy Fazekas, QMUL. (See Vamp sources
* for licence information.)
*/
#include <Python.h>
#include "vamp/vamp.h"
#include "PyExtensionModule.h"
#include "PyExtensionManager.h"
#include <algorithm>
#include "Debug.h"
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::find;
//static
const char* PyExtensionManager::m_exposedNames[] = {
"ParameterDescriptor",
"OutputDescriptor",
"FeatureSet",
"Feature",
"RealTime",
"frame2RealTime",
/* // using builtin objects:
"ParameterList",
"OutputList",
"FeatureList",
"OneSamplePerStep",
"FixedSampleRate",
"VariableSampleRate",
"TimeDomain",
"FrequencyDomain",
*/
NULL
};
PyExtensionManager::PyExtensionManager() :
m_pyGlobalNamespace(0),
m_pyVampyNamespace(0)
{
DSTREAM << "Creating extension manager." << endl;
}
bool
PyExtensionManager::initExtension()
{
DSTREAM << "Initialising extension module." << endl;
/// call the module initialiser first
initvampy();
/// these references are all borrowed
m_pyGlobalNamespace = PyImport_GetModuleDict();
if (!m_pyGlobalNamespace)
{cerr << "Vampy::PyExtensionManager::initExtension: GlobalNamespace failed." << endl; return false;}
PyObject *pyVampyModule = PyDict_GetItemString(m_pyGlobalNamespace,"vampy");
if (!pyVampyModule)
{cerr << "Vampy::PyExtensionManager::initExtension: VampyModule failed." << endl; return false;}
m_pyVampyNamespace = PyModule_GetDict(pyVampyModule);
if (!m_pyVampyNamespace)
{cerr << "Vampy::PyExtensionManager::initExtension: VampyNamespace failed." << endl; return false;}
/// initialise local namespaces
updateAllLocals();
DSTREAM << "Vampy: Extension namespaces updated." << endl;
return true;
}
PyExtensionManager::~PyExtensionManager()
{
if (!m_pyVampyNamespace) {
DSTREAM << "Vampy::~PyExtensionManager: manager was never initialised, or initialisation did not complete: not attempting cleanup" << endl;
return;
}
DSTREAM << "Cleaning locals..." << endl;
cleanAllLocals();
DSTREAM << "Cleaning module..." << endl;
if (!cleanModule()) {
cerr << "Vampy::~PyExtensionManager: failed to clean extension module." << endl;
} else {
DSTREAM << "Vampy::~PyExtensionManager: Extension module cleaned." << endl;
}
}
void
PyExtensionManager::setPlugModuleNames(vector<string> pyPlugs)
{
for (size_t i = 0; i < pyPlugs.size(); ++i) {
string modName = pyPlugs[i];
string tmp = modName.substr(modName.rfind(':')+1,modName.size()-1);
m_plugModuleNames.push_back(tmp);
DSTREAM << "Inserted module name: " << tmp << endl;
}
}
void
PyExtensionManager::deleteModuleName(string plugKey)
{
string name = plugKey.substr(plugKey.rfind(':')+1,plugKey.size()-1);
vector<string>::iterator it =
find (m_plugModuleNames.begin(), m_plugModuleNames.end(), name);
if (it != m_plugModuleNames.end()) m_plugModuleNames.erase(it);
DSTREAM << "PyExtensionManager::deleteModuleName: Deleted module name: " << name << endl;
}
void
PyExtensionManager::cleanAllLocals() const
{
for (size_t i = 0; i < m_plugModuleNames.size(); ++i) {
cleanLocalNamespace(m_plugModuleNames[i].c_str());
}
}
void
PyExtensionManager::updateAllLocals() const
{
for (size_t i = 0; i < m_plugModuleNames.size(); ++i) {
updateLocalNamespace(m_plugModuleNames[i].c_str());
}
}
void
PyExtensionManager::cleanLocalNamespace(const char* plugModuleName) const
{
DSTREAM << "Cleaning local namespace: " << plugModuleName << endl;
/// these references are all borrowed
PyObject *pyPlugModule = PyDict_GetItemString(m_pyGlobalNamespace,plugModuleName);
if (!pyPlugModule) return;
PyObject *pyPlugDict = PyModule_GetDict(pyPlugModule);
if (!pyPlugDict) return;
int i = 0;
while (PyExtensionManager::m_exposedNames[i]) {
const char* name = PyExtensionManager::m_exposedNames[i];
i++;
PyObject *key = PyString_FromString(name);
if (!key) break;
if (PyDict_Contains(pyPlugDict,key)) {
if (PyDict_SetItem(pyPlugDict,key,Py_None) != 0)
cerr << "Vampy::PyExtensionManager::cleanLocalNamespace: Failed: "
<< name << " of "<< plugModuleName << endl;
else DSTREAM << "Cleaned local name: " << name << endl;
}
Py_DECREF(key);
}
}
void
PyExtensionManager::updateLocalNamespace(const char* plugModuleName) const
{
DSTREAM << "Updating local namespace: " << plugModuleName << endl;
/// this allows the use of common syntax like:
/// from vampy import *
/// even after several unload/reload cycles
/// these references are all borrowed
PyObject *pyPlugModule = PyDict_GetItemString(m_pyGlobalNamespace,plugModuleName);
if (!pyPlugModule) return;
PyObject *pyPlugDict = PyModule_GetDict(pyPlugModule);
if (!pyPlugDict) return;
int i = 0;
while (PyExtensionManager::m_exposedNames[i]) {
const char* name = PyExtensionManager::m_exposedNames[i];
i++;
PyObject *key = PyString_FromString(name);
if (!key) break;
if (PyDict_Contains(pyPlugDict,key)) {
PyObject* item = PyDict_GetItem(m_pyVampyNamespace,key);
if (PyDict_SetItem(pyPlugDict,key,item) != 0)
cerr << "Vampy::PyExtensionManager::updateLocalNamespace: Failed: "
<< name << " of "<< plugModuleName << endl;
else DSTREAM << "Updated local name: " << name << endl;
} else {
DSTREAM << "Local namespace does not contain name: " << name << endl;
}
Py_DECREF(key);
}
}
bool
PyExtensionManager::cleanModule(void) const
{
PyObject *m = PyImport_AddModule("vampy");
if (!m) {
if (PyErr_Occurred()) {PyErr_Print(); PyErr_Clear();}
cerr << "Vampy::PyExtensionManager::cleanModule: PyImport_AddModule returned NULL!" << endl;
return false;
} else {
PyObject *dict = PyModule_GetDict(m);
#ifdef _DEBUG
Py_ssize_t ln = PyDict_Size(dict);
DSTREAM << "Vampy::PyExtensionManager::cleanModule: Size of module dict = " << (int) ln << endl;
#endif
/// Clean the module dictionary.
// printDict(dict);
PyDict_Clear(dict);
if (PyErr_Occurred())
{ PyErr_Print(); PyErr_Clear(); return false; }
PyObject *name = PyString_FromString("vampy");
if (name) PyDict_SetItemString(dict,"__name__",name);
Py_XDECREF(name);
#ifdef _DEBUG
ln = PyDict_Size(dict);
DSTREAM << "Vampy::PyExtensionManager::cleanModule: Size of module dict (cleaned) = " << (int) ln << endl;
#endif
return true;
}
}
void
PyExtensionManager::printDict(PyObject* inDict) const
{
Py_ssize_t pyPos = 0;
PyObject *pyKey, *pyDictValue;
cerr << endl << endl << "Module dictionary contents: " << endl;
while (PyDict_Next(inDict, &pyPos, &pyKey, &pyDictValue))
{
char *key = PyString_AS_STRING(pyKey);
char *val = PyString_AS_STRING(PyObject_Str(pyDictValue));
cerr << "key: [ '" << key << "' ] value: " << val << endl;
}
}