Coverage Report

Created: 2022-07-08 09:39

/home/mdboom/Work/builds/cpython/Modules/symtablemodule.c
Line
Count
Source (jump to first uncovered line)
1
#include "Python.h"
2
#include "pycore_symtable.h"      // struct symtable
3
4
#include "clinic/symtablemodule.c.h"
5
/*[clinic input]
6
module _symtable
7
[clinic start generated code]*/
8
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f4685845a7100605]*/
9
10
11
/*[clinic input]
12
_symtable.symtable
13
14
    source:    object
15
    filename:  object(converter='PyUnicode_FSDecoder')
16
    startstr:  str
17
    /
18
19
Return symbol and scope dictionaries used internally by compiler.
20
[clinic start generated code]*/
21
22
static PyObject *
23
_symtable_symtable_impl(PyObject *module, PyObject *source,
24
                        PyObject *filename, const char *startstr)
25
/*[clinic end generated code: output=59eb0d5fc7285ac4 input=9dd8a50c0c36a4d7]*/
26
{
27
    struct symtable *st;
28
    PyObject *t;
29
    int start;
30
    PyCompilerFlags cf = _PyCompilerFlags_INIT;
31
    PyObject *source_copy = NULL;
32
33
    cf.cf_flags = PyCF_SOURCE_IS_UTF8;
34
35
    const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy);
36
    if (str == NULL) {
  Branch (36:9): [True: 0, False: 15]
37
        return NULL;
38
    }
39
40
    if (strcmp(startstr, "exec") == 0)
  Branch (40:9): [True: 13, False: 2]
41
        start = Py_file_input;
42
    else if (strcmp(startstr, "eval") == 0)
  Branch (42:14): [True: 1, False: 1]
43
        start = Py_eval_input;
44
    else if (strcmp(startstr, "single") == 0)
  Branch (44:14): [True: 1, False: 0]
45
        start = Py_single_input;
46
    else {
47
        PyErr_SetString(PyExc_ValueError,
48
           "symtable() arg 3 must be 'exec' or 'eval' or 'single'");
49
        Py_DECREF(filename);
50
        Py_XDECREF(source_copy);
51
        return NULL;
52
    }
53
    st = _Py_SymtableStringObjectFlags(str, filename, start, &cf);
54
    Py_DECREF(filename);
55
    Py_XDECREF(source_copy);
56
    if (st == NULL) {
  Branch (56:9): [True: 2, False: 13]
57
        return NULL;
58
    }
59
    t = (PyObject *)st->st_top;
60
    Py_INCREF(t);
61
    _PySymtable_Free(st);
62
    return t;
63
}
64
65
static PyMethodDef symtable_methods[] = {
66
    _SYMTABLE_SYMTABLE_METHODDEF
67
    {NULL,              NULL}           /* sentinel */
68
};
69
70
static int
71
symtable_init_stentry_type(PyObject *m)
72
{
73
    return PyType_Ready(&PySTEntry_Type);
74
}
75
76
static int
77
symtable_init_constants(PyObject *m)
78
{
79
    if (PyModule_AddIntMacro(m, USE) < 0) 
return -10
;
  Branch (79:9): [True: 0, False: 1]
80
    if (PyModule_AddIntMacro(m, DEF_GLOBAL) < 0) 
return -10
;
  Branch (80:9): [True: 0, False: 1]
81
    if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) 
return -10
;
  Branch (81:9): [True: 0, False: 1]
82
    if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) 
return -10
;
  Branch (82:9): [True: 0, False: 1]
83
    if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) 
return -10
;
  Branch (83:9): [True: 0, False: 1]
84
    if (PyModule_AddIntMacro(m, DEF_FREE) < 0) 
return -10
;
  Branch (84:9): [True: 0, False: 1]
85
    if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) 
return -10
;
  Branch (85:9): [True: 0, False: 1]
86
    if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) 
return -10
;
  Branch (86:9): [True: 0, False: 1]
87
    if (PyModule_AddIntMacro(m, DEF_BOUND) < 0) 
return -10
;
  Branch (87:9): [True: 0, False: 1]
88
    if (PyModule_AddIntMacro(m, DEF_ANNOT) < 0) 
return -10
;
  Branch (88:9): [True: 0, False: 1]
89
90
    if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0)
  Branch (90:9): [True: 0, False: 1]
91
        return -1;
92
    if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) 
return -10
;
  Branch (92:9): [True: 0, False: 1]
93
    if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0)
  Branch (93:9): [True: 0, False: 1]
94
        return -1;
95
96
    if (PyModule_AddIntMacro(m, LOCAL) < 0) 
return -10
;
  Branch (96:9): [True: 0, False: 1]
97
    if (PyModule_AddIntMacro(m, GLOBAL_EXPLICIT) < 0) 
return -10
;
  Branch (97:9): [True: 0, False: 1]
98
    if (PyModule_AddIntMacro(m, GLOBAL_IMPLICIT) < 0) 
return -10
;
  Branch (98:9): [True: 0, False: 1]
99
    if (PyModule_AddIntMacro(m, FREE) < 0) 
return -10
;
  Branch (99:9): [True: 0, False: 1]
100
    if (PyModule_AddIntMacro(m, CELL) < 0) 
return -10
;
  Branch (100:9): [True: 0, False: 1]
101
102
    if (PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET) < 0) 
return -10
;
  Branch (102:9): [True: 0, False: 1]
103
    if (PyModule_AddIntMacro(m, SCOPE_MASK) < 0) 
return -10
;
  Branch (103:9): [True: 0, False: 1]
104
105
    return 0;
106
}
107
108
static PyModuleDef_Slot symtable_slots[] = {
109
    {Py_mod_exec, symtable_init_stentry_type},
110
    {Py_mod_exec, symtable_init_constants},
111
    {0, NULL}
112
};
113
114
static struct PyModuleDef symtablemodule = {
115
    PyModuleDef_HEAD_INIT,
116
    .m_name = "_symtable",
117
    .m_size = 0,
118
    .m_methods = symtable_methods,
119
    .m_slots = symtable_slots,
120
};
121
122
PyMODINIT_FUNC
123
PyInit__symtable(void)
124
{
125
    return PyModuleDef_Init(&symtablemodule);
126
}