LCOV - code coverage report
Current view: top level - Modules/_testcapi - getargs.c (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 3 388 0.8 %
Date: 2023-03-20 08:15:36 Functions: 1 53 1.9 %
Branches: 1 200 0.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Tests for Python/getargs.c and Python/modsupport.c;
       3                 :            :  * APIs that parse and build arguments.
       4                 :            :  */
       5                 :            : 
       6                 :            : #define PY_SSIZE_T_CLEAN
       7                 :            : 
       8                 :            : #include "parts.h"
       9                 :            : 
      10                 :            : static PyObject *
      11                 :          0 : parse_tuple_and_keywords(PyObject *self, PyObject *args)
      12                 :            : {
      13                 :            :     PyObject *sub_args;
      14                 :            :     PyObject *sub_kwargs;
      15                 :            :     const char *sub_format;
      16                 :            :     PyObject *sub_keywords;
      17                 :            : 
      18                 :            :     double buffers[8][4]; /* double ensures alignment where necessary */
      19                 :            :     PyObject *converted[8];
      20                 :            :     char *keywords[8 + 1]; /* space for NULL at end */
      21                 :            : 
      22                 :          0 :     PyObject *return_value = NULL;
      23                 :            : 
      24         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords",
      25                 :            :                           &sub_args, &sub_kwargs, &sub_format, &sub_keywords))
      26                 :            :     {
      27                 :          0 :         return NULL;
      28                 :            :     }
      29                 :            : 
      30   [ #  #  #  # ]:          0 :     if (!(PyList_CheckExact(sub_keywords) ||
      31                 :          0 :         PyTuple_CheckExact(sub_keywords)))
      32                 :            :     {
      33                 :          0 :         PyErr_SetString(PyExc_ValueError,
      34                 :            :             "parse_tuple_and_keywords: "
      35                 :            :             "sub_keywords must be either list or tuple");
      36                 :          0 :         return NULL;
      37                 :            :     }
      38                 :            : 
      39                 :          0 :     memset(buffers, 0, sizeof(buffers));
      40                 :          0 :     memset(converted, 0, sizeof(converted));
      41                 :          0 :     memset(keywords, 0, sizeof(keywords));
      42                 :            : 
      43         [ #  # ]:          0 :     Py_ssize_t size = PySequence_Fast_GET_SIZE(sub_keywords);
      44         [ #  # ]:          0 :     if (size > 8) {
      45                 :          0 :         PyErr_SetString(PyExc_ValueError,
      46                 :            :             "parse_tuple_and_keywords: too many keywords in sub_keywords");
      47                 :          0 :         goto exit;
      48                 :            :     }
      49                 :            : 
      50         [ #  # ]:          0 :     for (Py_ssize_t i = 0; i < size; i++) {
      51   [ #  #  #  #  :          0 :         PyObject *o = PySequence_Fast_GET_ITEM(sub_keywords, i);
                   #  # ]
      52         [ #  # ]:          0 :         if (!PyUnicode_FSConverter(o, (void *)(converted + i))) {
      53                 :          0 :             PyErr_Format(PyExc_ValueError,
      54                 :            :                 "parse_tuple_and_keywords: "
      55                 :            :                 "could not convert keywords[%zd] to narrow string", i);
      56                 :          0 :             goto exit;
      57                 :            :         }
      58                 :          0 :         keywords[i] = PyBytes_AS_STRING(converted[i]);
      59                 :            :     }
      60                 :            : 
      61                 :          0 :     int result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs,
      62                 :            :         sub_format, keywords,
      63                 :            :         buffers + 0, buffers + 1, buffers + 2, buffers + 3,
      64                 :            :         buffers + 4, buffers + 5, buffers + 6, buffers + 7);
      65                 :            : 
      66         [ #  # ]:          0 :     if (result) {
      67                 :          0 :         return_value = Py_NewRef(Py_None);
      68                 :            :     }
      69                 :            : 
      70                 :          0 : exit:
      71                 :          0 :     size = sizeof(converted) / sizeof(converted[0]);
      72         [ #  # ]:          0 :     for (Py_ssize_t i = 0; i < size; i++) {
      73                 :          0 :         Py_XDECREF(converted[i]);
      74                 :            :     }
      75                 :          0 :     return return_value;
      76                 :            : }
      77                 :            : 
      78                 :            : static PyObject *
      79                 :          0 : get_args(PyObject *self, PyObject *args)
      80                 :            : {
      81         [ #  # ]:          0 :     if (args == NULL) {
      82                 :          0 :         args = Py_None;
      83                 :            :     }
      84                 :          0 :     return Py_NewRef(args);
      85                 :            : }
      86                 :            : 
      87                 :            : static PyObject *
      88                 :          0 : get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
      89                 :            : {
      90         [ #  # ]:          0 :     if (kwargs == NULL) {
      91                 :          0 :         kwargs = Py_None;
      92                 :            :     }
      93                 :          0 :     return Py_NewRef(kwargs);
      94                 :            : }
      95                 :            : 
      96                 :            : static PyObject *
      97                 :          0 : getargs_w_star(PyObject *self, PyObject *args)
      98                 :            : {
      99                 :            :     Py_buffer buffer;
     100                 :            : 
     101         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) {
     102                 :          0 :         return NULL;
     103                 :            :     }
     104                 :            : 
     105         [ #  # ]:          0 :     if (2 <= buffer.len) {
     106                 :          0 :         char *str = buffer.buf;
     107                 :          0 :         str[0] = '[';
     108                 :          0 :         str[buffer.len-1] = ']';
     109                 :            :     }
     110                 :            : 
     111                 :          0 :     PyObject *result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
     112                 :          0 :     PyBuffer_Release(&buffer);
     113                 :          0 :     return result;
     114                 :            : }
     115                 :            : 
     116                 :            : static PyObject *
     117                 :          0 : test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored))
     118                 :            : {
     119                 :            :     /* Test that formats can begin with '|'. See issue #4720. */
     120                 :          0 :     PyObject *dict = NULL;
     121                 :            :     static char *kwlist[] = {NULL};
     122                 :          0 :     PyObject *tuple = PyTuple_New(0);
     123         [ #  # ]:          0 :     if (!tuple) {
     124                 :          0 :         return NULL;
     125                 :            :     }
     126                 :            :     int result;
     127         [ #  # ]:          0 :     if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) {
     128                 :          0 :         goto done;
     129                 :            :     }
     130                 :          0 :     dict = PyDict_New();
     131         [ #  # ]:          0 :     if (!dict) {
     132                 :          0 :         goto done;
     133                 :            :     }
     134                 :          0 :     result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse",
     135                 :            :                                          kwlist);
     136                 :          0 :   done:
     137                 :          0 :     Py_DECREF(tuple);
     138                 :          0 :     Py_XDECREF(dict);
     139         [ #  # ]:          0 :     if (!result) {
     140                 :          0 :         return NULL;
     141                 :            :     }
     142                 :          0 :     Py_RETURN_NONE;
     143                 :            : }
     144                 :            : 
     145                 :            : /* Test tuple argument processing */
     146                 :            : static PyObject *
     147                 :          0 : getargs_tuple(PyObject *self, PyObject *args)
     148                 :            : {
     149                 :            :     int a, b, c;
     150         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) {
     151                 :          0 :         return NULL;
     152                 :            :     }
     153                 :          0 :     return Py_BuildValue("iii", a, b, c);
     154                 :            : }
     155                 :            : 
     156                 :            : /* test PyArg_ParseTupleAndKeywords */
     157                 :            : static PyObject *
     158                 :          0 : getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
     159                 :            : {
     160                 :            :     static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
     161                 :            :     static const char fmt[] = "(ii)i|(i(ii))(iii)i";
     162                 :          0 :     int int_args[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
     163                 :            : 
     164         [ #  # ]:          0 :     if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
     165                 :            :         &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
     166                 :            :         &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
     167                 :            :     {
     168                 :          0 :         return NULL;
     169                 :            :     }
     170                 :          0 :     return Py_BuildValue("iiiiiiiiii",
     171                 :            :         int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
     172                 :            :         int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
     173                 :            : }
     174                 :            : 
     175                 :            : /* test PyArg_ParseTupleAndKeywords keyword-only arguments */
     176                 :            : static PyObject *
     177                 :          0 : getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
     178                 :            : {
     179                 :            :     static char *keywords[] = {"required", "optional", "keyword_only", NULL};
     180                 :          0 :     int required = -1;
     181                 :          0 :     int optional = -1;
     182                 :          0 :     int keyword_only = -1;
     183                 :            : 
     184         [ #  # ]:          0 :     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
     185                 :            :                                      &required, &optional, &keyword_only))
     186                 :            :     {
     187                 :          0 :         return NULL;
     188                 :            :     }
     189                 :          0 :     return Py_BuildValue("iii", required, optional, keyword_only);
     190                 :            : }
     191                 :            : 
     192                 :            : /* test PyArg_ParseTupleAndKeywords positional-only arguments */
     193                 :            : static PyObject *
     194                 :          0 : getargs_positional_only_and_keywords(PyObject *self, PyObject *args,
     195                 :            :                                      PyObject *kwargs)
     196                 :            : {
     197                 :            :     static char *keywords[] = {"", "", "keyword", NULL};
     198                 :          0 :     int required = -1;
     199                 :          0 :     int optional = -1;
     200                 :          0 :     int keyword = -1;
     201                 :            : 
     202         [ #  # ]:          0 :     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords,
     203                 :            :                                      &required, &optional, &keyword))
     204                 :            :     {
     205                 :          0 :         return NULL;
     206                 :            :     }
     207                 :          0 :     return Py_BuildValue("iii", required, optional, keyword);
     208                 :            : }
     209                 :            : 
     210                 :            : /* Functions to call PyArg_ParseTuple with integer format codes,
     211                 :            :    and return the result.
     212                 :            : */
     213                 :            : static PyObject *
     214                 :          0 : getargs_b(PyObject *self, PyObject *args)
     215                 :            : {
     216                 :            :     unsigned char value;
     217         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "b", &value)) {
     218                 :          0 :         return NULL;
     219                 :            :     }
     220                 :          0 :     return PyLong_FromUnsignedLong((unsigned long)value);
     221                 :            : }
     222                 :            : 
     223                 :            : static PyObject *
     224                 :          0 : getargs_B(PyObject *self, PyObject *args)
     225                 :            : {
     226                 :            :     unsigned char value;
     227         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "B", &value)) {
     228                 :          0 :         return NULL;
     229                 :            :     }
     230                 :          0 :     return PyLong_FromUnsignedLong((unsigned long)value);
     231                 :            : }
     232                 :            : 
     233                 :            : static PyObject *
     234                 :          0 : getargs_h(PyObject *self, PyObject *args)
     235                 :            : {
     236                 :            :     short value;
     237         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "h", &value)) {
     238                 :          0 :         return NULL;
     239                 :            :     }
     240                 :          0 :     return PyLong_FromLong((long)value);
     241                 :            : }
     242                 :            : 
     243                 :            : static PyObject *
     244                 :          0 : getargs_H(PyObject *self, PyObject *args)
     245                 :            : {
     246                 :            :     unsigned short value;
     247         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "H", &value)) {
     248                 :          0 :         return NULL;
     249                 :            :     }
     250                 :          0 :     return PyLong_FromUnsignedLong((unsigned long)value);
     251                 :            : }
     252                 :            : 
     253                 :            : static PyObject *
     254                 :          0 : getargs_I(PyObject *self, PyObject *args)
     255                 :            : {
     256                 :            :     unsigned int value;
     257         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "I", &value)) {
     258                 :          0 :         return NULL;
     259                 :            :     }
     260                 :          0 :     return PyLong_FromUnsignedLong((unsigned long)value);
     261                 :            : }
     262                 :            : 
     263                 :            : static PyObject *
     264                 :          0 : getargs_k(PyObject *self, PyObject *args)
     265                 :            : {
     266                 :            :     unsigned long value;
     267         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "k", &value)) {
     268                 :          0 :         return NULL;
     269                 :            :     }
     270                 :          0 :     return PyLong_FromUnsignedLong(value);
     271                 :            : }
     272                 :            : 
     273                 :            : static PyObject *
     274                 :          0 : getargs_i(PyObject *self, PyObject *args)
     275                 :            : {
     276                 :            :     int value;
     277         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "i", &value)) {
     278                 :          0 :         return NULL;
     279                 :            :     }
     280                 :          0 :     return PyLong_FromLong((long)value);
     281                 :            : }
     282                 :            : 
     283                 :            : static PyObject *
     284                 :          0 : getargs_l(PyObject *self, PyObject *args)
     285                 :            : {
     286                 :            :     long value;
     287         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "l", &value)) {
     288                 :          0 :         return NULL;
     289                 :            :     }
     290                 :          0 :     return PyLong_FromLong(value);
     291                 :            : }
     292                 :            : 
     293                 :            : static PyObject *
     294                 :          0 : getargs_n(PyObject *self, PyObject *args)
     295                 :            : {
     296                 :            :     Py_ssize_t value;
     297         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "n", &value)) {
     298                 :          0 :         return NULL;
     299                 :            :     }
     300                 :          0 :     return PyLong_FromSsize_t(value);
     301                 :            : }
     302                 :            : 
     303                 :            : static PyObject *
     304                 :          0 : getargs_p(PyObject *self, PyObject *args)
     305                 :            : {
     306                 :            :     int value;
     307         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "p", &value)) {
     308                 :          0 :         return NULL;
     309                 :            :     }
     310                 :          0 :     return PyLong_FromLong(value);
     311                 :            : }
     312                 :            : 
     313                 :            : static PyObject *
     314                 :          0 : getargs_L(PyObject *self, PyObject *args)
     315                 :            : {
     316                 :            :     long long value;
     317         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "L", &value)) {
     318                 :          0 :         return NULL;
     319                 :            :     }
     320                 :          0 :     return PyLong_FromLongLong(value);
     321                 :            : }
     322                 :            : 
     323                 :            : static PyObject *
     324                 :          0 : getargs_K(PyObject *self, PyObject *args)
     325                 :            : {
     326                 :            :     unsigned long long value;
     327         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "K", &value)) {
     328                 :          0 :         return NULL;
     329                 :            :     }
     330                 :          0 :     return PyLong_FromUnsignedLongLong(value);
     331                 :            : }
     332                 :            : 
     333                 :            : /* This function not only tests the 'k' getargs code, but also the
     334                 :            :    PyLong_AsUnsignedLongMask() function. */
     335                 :            : static PyObject *
     336                 :          0 : test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored))
     337                 :            : {
     338                 :            :     PyObject *tuple, *num;
     339                 :            :     unsigned long value;
     340                 :            : 
     341                 :          0 :     tuple = PyTuple_New(1);
     342         [ #  # ]:          0 :     if (tuple == NULL) {
     343                 :          0 :         return NULL;
     344                 :            :     }
     345                 :            : 
     346                 :            :     /* a number larger than ULONG_MAX even on 64-bit platforms */
     347                 :          0 :     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
     348         [ #  # ]:          0 :     if (num == NULL) {
     349                 :          0 :         return NULL;
     350                 :            :     }
     351                 :            : 
     352                 :          0 :     value = PyLong_AsUnsignedLongMask(num);
     353         [ #  # ]:          0 :     if (value != ULONG_MAX) {
     354                 :          0 :         PyErr_SetString(PyExc_AssertionError,
     355                 :            :             "test_k_code: "
     356                 :            :             "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
     357                 :          0 :         return NULL;
     358                 :            :     }
     359                 :            : 
     360                 :          0 :     PyTuple_SET_ITEM(tuple, 0, num);
     361                 :            : 
     362                 :          0 :     value = 0;
     363         [ #  # ]:          0 :     if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
     364                 :          0 :         return NULL;
     365                 :            :     }
     366         [ #  # ]:          0 :     if (value != ULONG_MAX) {
     367                 :          0 :         PyErr_SetString(PyExc_AssertionError,
     368                 :            :             "test_k_code: k code returned wrong value for long 0xFFF...FFF");
     369                 :          0 :         return NULL;
     370                 :            :     }
     371                 :            : 
     372                 :          0 :     Py_DECREF(num);
     373                 :          0 :     num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
     374         [ #  # ]:          0 :     if (num == NULL) {
     375                 :          0 :         return NULL;
     376                 :            :     }
     377                 :            : 
     378                 :          0 :     value = PyLong_AsUnsignedLongMask(num);
     379         [ #  # ]:          0 :     if (value != (unsigned long)-0x42) {
     380                 :          0 :         PyErr_SetString(PyExc_AssertionError,
     381                 :            :             "test_k_code: "
     382                 :            :             "PyLong_AsUnsignedLongMask() returned wrong value for long -0xFFF..000042");
     383                 :          0 :         return NULL;
     384                 :            :     }
     385                 :            : 
     386                 :          0 :     PyTuple_SET_ITEM(tuple, 0, num);
     387                 :            : 
     388                 :          0 :     value = 0;
     389         [ #  # ]:          0 :     if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
     390                 :          0 :         return NULL;
     391                 :            :     }
     392         [ #  # ]:          0 :     if (value != (unsigned long)-0x42) {
     393                 :          0 :         PyErr_SetString(PyExc_AssertionError,
     394                 :            :             "test_k_code: k code returned wrong value for long -0xFFF..000042");
     395                 :          0 :         return NULL;
     396                 :            :     }
     397                 :            : 
     398                 :          0 :     Py_DECREF(tuple);
     399                 :          0 :     Py_RETURN_NONE;
     400                 :            : }
     401                 :            : 
     402                 :            : static PyObject *
     403                 :          0 : getargs_f(PyObject *self, PyObject *args)
     404                 :            : {
     405                 :            :     float f;
     406         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "f", &f)) {
     407                 :          0 :         return NULL;
     408                 :            :     }
     409                 :          0 :     return PyFloat_FromDouble(f);
     410                 :            : }
     411                 :            : 
     412                 :            : static PyObject *
     413                 :          0 : getargs_d(PyObject *self, PyObject *args)
     414                 :            : {
     415                 :            :     double d;
     416         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "d", &d)) {
     417                 :          0 :         return NULL;
     418                 :            :     }
     419                 :          0 :     return PyFloat_FromDouble(d);
     420                 :            : }
     421                 :            : 
     422                 :            : static PyObject *
     423                 :          0 : getargs_D(PyObject *self, PyObject *args)
     424                 :            : {
     425                 :            :     Py_complex cval;
     426         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "D", &cval)) {
     427                 :          0 :         return NULL;
     428                 :            :     }
     429                 :          0 :     return PyComplex_FromCComplex(cval);
     430                 :            : }
     431                 :            : 
     432                 :            : static PyObject *
     433                 :          0 : getargs_S(PyObject *self, PyObject *args)
     434                 :            : {
     435                 :            :     PyObject *obj;
     436         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "S", &obj)) {
     437                 :          0 :         return NULL;
     438                 :            :     }
     439                 :          0 :     return Py_NewRef(obj);
     440                 :            : }
     441                 :            : 
     442                 :            : static PyObject *
     443                 :          0 : getargs_Y(PyObject *self, PyObject *args)
     444                 :            : {
     445                 :            :     PyObject *obj;
     446         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "Y", &obj)) {
     447                 :          0 :         return NULL;
     448                 :            :     }
     449                 :          0 :     return Py_NewRef(obj);
     450                 :            : }
     451                 :            : 
     452                 :            : static PyObject *
     453                 :          0 : getargs_U(PyObject *self, PyObject *args)
     454                 :            : {
     455                 :            :     PyObject *obj;
     456         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "U", &obj)) {
     457                 :          0 :         return NULL;
     458                 :            :     }
     459                 :          0 :     return Py_NewRef(obj);
     460                 :            : }
     461                 :            : 
     462                 :            : static PyObject *
     463                 :          0 : getargs_c(PyObject *self, PyObject *args)
     464                 :            : {
     465                 :            :     char c;
     466         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "c", &c)) {
     467                 :          0 :         return NULL;
     468                 :            :     }
     469                 :          0 :     return PyLong_FromLong((unsigned char)c);
     470                 :            : }
     471                 :            : 
     472                 :            : static PyObject *
     473                 :          0 : getargs_C(PyObject *self, PyObject *args)
     474                 :            : {
     475                 :            :     int c;
     476         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "C", &c)) {
     477                 :          0 :         return NULL;
     478                 :            :     }
     479                 :          0 :     return PyLong_FromLong(c);
     480                 :            : }
     481                 :            : 
     482                 :            : static PyObject *
     483                 :          0 : getargs_s(PyObject *self, PyObject *args)
     484                 :            : {
     485                 :            :     char *str;
     486         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "s", &str)) {
     487                 :          0 :         return NULL;
     488                 :            :     }
     489                 :          0 :     return PyBytes_FromString(str);
     490                 :            : }
     491                 :            : 
     492                 :            : static PyObject *
     493                 :          0 : getargs_s_star(PyObject *self, PyObject *args)
     494                 :            : {
     495                 :            :     Py_buffer buffer;
     496                 :            :     PyObject *bytes;
     497         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "s*", &buffer)) {
     498                 :          0 :         return NULL;
     499                 :            :     }
     500                 :          0 :     bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
     501                 :          0 :     PyBuffer_Release(&buffer);
     502                 :          0 :     return bytes;
     503                 :            : }
     504                 :            : 
     505                 :            : static PyObject *
     506                 :          0 : getargs_s_hash(PyObject *self, PyObject *args)
     507                 :            : {
     508                 :            :     char *str;
     509                 :            :     Py_ssize_t size;
     510         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "s#", &str, &size)) {
     511                 :          0 :         return NULL;
     512                 :            :     }
     513                 :          0 :     return PyBytes_FromStringAndSize(str, size);
     514                 :            : }
     515                 :            : 
     516                 :            : static PyObject *
     517                 :          0 : getargs_z(PyObject *self, PyObject *args)
     518                 :            : {
     519                 :            :     char *str;
     520         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "z", &str)) {
     521                 :          0 :         return NULL;
     522                 :            :     }
     523         [ #  # ]:          0 :     if (str != NULL) {
     524                 :          0 :         return PyBytes_FromString(str);
     525                 :            :     }
     526                 :          0 :     Py_RETURN_NONE;
     527                 :            : }
     528                 :            : 
     529                 :            : static PyObject *
     530                 :          0 : getargs_z_star(PyObject *self, PyObject *args)
     531                 :            : {
     532                 :            :     Py_buffer buffer;
     533                 :            :     PyObject *bytes;
     534         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "z*", &buffer)) {
     535                 :          0 :         return NULL;
     536                 :            :     }
     537         [ #  # ]:          0 :     if (buffer.buf != NULL) {
     538                 :          0 :         bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
     539                 :            :     }
     540                 :            :     else {
     541                 :          0 :         bytes = Py_NewRef(Py_None);
     542                 :            :     }
     543                 :          0 :     PyBuffer_Release(&buffer);
     544                 :          0 :     return bytes;
     545                 :            : }
     546                 :            : 
     547                 :            : static PyObject *
     548                 :          0 : getargs_z_hash(PyObject *self, PyObject *args)
     549                 :            : {
     550                 :            :     char *str;
     551                 :            :     Py_ssize_t size;
     552         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "z#", &str, &size)) {
     553                 :          0 :         return NULL;
     554                 :            :     }
     555         [ #  # ]:          0 :     if (str != NULL) {
     556                 :          0 :         return PyBytes_FromStringAndSize(str, size);
     557                 :            :     }
     558                 :          0 :     Py_RETURN_NONE;
     559                 :            : }
     560                 :            : 
     561                 :            : static PyObject *
     562                 :          0 : getargs_y(PyObject *self, PyObject *args)
     563                 :            : {
     564                 :            :     char *str;
     565         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "y", &str)) {
     566                 :          0 :         return NULL;
     567                 :            :     }
     568                 :          0 :     return PyBytes_FromString(str);
     569                 :            : }
     570                 :            : 
     571                 :            : static PyObject *
     572                 :          0 : getargs_y_star(PyObject *self, PyObject *args)
     573                 :            : {
     574                 :            :     Py_buffer buffer;
     575         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "y*", &buffer)) {
     576                 :          0 :         return NULL;
     577                 :            :     }
     578                 :          0 :     PyObject *bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
     579                 :          0 :     PyBuffer_Release(&buffer);
     580                 :          0 :     return bytes;
     581                 :            : }
     582                 :            : 
     583                 :            : static PyObject *
     584                 :          0 : getargs_y_hash(PyObject *self, PyObject *args)
     585                 :            : {
     586                 :            :     char *str;
     587                 :            :     Py_ssize_t size;
     588         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "y#", &str, &size)) {
     589                 :          0 :         return NULL;
     590                 :            :     }
     591                 :          0 :     return PyBytes_FromStringAndSize(str, size);
     592                 :            : }
     593                 :            : 
     594                 :            : static PyObject *
     595                 :          0 : getargs_u(PyObject *self, PyObject *args)
     596                 :            : {
     597                 :            :     Py_UNICODE *str;
     598         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "u", &str)) {
     599                 :          0 :         return NULL;
     600                 :            :     }
     601                 :          0 :     return PyUnicode_FromWideChar(str, -1);
     602                 :            : }
     603                 :            : 
     604                 :            : static PyObject *
     605                 :          0 : getargs_u_hash(PyObject *self, PyObject *args)
     606                 :            : {
     607                 :            :     Py_UNICODE *str;
     608                 :            :     Py_ssize_t size;
     609         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "u#", &str, &size)) {
     610                 :          0 :         return NULL;
     611                 :            :     }
     612                 :          0 :     return PyUnicode_FromWideChar(str, size);
     613                 :            : }
     614                 :            : 
     615                 :            : static PyObject *
     616                 :          0 : getargs_Z(PyObject *self, PyObject *args)
     617                 :            : {
     618                 :            :     Py_UNICODE *str;
     619         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "Z", &str)) {
     620                 :          0 :         return NULL;
     621                 :            :     }
     622         [ #  # ]:          0 :     if (str != NULL) {
     623                 :          0 :         return PyUnicode_FromWideChar(str, -1);
     624                 :            :     }
     625                 :          0 :     Py_RETURN_NONE;
     626                 :            : }
     627                 :            : 
     628                 :            : static PyObject *
     629                 :          0 : getargs_Z_hash(PyObject *self, PyObject *args)
     630                 :            : {
     631                 :            :     Py_UNICODE *str;
     632                 :            :     Py_ssize_t size;
     633         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "Z#", &str, &size)) {
     634                 :          0 :         return NULL;
     635                 :            :     }
     636         [ #  # ]:          0 :     if (str != NULL) {
     637                 :          0 :         return PyUnicode_FromWideChar(str, size);
     638                 :            :     }
     639                 :          0 :     Py_RETURN_NONE;
     640                 :            : }
     641                 :            : 
     642                 :            : static PyObject *
     643                 :          0 : getargs_es(PyObject *self, PyObject *args)
     644                 :            : {
     645                 :            :     PyObject *arg;
     646                 :          0 :     const char *encoding = NULL;
     647                 :            :     char *str;
     648                 :            : 
     649         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) {
     650                 :          0 :         return NULL;
     651                 :            :     }
     652         [ #  # ]:          0 :     if (!PyArg_Parse(arg, "es", encoding, &str)) {
     653                 :          0 :         return NULL;
     654                 :            :     }
     655                 :          0 :     PyObject *result = PyBytes_FromString(str);
     656                 :          0 :     PyMem_Free(str);
     657                 :          0 :     return result;
     658                 :            : }
     659                 :            : 
     660                 :            : static PyObject *
     661                 :          0 : getargs_et(PyObject *self, PyObject *args)
     662                 :            : {
     663                 :            :     PyObject *arg;
     664                 :          0 :     const char *encoding = NULL;
     665                 :            :     char *str;
     666                 :            : 
     667         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) {
     668                 :          0 :         return NULL;
     669                 :            :     }
     670         [ #  # ]:          0 :     if (!PyArg_Parse(arg, "et", encoding, &str)) {
     671                 :          0 :         return NULL;
     672                 :            :     }
     673                 :          0 :     PyObject *result = PyBytes_FromString(str);
     674                 :          0 :     PyMem_Free(str);
     675                 :          0 :     return result;
     676                 :            : }
     677                 :            : 
     678                 :            : static PyObject *
     679                 :          0 : getargs_es_hash(PyObject *self, PyObject *args)
     680                 :            : {
     681                 :            :     PyObject *arg;
     682                 :          0 :     const char *encoding = NULL;
     683                 :          0 :     PyByteArrayObject *buffer = NULL;
     684                 :          0 :     char *str = NULL;
     685                 :            :     Py_ssize_t size;
     686                 :            : 
     687         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) {
     688                 :          0 :         return NULL;
     689                 :            :     }
     690         [ #  # ]:          0 :     if (buffer != NULL) {
     691                 :          0 :         str = PyByteArray_AS_STRING(buffer);
     692                 :          0 :         size = PyByteArray_GET_SIZE(buffer);
     693                 :            :     }
     694         [ #  # ]:          0 :     if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) {
     695                 :          0 :         return NULL;
     696                 :            :     }
     697                 :          0 :     PyObject *result = PyBytes_FromStringAndSize(str, size);
     698         [ #  # ]:          0 :     if (buffer == NULL) {
     699                 :          0 :         PyMem_Free(str);
     700                 :            :     }
     701                 :          0 :     return result;
     702                 :            : }
     703                 :            : 
     704                 :            : static PyObject *
     705                 :          0 : getargs_et_hash(PyObject *self, PyObject *args)
     706                 :            : {
     707                 :            :     PyObject *arg;
     708                 :          0 :     const char *encoding = NULL;
     709                 :          0 :     PyByteArrayObject *buffer = NULL;
     710                 :          0 :     char *str = NULL;
     711                 :            :     Py_ssize_t size;
     712                 :            : 
     713         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) {
     714                 :          0 :         return NULL;
     715                 :            :     }
     716         [ #  # ]:          0 :     if (buffer != NULL) {
     717                 :          0 :         str = PyByteArray_AS_STRING(buffer);
     718                 :          0 :         size = PyByteArray_GET_SIZE(buffer);
     719                 :            :     }
     720         [ #  # ]:          0 :     if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) {
     721                 :          0 :         return NULL;
     722                 :            :     }
     723                 :          0 :     PyObject *result = PyBytes_FromStringAndSize(str, size);
     724         [ #  # ]:          0 :     if (buffer == NULL) {
     725                 :          0 :         PyMem_Free(str);
     726                 :            :     }
     727                 :          0 :     return result;
     728                 :            : }
     729                 :            : 
     730                 :            : /* Test the L code for PyArg_ParseTuple.  This should deliver a long long
     731                 :            :    for both long and int arguments.  The test may leak a little memory if
     732                 :            :    it fails.
     733                 :            : */
     734                 :            : static PyObject *
     735                 :          0 : test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored))
     736                 :            : {
     737                 :            :     PyObject *tuple, *num;
     738                 :            :     long long value;
     739                 :            : 
     740                 :          0 :     tuple = PyTuple_New(1);
     741         [ #  # ]:          0 :     if (tuple == NULL) {
     742                 :          0 :         return NULL;
     743                 :            :     }
     744                 :            : 
     745                 :          0 :     num = PyLong_FromLong(42);
     746         [ #  # ]:          0 :     if (num == NULL) {
     747                 :          0 :         return NULL;
     748                 :            :     }
     749                 :            : 
     750                 :          0 :     PyTuple_SET_ITEM(tuple, 0, num);
     751                 :            : 
     752                 :          0 :     value = -1;
     753         [ #  # ]:          0 :     if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
     754                 :          0 :         return NULL;
     755                 :            :     }
     756         [ #  # ]:          0 :     if (value != 42) {
     757                 :          0 :         PyErr_SetString(PyExc_AssertionError,
     758                 :            :             "test_L_code: L code returned wrong value for long 42");
     759                 :          0 :         return NULL;
     760                 :            :     }
     761                 :            : 
     762                 :          0 :     Py_DECREF(num);
     763                 :          0 :     num = PyLong_FromLong(42);
     764         [ #  # ]:          0 :     if (num == NULL) {
     765                 :          0 :         return NULL;
     766                 :            :     }
     767                 :            : 
     768                 :          0 :     PyTuple_SET_ITEM(tuple, 0, num);
     769                 :            : 
     770                 :          0 :     value = -1;
     771         [ #  # ]:          0 :     if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
     772                 :          0 :         return NULL;
     773                 :            :     }
     774         [ #  # ]:          0 :     if (value != 42) {
     775                 :          0 :         PyErr_SetString(PyExc_AssertionError,
     776                 :            :             "test_L_code: L code returned wrong value for int 42");
     777                 :          0 :         return NULL;
     778                 :            :     }
     779                 :            : 
     780                 :          0 :     Py_DECREF(tuple);
     781                 :          0 :     Py_RETURN_NONE;
     782                 :            : }
     783                 :            : 
     784                 :            : /* Test the s and z codes for PyArg_ParseTuple.
     785                 :            : */
     786                 :            : static PyObject *
     787                 :          0 : test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored))
     788                 :            : {
     789                 :            :     /* Unicode strings should be accepted */
     790                 :          0 :     PyObject *tuple = PyTuple_New(1);
     791         [ #  # ]:          0 :     if (tuple == NULL) {
     792                 :          0 :         return NULL;
     793                 :            :     }
     794                 :            : 
     795                 :          0 :     PyObject *obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
     796                 :            :                                      "latin-1", NULL);
     797         [ #  # ]:          0 :     if (obj == NULL) {
     798                 :          0 :         return NULL;
     799                 :            :     }
     800                 :            : 
     801                 :          0 :     PyTuple_SET_ITEM(tuple, 0, obj);
     802                 :            : 
     803                 :            :     /* These two blocks used to raise a TypeError:
     804                 :            :      * "argument must be string without null bytes, not str"
     805                 :            :      */
     806                 :            :     char *value;
     807         [ #  # ]:          0 :     if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) {
     808                 :          0 :         return NULL;
     809                 :            :     }
     810                 :            : 
     811         [ #  # ]:          0 :     if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) {
     812                 :          0 :         return NULL;
     813                 :            :     }
     814                 :            : 
     815                 :          0 :     Py_DECREF(tuple);
     816                 :          0 :     Py_RETURN_NONE;
     817                 :            : }
     818                 :            : 
     819                 :            : #undef PyArg_ParseTupleAndKeywords
     820                 :            : PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
     821                 :            :                                             const char *, char **, ...);
     822                 :            : 
     823                 :            : static PyObject *
     824                 :          0 : getargs_s_hash_int(PyObject *self, PyObject *args, PyObject *kwargs)
     825                 :            : {
     826                 :            :     static char *keywords[] = {"", "", "x", NULL};
     827                 :          0 :     Py_buffer buf = {NULL};
     828                 :            :     const char *s;
     829                 :            :     int len;
     830                 :          0 :     int i = 0;
     831         [ #  # ]:          0 :     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "w*|s#i", keywords,
     832                 :            :                                      &buf, &s, &len, &i))
     833                 :            :     {
     834                 :          0 :         return NULL;
     835                 :            :     }
     836                 :          0 :     PyBuffer_Release(&buf);
     837                 :          0 :     Py_RETURN_NONE;
     838                 :            : }
     839                 :            : 
     840                 :            : static PyObject *
     841                 :          0 : getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs)
     842                 :            : {
     843                 :            :     static char *keywords[] = {"", "", "x", NULL};
     844                 :          0 :     Py_buffer buf = {NULL};
     845                 :            :     const char *s;
     846                 :            :     int len;
     847                 :          0 :     int i = 0;
     848         [ #  # ]:          0 :     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "w*|(s#)i", keywords,
     849                 :            :                                      &buf, &s, &len, &i))
     850                 :            :     {
     851                 :          0 :         return NULL;
     852                 :            :     }
     853                 :          0 :     PyBuffer_Release(&buf);
     854                 :          0 :     Py_RETURN_NONE;
     855                 :            : }
     856                 :            : 
     857                 :            : static PyObject *
     858                 :          0 : gh_99240_clear_args(PyObject *self, PyObject *args)
     859                 :            : {
     860                 :          0 :     char *a = NULL;
     861                 :          0 :     char *b = NULL;
     862                 :            : 
     863         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "eses", "idna", &a, "idna", &b)) {
     864   [ #  #  #  # ]:          0 :         if (a || b) {
     865                 :          0 :             PyErr_Clear();
     866                 :          0 :             PyErr_SetString(PyExc_AssertionError, "Arguments are not cleared.");
     867                 :            :         }
     868                 :          0 :         return NULL;
     869                 :            :     }
     870                 :          0 :     PyMem_Free(a);
     871                 :          0 :     PyMem_Free(b);
     872                 :          0 :     Py_RETURN_NONE;
     873                 :            : }
     874                 :            : 
     875                 :            : static PyMethodDef test_methods[] = {
     876                 :            :     {"get_args",                get_args,                        METH_VARARGS},
     877                 :            :     {"get_kwargs", _PyCFunction_CAST(get_kwargs), METH_VARARGS|METH_KEYWORDS},
     878                 :            :     {"getargs_B",               getargs_B,                       METH_VARARGS},
     879                 :            :     {"getargs_C",               getargs_C,                       METH_VARARGS},
     880                 :            :     {"getargs_D",               getargs_D,                       METH_VARARGS},
     881                 :            :     {"getargs_H",               getargs_H,                       METH_VARARGS},
     882                 :            :     {"getargs_I",               getargs_I,                       METH_VARARGS},
     883                 :            :     {"getargs_K",               getargs_K,                       METH_VARARGS},
     884                 :            :     {"getargs_L",               getargs_L,                       METH_VARARGS},
     885                 :            :     {"getargs_S",               getargs_S,                       METH_VARARGS},
     886                 :            :     {"getargs_U",               getargs_U,                       METH_VARARGS},
     887                 :            :     {"getargs_Y",               getargs_Y,                       METH_VARARGS},
     888                 :            :     {"getargs_Z",               getargs_Z,                       METH_VARARGS},
     889                 :            :     {"getargs_Z_hash",          getargs_Z_hash,                  METH_VARARGS},
     890                 :            :     {"getargs_b",               getargs_b,                       METH_VARARGS},
     891                 :            :     {"getargs_c",               getargs_c,                       METH_VARARGS},
     892                 :            :     {"getargs_d",               getargs_d,                       METH_VARARGS},
     893                 :            :     {"getargs_es",              getargs_es,                      METH_VARARGS},
     894                 :            :     {"getargs_es_hash",         getargs_es_hash,                 METH_VARARGS},
     895                 :            :     {"getargs_et",              getargs_et,                      METH_VARARGS},
     896                 :            :     {"getargs_et_hash",         getargs_et_hash,                 METH_VARARGS},
     897                 :            :     {"getargs_f",               getargs_f,                       METH_VARARGS},
     898                 :            :     {"getargs_h",               getargs_h,                       METH_VARARGS},
     899                 :            :     {"getargs_i",               getargs_i,                       METH_VARARGS},
     900                 :            :     {"getargs_k",               getargs_k,                       METH_VARARGS},
     901                 :            :     {"getargs_keyword_only", _PyCFunction_CAST(getargs_keyword_only), METH_VARARGS|METH_KEYWORDS},
     902                 :            :     {"getargs_keywords", _PyCFunction_CAST(getargs_keywords), METH_VARARGS|METH_KEYWORDS},
     903                 :            :     {"getargs_l",               getargs_l,                       METH_VARARGS},
     904                 :            :     {"getargs_n",               getargs_n,                       METH_VARARGS},
     905                 :            :     {"getargs_p",               getargs_p,                       METH_VARARGS},
     906                 :            :     {"getargs_positional_only_and_keywords", _PyCFunction_CAST(getargs_positional_only_and_keywords), METH_VARARGS|METH_KEYWORDS},
     907                 :            :     {"getargs_s",               getargs_s,                       METH_VARARGS},
     908                 :            :     {"getargs_s_hash",          getargs_s_hash,                  METH_VARARGS},
     909                 :            :     {"getargs_s_hash_int", _PyCFunction_CAST(getargs_s_hash_int), METH_VARARGS|METH_KEYWORDS},
     910                 :            :     {"getargs_s_hash_int2", _PyCFunction_CAST(getargs_s_hash_int2), METH_VARARGS|METH_KEYWORDS},
     911                 :            :     {"getargs_s_star",          getargs_s_star,                  METH_VARARGS},
     912                 :            :     {"getargs_tuple",           getargs_tuple,                   METH_VARARGS},
     913                 :            :     {"getargs_u",               getargs_u,                       METH_VARARGS},
     914                 :            :     {"getargs_u_hash",          getargs_u_hash,                  METH_VARARGS},
     915                 :            :     {"getargs_w_star",          getargs_w_star,                  METH_VARARGS},
     916                 :            :     {"getargs_y",               getargs_y,                       METH_VARARGS},
     917                 :            :     {"getargs_y_hash",          getargs_y_hash,                  METH_VARARGS},
     918                 :            :     {"getargs_y_star",          getargs_y_star,                  METH_VARARGS},
     919                 :            :     {"getargs_z",               getargs_z,                       METH_VARARGS},
     920                 :            :     {"getargs_z_hash",          getargs_z_hash,                  METH_VARARGS},
     921                 :            :     {"getargs_z_star",          getargs_z_star,                  METH_VARARGS},
     922                 :            :     {"parse_tuple_and_keywords", parse_tuple_and_keywords,       METH_VARARGS},
     923                 :            :     {"test_L_code",             test_L_code,                     METH_NOARGS},
     924                 :            :     {"test_empty_argparse",     test_empty_argparse,             METH_NOARGS},
     925                 :            :     {"test_k_code",             test_k_code,                     METH_NOARGS},
     926                 :            :     {"test_s_code",             test_s_code,                     METH_NOARGS},
     927                 :            :     {"gh_99240_clear_args",     gh_99240_clear_args,             METH_VARARGS},
     928                 :            :     {NULL},
     929                 :            : };
     930                 :            : 
     931                 :            : int
     932                 :          2 : _PyTestCapi_Init_GetArgs(PyObject *mod)
     933                 :            : {
     934         [ -  + ]:          2 :     if (PyModule_AddFunctions(mod, test_methods) < 0) {
     935                 :          0 :         return -1;
     936                 :            :     }
     937                 :            : 
     938                 :          2 :     return 0;
     939                 :            : }

Generated by: LCOV version 1.14