Mercurial > hg > vampy
comparison PyExtensionManager.cpp @ 67:146d14ab15e7
Debug output: off by default, on with VAMPY_VERBOSE environment variable
author | Chris Cannam |
---|---|
date | Mon, 17 Nov 2014 10:03:44 +0000 |
parents | 5664fe298af2 |
children | f92587bedb2c |
comparison
equal
deleted
inserted
replaced
66:5664fe298af2 | 67:146d14ab15e7 |
---|---|
1 /* -*- c-basic-offset: 8 indent-tabs-mode: t -*- */ | |
1 /* | 2 /* |
2 | 3 |
3 * Vampy : This plugin is a wrapper around the Vamp plugin API. | 4 * Vampy : This plugin is a wrapper around the Vamp plugin API. |
4 * It allows for writing Vamp plugins in Python. | 5 * It allows for writing Vamp plugins in Python. |
5 | 6 |
12 #include <Python.h> | 13 #include <Python.h> |
13 #include "vamp/vamp.h" | 14 #include "vamp/vamp.h" |
14 #include "PyExtensionModule.h" | 15 #include "PyExtensionModule.h" |
15 #include "PyExtensionManager.h" | 16 #include "PyExtensionManager.h" |
16 #include <algorithm> | 17 #include <algorithm> |
18 #include "Debug.h" | |
17 | 19 |
18 using std::cerr; | 20 using std::cerr; |
19 using std::endl; | 21 using std::endl; |
20 using std::string; | 22 using std::string; |
21 using std::vector; | 23 using std::vector; |
42 NULL | 44 NULL |
43 }; | 45 }; |
44 | 46 |
45 PyExtensionManager::PyExtensionManager() | 47 PyExtensionManager::PyExtensionManager() |
46 { | 48 { |
47 #ifdef _DEBUG | 49 DSTREAM << "Creating extension manager." << endl; |
48 cerr << "Creating extension manager." << endl; | |
49 #endif | |
50 } | 50 } |
51 | 51 |
52 bool | 52 bool |
53 PyExtensionManager::initExtension() | 53 PyExtensionManager::initExtension() |
54 { | 54 { |
55 cerr << "Initialising extension module." << endl; | 55 DSTREAM << "Initialising extension module." << endl; |
56 | 56 |
57 /// call the module initialiser first | 57 /// call the module initialiser first |
58 initvampy(); | 58 initvampy(); |
59 | 59 |
60 /// these references are all borrowed | 60 /// these references are all borrowed |
68 if (!m_pyVampyNamespace) | 68 if (!m_pyVampyNamespace) |
69 {cerr << "Vampy::PyExtensionManager::initExtension: VampyNamespace failed." << endl; return false;} | 69 {cerr << "Vampy::PyExtensionManager::initExtension: VampyNamespace failed." << endl; return false;} |
70 | 70 |
71 /// initialise local namespaces | 71 /// initialise local namespaces |
72 updateAllLocals(); | 72 updateAllLocals(); |
73 #ifdef _DEBUG | 73 |
74 cerr << "Vampy: Extension namespaces updated." << endl; | 74 DSTREAM << "Vampy: Extension namespaces updated." << endl; |
75 #endif | 75 |
76 return true; | 76 return true; |
77 } | 77 } |
78 | 78 |
79 | 79 |
80 PyExtensionManager::~PyExtensionManager() | 80 PyExtensionManager::~PyExtensionManager() |
81 { | 81 { |
82 #ifdef _DEBUG | 82 DSTREAM << "Cleaning locals..." << endl; |
83 cerr << "Cleaning locals..." << endl; | |
84 #endif | |
85 | 83 |
86 cleanAllLocals(); | 84 cleanAllLocals(); |
87 | 85 |
88 #ifdef _DEBUG | 86 DSTREAM << "Cleaning module..." << endl; |
89 cerr << "Cleaning module..." << endl; | |
90 #endif | |
91 | 87 |
92 if (!cleanModule()) | 88 if (!cleanModule()) |
93 cerr << "Vampy::~PyExtensionManager: failed to clean extension module." << endl; | 89 cerr << "Vampy::~PyExtensionManager: failed to clean extension module." << endl; |
94 cerr << "Vampy::~PyExtensionManager: Extension module cleaned." << endl; | 90 DSTREAM << "Vampy::~PyExtensionManager: Extension module cleaned." << endl; |
95 } | 91 } |
96 | 92 |
97 | 93 |
98 | 94 |
99 void | 95 void |
102 for (size_t i = 0; i < pyPlugs.size(); ++i) { | 98 for (size_t i = 0; i < pyPlugs.size(); ++i) { |
103 string modName = pyPlugs[i]; | 99 string modName = pyPlugs[i]; |
104 string tmp = modName.substr(modName.rfind(':')+1,modName.size()-1); | 100 string tmp = modName.substr(modName.rfind(':')+1,modName.size()-1); |
105 m_plugModuleNames.push_back(tmp); | 101 m_plugModuleNames.push_back(tmp); |
106 | 102 |
107 #ifdef _DEBUG_VALUES | 103 DSTREAM << "Inserted module name: " << tmp << endl; |
108 cerr << "Inserted module name: " << tmp << endl; | |
109 #endif | |
110 | |
111 } | 104 } |
112 } | 105 } |
113 | 106 |
114 void | 107 void |
115 PyExtensionManager::deleteModuleName(string plugKey) | 108 PyExtensionManager::deleteModuleName(string plugKey) |
116 { | 109 { |
117 string name = plugKey.substr(plugKey.rfind(':')+1,plugKey.size()-1); | 110 string name = plugKey.substr(plugKey.rfind(':')+1,plugKey.size()-1); |
118 vector<string>::iterator it = | 111 vector<string>::iterator it = |
119 find (m_plugModuleNames.begin(), m_plugModuleNames.end(), name); | 112 find (m_plugModuleNames.begin(), m_plugModuleNames.end(), name); |
120 if (it != m_plugModuleNames.end()) m_plugModuleNames.erase(it); | 113 if (it != m_plugModuleNames.end()) m_plugModuleNames.erase(it); |
121 #ifdef _DEBUG_VALUES | 114 |
122 cerr << "PyExtensionManager::deleteModuleName: Deleted module name: " << name << endl; | 115 DSTREAM << "PyExtensionManager::deleteModuleName: Deleted module name: " << name << endl; |
123 #endif | |
124 } | 116 } |
125 | 117 |
126 | 118 |
127 void | 119 void |
128 PyExtensionManager::cleanAllLocals() const | 120 PyExtensionManager::cleanAllLocals() const |
158 if (!key) break; | 150 if (!key) break; |
159 if (PyDict_Contains(pyPlugDict,key)) { | 151 if (PyDict_Contains(pyPlugDict,key)) { |
160 if (PyDict_SetItem(pyPlugDict,key,Py_None) != 0) | 152 if (PyDict_SetItem(pyPlugDict,key,Py_None) != 0) |
161 cerr << "Vampy::PyExtensionManager::cleanLocalNamespace: Failed: " | 153 cerr << "Vampy::PyExtensionManager::cleanLocalNamespace: Failed: " |
162 << name << " of "<< plugModuleName << endl; | 154 << name << " of "<< plugModuleName << endl; |
163 #ifdef _DEBUG_VALUES | 155 else DSTREAM << "Cleaned local name: " << name << endl; |
164 else cerr << "Cleaned local name: " << name << endl; | |
165 #endif | |
166 } | 156 } |
167 Py_DECREF(key); | 157 Py_DECREF(key); |
168 } | 158 } |
169 } | 159 } |
170 | 160 |
190 if (PyDict_Contains(pyPlugDict,key)) { | 180 if (PyDict_Contains(pyPlugDict,key)) { |
191 PyObject* item = PyDict_GetItem(m_pyVampyNamespace,key); | 181 PyObject* item = PyDict_GetItem(m_pyVampyNamespace,key); |
192 if (PyDict_SetItem(pyPlugDict,key,item) != 0) | 182 if (PyDict_SetItem(pyPlugDict,key,item) != 0) |
193 cerr << "Vampy::PyExtensionManager::updateLocalNamespace: Failed: " | 183 cerr << "Vampy::PyExtensionManager::updateLocalNamespace: Failed: " |
194 << name << " of "<< plugModuleName << endl; | 184 << name << " of "<< plugModuleName << endl; |
195 #ifdef _DEBUG_VALUES | 185 else DSTREAM << "Updated local name: " << name << endl; |
196 else cerr << "Updated local name: " << name << endl; | |
197 #endif | |
198 } | 186 } |
199 Py_DECREF(key); | 187 Py_DECREF(key); |
200 } | 188 } |
201 } | 189 } |
202 | 190 |
212 return false; | 200 return false; |
213 } else { | 201 } else { |
214 PyObject *dict = PyModule_GetDict(m); | 202 PyObject *dict = PyModule_GetDict(m); |
215 #ifdef _DEBUG | 203 #ifdef _DEBUG |
216 Py_ssize_t ln = PyDict_Size(dict); | 204 Py_ssize_t ln = PyDict_Size(dict); |
217 cerr << "Vampy::PyExtensionManager::cleanModule: Size of module dict = " << (int) ln << endl; | 205 DSTREAM << "Vampy::PyExtensionManager::cleanModule: Size of module dict = " << (int) ln << endl; |
218 #endif | 206 #endif |
219 /// Clean the module dictionary. | 207 /// Clean the module dictionary. |
220 // printDict(dict); | 208 // printDict(dict); |
221 PyDict_Clear(dict); | 209 PyDict_Clear(dict); |
222 if (PyErr_Occurred()) | 210 if (PyErr_Occurred()) |
224 PyObject *name = PyString_FromString("vampy"); | 212 PyObject *name = PyString_FromString("vampy"); |
225 if (name) PyDict_SetItemString(dict,"__name__",name); | 213 if (name) PyDict_SetItemString(dict,"__name__",name); |
226 Py_XDECREF(name); | 214 Py_XDECREF(name); |
227 #ifdef _DEBUG | 215 #ifdef _DEBUG |
228 ln = PyDict_Size(dict); | 216 ln = PyDict_Size(dict); |
229 cerr << "Vampy::PyExtensionManager::cleanModule: Size of module dict (cleaned) = " << (int) ln << endl; | 217 DSTREAM << "Vampy::PyExtensionManager::cleanModule: Size of module dict (cleaned) = " << (int) ln << endl; |
230 #endif | 218 #endif |
231 return true; | 219 return true; |
232 } | 220 } |
233 } | 221 } |
234 | 222 |