-
Notifications
You must be signed in to change notification settings - Fork 1
/
hamr_python_deleter_impl.h
43 lines (39 loc) · 1.11 KB
/
hamr_python_deleter_impl.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
#include "hamr_gil_state.h"
#include <Python.h>
#include <iostream>
namespace hamr
{
// --------------------------------------------------------------------------
template <typename T>
python_deleter<T>::python_deleter(T *ptr, size_t n, PyObject *obj)
: m_ptr(ptr), m_elem(n), m_object(obj)
{
#if defined(HAMR_VERBOSE)
if (hamr::get_verbose())
{
std::cerr << "created python_deleter for array of " << n
<< " objects of type " << typeid(T).name() << sizeof(T)
<< " holding a reference to " << m_object << std::endl;
}
#endif
hamr::gil_state gil;
Py_INCREF(obj);
}
// --------------------------------------------------------------------------
template <typename T>
void python_deleter<T>::operator()(T *ptr)
{
(void)ptr;
assert(ptr == m_ptr);
#if defined(HAMR_VERBOSE)
if (hamr::get_verbose())
{
std::cerr << "python_deleter deleting array of " << m_elem
<< " objects of type " << typeid(T).name() << sizeof(T)
<< " release reference to " << m_object << std::endl;
}
#endif
hamr::gil_state gil;
Py_DECREF(m_object);
}
}