LCOV - code coverage report
Current view: top level - Python - modsupport.c (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 190 345 55.1 %
Date: 2023-03-20 08:15:36 Functions: 18 21 85.7 %
Branches: 84 177 47.5 %

           Branch data     Line data    Source code
       1                 :            : 
       2                 :            : /* Module support implementation */
       3                 :            : 
       4                 :            : #include "Python.h"
       5                 :            : #include "pycore_abstract.h"   // _PyIndex_Check()
       6                 :            : 
       7                 :            : #define FLAG_SIZE_T 1
       8                 :            : typedef double va_double;
       9                 :            : 
      10                 :            : static PyObject *va_build_value(const char *, va_list, int);
      11                 :            : static PyObject **va_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len, const char *, va_list, int, Py_ssize_t*);
      12                 :            : 
      13                 :            : 
      14                 :            : int
      15                 :         22 : _Py_convert_optional_to_ssize_t(PyObject *obj, void *result)
      16                 :            : {
      17                 :            :     Py_ssize_t limit;
      18         [ -  + ]:         22 :     if (obj == Py_None) {
      19                 :          0 :         return 1;
      20                 :            :     }
      21         [ +  - ]:         22 :     else if (_PyIndex_Check(obj)) {
      22                 :         22 :         limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
      23   [ -  +  -  - ]:         22 :         if (limit == -1 && PyErr_Occurred()) {
      24                 :          0 :             return 0;
      25                 :            :         }
      26                 :            :     }
      27                 :            :     else {
      28                 :          0 :         PyErr_Format(PyExc_TypeError,
      29                 :            :                      "argument should be integer or None, not '%.200s'",
      30                 :          0 :                      Py_TYPE(obj)->tp_name);
      31                 :          0 :         return 0;
      32                 :            :     }
      33                 :         22 :     *((Py_ssize_t *)result) = limit;
      34                 :         22 :     return 1;
      35                 :            : }
      36                 :            : 
      37                 :            : 
      38                 :            : /* Helper for mkvalue() to scan the length of a format */
      39                 :            : 
      40                 :            : static Py_ssize_t
      41                 :    1070476 : countformat(const char *format, char endchar)
      42                 :            : {
      43                 :    1070476 :     Py_ssize_t count = 0;
      44                 :    1070476 :     int level = 0;
      45   [ +  +  +  + ]:    4352146 :     while (level > 0 || *format != endchar) {
      46   [ -  +  +  +  :    3281670 :         switch (*format) {
                      + ]
      47                 :          0 :         case '\0':
      48                 :            :             /* Premature end */
      49                 :          0 :             PyErr_SetString(PyExc_SystemError,
      50                 :            :                             "unmatched paren in format");
      51                 :          0 :             return -1;
      52                 :     529814 :         case '(':
      53                 :            :         case '[':
      54                 :            :         case '{':
      55         [ +  - ]:     529814 :             if (level == 0) {
      56                 :     529814 :                 count++;
      57                 :            :             }
      58                 :     529814 :             level++;
      59                 :     529814 :             break;
      60                 :     529814 :         case ')':
      61                 :            :         case ']':
      62                 :            :         case '}':
      63                 :     529814 :             level--;
      64                 :     529814 :             break;
      65                 :      23036 :         case '#':
      66                 :            :         case '&':
      67                 :            :         case ',':
      68                 :            :         case ':':
      69                 :            :         case ' ':
      70                 :            :         case '\t':
      71                 :      23036 :             break;
      72                 :    2199006 :         default:
      73         [ +  + ]:    2199006 :             if (level == 0) {
      74                 :    1118605 :                 count++;
      75                 :            :             }
      76                 :            :         }
      77                 :    3281670 :         format++;
      78                 :            :     }
      79                 :    1070476 :     return count;
      80                 :            : }
      81                 :            : 
      82                 :            : 
      83                 :            : /* Generic function to create a value -- the inverse of getargs() */
      84                 :            : /* After an original idea and first implementation by Steven Miale */
      85                 :            : 
      86                 :            : static PyObject *do_mktuple(const char**, va_list *, char, Py_ssize_t, int);
      87                 :            : static int do_mkstack(PyObject **, const char**, va_list *, char, Py_ssize_t, int);
      88                 :            : static PyObject *do_mklist(const char**, va_list *, char, Py_ssize_t, int);
      89                 :            : static PyObject *do_mkdict(const char**, va_list *, char, Py_ssize_t, int);
      90                 :            : static PyObject *do_mkvalue(const char**, va_list *, int);
      91                 :            : 
      92                 :            : 
      93                 :            : static void
      94                 :          0 : do_ignore(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
      95                 :            : {
      96                 :            :     assert(PyErr_Occurred());
      97                 :          0 :     PyObject *v = PyTuple_New(n);
      98         [ #  # ]:          0 :     for (Py_ssize_t i = 0; i < n; i++) {
      99                 :          0 :         PyObject *exc = PyErr_GetRaisedException();
     100                 :          0 :         PyObject *w = do_mkvalue(p_format, p_va, flags);
     101                 :          0 :         PyErr_SetRaisedException(exc);
     102         [ #  # ]:          0 :         if (w != NULL) {
     103         [ #  # ]:          0 :             if (v != NULL) {
     104                 :          0 :                 PyTuple_SET_ITEM(v, i, w);
     105                 :            :             }
     106                 :            :             else {
     107                 :          0 :                 Py_DECREF(w);
     108                 :            :             }
     109                 :            :         }
     110                 :            :     }
     111                 :          0 :     Py_XDECREF(v);
     112         [ #  # ]:          0 :     if (**p_format != endchar) {
     113                 :          0 :         PyErr_SetString(PyExc_SystemError,
     114                 :            :                         "Unmatched paren in format");
     115                 :          0 :         return;
     116                 :            :     }
     117         [ #  # ]:          0 :     if (endchar) {
     118                 :          0 :         ++*p_format;
     119                 :            :     }
     120                 :            : }
     121                 :            : 
     122                 :            : static PyObject *
     123                 :       3069 : do_mkdict(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
     124                 :            : {
     125                 :            :     PyObject *d;
     126                 :            :     Py_ssize_t i;
     127         [ -  + ]:       3069 :     if (n < 0)
     128                 :          0 :         return NULL;
     129         [ -  + ]:       3069 :     if (n % 2) {
     130                 :          0 :         PyErr_SetString(PyExc_SystemError,
     131                 :            :                         "Bad dict format");
     132                 :          0 :         do_ignore(p_format, p_va, endchar, n, flags);
     133                 :          0 :         return NULL;
     134                 :            :     }
     135                 :            :     /* Note that we can't bail immediately on error as this will leak
     136                 :            :        refcounts on any 'N' arguments. */
     137         [ -  + ]:       3069 :     if ((d = PyDict_New()) == NULL) {
     138                 :          0 :         do_ignore(p_format, p_va, endchar, n, flags);
     139                 :          0 :         return NULL;
     140                 :            :     }
     141         [ +  + ]:      14606 :     for (i = 0; i < n; i+= 2) {
     142                 :            :         PyObject *k, *v;
     143                 :            : 
     144                 :      11537 :         k = do_mkvalue(p_format, p_va, flags);
     145         [ -  + ]:      11537 :         if (k == NULL) {
     146                 :          0 :             do_ignore(p_format, p_va, endchar, n - i - 1, flags);
     147                 :          0 :             Py_DECREF(d);
     148                 :          0 :             return NULL;
     149                 :            :         }
     150                 :      11537 :         v = do_mkvalue(p_format, p_va, flags);
     151   [ +  -  -  + ]:      11537 :         if (v == NULL || PyDict_SetItem(d, k, v) < 0) {
     152                 :          0 :             do_ignore(p_format, p_va, endchar, n - i - 2, flags);
     153                 :          0 :             Py_DECREF(k);
     154                 :          0 :             Py_XDECREF(v);
     155                 :          0 :             Py_DECREF(d);
     156                 :          0 :             return NULL;
     157                 :            :         }
     158                 :      11537 :         Py_DECREF(k);
     159                 :      11537 :         Py_DECREF(v);
     160                 :            :     }
     161         [ -  + ]:       3069 :     if (**p_format != endchar) {
     162                 :          0 :         Py_DECREF(d);
     163                 :          0 :         PyErr_SetString(PyExc_SystemError,
     164                 :            :                         "Unmatched paren in format");
     165                 :          0 :         return NULL;
     166                 :            :     }
     167         [ +  - ]:       3069 :     if (endchar)
     168                 :       3069 :         ++*p_format;
     169                 :       3069 :     return d;
     170                 :            : }
     171                 :            : 
     172                 :            : static PyObject *
     173                 :          0 : do_mklist(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
     174                 :            : {
     175                 :            :     PyObject *v;
     176                 :            :     Py_ssize_t i;
     177         [ #  # ]:          0 :     if (n < 0)
     178                 :          0 :         return NULL;
     179                 :            :     /* Note that we can't bail immediately on error as this will leak
     180                 :            :        refcounts on any 'N' arguments. */
     181                 :          0 :     v = PyList_New(n);
     182         [ #  # ]:          0 :     if (v == NULL) {
     183                 :          0 :         do_ignore(p_format, p_va, endchar, n, flags);
     184                 :          0 :         return NULL;
     185                 :            :     }
     186         [ #  # ]:          0 :     for (i = 0; i < n; i++) {
     187                 :          0 :         PyObject *w = do_mkvalue(p_format, p_va, flags);
     188         [ #  # ]:          0 :         if (w == NULL) {
     189                 :          0 :             do_ignore(p_format, p_va, endchar, n - i - 1, flags);
     190                 :          0 :             Py_DECREF(v);
     191                 :          0 :             return NULL;
     192                 :            :         }
     193                 :          0 :         PyList_SET_ITEM(v, i, w);
     194                 :            :     }
     195         [ #  # ]:          0 :     if (**p_format != endchar) {
     196                 :          0 :         Py_DECREF(v);
     197                 :          0 :         PyErr_SetString(PyExc_SystemError,
     198                 :            :                         "Unmatched paren in format");
     199                 :          0 :         return NULL;
     200                 :            :     }
     201         [ #  # ]:          0 :     if (endchar)
     202                 :          0 :         ++*p_format;
     203                 :          0 :     return v;
     204                 :            : }
     205                 :            : 
     206                 :            : static int
     207                 :       9287 : do_mkstack(PyObject **stack, const char **p_format, va_list *p_va,
     208                 :            :            char endchar, Py_ssize_t n, int flags)
     209                 :            : {
     210                 :            :     Py_ssize_t i;
     211                 :            : 
     212         [ -  + ]:       9287 :     if (n < 0) {
     213                 :          0 :         return -1;
     214                 :            :     }
     215                 :            :     /* Note that we can't bail immediately on error as this will leak
     216                 :            :        refcounts on any 'N' arguments. */
     217         [ +  + ]:      38607 :     for (i = 0; i < n; i++) {
     218                 :      29320 :         PyObject *w = do_mkvalue(p_format, p_va, flags);
     219         [ -  + ]:      29320 :         if (w == NULL) {
     220                 :          0 :             do_ignore(p_format, p_va, endchar, n - i - 1, flags);
     221                 :          0 :             goto error;
     222                 :            :         }
     223                 :      29320 :         stack[i] = w;
     224                 :            :     }
     225         [ -  + ]:       9287 :     if (**p_format != endchar) {
     226                 :          0 :         PyErr_SetString(PyExc_SystemError,
     227                 :            :                         "Unmatched paren in format");
     228                 :          0 :         goto error;
     229                 :            :     }
     230         [ -  + ]:       9287 :     if (endchar) {
     231                 :          0 :         ++*p_format;
     232                 :            :     }
     233                 :       9287 :     return 0;
     234                 :            : 
     235                 :          0 : error:
     236                 :          0 :     n = i;
     237         [ #  # ]:          0 :     for (i=0; i < n; i++) {
     238                 :          0 :         Py_DECREF(stack[i]);
     239                 :            :     }
     240                 :          0 :     return -1;
     241                 :            : }
     242                 :            : 
     243                 :            : static PyObject *
     244                 :     533912 : do_mktuple(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
     245                 :            : {
     246                 :            :     PyObject *v;
     247                 :            :     Py_ssize_t i;
     248         [ -  + ]:     533912 :     if (n < 0)
     249                 :          0 :         return NULL;
     250                 :            :     /* Note that we can't bail immediately on error as this will leak
     251                 :            :        refcounts on any 'N' arguments. */
     252         [ -  + ]:     533912 :     if ((v = PyTuple_New(n)) == NULL) {
     253                 :          0 :         do_ignore(p_format, p_va, endchar, n, flags);
     254                 :          0 :         return NULL;
     255                 :            :     }
     256         [ +  + ]:    1605729 :     for (i = 0; i < n; i++) {
     257                 :    1071817 :         PyObject *w = do_mkvalue(p_format, p_va, flags);
     258         [ -  + ]:    1071817 :         if (w == NULL) {
     259                 :          0 :             do_ignore(p_format, p_va, endchar, n - i - 1, flags);
     260                 :          0 :             Py_DECREF(v);
     261                 :          0 :             return NULL;
     262                 :            :         }
     263                 :    1071817 :         PyTuple_SET_ITEM(v, i, w);
     264                 :            :     }
     265         [ -  + ]:     533912 :     if (**p_format != endchar) {
     266                 :          0 :         Py_DECREF(v);
     267                 :          0 :         PyErr_SetString(PyExc_SystemError,
     268                 :            :                         "Unmatched paren in format");
     269                 :          0 :         return NULL;
     270                 :            :     }
     271         [ +  + ]:     533912 :     if (endchar)
     272                 :     526745 :         ++*p_format;
     273                 :     533912 :     return v;
     274                 :            : }
     275                 :            : 
     276                 :            : static PyObject *
     277                 :    1648419 : do_mkvalue(const char **p_format, va_list *p_va, int flags)
     278                 :            : {
     279                 :            : #define ERROR_NEED_PY_SSIZE_T_CLEAN \
     280                 :            :     { \
     281                 :            :         PyErr_SetString(PyExc_SystemError, \
     282                 :            :                         "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"); \
     283                 :            :         return NULL; \
     284                 :            :     }
     285                 :            : 
     286                 :            :     for (;;) {
     287   [ +  -  +  +  :    1648419 :         switch (*(*p_format)++) {
          -  +  +  -  -  
          +  -  +  -  +  
          -  +  +  +  -  
                      - ]
     288                 :     526745 :         case '(':
     289                 :     526745 :             return do_mktuple(p_format, p_va, ')',
     290                 :            :                               countformat(*p_format, ')'), flags);
     291                 :            : 
     292                 :          0 :         case '[':
     293                 :          0 :             return do_mklist(p_format, p_va, ']',
     294                 :            :                              countformat(*p_format, ']'), flags);
     295                 :            : 
     296                 :       3069 :         case '{':
     297                 :       3069 :             return do_mkdict(p_format, p_va, '}',
     298                 :            :                              countformat(*p_format, '}'), flags);
     299                 :            : 
     300                 :     528882 :         case 'b':
     301                 :            :         case 'B':
     302                 :            :         case 'h':
     303                 :            :         case 'i':
     304                 :     528882 :             return PyLong_FromLong((long)va_arg(*p_va, int));
     305                 :            : 
     306                 :          0 :         case 'H':
     307                 :          0 :             return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
     308                 :            : 
     309                 :         20 :         case 'I':
     310                 :            :         {
     311                 :            :             unsigned int n;
     312                 :         20 :             n = va_arg(*p_va, unsigned int);
     313                 :         20 :             return PyLong_FromUnsignedLong(n);
     314                 :            :         }
     315                 :            : 
     316                 :       4100 :         case 'n':
     317                 :            : #if SIZEOF_SIZE_T!=SIZEOF_LONG
     318                 :            :             return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
     319                 :            : #endif
     320                 :            :             /* Fall through from 'n' to 'l' if Py_ssize_t is long */
     321                 :            :         case 'l':
     322                 :       4100 :             return PyLong_FromLong(va_arg(*p_va, long));
     323                 :            : 
     324                 :          0 :         case 'k':
     325                 :            :         {
     326                 :            :             unsigned long n;
     327                 :          0 :             n = va_arg(*p_va, unsigned long);
     328                 :          0 :             return PyLong_FromUnsignedLong(n);
     329                 :            :         }
     330                 :            : 
     331                 :          0 :         case 'L':
     332                 :          0 :             return PyLong_FromLongLong((long long)va_arg(*p_va, long long));
     333                 :            : 
     334                 :          8 :         case 'K':
     335                 :          8 :             return PyLong_FromUnsignedLongLong((long long)va_arg(*p_va, unsigned long long));
     336                 :            : 
     337                 :          0 :         case 'u':
     338                 :            :         {
     339                 :            :             PyObject *v;
     340                 :          0 :             Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
     341                 :            :             Py_ssize_t n;
     342         [ #  # ]:          0 :             if (**p_format == '#') {
     343                 :          0 :                 ++*p_format;
     344         [ #  # ]:          0 :                 if (flags & FLAG_SIZE_T) {
     345                 :          0 :                     n = va_arg(*p_va, Py_ssize_t);
     346                 :            :                 }
     347                 :            :                 else {
     348                 :          0 :                     n = va_arg(*p_va, int);
     349                 :          0 :                     ERROR_NEED_PY_SSIZE_T_CLEAN;
     350                 :            :                 }
     351                 :            :             }
     352                 :            :             else
     353                 :          0 :                 n = -1;
     354         [ #  # ]:          0 :             if (u == NULL) {
     355                 :          0 :                 v = Py_NewRef(Py_None);
     356                 :            :             }
     357                 :            :             else {
     358         [ #  # ]:          0 :                 if (n < 0)
     359                 :          0 :                     n = wcslen(u);
     360                 :          0 :                 v = PyUnicode_FromWideChar(u, n);
     361                 :            :             }
     362                 :          0 :             return v;
     363                 :            :         }
     364                 :     520023 :         case 'f':
     365                 :            :         case 'd':
     366                 :     520023 :             return PyFloat_FromDouble(
     367                 :     520023 :                 (double)va_arg(*p_va, va_double));
     368                 :            : 
     369                 :          0 :         case 'D':
     370                 :          0 :             return PyComplex_FromCComplex(
     371                 :          0 :                 *((Py_complex *)va_arg(*p_va, Py_complex *)));
     372                 :            : 
     373                 :        228 :         case 'c':
     374                 :            :         {
     375                 :            :             char p[1];
     376                 :        228 :             p[0] = (char)va_arg(*p_va, int);
     377                 :        228 :             return PyBytes_FromStringAndSize(p, 1);
     378                 :            :         }
     379                 :          0 :         case 'C':
     380                 :            :         {
     381                 :          0 :             int i = va_arg(*p_va, int);
     382                 :          0 :             return PyUnicode_FromOrdinal(i);
     383                 :            :         }
     384                 :            : 
     385                 :      11558 :         case 's':
     386                 :            :         case 'z':
     387                 :            :         case 'U':   /* XXX deprecated alias */
     388                 :            :         {
     389                 :            :             PyObject *v;
     390                 :      11558 :             const char *str = va_arg(*p_va, const char *);
     391                 :            :             Py_ssize_t n;
     392         [ -  + ]:      11558 :             if (**p_format == '#') {
     393                 :          0 :                 ++*p_format;
     394         [ #  # ]:          0 :                 if (flags & FLAG_SIZE_T) {
     395                 :          0 :                     n = va_arg(*p_va, Py_ssize_t);
     396                 :            :                 }
     397                 :            :                 else {
     398                 :          0 :                     n = va_arg(*p_va, int);
     399                 :          0 :                     ERROR_NEED_PY_SSIZE_T_CLEAN;
     400                 :            :                 }
     401                 :            :             }
     402                 :            :             else
     403                 :      11558 :                 n = -1;
     404         [ +  + ]:      11558 :             if (str == NULL) {
     405                 :       1371 :                 v = Py_NewRef(Py_None);
     406                 :            :             }
     407                 :            :             else {
     408         [ +  - ]:      10187 :                 if (n < 0) {
     409                 :      10187 :                     size_t m = strlen(str);
     410         [ -  + ]:      10187 :                     if (m > PY_SSIZE_T_MAX) {
     411                 :          0 :                         PyErr_SetString(PyExc_OverflowError,
     412                 :            :                             "string too long for Python string");
     413                 :          0 :                         return NULL;
     414                 :            :                     }
     415                 :      10187 :                     n = (Py_ssize_t)m;
     416                 :            :                 }
     417                 :      10187 :                 v = PyUnicode_FromStringAndSize(str, n);
     418                 :            :             }
     419                 :      11558 :             return v;
     420                 :            :         }
     421                 :            : 
     422                 :         82 :         case 'y':
     423                 :            :         {
     424                 :            :             PyObject *v;
     425                 :         82 :             const char *str = va_arg(*p_va, const char *);
     426                 :            :             Py_ssize_t n;
     427         [ +  + ]:         82 :             if (**p_format == '#') {
     428                 :          4 :                 ++*p_format;
     429         [ +  - ]:          4 :                 if (flags & FLAG_SIZE_T) {
     430                 :          4 :                     n = va_arg(*p_va, Py_ssize_t);
     431                 :            :                 }
     432                 :            :                 else {
     433                 :          0 :                     n = va_arg(*p_va, int);
     434                 :          0 :                     ERROR_NEED_PY_SSIZE_T_CLEAN;
     435                 :            :                 }
     436                 :            :             }
     437                 :            :             else
     438                 :         78 :                 n = -1;
     439         [ -  + ]:         82 :             if (str == NULL) {
     440                 :          0 :                 v = Py_NewRef(Py_None);
     441                 :            :             }
     442                 :            :             else {
     443         [ +  + ]:         82 :                 if (n < 0) {
     444                 :         78 :                     size_t m = strlen(str);
     445         [ -  + ]:         78 :                     if (m > PY_SSIZE_T_MAX) {
     446                 :          0 :                         PyErr_SetString(PyExc_OverflowError,
     447                 :            :                             "string too long for Python bytes");
     448                 :          0 :                         return NULL;
     449                 :            :                     }
     450                 :         78 :                     n = (Py_ssize_t)m;
     451                 :            :                 }
     452                 :         82 :                 v = PyBytes_FromStringAndSize(str, n);
     453                 :            :             }
     454                 :         82 :             return v;
     455                 :            :         }
     456                 :            : 
     457                 :      53704 :         case 'N':
     458                 :            :         case 'S':
     459                 :            :         case 'O':
     460         [ +  + ]:      53704 :         if (**p_format == '&') {
     461                 :            :             typedef PyObject *(*converter)(void *);
     462                 :      11516 :             converter func = va_arg(*p_va, converter);
     463                 :      11516 :             void *arg = va_arg(*p_va, void *);
     464                 :      11516 :             ++*p_format;
     465                 :      11516 :             return (*func)(arg);
     466                 :            :         }
     467                 :            :         else {
     468                 :            :             PyObject *v;
     469                 :      42188 :             v = va_arg(*p_va, PyObject *);
     470         [ +  - ]:      42188 :             if (v != NULL) {
     471         [ +  + ]:      42188 :                 if (*(*p_format - 1) != 'N')
     472                 :      37954 :                     Py_INCREF(v);
     473                 :            :             }
     474         [ #  # ]:          0 :             else if (!PyErr_Occurred())
     475                 :            :                 /* If a NULL was passed
     476                 :            :                  * because a call that should
     477                 :            :                  * have constructed a value
     478                 :            :                  * failed, that's OK, and we
     479                 :            :                  * pass the error on; but if
     480                 :            :                  * no error occurred it's not
     481                 :            :                  * clear that the caller knew
     482                 :            :                  * what she was doing. */
     483                 :          0 :                 PyErr_SetString(PyExc_SystemError,
     484                 :            :                     "NULL object passed to Py_BuildValue");
     485                 :      42188 :             return v;
     486                 :            :         }
     487                 :            : 
     488                 :          0 :         case ':':
     489                 :            :         case ',':
     490                 :            :         case ' ':
     491                 :            :         case '\t':
     492                 :          0 :             break;
     493                 :            : 
     494                 :          0 :         default:
     495                 :          0 :             PyErr_SetString(PyExc_SystemError,
     496                 :            :                 "bad format char passed to Py_BuildValue");
     497                 :          0 :             return NULL;
     498                 :            : 
     499                 :            :         }
     500                 :            :     }
     501                 :            : 
     502                 :            : #undef ERROR_NEED_PY_SSIZE_T_CLEAN
     503                 :            : }
     504                 :            : 
     505                 :            : 
     506                 :            : PyObject *
     507                 :     524067 : Py_BuildValue(const char *format, ...)
     508                 :            : {
     509                 :            :     va_list va;
     510                 :            :     PyObject* retval;
     511                 :     524067 :     va_start(va, format);
     512                 :     524067 :     retval = va_build_value(format, va, 0);
     513                 :     524067 :     va_end(va);
     514                 :     524067 :     return retval;
     515                 :            : }
     516                 :            : 
     517                 :            : PyObject *
     518                 :       6673 : _Py_BuildValue_SizeT(const char *format, ...)
     519                 :            : {
     520                 :            :     va_list va;
     521                 :            :     PyObject* retval;
     522                 :       6673 :     va_start(va, format);
     523                 :       6673 :     retval = va_build_value(format, va, FLAG_SIZE_T);
     524                 :       6673 :     va_end(va);
     525                 :       6673 :     return retval;
     526                 :            : }
     527                 :            : 
     528                 :            : PyObject *
     529                 :          0 : Py_VaBuildValue(const char *format, va_list va)
     530                 :            : {
     531                 :          0 :     return va_build_value(format, va, 0);
     532                 :            : }
     533                 :            : 
     534                 :            : PyObject *
     535                 :        635 : _Py_VaBuildValue_SizeT(const char *format, va_list va)
     536                 :            : {
     537                 :        635 :     return va_build_value(format, va, FLAG_SIZE_T);
     538                 :            : }
     539                 :            : 
     540                 :            : static PyObject *
     541                 :     531375 : va_build_value(const char *format, va_list va, int flags)
     542                 :            : {
     543                 :     531375 :     const char *f = format;
     544                 :     531375 :     Py_ssize_t n = countformat(f, '\0');
     545                 :            :     va_list lva;
     546                 :            :     PyObject *retval;
     547                 :            : 
     548         [ -  + ]:     531375 :     if (n < 0)
     549                 :          0 :         return NULL;
     550         [ -  + ]:     531375 :     if (n == 0) {
     551                 :          0 :         Py_RETURN_NONE;
     552                 :            :     }
     553                 :     531375 :     va_copy(lva, va);
     554         [ +  + ]:     531375 :     if (n == 1) {
     555                 :     524208 :         retval = do_mkvalue(&f, &lva, flags);
     556                 :            :     } else {
     557                 :       7167 :         retval = do_mktuple(&f, &lva, '\0', n, flags);
     558                 :            :     }
     559                 :     531375 :     va_end(lva);
     560                 :     531375 :     return retval;
     561                 :            : }
     562                 :            : 
     563                 :            : PyObject **
     564                 :       4747 : _Py_VaBuildStack(PyObject **small_stack, Py_ssize_t small_stack_len,
     565                 :            :                 const char *format, va_list va, Py_ssize_t *p_nargs)
     566                 :            : {
     567                 :       4747 :     return va_build_stack(small_stack, small_stack_len, format, va, 0, p_nargs);
     568                 :            : }
     569                 :            : 
     570                 :            : PyObject **
     571                 :       4540 : _Py_VaBuildStack_SizeT(PyObject **small_stack, Py_ssize_t small_stack_len,
     572                 :            :                        const char *format, va_list va, Py_ssize_t *p_nargs)
     573                 :            : {
     574                 :       4540 :     return va_build_stack(small_stack, small_stack_len, format, va, FLAG_SIZE_T, p_nargs);
     575                 :            : }
     576                 :            : 
     577                 :            : static PyObject **
     578                 :       9287 : va_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len,
     579                 :            :                const char *format, va_list va, int flags, Py_ssize_t *p_nargs)
     580                 :            : {
     581                 :            :     const char *f;
     582                 :            :     Py_ssize_t n;
     583                 :            :     va_list lva;
     584                 :            :     PyObject **stack;
     585                 :            :     int res;
     586                 :            : 
     587                 :       9287 :     n = countformat(format, '\0');
     588         [ -  + ]:       9287 :     if (n < 0) {
     589                 :          0 :         *p_nargs = 0;
     590                 :          0 :         return NULL;
     591                 :            :     }
     592                 :            : 
     593         [ -  + ]:       9287 :     if (n == 0) {
     594                 :          0 :         *p_nargs = 0;
     595                 :          0 :         return small_stack;
     596                 :            :     }
     597                 :            : 
     598         [ +  + ]:       9287 :     if (n <= small_stack_len) {
     599                 :       9137 :         stack = small_stack;
     600                 :            :     }
     601                 :            :     else {
     602                 :        150 :         stack = PyMem_Malloc(n * sizeof(stack[0]));
     603         [ -  + ]:        150 :         if (stack == NULL) {
     604                 :          0 :             PyErr_NoMemory();
     605                 :          0 :             return NULL;
     606                 :            :         }
     607                 :            :     }
     608                 :            : 
     609                 :       9287 :     va_copy(lva, va);
     610                 :       9287 :     f = format;
     611                 :       9287 :     res = do_mkstack(stack, &f, &lva, '\0', n, flags);
     612                 :       9287 :     va_end(lva);
     613                 :            : 
     614         [ -  + ]:       9287 :     if (res < 0) {
     615         [ #  # ]:          0 :         if (stack != small_stack) {
     616                 :          0 :             PyMem_Free(stack);
     617                 :            :         }
     618                 :          0 :         return NULL;
     619                 :            :     }
     620                 :            : 
     621                 :       9287 :     *p_nargs = n;
     622                 :       9287 :     return stack;
     623                 :            : }
     624                 :            : 
     625                 :            : 
     626                 :            : int
     627                 :      10440 : PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
     628                 :            : {
     629         [ -  + ]:      10440 :     if (!PyModule_Check(mod)) {
     630                 :          0 :         PyErr_SetString(PyExc_TypeError,
     631                 :            :                         "PyModule_AddObjectRef() first argument "
     632                 :            :                         "must be a module");
     633                 :          0 :         return -1;
     634                 :            :     }
     635         [ -  + ]:      10440 :     if (!value) {
     636         [ #  # ]:          0 :         if (!PyErr_Occurred()) {
     637                 :          0 :             PyErr_SetString(PyExc_SystemError,
     638                 :            :                             "PyModule_AddObjectRef() must be called "
     639                 :            :                             "with an exception raised if value is NULL");
     640                 :            :         }
     641                 :          0 :         return -1;
     642                 :            :     }
     643                 :            : 
     644                 :      10440 :     PyObject *dict = PyModule_GetDict(mod);
     645         [ -  + ]:      10440 :     if (dict == NULL) {
     646                 :            :         /* Internal error -- modules must have a dict! */
     647                 :          0 :         PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
     648                 :            :                      PyModule_GetName(mod));
     649                 :          0 :         return -1;
     650                 :            :     }
     651                 :            : 
     652         [ -  + ]:      10440 :     if (PyDict_SetItemString(dict, name, value)) {
     653                 :          0 :         return -1;
     654                 :            :     }
     655                 :      10440 :     return 0;
     656                 :            : }
     657                 :            : 
     658                 :            : 
     659                 :            : int
     660                 :        951 : PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
     661                 :            : {
     662                 :        951 :     int res = PyModule_AddObjectRef(mod, name, value);
     663         [ +  - ]:        951 :     if (res == 0) {
     664                 :        951 :         Py_DECREF(value);
     665                 :            :     }
     666                 :        951 :     return res;
     667                 :            : }
     668                 :            : 
     669                 :            : int
     670                 :       7807 : PyModule_AddIntConstant(PyObject *m, const char *name, long value)
     671                 :            : {
     672                 :       7807 :     PyObject *obj = PyLong_FromLong(value);
     673         [ -  + ]:       7807 :     if (!obj) {
     674                 :          0 :         return -1;
     675                 :            :     }
     676                 :       7807 :     int res = PyModule_AddObjectRef(m, name, obj);
     677                 :       7807 :     Py_DECREF(obj);
     678                 :       7807 :     return res;
     679                 :            : }
     680                 :            : 
     681                 :            : int
     682                 :        120 : PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
     683                 :            : {
     684                 :        120 :     PyObject *obj = PyUnicode_FromString(value);
     685         [ -  + ]:        120 :     if (!obj) {
     686                 :          0 :         return -1;
     687                 :            :     }
     688                 :        120 :     int res = PyModule_AddObjectRef(m, name, obj);
     689                 :        120 :     Py_DECREF(obj);
     690                 :        120 :     return res;
     691                 :            : }
     692                 :            : 
     693                 :            : int
     694                 :        817 : PyModule_AddType(PyObject *module, PyTypeObject *type)
     695                 :            : {
     696         [ -  + ]:        817 :     if (PyType_Ready(type) < 0) {
     697                 :          0 :         return -1;
     698                 :            :     }
     699                 :            : 
     700                 :        817 :     const char *name = _PyType_Name(type);
     701                 :            :     assert(name != NULL);
     702                 :            : 
     703                 :        817 :     return PyModule_AddObjectRef(module, name, (PyObject *)type);
     704                 :            : }

Generated by: LCOV version 1.14