LCOV - code coverage report
Current view: top level - Python - intrinsics.c (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 53 110 48.2 %
Date: 2023-03-20 08:15:36 Functions: 5 8 62.5 %
Branches: 25 60 41.7 %

           Branch data     Line data    Source code
       1                 :            : 
       2                 :            : #define _PY_INTERPRETER
       3                 :            : 
       4                 :            : #include "Python.h"
       5                 :            : #include "pycore_frame.h"
       6                 :            : #include "pycore_runtime.h"
       7                 :            : #include "pycore_global_objects.h"
       8                 :            : #include "pycore_intrinsics.h"
       9                 :            : #include "pycore_pyerrors.h"
      10                 :            : 
      11                 :            : 
      12                 :            : /******** Unary functions ********/
      13                 :            : 
      14                 :            : static PyObject *
      15                 :          0 : no_intrinsic(PyThreadState* tstate, PyObject *unused)
      16                 :            : {
      17                 :          0 :     _PyErr_SetString(tstate, PyExc_SystemError, "invalid intrinsic function");
      18                 :          0 :     return NULL;
      19                 :            : }
      20                 :            : 
      21                 :            : static PyObject *
      22                 :         38 : print_expr(PyThreadState* tstate, PyObject *value)
      23                 :            : {
      24                 :         38 :     PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(displayhook));
      25                 :            :     // Can't use ERROR_IF here.
      26         [ -  + ]:         38 :     if (hook == NULL) {
      27                 :          0 :         _PyErr_SetString(tstate, PyExc_RuntimeError,
      28                 :            :                             "lost sys.displayhook");
      29                 :          0 :         return NULL;
      30                 :            :     }
      31                 :         38 :     return PyObject_CallOneArg(hook, value);
      32                 :            : }
      33                 :            : 
      34                 :            : static int
      35                 :        162 : import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
      36                 :            : {
      37                 :            :     PyObject *all, *dict, *name, *value;
      38                 :        162 :     int skip_leading_underscores = 0;
      39                 :            :     int pos, err;
      40                 :            : 
      41         [ -  + ]:        162 :     if (_PyObject_LookupAttr(v, &_Py_ID(__all__), &all) < 0) {
      42                 :          0 :         return -1; /* Unexpected error */
      43                 :            :     }
      44         [ +  + ]:        162 :     if (all == NULL) {
      45         [ -  + ]:        109 :         if (_PyObject_LookupAttr(v, &_Py_ID(__dict__), &dict) < 0) {
      46                 :          0 :             return -1;
      47                 :            :         }
      48         [ -  + ]:        109 :         if (dict == NULL) {
      49                 :          0 :             _PyErr_SetString(tstate, PyExc_ImportError,
      50                 :            :                     "from-import-* object has no __dict__ and no __all__");
      51                 :          0 :             return -1;
      52                 :            :         }
      53                 :        109 :         all = PyMapping_Keys(dict);
      54                 :        109 :         Py_DECREF(dict);
      55         [ -  + ]:        109 :         if (all == NULL)
      56                 :          0 :             return -1;
      57                 :        109 :         skip_leading_underscores = 1;
      58                 :            :     }
      59                 :            : 
      60                 :        162 :     for (pos = 0, err = 0; ; pos++) {
      61                 :      14997 :         name = PySequence_GetItem(all, pos);
      62         [ +  + ]:      14997 :         if (name == NULL) {
      63         [ -  + ]:        162 :             if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
      64                 :          0 :                 err = -1;
      65                 :            :             }
      66                 :            :             else {
      67                 :        162 :                 _PyErr_Clear(tstate);
      68                 :            :             }
      69                 :        162 :             break;
      70                 :            :         }
      71         [ -  + ]:      14835 :         if (!PyUnicode_Check(name)) {
      72                 :          0 :             PyObject *modname = PyObject_GetAttr(v, &_Py_ID(__name__));
      73         [ #  # ]:          0 :             if (modname == NULL) {
      74                 :          0 :                 Py_DECREF(name);
      75                 :          0 :                 err = -1;
      76                 :          0 :                 break;
      77                 :            :             }
      78         [ #  # ]:          0 :             if (!PyUnicode_Check(modname)) {
      79                 :          0 :                 _PyErr_Format(tstate, PyExc_TypeError,
      80                 :            :                               "module __name__ must be a string, not %.100s",
      81                 :          0 :                               Py_TYPE(modname)->tp_name);
      82                 :            :             }
      83                 :            :             else {
      84   [ #  #  #  # ]:          0 :                 _PyErr_Format(tstate, PyExc_TypeError,
      85                 :            :                               "%s in %U.%s must be str, not %.100s",
      86                 :            :                               skip_leading_underscores ? "Key" : "Item",
      87                 :            :                               modname,
      88                 :            :                               skip_leading_underscores ? "__dict__" : "__all__",
      89                 :          0 :                               Py_TYPE(name)->tp_name);
      90                 :            :             }
      91                 :          0 :             Py_DECREF(modname);
      92                 :          0 :             Py_DECREF(name);
      93                 :          0 :             err = -1;
      94                 :          0 :             break;
      95                 :            :         }
      96         [ +  + ]:      14835 :         if (skip_leading_underscores) {
      97         [ -  + ]:      14075 :             if (PyUnicode_READY(name) == -1) {
      98                 :          0 :                 Py_DECREF(name);
      99                 :          0 :                 err = -1;
     100                 :          0 :                 break;
     101                 :            :             }
     102         [ +  + ]:      14075 :             if (PyUnicode_READ_CHAR(name, 0) == '_') {
     103                 :        725 :                 Py_DECREF(name);
     104                 :        725 :                 continue;
     105                 :            :             }
     106                 :            :         }
     107                 :      14110 :         value = PyObject_GetAttr(v, name);
     108         [ -  + ]:      14110 :         if (value == NULL)
     109                 :          0 :             err = -1;
     110         [ +  - ]:      14110 :         else if (PyDict_CheckExact(locals))
     111                 :      14110 :             err = PyDict_SetItem(locals, name, value);
     112                 :            :         else
     113                 :          0 :             err = PyObject_SetItem(locals, name, value);
     114                 :      14110 :         Py_DECREF(name);
     115                 :      14110 :         Py_XDECREF(value);
     116         [ -  + ]:      14110 :         if (err < 0)
     117                 :          0 :             break;
     118                 :            :     }
     119                 :        162 :     Py_DECREF(all);
     120                 :        162 :     return err;
     121                 :            : }
     122                 :            : 
     123                 :            : static PyObject *
     124                 :        162 : import_star(PyThreadState* tstate, PyObject *from)
     125                 :            : {
     126                 :        162 :     _PyInterpreterFrame *frame = tstate->cframe->current_frame;
     127         [ -  + ]:        162 :     if (_PyFrame_FastToLocalsWithError(frame) < 0) {
     128                 :          0 :         return NULL;
     129                 :            :     }
     130                 :            : 
     131                 :        162 :     PyObject *locals = frame->f_locals;
     132         [ -  + ]:        162 :     if (locals == NULL) {
     133                 :          0 :         _PyErr_SetString(tstate, PyExc_SystemError,
     134                 :            :                             "no locals found during 'import *'");
     135                 :          0 :         return NULL;
     136                 :            :     }
     137                 :        162 :     int err = import_all_from(tstate, locals, from);
     138                 :        162 :     _PyFrame_LocalsToFast(frame, 0);
     139         [ -  + ]:        162 :     if (err < 0) {
     140                 :          0 :         return NULL;
     141                 :            :     }
     142                 :        162 :     Py_RETURN_NONE;
     143                 :            : }
     144                 :            : 
     145                 :            : static PyObject *
     146                 :       1939 : stopiteration_error(PyThreadState* tstate, PyObject *exc)
     147                 :            : {
     148                 :       1939 :     _PyInterpreterFrame *frame = tstate->cframe->current_frame;
     149                 :            :     assert(frame->owner == FRAME_OWNED_BY_GENERATOR);
     150                 :            :     assert(PyExceptionInstance_Check(exc));
     151                 :       1939 :     const char *msg = NULL;
     152         [ -  + ]:       1939 :     if (PyErr_GivenExceptionMatches(exc, PyExc_StopIteration)) {
     153                 :          0 :         msg = "generator raised StopIteration";
     154         [ #  # ]:          0 :         if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) {
     155                 :          0 :             msg = "async generator raised StopIteration";
     156                 :            :         }
     157         [ #  # ]:          0 :         else if (frame->f_code->co_flags & CO_COROUTINE) {
     158                 :          0 :             msg = "coroutine raised StopIteration";
     159                 :            :         }
     160                 :            :     }
     161   [ -  +  -  - ]:       1939 :     else if ((frame->f_code->co_flags & CO_ASYNC_GENERATOR) &&
     162                 :          0 :             PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration))
     163                 :            :     {
     164                 :            :         /* code in `gen` raised a StopAsyncIteration error:
     165                 :            :         raise a RuntimeError.
     166                 :            :         */
     167                 :          0 :         msg = "async generator raised StopAsyncIteration";
     168                 :            :     }
     169         [ -  + ]:       1939 :     if (msg != NULL) {
     170                 :          0 :         PyObject *message = _PyUnicode_FromASCII(msg, strlen(msg));
     171         [ #  # ]:          0 :         if (message == NULL) {
     172                 :          0 :             return NULL;
     173                 :            :         }
     174                 :          0 :         PyObject *error = PyObject_CallOneArg(PyExc_RuntimeError, message);
     175         [ #  # ]:          0 :         if (error == NULL) {
     176                 :          0 :             Py_DECREF(message);
     177                 :          0 :             return NULL;
     178                 :            :         }
     179                 :            :         assert(PyExceptionInstance_Check(error));
     180                 :          0 :         PyException_SetCause(error, Py_NewRef(exc));
     181                 :            :         // Steal exc reference, rather than Py_NewRef+Py_DECREF
     182                 :          0 :         PyException_SetContext(error, Py_NewRef(exc));
     183                 :          0 :         Py_DECREF(message);
     184                 :          0 :         return error;
     185                 :            :     }
     186                 :       1939 :     return Py_NewRef(exc);
     187                 :            : }
     188                 :            : 
     189                 :            : static PyObject *
     190                 :          0 : unary_pos(PyThreadState* unused, PyObject *value)
     191                 :            : {
     192                 :          0 :     return PyNumber_Positive(value);
     193                 :            : }
     194                 :            : 
     195                 :            : static PyObject *
     196                 :       1212 : list_to_tuple(PyThreadState* unused, PyObject *v)
     197                 :            : {
     198                 :            :     assert(PyList_Check(v));
     199                 :       1212 :     return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
     200                 :            : }
     201                 :            : 
     202                 :            : const instrinsic_func1
     203                 :            : _PyIntrinsics_UnaryFunctions[] = {
     204                 :            :     [0] = no_intrinsic,
     205                 :            :     [INTRINSIC_PRINT] = print_expr,
     206                 :            :     [INTRINSIC_IMPORT_STAR] = import_star,
     207                 :            :     [INTRINSIC_STOPITERATION_ERROR] = stopiteration_error,
     208                 :            :     [INTRINSIC_ASYNC_GEN_WRAP] = _PyAsyncGenValueWrapperNew,
     209                 :            :     [INTRINSIC_UNARY_POSITIVE] = unary_pos,
     210                 :            :     [INTRINSIC_LIST_TO_TUPLE] = list_to_tuple,
     211                 :            : };
     212                 :            : 
     213                 :            : 
     214                 :            : /******** Binary functions ********/
     215                 :            : 
     216                 :            : 
     217                 :            : static PyObject *
     218                 :          0 : prep_reraise_star(PyThreadState* unused, PyObject *orig, PyObject *excs)
     219                 :            : {
     220                 :            :     assert(PyList_Check(excs));
     221                 :          0 :     return _PyExc_PrepReraiseStar(orig, excs);
     222                 :            : }
     223                 :            : 
     224                 :            : const instrinsic_func2
     225                 :            : _PyIntrinsics_BinaryFunctions[] = {
     226                 :            :     [INTRINSIC_PREP_RERAISE_STAR] = prep_reraise_star,
     227                 :            : };
     228                 :            : 

Generated by: LCOV version 1.14