1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-27 22:50:26 +03:00

talloc: Add python talloc module, move convenience functions to it.

Autobuild-User: Jelmer Vernooij <jelmer@samba.org>
Autobuild-Date: Fri Nov  5 02:48:21 UTC 2010 on sn-devel-104
This commit is contained in:
Jelmer Vernooij 2010-11-05 03:00:45 +01:00
parent 4edabb3256
commit cd172e7724
7 changed files with 102 additions and 63 deletions

84
lib/talloc/pytalloc.c Normal file
View File

@ -0,0 +1,84 @@
/*
Unix SMB/CIFS implementation.
Python Talloc Module
Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include <talloc.h>
#include <pytalloc.h>
/* print a talloc tree report for a talloc python object */
static PyObject *py_talloc_report_full(PyObject *self, PyObject *args)
{
PyObject *py_obj;
PyTypeObject *type;
if (!PyArg_ParseTuple(args, "O", &py_obj))
return NULL;
if (py_obj == Py_None) {
talloc_report_full(NULL, stdout);
} else {
type = (PyTypeObject*)PyObject_Type(py_obj);
talloc_report_full(py_talloc_get_mem_ctx(py_obj), stdout);
}
return Py_None;
}
/* enable null tracking */
static PyObject *py_talloc_enable_null_tracking(PyObject *self, PyObject *args)
{
talloc_enable_null_tracking();
return Py_None;
}
/* return the number of talloc blocks */
static PyObject *py_talloc_total_blocks(PyObject *self, PyObject *args)
{
PyObject *py_obj;
PyTypeObject *type;
if (!PyArg_ParseTuple(args, "O", &py_obj))
return NULL;
if (py_obj == Py_None) {
return PyLong_FromLong(talloc_total_blocks(NULL));
}
type = (PyTypeObject*)PyObject_Type(py_obj);
return PyLong_FromLong(talloc_total_blocks(py_talloc_get_mem_ctx(py_obj)));
}
static PyMethodDef talloc_methods[] = {
{ "report_full", (PyCFunction)py_talloc_report_full, METH_VARARGS,
"show a talloc tree for an object"},
{ "enable_null_tracking", (PyCFunction)py_talloc_enable_null_tracking, METH_VARARGS,
"enable tracking of the NULL object"},
{ "total_blocks", (PyCFunction)py_talloc_total_blocks, METH_VARARGS,
"return talloc block count"},
{ NULL }
};
void inittalloc(void)
{
PyObject *m;
m = Py_InitModule3("talloc", talloc_methods, "Debug utilities for talloc-wrapped objects.");
if (m == NULL)
return;
}

View File

@ -32,7 +32,7 @@ def set_options(opt):
action="store_true", dest='TALLOC_COMPAT1', default=False)
if opt.IN_LAUNCH_DIR():
opt.add_option('--disable-python',
help=("disable the pytevent module"),
help=("disable the pytalloc module"),
action="store_true", dest='disable_python', default=False)
@ -112,6 +112,11 @@ def build(bld):
vnum=VERSION,
)
bld.INSTALL_FILES('${INCLUDEDIR}', 'pytalloc.h')
bld.SAMBA_PYTHON('pytalloc',
'pytalloc.c',
deps='talloc pytalloc-util',
enabled=True,
realname='talloc.so')
if not getattr(bld.env, '_SAMBA_BUILD_', 0) == 4:
# s4 already has the talloc testsuite builtin to smbtorture

View File

