forked from vamp-plugins/vampy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyExtensionManager.h
87 lines (64 loc) · 2.59 KB
/
PyExtensionManager.h
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
/* -*- 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.)
*/
/*
PyExtensionManager: This class is responsible for initialisation
and cleanup of the extension module, as well as the loaded plugin
module namespaces.
NOTES: Why do we need to clean up the module?
The module exposed by Vampy to the embedded interpreter contains
callback functions. These functions are accessed via function
pointers stored in the extension module's namespace dictionary.
Unfortunately, when the shared library is unloaded and reloaded
during a host session, these addresses might change.
Therefore, we reinitialise the module dict before each use.
However, this will cause garbage collection errors or segmentation
faults, when elements of the dict of the previous session are
attempted to free. Therefore, we clear the dictinary describing
the module namespace and replace all fuction pointers with Py_None
objects in individual plugin module namespaces. The reference
count on these can be safely decremented next time vampy is loaded
and the namespaces are reinitialised.
Why doesn't the GC clean this up correctly?
In a normal Python session the GC would deallocate the module
dict at the end. In embedded python, although the GC appears
to be called when the shared lib is unloaded, the interpreter
is reused. Since there is no C/API call to unload modules,
and at the time of unloading vampy the wrapped function pointers
are still valid, the GC doesn't collect them, nor are they freed
by the interpreter. When vampy is reloaded however, the module
dict will contain invalid addresses. The above procedure solves
this problem.
*/
#ifndef _PYEXTENSIONMANAGER_H_
#define _PYEXTENSIONMANAGER_H_
using std::cerr;
using std::endl;
using std::string;
using std::vector;
class PyExtensionManager
{
public:
PyExtensionManager();
~PyExtensionManager();
bool initExtension();
void setPlugModuleNames(vector<string> pyPlugs);
void deleteModuleName(string plugKey);
static const char* m_exposedNames[];
private:
vector<string> m_plugModuleNames;
PyObject* m_pyGlobalNamespace;
PyObject* m_pyVampyNamespace;
void cleanAllLocals() const;
void cleanLocalNamespace(const char* plugModuleName) const;
void updateAllLocals() const;
void updateLocalNamespace(const char* plugModuleName) const;
void printDict(PyObject* inDict) const;
bool cleanModule() const;
};
#endif