LCOV - code coverage report
Current view: top level - source4/librpc/ndr - py_misc.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 67 79 84.8 %
Date: 2021-09-23 10:06:22 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    Samba utility functions
       4             : 
       5             :    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
       6             :    
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             :    
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             :    
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : #include <Python.h>
      21             : #include "python/py3compat.h"
      22             : #include "librpc/gen_ndr/misc.h"
      23             : 
      24             : #if PY_MAJOR_VERSION >= 3
      25    11376829 : static PyObject *py_GUID_richcmp(PyObject *py_self, PyObject *py_other, int op)
      26             : {
      27             :         int ret;
      28    11376829 :         struct GUID *self = pytalloc_get_ptr(py_self), *other;
      29    11376829 :         other = pytalloc_get_ptr(py_other);
      30    11376829 :         if (other == NULL) {
      31           0 :                 Py_INCREF(Py_NotImplemented);
      32           0 :                 return Py_NotImplemented;
      33             :         }
      34             : 
      35    11376829 :         ret = GUID_compare(self, other);
      36             : 
      37    11376829 :         switch (op) {
      38        3616 :                 case Py_EQ: if (ret == 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      39    11373208 :                 case Py_NE: if (ret != 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      40           2 :                 case Py_LT: if (ret <  0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      41           3 :                 case Py_GT: if (ret >  0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      42           0 :                 case Py_LE: if (ret <= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      43           0 :                 case Py_GE: if (ret >= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      44             :         }
      45           0 :         Py_INCREF(Py_NotImplemented);
      46           0 :         return Py_NotImplemented;
      47             : }
      48             : #else
      49             : static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
      50             : {
      51             :         int ret;
      52             :         struct GUID *self = pytalloc_get_ptr(py_self), *other;
      53             :         other = pytalloc_get_ptr(py_other);
      54             :         if (other == NULL)
      55             :                 return -1;
      56             : 
      57             :         ret = GUID_compare(self, other);
      58             :         if (ret < 0) {
      59             :                 return -1;
      60             :         } else if (ret > 0) {
      61             :                 return 1;
      62             :         } else {
      63             :                 return 0;
      64             :         }
      65             : }
      66             : #endif
      67             : 
      68      766022 : static PyObject *py_GUID_str(PyObject *py_self)
      69             : {
      70      766022 :         struct GUID *self = pytalloc_get_ptr(py_self);
      71             :         struct GUID_txt_buf buf;
      72      766022 :         PyObject *ret = PyUnicode_FromString(GUID_buf_string(self, &buf));
      73      766022 :         return ret;
      74             : }
      75             : 
      76        4667 : static PyObject *py_GUID_repr(PyObject *py_self)
      77             : {
      78        4667 :         struct GUID *self = pytalloc_get_ptr(py_self);
      79             :         struct GUID_txt_buf buf;
      80        4667 :         PyObject *ret = PyUnicode_FromFormat(
      81             :                 "GUID('%s')", GUID_buf_string(self, &buf));
      82        4667 :         return ret;
      83             : }
      84             : 
      85    11869306 : static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
      86             : {
      87    11869306 :         PyObject *str = NULL;
      88             :         NTSTATUS status;
      89    11869306 :         struct GUID *guid = pytalloc_get_ptr(self);
      90    11869306 :         const char *kwnames[] = { "str", NULL };
      91             : 
      92    11869306 :         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &str))
      93           0 :                 return -1;
      94             : 
      95    11869306 :         if (str != NULL) {
      96             :                 DATA_BLOB guid_val;
      97             :                 Py_ssize_t _size;
      98             : 
      99    11863927 :                 if (PyUnicode_Check(str)) {
     100    11379801 :                         guid_val.data =
     101    11379801 :                                 discard_const_p(uint8_t,
     102             :                                                 PyUnicode_AsUTF8AndSize(str, &_size));
     103      484126 :                 } else if (PyBytes_Check(str)) {
     104      484126 :                         guid_val.data =
     105      484126 :                                 discard_const_p(uint8_t,
     106             :                                                 PyBytes_AsString(str));
     107      484126 :                         _size = PyBytes_Size(str);
     108             :                 } else {
     109           0 :                         PyErr_SetString(PyExc_TypeError,
     110             :                                         "Expected a string or bytes argument to GUID()");
     111           0 :                         return -1;
     112             :                 }
     113    11863927 :                 guid_val.length = _size;
     114    11863927 :                 status = GUID_from_data_blob(&guid_val, guid);
     115    11863927 :                 if (!NT_STATUS_IS_OK(status)) {
     116           5 :                         PyErr_SetNTSTATUS(status);
     117           5 :                         return -1;
     118             :                 }
     119             :         }
     120             : 
     121    10826512 :         return 0;
     122             : }
     123             : 
     124        4843 : static void py_GUID_patch(PyTypeObject *type)
     125             : {
     126        5015 :         type->tp_init = py_GUID_init;
     127        5015 :         type->tp_str = py_GUID_str;
     128        5015 :         type->tp_repr = py_GUID_repr;
     129             : #if PY_MAJOR_VERSION >= 3
     130        5015 :         type->tp_richcompare = py_GUID_richcmp;
     131             : #else
     132             :         type->tp_compare = py_GUID_cmp;
     133             : #endif
     134        4843 : }
     135             : 
     136             : #define PY_GUID_PATCH py_GUID_patch
     137             : 
     138          53 : static int py_policy_handle_init(PyObject *self, PyObject *args, PyObject *kwargs)
     139             : {
     140          53 :         char *str = NULL;
     141             :         NTSTATUS status;
     142          53 :         struct policy_handle *handle = pytalloc_get_ptr(self);
     143          53 :         const char *kwnames[] = { "uuid", "type", NULL };
     144             : 
     145          53 :         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|sI", discard_const_p(char *, kwnames), &str, &handle->handle_type))
     146           0 :                 return -1;
     147             : 
     148          53 :         if (str != NULL) {
     149           3 :                 status = GUID_from_string(str, &handle->uuid);
     150           3 :                 if (!NT_STATUS_IS_OK(status)) {
     151           0 :                         PyErr_SetNTSTATUS(status);
     152           0 :                         return -1;
     153             :                 }
     154             :         }
     155             : 
     156          50 :         return 0;
     157             : }
     158             : 
     159           1 : static PyObject *py_policy_handle_repr(PyObject *py_self)
     160             : {
     161           1 :         struct policy_handle *self = pytalloc_get_ptr(py_self);
     162             :         struct GUID_txt_buf buf;
     163           1 :         PyObject *ret = PyUnicode_FromFormat(
     164             :                 "policy_handle(%d, '%s')",
     165             :                 self->handle_type,
     166           1 :                 GUID_buf_string(&self->uuid, &buf));
     167           1 :         return ret;
     168             : }
     169             : 
     170           1 : static PyObject *py_policy_handle_str(PyObject *py_self)
     171             : {
     172           1 :         struct policy_handle *self = pytalloc_get_ptr(py_self);
     173             :         struct GUID_txt_buf buf;
     174           1 :         PyObject *ret = PyUnicode_FromFormat(
     175             :                 "%d, %s",
     176             :                 self->handle_type,
     177           1 :                 GUID_buf_string(&self->uuid, &buf));
     178           1 :         return ret;
     179             : }
     180             : 
     181        4843 : static void py_policy_handle_patch(PyTypeObject *type)
     182             : {
     183        5015 :         type->tp_init = py_policy_handle_init;
     184        5015 :         type->tp_repr = py_policy_handle_repr;
     185        5015 :         type->tp_str = py_policy_handle_str;
     186        4843 : }
     187             : 
     188             : #define PY_POLICY_HANDLE_PATCH py_policy_handle_patch
     189             : 

Generated by: LCOV version 1.13