LCOV - code coverage report
Current view: top level - lib/talloc - test_pytalloc.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 71 80 88.8 %
Date: 2021-09-23 10:06:22 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /*
       2             :    Samba Unix SMB/CIFS implementation.
       3             : 
       4             :    C utilities for the pytalloc test suite.
       5             :    Provides the "_test_pytalloc" Python module.
       6             : 
       7             :    NOTE: Please read talloc_guide.txt for full documentation
       8             : 
       9             :    Copyright (C) Petr Viktorin 2015
      10             : 
      11             :      ** NOTE! The following LGPL license applies to the talloc
      12             :      ** library. This does NOT imply that all of Samba is released
      13             :      ** under the LGPL
      14             : 
      15             :    This library is free software; you can redistribute it and/or
      16             :    modify it under the terms of the GNU Lesser General Public
      17             :    License as published by the Free Software Foundation; either
      18             :    version 3 of the License, or (at your option) any later version.
      19             : 
      20             :    This library is distributed in the hope that it will be useful,
      21             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      22             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23             :    Lesser General Public License for more details.
      24             : 
      25             :    You should have received a copy of the GNU Lesser General Public
      26             :    License along with this library; if not, see <http://www.gnu.org/licenses/>.
      27             : */
      28             : 
      29             : #include <Python.h>
      30             : #include <talloc.h>
      31             : #include <pytalloc.h>
      32             : 
      33           6 : static PyObject *testpytalloc_new(PyTypeObject *mod,
      34             :                 PyObject *Py_UNUSED(ignored))
      35             : {
      36           6 :         char *obj = talloc_strdup(NULL, "This is a test string");;
      37           6 :         return pytalloc_steal(pytalloc_GetObjectType(), obj);
      38             : }
      39             : 
      40           1 : static PyObject *testpytalloc_get_object_type(PyObject *mod,
      41             :                 PyObject *Py_UNUSED(ignored))
      42             : {
      43           1 :         PyObject *type = (PyObject *)pytalloc_GetObjectType();
      44           1 :         Py_INCREF(type);
      45           1 :         return type;
      46             : }
      47             : 
      48           4 : static PyObject *testpytalloc_base_new(PyTypeObject *mod,
      49             :                 PyObject *Py_UNUSED(ignored))
      50             : {
      51           4 :         char *obj = talloc_strdup(NULL, "This is a test string for a BaseObject");;
      52           4 :         return pytalloc_steal(pytalloc_GetBaseObjectType(), obj);
      53             : }
      54             : 
      55           1 : static PyObject *testpytalloc_base_get_object_type(PyObject *mod,
      56             :                 PyObject *Py_UNUSED(ignored))
      57             : {
      58           1 :         PyObject *type = (PyObject *)pytalloc_GetBaseObjectType();
      59           1 :         Py_INCREF(type);
      60           1 :         return type;
      61             : }
      62             : 
      63           1 : static PyObject *testpytalloc_reference(PyObject *mod, PyObject *args) {
      64           1 :         PyObject *source = NULL;
      65             :         void *ptr;
      66             : 
      67           1 :         if (!PyArg_ParseTuple(args, "O!", pytalloc_GetObjectType(), &source))
      68           0 :                 return NULL;
      69             : 
      70           1 :         ptr = pytalloc_get_ptr(source);
      71           1 :         return pytalloc_reference_ex(pytalloc_GetObjectType(), ptr, ptr);
      72             : }
      73             : 
      74           1 : static PyObject *testpytalloc_base_reference(PyObject *mod, PyObject *args) {
      75           1 :         PyObject *source = NULL;
      76             :         void *mem_ctx;
      77             : 
      78           1 :         if (!PyArg_ParseTuple(args, "O!", pytalloc_GetBaseObjectType(), &source)) {
      79           0 :                 return NULL;
      80             :         }
      81           1 :         mem_ctx = pytalloc_get_mem_ctx(source);
      82           1 :         return pytalloc_reference_ex(pytalloc_GetBaseObjectType(), mem_ctx, mem_ctx);
      83             : }
      84             : 
      85             : static PyMethodDef test_talloc_methods[] = {
      86             :         { "new", (PyCFunction)testpytalloc_new, METH_NOARGS,
      87             :                 "create a talloc Object with a testing string"},
      88             :         { "get_object_type", (PyCFunction)testpytalloc_get_object_type, METH_NOARGS,
      89             :                 "call pytalloc_GetObjectType"},
      90             :         { "base_new", (PyCFunction)testpytalloc_base_new, METH_NOARGS,
      91             :                 "create a talloc BaseObject with a testing string"},
      92             :         { "base_get_object_type", (PyCFunction)testpytalloc_base_get_object_type, METH_NOARGS,
      93             :                 "call pytalloc_GetBaseObjectType"},
      94             :         { "reference", (PyCFunction)testpytalloc_reference, METH_VARARGS,
      95             :                 "call pytalloc_reference_ex"},
      96             :         { "base_reference", (PyCFunction)testpytalloc_base_reference, METH_VARARGS,
      97             :                 "call pytalloc_reference_ex"},
      98             :         {0}
      99             : };
     100             : 
     101             : static PyTypeObject DObject_Type;
     102             : 
     103           2 : static int dobject_destructor(void *ptr)
     104             : {
     105           2 :         PyObject *destructor_func = *talloc_get_type(ptr, PyObject*);
     106             :         PyObject *ret;
     107           2 :         ret = PyObject_CallObject(destructor_func, NULL);
     108           2 :         Py_DECREF(destructor_func);
     109           2 :         if (ret == NULL) {
     110           0 :                 PyErr_Print();
     111             :         } else {
     112           2 :                 Py_DECREF(ret);
     113             :         }
     114           2 :         return 0;
     115             : }
     116             : 
     117           2 : static PyObject *dobject_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     118             : {
     119           2 :         PyObject *destructor_func = NULL;
     120             :         PyObject **obj;
     121             : 
     122           2 :         if (!PyArg_ParseTuple(args, "O", &destructor_func))
     123           0 :                 return NULL;
     124           2 :         Py_INCREF(destructor_func);
     125             : 
     126           2 :         obj = talloc(NULL, PyObject*);
     127           2 :         *obj = destructor_func;
     128             : 
     129           2 :         talloc_set_destructor((void*)obj, dobject_destructor);
     130           2 :         return pytalloc_steal(&DObject_Type, obj);
     131             : }
     132             : 
     133             : static PyTypeObject DObject_Type = {
     134             :         .tp_name = "_test_pytalloc.DObject",
     135             :         .tp_basicsize = sizeof(pytalloc_Object),
     136             :         .tp_methods = NULL,
     137             :         .tp_new = dobject_new,
     138             :         .tp_flags = Py_TPFLAGS_DEFAULT,
     139             :         .tp_doc = "test talloc object that calls a function when underlying data is freed\n",
     140             : };
     141             : 
     142             : static PyTypeObject DBaseObject_Type;
     143             : 
     144           2 : static int d_base_object_destructor(void *ptr)
     145             : {
     146           2 :         PyObject *destructor_func = *talloc_get_type(ptr, PyObject*);
     147             :         PyObject *ret;
     148           2 :         ret = PyObject_CallObject(destructor_func, NULL);
     149           2 :         Py_DECREF(destructor_func);
     150           2 :         if (ret == NULL) {
     151           0 :                 PyErr_Print();
     152             :         } else {
     153           2 :                 Py_DECREF(ret);
     154             :         }
     155           2 :         return 0;
     156             : }
     157             : 
     158           2 : static PyObject *d_base_object_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     159             : {
     160           2 :         PyObject *destructor_func = NULL;
     161             :         PyObject **obj;
     162             : 
     163           2 :         if (!PyArg_ParseTuple(args, "O", &destructor_func))
     164           0 :                 return NULL;
     165           2 :         Py_INCREF(destructor_func);
     166             : 
     167           2 :         obj = talloc(NULL, PyObject*);
     168           2 :         *obj = destructor_func;
     169             : 
     170           2 :         talloc_set_destructor((void*)obj, d_base_object_destructor);
     171           2 :         return pytalloc_steal(&DBaseObject_Type, obj);
     172             : }
     173             : 
     174             : static PyTypeObject DBaseObject_Type = {
     175             :         .tp_name = "_test_pytalloc.DBaseObject",
     176             :         .tp_methods = NULL,
     177             :         .tp_new = d_base_object_new,
     178             :         .tp_flags = Py_TPFLAGS_DEFAULT,
     179             :         .tp_doc = "test talloc object that calls a function when underlying data is freed\n",
     180             : };
     181             : 
     182             : #define MODULE_DOC PyDoc_STR("Test utility module for pytalloc")
     183             : 
     184             : #if PY_MAJOR_VERSION >= 3
     185             : static struct PyModuleDef moduledef = {
     186             :     PyModuleDef_HEAD_INIT,
     187             :     .m_name = "_test_pytalloc",
     188             :     .m_doc = PyDoc_STR("Test utility module for pytalloc"),
     189             :     .m_size = -1,
     190             :     .m_methods = test_talloc_methods,
     191             : };
     192             : #endif
     193             : 
     194             : static PyObject *module_init(void);
     195           2 : static PyObject *module_init(void)
     196             : {
     197             :         PyObject *m;
     198             : 
     199           2 :         DObject_Type.tp_base = pytalloc_GetObjectType();
     200           2 :         if (PyType_Ready(&DObject_Type) < 0) {
     201           0 :                 return NULL;
     202             :         }
     203             : 
     204           2 :         DBaseObject_Type.tp_basicsize = pytalloc_BaseObject_size();
     205           2 :         DBaseObject_Type.tp_base = pytalloc_GetBaseObjectType();
     206           2 :         if (PyType_Ready(&DBaseObject_Type) < 0) {
     207           0 :                 return NULL;
     208             :         }
     209             : 
     210             : #if PY_MAJOR_VERSION >= 3
     211           2 :         m = PyModule_Create(&moduledef);
     212             : #else
     213             :         m = Py_InitModule3("_test_pytalloc", test_talloc_methods, MODULE_DOC);
     214             : #endif
     215             : 
     216           2 :         if (m == NULL) {
     217           0 :                 return NULL;
     218             :         }
     219             : 
     220           2 :         Py_INCREF(&DObject_Type);
     221           2 :         Py_INCREF(DObject_Type.tp_base);
     222           2 :         PyModule_AddObject(m, "DObject", (PyObject *)&DObject_Type);
     223             : 
     224           2 :         Py_INCREF(&DBaseObject_Type);
     225           2 :         Py_INCREF(DBaseObject_Type.tp_base);
     226           2 :         PyModule_AddObject(m, "DBaseObject", (PyObject *)&DBaseObject_Type);
     227             : 
     228           2 :         return m;
     229             : }
     230             : 
     231             : 
     232             : #if PY_MAJOR_VERSION >= 3
     233             : PyMODINIT_FUNC PyInit__test_pytalloc(void);
     234           2 : PyMODINIT_FUNC PyInit__test_pytalloc(void)
     235             : {
     236           2 :         return module_init();
     237             : }
     238             : #else
     239             : void init_test_pytalloc(void);
     240             : void init_test_pytalloc(void)
     241             : {
     242             :         module_init();
     243             : }
     244             : #endif

Generated by: LCOV version 1.13