@ -166,50 +166,6 @@ static PyObject *py_interface_ips(PyObject *self, PyObject *args)
return pylist;
}
/* print a talloc tree report for a talloc python object */
static PyObject *py_talloc_report_full(PyObject *self, PyObject *args)
{
PyObject *py_obj;
PyTypeObject *type;
if (!PyArg_ParseTuple(args, "O", &py_obj))
return NULL;
if (py_obj == Py_None) {
talloc_report_full(NULL, stdout);
} else {
type = (PyTypeObject*)PyObject_Type(py_obj);
talloc_report_full(py_talloc_get_mem_ctx(py_obj), stdout);
}
return Py_None;
}
/* enable null tracking */
static PyObject *py_talloc_enable_null_tracking(PyObject *self, PyObject *args)
{
talloc_enable_null_tracking();
return Py_None;
}
/* return the number of talloc blocks */
static PyObject *py_talloc_total_blocks(PyObject *self, PyObject *args)
{
PyObject *py_obj;
PyTypeObject *type;
if (!PyArg_ParseTuple(args, "O", &py_obj))
return NULL;
if (py_obj == Py_None) {
return PyLong_FromLong(talloc_total_blocks(NULL));
}
type = (PyTypeObject*)PyObject_Type(py_obj);
return PyLong_FromLong(talloc_total_blocks(py_talloc_get_mem_ctx(py_obj)));
}
static PyMethodDef py_misc_methods[] = {
{ "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
"generate_random_str(len) -> string\n"
@ -227,12 +183,6 @@ static PyMethodDef py_misc_methods[] = {
"set debug level" },
{ "interface_ips", (PyCFunction)py_interface_ips, METH_VARARGS,
"get interface IP address list"},
{ "talloc_report_full", (PyCFunction)py_talloc_report_full, METH_VARARGS,
"show a talloc tree for an object"},
{ "talloc_enable_null_tracking", (PyCFunction)py_talloc_enable_null_tracking, METH_VARARGS,
"enable tracking of the NULL object"},
{ "talloc_total_blocks", (PyCFunction)py_talloc_total_blocks, METH_VARARGS,
"return talloc block count"},
{ NULL }
};

View File

@ -327,6 +327,3 @@ interface_ips = _glue.interface_ips
set_debug_level = _glue.set_debug_level
unix2nttime = _glue.unix2nttime
generate_random_password = _glue.generate_random_password
talloc_report_full = _glue.talloc_report_full
talloc_enable_null_tracking = _glue.talloc_enable_null_tracking
talloc_total_blocks = _glue.talloc_total_blocks

View File

@ -32,9 +32,10 @@ from samba.net import Net
import logging
from samba.drs_utils import drs_Replicate
from samba.dsdb import DS_DOMAIN_FUNCTION_2008_R2
import talloc
# this makes debugging easier
samba.talloc_enable_null_tracking()
talloc.enable_null_tracking()
class join_ctx:
'''hold join context variables'''

View File

@ -17,15 +17,16 @@ sys.path.insert(0, "bin/python")
import samba
import samba.tests
from samba.dcerpc import drsuapi
import talloc
samba.talloc_enable_null_tracking()
talloc.enable_null_tracking()
class TallocTests(samba.tests.TestCase):
'''test talloc behaviour of pidl generated python code'''
def check_blocks(self, object, num_expected):
'''check that the number of allocated blocks is correct'''
nblocks = samba.talloc_total_blocks(object)
nblocks = talloc.total_blocks(object)
if object is None:
nblocks -= self.initial_blocks
self.assertEquals(nblocks, num_expected)
@ -61,7 +62,7 @@ class TallocTests(samba.tests.TestCase):
self.check_blocks(None, 6)
def test_run(self):
self.initial_blocks = samba.talloc_total_blocks(None)
self.initial_blocks = talloc.total_blocks(None)
self.check_blocks(None, 0)
self.pas_test()
self.check_blocks(None, 0)

View File

@ -10,15 +10,16 @@ sys.path.insert(0, "bin/python")
import samba
import samba.tests
from samba.dcerpc import drsuapi
import talloc
samba.talloc_enable_null_tracking()
talloc.enable_null_tracking()
class RpcTests(object):
'''test type behaviour of pidl generated python RPC code'''
def check_blocks(self, object, num_expected):
'''check that the number of allocated blocks is correct'''
nblocks = samba.talloc_total_blocks(object)
nblocks = talloc.total_blocks(object)
if object is None:
nblocks -= self.initial_blocks
leaked_blocks = (nblocks - num_expected)
@ -89,7 +90,7 @@ class RpcTests(object):
pass
elif isinstance(value, type):
try:
initial_blocks = samba.talloc_total_blocks(None)
initial_blocks = talloc.total_blocks(None)
self.check_type(interface, n, value)
self.check_blocks(None, initial_blocks)
except Exception, e:
@ -110,12 +111,12 @@ class RpcTests(object):
continue
print "Checking interface %s" % iname
iface = getattr(samba.dcerpc, iname)
initial_blocks = samba.talloc_total_blocks(None)
initial_blocks = talloc.total_blocks(None)
self.check_interface(iface, iname)
self.check_blocks(None, initial_blocks)
def run(self):
self.initial_blocks = samba.talloc_total_blocks(None)
self.initial_blocks = talloc.total_blocks(None)
self.errcount = 0
self.check_all_interfaces()
return self.errcount