LCOV - code coverage report
Current view: top level - python - modules.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 37 60 61.7 %
Date: 2024-04-21 15:09:00 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    Samba utility functions
       4             :    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
       5             :    
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             :    
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             :    
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "lib/replace/system/python.h"
      21             : #include "py3compat.h"
      22             : #include "includes.h"
      23             : #include "python/modules.h"
      24             : #include "dynconfig/dynconfig.h"
      25             : 
      26          93 : static bool PySys_PathPrepend(PyObject *list, const char *path)
      27             : {
      28           2 :         bool ok;
      29          93 :         PyObject *py_path = PyUnicode_FromString(path);
      30          93 :         if (py_path == NULL) {
      31           0 :                 return false;
      32             :         }
      33          93 :         ok = PyList_Insert(list, 0, py_path) == 0;
      34          93 :         Py_XDECREF(py_path);
      35          91 :         return ok;
      36             : }
      37             : 
      38          93 : bool py_update_path(void)
      39             : {
      40          93 :         PyObject *mod_sys = NULL;
      41          93 :         PyObject *py_path = NULL;
      42             : 
      43          93 :         mod_sys = PyImport_ImportModule("sys");
      44          93 :         if (mod_sys == NULL) {
      45           0 :                 return false;
      46             :         }
      47             : 
      48          93 :         py_path = PyObject_GetAttrString(mod_sys, "path");
      49          93 :         if (py_path == NULL) {
      50           0 :                 goto error;
      51             :         }
      52             : 
      53          93 :         if (!PyList_Check(py_path)) {
      54           0 :                 goto error;
      55             :         }
      56             : 
      57          93 :         if (!PySys_PathPrepend(py_path, dyn_PYTHONDIR)) {
      58           0 :                 goto error;
      59             :         }
      60             : 
      61          93 :         if (strcmp(dyn_PYTHONARCHDIR, dyn_PYTHONDIR) != 0) {
      62           0 :                 if (!PySys_PathPrepend(py_path, dyn_PYTHONARCHDIR)) {
      63           0 :                         goto error;
      64             :                 }
      65             :         }
      66          93 :         Py_XDECREF(py_path);
      67          93 :         Py_XDECREF(mod_sys);
      68          91 :         return true;
      69           0 : error:
      70           0 :         Py_XDECREF(py_path);
      71           0 :         Py_XDECREF(mod_sys);
      72           0 :         return false;
      73             : }
      74             : 
      75       18763 : char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
      76             :                            const char *paramname)
      77             : {
      78         171 :         char **ret;
      79         171 :         Py_ssize_t i;
      80       18763 :         if (!PyList_Check(list)) {
      81           0 :                 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
      82           0 :                 return NULL;
      83             :         }
      84       18763 :         ret = talloc_array(NULL, char *, PyList_Size(list)+1);
      85       18763 :         if (ret == NULL) {
      86           0 :                 PyErr_NoMemory();
      87           0 :                 return NULL;
      88             :         }
      89             : 
      90       89339 :         for (i = 0; i < PyList_Size(list); i++) {
      91         940 :                 const char *value;
      92         940 :                 Py_ssize_t size;
      93       70576 :                 PyObject *item = PyList_GetItem(list, i);
      94       70576 :                 if (!PyUnicode_Check(item)) {
      95           0 :                         PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
      96           0 :                         talloc_free(ret);
      97           0 :                         return NULL;
      98             :                 }
      99       70576 :                 value = PyUnicode_AsUTF8AndSize(item, &size);
     100       70576 :                 if (value == NULL) {
     101           0 :                         talloc_free(ret);
     102           0 :                         return NULL;
     103             :                 }
     104       70576 :                 ret[i] = talloc_strndup(ret, value, size);
     105       70576 :                 if (ret[i] == NULL) {
     106           0 :                         PyErr_NoMemory();
     107           0 :                         talloc_free(ret);
     108           0 :                         return NULL;
     109             :                 }
     110             :         }
     111       18763 :         ret[i] = NULL;
     112       18763 :         return ret;
     113             : }
     114             : 

Generated by: LCOV version 1.14