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
|
/*
* Copyright (c) 2020 One Identity
* Copyright (c) 2020 László Várady
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "python-bookmark.h"
#include "compat/compat-python.h"
#include "python-helpers.h"
#include <structmember.h>
typedef struct _PyBookmarkData
{
PyBookmark *py_bookmark;
} PyBookmarkData;
int
py_is_bookmark(PyObject *obj)
{
return PyType_IsSubtype(Py_TYPE(obj), &py_bookmark_type);
}
static void
py_bookmark_free(PyBookmark *self)
{
Py_CLEAR(self->data);
Py_CLEAR(self->save);
Py_TYPE(self)->tp_free((PyObject *) self);
}
PyBookmark *
py_bookmark_new(PyObject *data, PyObject *save)
{
PyBookmark *self = PyObject_New(PyBookmark, &py_bookmark_type);
if (!self)
return NULL;
Py_XINCREF(data);
self->data = data;
Py_XINCREF(save);
self->save = save;
return self;
}
PyBookmark *
bookmark_to_py_bookmark(Bookmark *bookmark)
{
PyBookmarkData *data = (PyBookmarkData *) &bookmark->container;
return data->py_bookmark;
}
static void
py_bookmark_save(Bookmark *bookmark)
{
PyBookmark *self = bookmark_to_py_bookmark(bookmark);
PyGILState_STATE gstate = PyGILState_Ensure();
if (self->save)
_py_invoke_void_function(self->save, self->data, "Bookmark", NULL);
Py_XDECREF(self);
PyGILState_Release(gstate);
}
void
py_bookmark_fill(Bookmark *bookmark, PyBookmark *py_bookmark)
{
bookmark->save = py_bookmark_save;
PyBookmarkData *data = (PyBookmarkData *) &bookmark->container;
Py_XINCREF(py_bookmark);
data->py_bookmark = py_bookmark;
}
static PyMemberDef
py_bookmark_members[] =
{
{ "data", T_OBJECT, offsetof(PyBookmark, data), 0 },
{ "save", T_OBJECT, offsetof(PyBookmark, save), 0 },
{NULL}
};
PyTypeObject py_bookmark_type =
{
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "Bookmark",
.tp_basicsize = sizeof(PyBookmark),
.tp_dealloc = (destructor) py_bookmark_free,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = "Bookmark class encapsulating a syslog-ng bookmark",
.tp_new = PyType_GenericNew,
.tp_members = py_bookmark_members,
0,
};
void
py_bookmark_global_init(void)
{
PyType_Ready(&py_bookmark_type);
}
|