LCOV - code coverage report
Current view: top level - libreoffice/workdir/unxlngi6.pro/UnpackedTarball/python3/Objects - funcobject.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 191 417 45.8 %
Date: 2012-12-17 Functions: 22 45 48.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : 
       2             : /* Function object implementation */
       3             : 
       4             : #include "Python.h"
       5             : #include "code.h"
       6             : #include "structmember.h"
       7             : 
       8             : PyObject *
       9        1705 : PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
      10             : {
      11        1705 :     PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
      12             :                                         &PyFunction_Type);
      13             :     static PyObject *__name__ = 0;
      14        1705 :     if (op != NULL) {
      15             :         PyObject *doc;
      16             :         PyObject *consts;
      17             :         PyObject *module;
      18        1705 :         op->func_weakreflist = NULL;
      19        1705 :         Py_INCREF(code);
      20        1705 :         op->func_code = code;
      21        1705 :         Py_INCREF(globals);
      22        1705 :         op->func_globals = globals;
      23        1705 :         op->func_name = ((PyCodeObject *)code)->co_name;
      24        1705 :         Py_INCREF(op->func_name);
      25        1705 :         op->func_defaults = NULL; /* No default arguments */
      26        1705 :         op->func_kwdefaults = NULL; /* No keyword only defaults */
      27        1705 :         op->func_closure = NULL;
      28        1705 :         consts = ((PyCodeObject *)code)->co_consts;
      29        1705 :         if (PyTuple_Size(consts) >= 1) {
      30        1704 :             doc = PyTuple_GetItem(consts, 0);
      31        1704 :             if (!PyUnicode_Check(doc))
      32        1091 :                 doc = Py_None;
      33             :         }
      34             :         else
      35           1 :             doc = Py_None;
      36        1705 :         Py_INCREF(doc);
      37        1705 :         op->func_doc = doc;
      38        1705 :         op->func_dict = NULL;
      39        1705 :         op->func_module = NULL;
      40        1705 :         op->func_annotations = NULL;
      41             : 
      42             :         /* __module__: If module name is in globals, use it.
      43             :            Otherwise, use None.
      44             :         */
      45        1705 :         if (!__name__) {
      46           1 :             __name__ = PyUnicode_InternFromString("__name__");
      47           1 :             if (!__name__) {
      48           0 :                 Py_DECREF(op);
      49           0 :                 return NULL;
      50             :             }
      51             :         }
      52        1705 :         module = PyDict_GetItem(globals, __name__);
      53        1705 :         if (module) {
      54        1705 :             Py_INCREF(module);
      55        1705 :             op->func_module = module;
      56             :         }
      57        1705 :         if (qualname)
      58        1705 :             op->func_qualname = qualname;
      59             :         else
      60           0 :             op->func_qualname = op->func_name;
      61        1705 :         Py_INCREF(op->func_qualname);
      62             :     }
      63             :     else
      64           0 :         return NULL;
      65        1705 :     _PyObject_GC_TRACK(op);
      66        1705 :     return (PyObject *)op;
      67             : }
      68             : 
      69             : PyObject *
      70           0 : PyFunction_New(PyObject *code, PyObject *globals)
      71             : {
      72           0 :     return PyFunction_NewWithQualName(code, globals, NULL);
      73             : }
      74             : 
      75             : PyObject *
      76           0 : PyFunction_GetCode(PyObject *op)
      77             : {
      78           0 :     if (!PyFunction_Check(op)) {
      79           0 :         PyErr_BadInternalCall();
      80           0 :         return NULL;
      81             :     }
      82           0 :     return ((PyFunctionObject *) op) -> func_code;
      83             : }
      84             : 
      85             : PyObject *
      86           0 : PyFunction_GetGlobals(PyObject *op)
      87             : {
      88           0 :     if (!PyFunction_Check(op)) {
      89           0 :         PyErr_BadInternalCall();
      90           0 :         return NULL;
      91             :     }
      92           0 :     return ((PyFunctionObject *) op) -> func_globals;
      93             : }
      94             : 
      95             : PyObject *
      96           0 : PyFunction_GetModule(PyObject *op)
      97             : {
      98           0 :     if (!PyFunction_Check(op)) {
      99           0 :         PyErr_BadInternalCall();
     100           0 :         return NULL;
     101             :     }
     102           0 :     return ((PyFunctionObject *) op) -> func_module;
     103             : }
     104             : 
     105             : PyObject *
     106           0 : PyFunction_GetDefaults(PyObject *op)
     107             : {
     108           0 :     if (!PyFunction_Check(op)) {
     109           0 :         PyErr_BadInternalCall();
     110           0 :         return NULL;
     111             :     }
     112           0 :     return ((PyFunctionObject *) op) -> func_defaults;
     113             : }
     114             : 
     115             : int
     116         302 : PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
     117             : {
     118         302 :     if (!PyFunction_Check(op)) {
     119           0 :         PyErr_BadInternalCall();
     120           0 :         return -1;
     121             :     }
     122         302 :     if (defaults == Py_None)
     123           0 :         defaults = NULL;
     124         302 :     else if (defaults && PyTuple_Check(defaults)) {
     125         302 :         Py_INCREF(defaults);
     126             :     }
     127             :     else {
     128           0 :         PyErr_SetString(PyExc_SystemError, "non-tuple default args");
     129           0 :         return -1;
     130             :     }
     131         302 :     Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
     132         302 :     ((PyFunctionObject *) op) -> func_defaults = defaults;
     133         302 :     return 0;
     134             : }
     135             : 
     136             : PyObject *
     137           0 : PyFunction_GetKwDefaults(PyObject *op)
     138             : {
     139           0 :     if (!PyFunction_Check(op)) {
     140           0 :         PyErr_BadInternalCall();
     141           0 :         return NULL;
     142             :     }
     143           0 :     return ((PyFunctionObject *) op) -> func_kwdefaults;
     144             : }
     145             : 
     146             : int
     147           5 : PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
     148             : {
     149           5 :     if (!PyFunction_Check(op)) {
     150           0 :         PyErr_BadInternalCall();
     151           0 :         return -1;
     152             :     }
     153           5 :     if (defaults == Py_None)
     154           0 :         defaults = NULL;
     155           5 :     else if (defaults && PyDict_Check(defaults)) {
     156           5 :         Py_INCREF(defaults);
     157             :     }
     158             :     else {
     159           0 :         PyErr_SetString(PyExc_SystemError,
     160             :                         "non-dict keyword only default args");
     161           0 :         return -1;
     162             :     }
     163           5 :     Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
     164           5 :     ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
     165           5 :     return 0;
     166             : }
     167             : 
     168             : PyObject *
     169           0 : PyFunction_GetClosure(PyObject *op)
     170             : {
     171           0 :     if (!PyFunction_Check(op)) {
     172           0 :         PyErr_BadInternalCall();
     173           0 :         return NULL;
     174             :     }
     175           0 :     return ((PyFunctionObject *) op) -> func_closure;
     176             : }
     177             : 
     178             : int
     179         377 : PyFunction_SetClosure(PyObject *op, PyObject *closure)
     180             : {
     181         377 :     if (!PyFunction_Check(op)) {
     182           0 :         PyErr_BadInternalCall();
     183           0 :         return -1;
     184             :     }
     185         377 :     if (closure == Py_None)
     186           0 :         closure = NULL;
     187         377 :     else if (PyTuple_Check(closure)) {
     188         377 :         Py_INCREF(closure);
     189             :     }
     190             :     else {
     191           0 :         PyErr_Format(PyExc_SystemError,
     192             :                      "expected tuple for closure, got '%.100s'",
     193           0 :                      closure->ob_type->tp_name);
     194           0 :         return -1;
     195             :     }
     196         377 :     Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
     197         377 :     ((PyFunctionObject *) op) -> func_closure = closure;
     198         377 :     return 0;
     199             : }
     200             : 
     201             : PyObject *
     202           0 : PyFunction_GetAnnotations(PyObject *op)
     203             : {
     204           0 :     if (!PyFunction_Check(op)) {
     205           0 :         PyErr_BadInternalCall();
     206           0 :         return NULL;
     207             :     }
     208           0 :     return ((PyFunctionObject *) op) -> func_annotations;
     209             : }
     210             : 
     211             : int
     212           0 : PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
     213             : {
     214           0 :     if (!PyFunction_Check(op)) {
     215           0 :         PyErr_BadInternalCall();
     216           0 :         return -1;
     217             :     }
     218           0 :     if (annotations == Py_None)
     219           0 :         annotations = NULL;
     220           0 :     else if (annotations && PyDict_Check(annotations)) {
     221           0 :         Py_INCREF(annotations);
     222             :     }
     223             :     else {
     224           0 :         PyErr_SetString(PyExc_SystemError,
     225             :                         "non-dict annotations");
     226           0 :         return -1;
     227             :     }
     228           0 :     Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
     229           0 :     ((PyFunctionObject *) op) -> func_annotations = annotations;
     230           0 :     return 0;
     231             : }
     232             : 
     233             : /* Methods */
     234             : 
     235             : #define OFF(x) offsetof(PyFunctionObject, x)
     236             : 
     237             : static PyMemberDef func_memberlist[] = {
     238             :     {"__closure__",   T_OBJECT,     OFF(func_closure),
     239             :      RESTRICTED|READONLY},
     240             :     {"__doc__",       T_OBJECT,     OFF(func_doc), PY_WRITE_RESTRICTED},
     241             :     {"__globals__",   T_OBJECT,     OFF(func_globals),
     242             :      RESTRICTED|READONLY},
     243             :     {"__module__",    T_OBJECT,     OFF(func_module), PY_WRITE_RESTRICTED},
     244             :     {NULL}  /* Sentinel */
     245             : };
     246             : 
     247             : static PyObject *
     248           1 : func_get_code(PyFunctionObject *op)
     249             : {
     250           1 :     Py_INCREF(op->func_code);
     251           1 :     return op->func_code;
     252             : }
     253             : 
     254             : static int
     255           0 : func_set_code(PyFunctionObject *op, PyObject *value)
     256             : {
     257             :     PyObject *tmp;
     258             :     Py_ssize_t nfree, nclosure;
     259             : 
     260             :     /* Not legal to del f.func_code or to set it to anything
     261             :      * other than a code object. */
     262           0 :     if (value == NULL || !PyCode_Check(value)) {
     263           0 :         PyErr_SetString(PyExc_TypeError,
     264             :                         "__code__ must be set to a code object");
     265           0 :         return -1;
     266             :     }
     267           0 :     nfree = PyCode_GetNumFree((PyCodeObject *)value);
     268           0 :     nclosure = (op->func_closure == NULL ? 0 :
     269           0 :             PyTuple_GET_SIZE(op->func_closure));
     270           0 :     if (nclosure != nfree) {
     271           0 :         PyErr_Format(PyExc_ValueError,
     272             :                      "%U() requires a code object with %zd free vars,"
     273             :                      " not %zd",
     274             :                      op->func_name,
     275             :                      nclosure, nfree);
     276           0 :         return -1;
     277             :     }
     278           0 :     tmp = op->func_code;
     279           0 :     Py_INCREF(value);
     280           0 :     op->func_code = value;
     281           0 :     Py_DECREF(tmp);
     282           0 :     return 0;
     283             : }
     284             : 
     285             : static PyObject *
     286          42 : func_get_name(PyFunctionObject *op)
     287             : {
     288          42 :     Py_INCREF(op->func_name);
     289          42 :     return op->func_name;
     290             : }
     291             : 
     292             : static int
     293          24 : func_set_name(PyFunctionObject *op, PyObject *value)
     294             : {
     295             :     PyObject *tmp;
     296             : 
     297             :     /* Not legal to del f.func_name or to set it to anything
     298             :      * other than a string object. */
     299          24 :     if (value == NULL || !PyUnicode_Check(value)) {
     300           0 :         PyErr_SetString(PyExc_TypeError,
     301             :                         "__name__ must be set to a string object");
     302           0 :         return -1;
     303             :     }
     304          24 :     tmp = op->func_name;
     305          24 :     Py_INCREF(value);
     306          24 :     op->func_name = value;
     307          24 :     Py_DECREF(tmp);
     308          24 :     return 0;
     309             : }
     310             : 
     311             : static PyObject *
     312          40 : func_get_qualname(PyFunctionObject *op)
     313             : {
     314          40 :     Py_INCREF(op->func_qualname);
     315          40 :     return op->func_qualname;
     316             : }
     317             : 
     318             : static int
     319          22 : func_set_qualname(PyFunctionObject *op, PyObject *value)
     320             : {
     321             :     PyObject *tmp;
     322             : 
     323             :     /* Not legal to del f.__qualname__ or to set it to anything
     324             :      * other than a string object. */
     325          22 :     if (value == NULL || !PyUnicode_Check(value)) {
     326           0 :         PyErr_SetString(PyExc_TypeError,
     327             :                         "__qualname__ must be set to a string object");
     328           0 :         return -1;
     329             :     }
     330          22 :     tmp = op->func_qualname;
     331          22 :     Py_INCREF(value);
     332          22 :     op->func_qualname = value;
     333          22 :     Py_DECREF(tmp);
     334          22 :     return 0;
     335             : }
     336             : 
     337             : static PyObject *
     338           0 : func_get_defaults(PyFunctionObject *op)
     339             : {
     340           0 :     if (op->func_defaults == NULL) {
     341           0 :         Py_INCREF(Py_None);
     342           0 :         return Py_None;
     343             :     }
     344           0 :     Py_INCREF(op->func_defaults);
     345           0 :     return op->func_defaults;
     346             : }
     347             : 
     348             : static int
     349           0 : func_set_defaults(PyFunctionObject *op, PyObject *value)
     350             : {
     351             :     PyObject *tmp;
     352             : 
     353             :     /* Legal to del f.func_defaults.
     354             :      * Can only set func_defaults to NULL or a tuple. */
     355           0 :     if (value == Py_None)
     356           0 :         value = NULL;
     357           0 :     if (value != NULL && !PyTuple_Check(value)) {
     358           0 :         PyErr_SetString(PyExc_TypeError,
     359             :                         "__defaults__ must be set to a tuple object");
     360           0 :         return -1;
     361             :     }
     362           0 :     tmp = op->func_defaults;
     363           0 :     Py_XINCREF(value);
     364           0 :     op->func_defaults = value;
     365           0 :     Py_XDECREF(tmp);
     366           0 :     return 0;
     367             : }
     368             : 
     369             : static PyObject *
     370           0 : func_get_kwdefaults(PyFunctionObject *op)
     371             : {
     372           0 :     if (op->func_kwdefaults == NULL) {
     373           0 :         Py_INCREF(Py_None);
     374           0 :         return Py_None;
     375             :     }
     376           0 :     Py_INCREF(op->func_kwdefaults);
     377           0 :     return op->func_kwdefaults;
     378             : }
     379             : 
     380             : static int
     381           0 : func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
     382             : {
     383             :     PyObject *tmp;
     384             : 
     385           0 :     if (value == Py_None)
     386           0 :         value = NULL;
     387             :     /* Legal to del f.func_kwdefaults.
     388             :      * Can only set func_kwdefaults to NULL or a dict. */
     389           0 :     if (value != NULL && !PyDict_Check(value)) {
     390           0 :         PyErr_SetString(PyExc_TypeError,
     391             :             "__kwdefaults__ must be set to a dict object");
     392           0 :         return -1;
     393             :     }
     394           0 :     tmp = op->func_kwdefaults;
     395           0 :     Py_XINCREF(value);
     396           0 :     op->func_kwdefaults = value;
     397           0 :     Py_XDECREF(tmp);
     398           0 :     return 0;
     399             : }
     400             : 
     401             : static PyObject *
     402           4 : func_get_annotations(PyFunctionObject *op)
     403             : {
     404           4 :     if (op->func_annotations == NULL) {
     405           4 :         op->func_annotations = PyDict_New();
     406           4 :         if (op->func_annotations == NULL)
     407           0 :             return NULL;
     408             :     }
     409           4 :     Py_INCREF(op->func_annotations);
     410           4 :     return op->func_annotations;
     411             : }
     412             : 
     413             : static int
     414           4 : func_set_annotations(PyFunctionObject *op, PyObject *value)
     415             : {
     416             :     PyObject *tmp;
     417             : 
     418           4 :     if (value == Py_None)
     419           0 :         value = NULL;
     420             :     /* Legal to del f.func_annotations.
     421             :      * Can only set func_annotations to NULL (through C api)
     422             :      * or a dict. */
     423           4 :     if (value != NULL && !PyDict_Check(value)) {
     424           0 :         PyErr_SetString(PyExc_TypeError,
     425             :             "__annotations__ must be set to a dict object");
     426           0 :         return -1;
     427             :     }
     428           4 :     tmp = op->func_annotations;
     429           4 :     Py_XINCREF(value);
     430           4 :     op->func_annotations = value;
     431           4 :     Py_XDECREF(tmp);
     432           4 :     return 0;
     433             : }
     434             : 
     435             : static PyGetSetDef func_getsetlist[] = {
     436             :     {"__code__", (getter)func_get_code, (setter)func_set_code},
     437             :     {"__defaults__", (getter)func_get_defaults,
     438             :      (setter)func_set_defaults},
     439             :     {"__kwdefaults__", (getter)func_get_kwdefaults,
     440             :      (setter)func_set_kwdefaults},
     441             :     {"__annotations__", (getter)func_get_annotations,
     442             :      (setter)func_set_annotations},
     443             :     {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
     444             :     {"__name__", (getter)func_get_name, (setter)func_set_name},
     445             :     {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
     446             :     {NULL} /* Sentinel */
     447             : };
     448             : 
     449             : PyDoc_STRVAR(func_doc,
     450             : "function(code, globals[, name[, argdefs[, closure]]])\n\
     451             : \n\
     452             : Create a function object from a code object and a dictionary.\n\
     453             : The optional name string overrides the name from the code object.\n\
     454             : The optional argdefs tuple specifies the default argument values.\n\
     455             : The optional closure tuple supplies the bindings for free variables.");
     456             : 
     457             : /* func_new() maintains the following invariants for closures.  The
     458             :    closure must correspond to the free variables of the code object.
     459             : 
     460             :    if len(code.co_freevars) == 0:
     461             :        closure = NULL
     462             :    else:
     463             :        len(closure) == len(code.co_freevars)
     464             :    for every elt in closure, type(elt) == cell
     465             : */
     466             : 
     467             : static PyObject *
     468           0 : func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
     469             : {
     470             :     PyCodeObject *code;
     471             :     PyObject *globals;
     472           0 :     PyObject *name = Py_None;
     473           0 :     PyObject *defaults = Py_None;
     474           0 :     PyObject *closure = Py_None;
     475             :     PyFunctionObject *newfunc;
     476             :     Py_ssize_t nfree, nclosure;
     477             :     static char *kwlist[] = {"code", "globals", "name",
     478             :                              "argdefs", "closure", 0};
     479             : 
     480           0 :     if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
     481             :                           kwlist,
     482             :                           &PyCode_Type, &code,
     483             :                           &PyDict_Type, &globals,
     484             :                           &name, &defaults, &closure))
     485           0 :         return NULL;
     486           0 :     if (name != Py_None && !PyUnicode_Check(name)) {
     487           0 :         PyErr_SetString(PyExc_TypeError,
     488             :                         "arg 3 (name) must be None or string");
     489           0 :         return NULL;
     490             :     }
     491           0 :     if (defaults != Py_None && !PyTuple_Check(defaults)) {
     492           0 :         PyErr_SetString(PyExc_TypeError,
     493             :                         "arg 4 (defaults) must be None or tuple");
     494           0 :         return NULL;
     495             :     }
     496           0 :     nfree = PyTuple_GET_SIZE(code->co_freevars);
     497           0 :     if (!PyTuple_Check(closure)) {
     498           0 :         if (nfree && closure == Py_None) {
     499           0 :             PyErr_SetString(PyExc_TypeError,
     500             :                             "arg 5 (closure) must be tuple");
     501           0 :             return NULL;
     502             :         }
     503           0 :         else if (closure != Py_None) {
     504           0 :             PyErr_SetString(PyExc_TypeError,
     505             :                 "arg 5 (closure) must be None or tuple");
     506           0 :             return NULL;
     507             :         }
     508             :     }
     509             : 
     510             :     /* check that the closure is well-formed */
     511           0 :     nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
     512           0 :     if (nfree != nclosure)
     513           0 :         return PyErr_Format(PyExc_ValueError,
     514             :                             "%U requires closure of length %zd, not %zd",
     515           0 :                             code->co_name, nfree, nclosure);
     516           0 :     if (nclosure) {
     517             :         Py_ssize_t i;
     518           0 :         for (i = 0; i < nclosure; i++) {
     519           0 :             PyObject *o = PyTuple_GET_ITEM(closure, i);
     520           0 :             if (!PyCell_Check(o)) {
     521           0 :                 return PyErr_Format(PyExc_TypeError,
     522             :                     "arg 5 (closure) expected cell, found %s",
     523           0 :                                     o->ob_type->tp_name);
     524             :             }
     525             :         }
     526             :     }
     527             : 
     528           0 :     newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
     529             :                                                  globals);
     530           0 :     if (newfunc == NULL)
     531           0 :         return NULL;
     532             : 
     533           0 :     if (name != Py_None) {
     534           0 :         Py_INCREF(name);
     535           0 :         Py_DECREF(newfunc->func_name);
     536           0 :         newfunc->func_name = name;
     537             :     }
     538           0 :     if (defaults != Py_None) {
     539           0 :         Py_INCREF(defaults);
     540           0 :         newfunc->func_defaults  = defaults;
     541             :     }
     542           0 :     if (closure != Py_None) {
     543           0 :         Py_INCREF(closure);
     544           0 :         newfunc->func_closure = closure;
     545             :     }
     546             : 
     547           0 :     return (PyObject *)newfunc;
     548             : }
     549             : 
     550             : static void
     551         554 : func_dealloc(PyFunctionObject *op)
     552             : {
     553         554 :     _PyObject_GC_UNTRACK(op);
     554         554 :     if (op->func_weakreflist != NULL)
     555           0 :         PyObject_ClearWeakRefs((PyObject *) op);
     556         554 :     Py_DECREF(op->func_code);
     557         554 :     Py_DECREF(op->func_globals);
     558         554 :     Py_XDECREF(op->func_module);
     559         554 :     Py_DECREF(op->func_name);
     560         554 :     Py_XDECREF(op->func_defaults);
     561         554 :     Py_XDECREF(op->func_kwdefaults);
     562         554 :     Py_XDECREF(op->func_doc);
     563         554 :     Py_XDECREF(op->func_dict);
     564         554 :     Py_XDECREF(op->func_closure);
     565         554 :     Py_XDECREF(op->func_annotations);
     566         554 :     Py_XDECREF(op->func_qualname);
     567         554 :     PyObject_GC_Del(op);
     568         554 : }
     569             : 
     570             : static PyObject*
     571           0 : func_repr(PyFunctionObject *op)
     572             : {
     573           0 :     return PyUnicode_FromFormat("<function %U at %p>",
     574             :                                op->func_qualname, op);
     575             : }
     576             : 
     577             : static int
     578        3428 : func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
     579             : {
     580        3428 :     Py_VISIT(f->func_code);
     581        3428 :     Py_VISIT(f->func_globals);
     582        3428 :     Py_VISIT(f->func_module);
     583        3428 :     Py_VISIT(f->func_defaults);
     584        3428 :     Py_VISIT(f->func_kwdefaults);
     585        3428 :     Py_VISIT(f->func_doc);
     586        3428 :     Py_VISIT(f->func_name);
     587        3428 :     Py_VISIT(f->func_dict);
     588        3428 :     Py_VISIT(f->func_closure);
     589        3428 :     Py_VISIT(f->func_annotations);
     590        3428 :     Py_VISIT(f->func_qualname);
     591        3428 :     return 0;
     592             : }
     593             : 
     594             : static PyObject *
     595        7272 : function_call(PyObject *func, PyObject *arg, PyObject *kw)
     596             : {
     597             :     PyObject *result;
     598             :     PyObject *argdefs;
     599        7272 :     PyObject *kwtuple = NULL;
     600             :     PyObject **d, **k;
     601             :     Py_ssize_t nk, nd;
     602             : 
     603        7272 :     argdefs = PyFunction_GET_DEFAULTS(func);
     604        7272 :     if (argdefs != NULL && PyTuple_Check(argdefs)) {
     605         929 :         d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
     606         929 :         nd = PyTuple_GET_SIZE(argdefs);
     607             :     }
     608             :     else {
     609        6343 :         d = NULL;
     610        6343 :         nd = 0;
     611             :     }
     612             : 
     613        7756 :     if (kw != NULL && PyDict_Check(kw)) {
     614             :         Py_ssize_t pos, i;
     615         484 :         nk = PyDict_Size(kw);
     616         484 :         kwtuple = PyTuple_New(2*nk);
     617         484 :         if (kwtuple == NULL)
     618           0 :             return NULL;
     619         484 :         k = &PyTuple_GET_ITEM(kwtuple, 0);
     620         484 :         pos = i = 0;
     621         978 :         while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
     622          10 :             Py_INCREF(k[i]);
     623          10 :             Py_INCREF(k[i+1]);
     624          10 :             i += 2;
     625             :         }
     626         484 :         nk = i/2;
     627             :     }
     628             :     else {
     629        6788 :         k = NULL;
     630        6788 :         nk = 0;
     631             :     }
     632             : 
     633        7272 :     result = PyEval_EvalCodeEx(
     634             :         PyFunction_GET_CODE(func),
     635             :         PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
     636             :         &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
     637             :         k, nk, d, nd,
     638             :         PyFunction_GET_KW_DEFAULTS(func),
     639             :         PyFunction_GET_CLOSURE(func));
     640             : 
     641        7272 :     Py_XDECREF(kwtuple);
     642             : 
     643        7272 :     return result;
     644             : }
     645             : 
     646             : /* Bind a function to an object */
     647             : static PyObject *
     648       14772 : func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
     649             : {
     650       14772 :     if (obj == Py_None || obj == NULL) {
     651          66 :         Py_INCREF(func);
     652          66 :         return func;
     653             :     }
     654       14706 :     return PyMethod_New(func, obj);
     655             : }
     656             : 
     657             : PyTypeObject PyFunction_Type = {
     658             :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     659             :     "function",
     660             :     sizeof(PyFunctionObject),
     661             :     0,
     662             :     (destructor)func_dealloc,                   /* tp_dealloc */
     663             :     0,                                          /* tp_print */
     664             :     0,                                          /* tp_getattr */
     665             :     0,                                          /* tp_setattr */
     666             :     0,                                          /* tp_reserved */
     667             :     (reprfunc)func_repr,                        /* tp_repr */
     668             :     0,                                          /* tp_as_number */
     669             :     0,                                          /* tp_as_sequence */
     670             :     0,                                          /* tp_as_mapping */
     671             :     0,                                          /* tp_hash */
     672             :     function_call,                              /* tp_call */
     673             :     0,                                          /* tp_str */
     674             :     0,                                          /* tp_getattro */
     675             :     0,                                          /* tp_setattro */
     676             :     0,                                          /* tp_as_buffer */
     677             :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     678             :     func_doc,                                   /* tp_doc */
     679             :     (traverseproc)func_traverse,                /* tp_traverse */
     680             :     0,                                          /* tp_clear */
     681             :     0,                                          /* tp_richcompare */
     682             :     offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
     683             :     0,                                          /* tp_iter */
     684             :     0,                                          /* tp_iternext */
     685             :     0,                                          /* tp_methods */
     686             :     func_memberlist,                            /* tp_members */
     687             :     func_getsetlist,                            /* tp_getset */
     688             :     0,                                          /* tp_base */
     689             :     0,                                          /* tp_dict */
     690             :     func_descr_get,                             /* tp_descr_get */
     691             :     0,                                          /* tp_descr_set */
     692             :     offsetof(PyFunctionObject, func_dict),      /* tp_dictoffset */
     693             :     0,                                          /* tp_init */
     694             :     0,                                          /* tp_alloc */
     695             :     func_new,                                   /* tp_new */
     696             : };
     697             : 
     698             : 
     699             : /* Class method object */
     700             : 
     701             : /* A class method receives the class as implicit first argument,
     702             :    just like an instance method receives the instance.
     703             :    To declare a class method, use this idiom:
     704             : 
     705             :      class C:
     706             :      def f(cls, arg1, arg2, ...): ...
     707             :      f = classmethod(f)
     708             : 
     709             :    It can be called either on the class (e.g. C.f()) or on an instance
     710             :    (e.g. C().f()); the instance is ignored except for its class.
     711             :    If a class method is called for a derived class, the derived class
     712             :    object is passed as the implied first argument.
     713             : 
     714             :    Class methods are different than C++ or Java static methods.
     715             :    If you want those, see static methods below.
     716             : */
     717             : 
     718             : typedef struct {
     719             :     PyObject_HEAD
     720             :     PyObject *cm_callable;
     721             :     PyObject *cm_dict;
     722             : } classmethod;
     723             : 
     724             : static void
     725           0 : cm_dealloc(classmethod *cm)
     726             : {
     727           0 :     _PyObject_GC_UNTRACK((PyObject *)cm);
     728           0 :     Py_XDECREF(cm->cm_callable);
     729           0 :     Py_XDECREF(cm->cm_dict);
     730           0 :     Py_TYPE(cm)->tp_free((PyObject *)cm);
     731           0 : }
     732             : 
     733             : static int
     734         138 : cm_traverse(classmethod *cm, visitproc visit, void *arg)
     735             : {
     736         138 :     Py_VISIT(cm->cm_callable);
     737         138 :     Py_VISIT(cm->cm_dict);
     738         138 :     return 0;
     739             : }
     740             : 
     741             : static int
     742           0 : cm_clear(classmethod *cm)
     743             : {
     744           0 :     Py_CLEAR(cm->cm_callable);
     745           0 :     Py_CLEAR(cm->cm_dict);
     746           0 :     return 0;
     747             : }
     748             : 
     749             : 
     750             : static PyObject *
     751         673 : cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
     752             : {
     753         673 :     classmethod *cm = (classmethod *)self;
     754             : 
     755         673 :     if (cm->cm_callable == NULL) {
     756           0 :         PyErr_SetString(PyExc_RuntimeError,
     757             :                         "uninitialized classmethod object");
     758           0 :         return NULL;
     759             :     }
     760         673 :     if (type == NULL)
     761           0 :         type = (PyObject *)(Py_TYPE(obj));
     762         673 :     return PyMethod_New(cm->cm_callable, type);
     763             : }
     764             : 
     765             : static int
     766          37 : cm_init(PyObject *self, PyObject *args, PyObject *kwds)
     767             : {
     768          37 :     classmethod *cm = (classmethod *)self;
     769             :     PyObject *callable;
     770             : 
     771          37 :     if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
     772           0 :         return -1;
     773          37 :     if (!_PyArg_NoKeywords("classmethod", kwds))
     774           0 :         return -1;
     775          37 :     Py_INCREF(callable);
     776          37 :     cm->cm_callable = callable;
     777          37 :     return 0;
     778             : }
     779             : 
     780             : static PyMemberDef cm_memberlist[] = {
     781             :     {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
     782             :     {NULL}  /* Sentinel */
     783             : };
     784             : 
     785             : static PyObject *
     786          11 : cm_get___isabstractmethod__(classmethod *cm, void *closure)
     787             : {
     788          11 :     int res = _PyObject_IsAbstract(cm->cm_callable);
     789          11 :     if (res == -1) {
     790           0 :         return NULL;
     791             :     }
     792          11 :     else if (res) {
     793           0 :         Py_RETURN_TRUE;
     794             :     }
     795          11 :     Py_RETURN_FALSE;
     796             : }
     797             : 
     798             : static PyGetSetDef cm_getsetlist[] = {
     799             :     {"__isabstractmethod__",
     800             :      (getter)cm_get___isabstractmethod__, NULL,
     801             :      NULL,
     802             :      NULL},
     803             :     {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
     804             :     {NULL} /* Sentinel */
     805             : };
     806             : 
     807             : PyDoc_STRVAR(classmethod_doc,
     808             : "classmethod(function) -> method\n\
     809             : \n\
     810             : Convert a function to be a class method.\n\
     811             : \n\
     812             : A class method receives the class as implicit first argument,\n\
     813             : just like an instance method receives the instance.\n\
     814             : To declare a class method, use this idiom:\n\
     815             : \n\
     816             :   class C:\n\
     817             :       def f(cls, arg1, arg2, ...): ...\n\
     818             :       f = classmethod(f)\n\
     819             : \n\
     820             : It can be called either on the class (e.g. C.f()) or on an instance\n\
     821             : (e.g. C().f()).  The instance is ignored except for its class.\n\
     822             : If a class method is called for a derived class, the derived class\n\
     823             : object is passed as the implied first argument.\n\
     824             : \n\
     825             : Class methods are different than C++ or Java static methods.\n\
     826             : If you want those, see the staticmethod builtin.");
     827             : 
     828             : PyTypeObject PyClassMethod_Type = {
     829             :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     830             :     "classmethod",
     831             :     sizeof(classmethod),
     832             :     0,
     833             :     (destructor)cm_dealloc,                     /* tp_dealloc */
     834             :     0,                                          /* tp_print */
     835             :     0,                                          /* tp_getattr */
     836             :     0,                                          /* tp_setattr */
     837             :     0,                                          /* tp_reserved */
     838             :     0,                                          /* tp_repr */
     839             :     0,                                          /* tp_as_number */
     840             :     0,                                          /* tp_as_sequence */
     841             :     0,                                          /* tp_as_mapping */
     842             :     0,                                          /* tp_hash */
     843             :     0,                                          /* tp_call */
     844             :     0,                                          /* tp_str */
     845             :     0,                                          /* tp_getattro */
     846             :     0,                                          /* tp_setattro */
     847             :     0,                                          /* tp_as_buffer */
     848             :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
     849             :     classmethod_doc,                            /* tp_doc */
     850             :     (traverseproc)cm_traverse,                  /* tp_traverse */
     851             :     (inquiry)cm_clear,                          /* tp_clear */
     852             :     0,                                          /* tp_richcompare */
     853             :     0,                                          /* tp_weaklistoffset */
     854             :     0,                                          /* tp_iter */
     855             :     0,                                          /* tp_iternext */
     856             :     0,                                          /* tp_methods */
     857             :     cm_memberlist,              /* tp_members */
     858             :     cm_getsetlist,                              /* tp_getset */
     859             :     0,                                          /* tp_base */
     860             :     0,                                          /* tp_dict */
     861             :     cm_descr_get,                               /* tp_descr_get */
     862             :     0,                                          /* tp_descr_set */
     863             :     offsetof(classmethod, cm_dict),             /* tp_dictoffset */
     864             :     cm_init,                                    /* tp_init */
     865             :     PyType_GenericAlloc,                        /* tp_alloc */
     866             :     PyType_GenericNew,                          /* tp_new */
     867             :     PyObject_GC_Del,                            /* tp_free */
     868             : };
     869             : 
     870             : PyObject *
     871           0 : PyClassMethod_New(PyObject *callable)
     872             : {
     873           0 :     classmethod *cm = (classmethod *)
     874             :         PyType_GenericAlloc(&PyClassMethod_Type, 0);
     875           0 :     if (cm != NULL) {
     876           0 :         Py_INCREF(callable);
     877           0 :         cm->cm_callable = callable;
     878             :     }
     879           0 :     return (PyObject *)cm;
     880             : }
     881             : 
     882             : 
     883             : /* Static method object */
     884             : 
     885             : /* A static method does not receive an implicit first argument.
     886             :    To declare a static method, use this idiom:
     887             : 
     888             :      class C:
     889             :      def f(arg1, arg2, ...): ...
     890             :      f = staticmethod(f)
     891             : 
     892             :    It can be called either on the class (e.g. C.f()) or on an instance
     893             :    (e.g. C().f()); the instance is ignored except for its class.
     894             : 
     895             :    Static methods in Python are similar to those found in Java or C++.
     896             :    For a more advanced concept, see class methods above.
     897             : */
     898             : 
     899             : typedef struct {
     900             :     PyObject_HEAD
     901             :     PyObject *sm_callable;
     902             :     PyObject *sm_dict;
     903             : } staticmethod;
     904             : 
     905             : static void
     906           0 : sm_dealloc(staticmethod *sm)
     907             : {
     908           0 :     _PyObject_GC_UNTRACK((PyObject *)sm);
     909           0 :     Py_XDECREF(sm->sm_callable);
     910           0 :     Py_XDECREF(sm->sm_dict);
     911           0 :     Py_TYPE(sm)->tp_free((PyObject *)sm);
     912           0 : }
     913             : 
     914             : static int
     915          30 : sm_traverse(staticmethod *sm, visitproc visit, void *arg)
     916             : {
     917          30 :     Py_VISIT(sm->sm_callable);
     918          30 :     Py_VISIT(sm->sm_dict);
     919          30 :     return 0;
     920             : }
     921             : 
     922             : static int
     923           0 : sm_clear(staticmethod *sm)
     924             : {
     925           0 :     Py_CLEAR(sm->sm_callable);
     926           0 :     Py_CLEAR(sm->sm_dict);
     927           0 :     return 0;
     928             : }
     929             : 
     930             : static PyObject *
     931          30 : sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
     932             : {
     933          30 :     staticmethod *sm = (staticmethod *)self;
     934             : 
     935          30 :     if (sm->sm_callable == NULL) {
     936           0 :         PyErr_SetString(PyExc_RuntimeError,
     937             :                         "uninitialized staticmethod object");
     938           0 :         return NULL;
     939             :     }
     940          30 :     Py_INCREF(sm->sm_callable);
     941          30 :     return sm->sm_callable;
     942             : }
     943             : 
     944             : static int
     945           0 : sm_init(PyObject *self, PyObject *args, PyObject *kwds)
     946             : {
     947           0 :     staticmethod *sm = (staticmethod *)self;
     948             :     PyObject *callable;
     949             : 
     950           0 :     if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
     951           0 :         return -1;
     952           0 :     if (!_PyArg_NoKeywords("staticmethod", kwds))
     953           0 :         return -1;
     954           0 :     Py_INCREF(callable);
     955           0 :     sm->sm_callable = callable;
     956           0 :     return 0;
     957             : }
     958             : 
     959             : static PyMemberDef sm_memberlist[] = {
     960             :     {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
     961             :     {NULL}  /* Sentinel */
     962             : };
     963             : 
     964             : static PyObject *
     965           0 : sm_get___isabstractmethod__(staticmethod *sm, void *closure)
     966             : {
     967           0 :     int res = _PyObject_IsAbstract(sm->sm_callable);
     968           0 :     if (res == -1) {
     969           0 :         return NULL;
     970             :     }
     971           0 :     else if (res) {
     972           0 :         Py_RETURN_TRUE;
     973             :     }
     974           0 :     Py_RETURN_FALSE;
     975             : }
     976             : 
     977             : static PyGetSetDef sm_getsetlist[] = {
     978             :     {"__isabstractmethod__",
     979             :      (getter)sm_get___isabstractmethod__, NULL,
     980             :      NULL,
     981             :      NULL},
     982             :     {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
     983             :     {NULL} /* Sentinel */
     984             : };
     985             : 
     986             : PyDoc_STRVAR(staticmethod_doc,
     987             : "staticmethod(function) -> method\n\
     988             : \n\
     989             : Convert a function to be a static method.\n\
     990             : \n\
     991             : A static method does not receive an implicit first argument.\n\
     992             : To declare a static method, use this idiom:\n\
     993             : \n\
     994             :      class C:\n\
     995             :      def f(arg1, arg2, ...): ...\n\
     996             :      f = staticmethod(f)\n\
     997             : \n\
     998             : It can be called either on the class (e.g. C.f()) or on an instance\n\
     999             : (e.g. C().f()).  The instance is ignored except for its class.\n\
    1000             : \n\
    1001             : Static methods in Python are similar to those found in Java or C++.\n\
    1002             : For a more advanced concept, see the classmethod builtin.");
    1003             : 
    1004             : PyTypeObject PyStaticMethod_Type = {
    1005             :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1006             :     "staticmethod",
    1007             :     sizeof(staticmethod),
    1008             :     0,
    1009             :     (destructor)sm_dealloc,                     /* tp_dealloc */
    1010             :     0,                                          /* tp_print */
    1011             :     0,                                          /* tp_getattr */
    1012             :     0,                                          /* tp_setattr */
    1013             :     0,                                          /* tp_reserved */
    1014             :     0,                                          /* tp_repr */
    1015             :     0,                                          /* tp_as_number */
    1016             :     0,                                          /* tp_as_sequence */
    1017             :     0,                                          /* tp_as_mapping */
    1018             :     0,                                          /* tp_hash */
    1019             :     0,                                          /* tp_call */
    1020             :     0,                                          /* tp_str */
    1021             :     0,                                          /* tp_getattro */
    1022             :     0,                                          /* tp_setattro */
    1023             :     0,                                          /* tp_as_buffer */
    1024             :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
    1025             :     staticmethod_doc,                           /* tp_doc */
    1026             :     (traverseproc)sm_traverse,                  /* tp_traverse */
    1027             :     (inquiry)sm_clear,                          /* tp_clear */
    1028             :     0,                                          /* tp_richcompare */
    1029             :     0,                                          /* tp_weaklistoffset */
    1030             :     0,                                          /* tp_iter */
    1031             :     0,                                          /* tp_iternext */
    1032             :     0,                                          /* tp_methods */
    1033             :     sm_memberlist,              /* tp_members */
    1034             :     sm_getsetlist,                              /* tp_getset */
    1035             :     0,                                          /* tp_base */
    1036             :     0,                                          /* tp_dict */
    1037             :     sm_descr_get,                               /* tp_descr_get */
    1038             :     0,                                          /* tp_descr_set */
    1039             :     offsetof(staticmethod, sm_dict),            /* tp_dictoffset */
    1040             :     sm_init,                                    /* tp_init */
    1041             :     PyType_GenericAlloc,                        /* tp_alloc */
    1042             :     PyType_GenericNew,                          /* tp_new */
    1043             :     PyObject_GC_Del,                            /* tp_free */
    1044             : };
    1045             : 
    1046             : PyObject *
    1047           9 : PyStaticMethod_New(PyObject *callable)
    1048             : {
    1049           9 :     staticmethod *sm = (staticmethod *)
    1050             :         PyType_GenericAlloc(&PyStaticMethod_Type, 0);
    1051           9 :     if (sm != NULL) {
    1052           9 :         Py_INCREF(callable);
    1053           9 :         sm->sm_callable = callable;
    1054             :     }
    1055           9 :     return (PyObject *)sm;
    1056             : }

Generated by: LCOV version 1.10