Coverage Report

Created: 2025-11-24 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Modules/symtablemodule.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_pythonrun.h"     // _Py_SourceAsString()
3
#include "pycore_symtable.h"      // struct symtable
4
5
#include "clinic/symtablemodule.c.h"
6
/*[clinic input]
7
module _symtable
8
[clinic start generated code]*/
9
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f4685845a7100605]*/
10
11
12
/*[clinic input]
13
_symtable.symtable
14
15
    source:    object
16
    filename:  unicode_fs_decoded
17
    startstr:  str
18
    /
19
    *
20
    module as modname: object = None
21
22
Return symbol and scope dictionaries used internally by compiler.
23
[clinic start generated code]*/
24
25
static PyObject *
26
_symtable_symtable_impl(PyObject *module, PyObject *source,
27
                        PyObject *filename, const char *startstr,
28
                        PyObject *modname)
29
/*[clinic end generated code: output=235ec5a87a9ce178 input=fbf9adaa33c7070d]*/
30
0
{
31
0
    struct symtable *st;
32
0
    PyObject *t;
33
0
    int start;
34
0
    PyCompilerFlags cf = _PyCompilerFlags_INIT;
35
0
    PyObject *source_copy = NULL;
36
37
0
    cf.cf_flags = PyCF_SOURCE_IS_UTF8;
38
39
0
    const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy);
40
0
    if (str == NULL) {
41
0
        return NULL;
42
0
    }
43
44
0
    if (strcmp(startstr, "exec") == 0)
45
0
        start = Py_file_input;
46
0
    else if (strcmp(startstr, "eval") == 0)
47
0
        start = Py_eval_input;
48
0
    else if (strcmp(startstr, "single") == 0)
49
0
        start = Py_single_input;
50
0
    else {
51
0
        PyErr_SetString(PyExc_ValueError,
52
0
           "symtable() arg 3 must be 'exec' or 'eval' or 'single'");
53
0
        Py_XDECREF(source_copy);
54
0
        return NULL;
55
0
    }
56
0
    if (modname == Py_None) {
57
0
        modname = NULL;
58
0
    }
59
0
    else if (!PyUnicode_Check(modname)) {
60
0
        PyErr_Format(PyExc_TypeError,
61
0
                     "symtable() argument 'module' must be str or None, not %T",
62
0
                     modname);
63
0
        Py_XDECREF(source_copy);
64
0
        return NULL;
65
0
    }
66
0
    st = _Py_SymtableStringObjectFlags(str, filename, start, &cf, modname);
67
0
    Py_XDECREF(source_copy);
68
0
    if (st == NULL) {
69
0
        return NULL;
70
0
    }
71
0
    t = Py_NewRef(st->st_top);
72
0
    _PySymtable_Free(st);
73
0
    return t;
74
0
}
75
76
static PyMethodDef symtable_methods[] = {
77
    _SYMTABLE_SYMTABLE_METHODDEF
78
    {NULL,              NULL}           /* sentinel */
79
};
80
81
static int
82
symtable_init_constants(PyObject *m)
83
0
{
84
0
    if (PyModule_AddIntMacro(m, USE) < 0) return -1;
85
0
    if (PyModule_AddIntMacro(m, DEF_GLOBAL) < 0) return -1;
86
0
    if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1;
87
0
    if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1;
88
0
    if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1;
89
0
    if (PyModule_AddIntMacro(m, DEF_TYPE_PARAM) < 0) return -1;
90
0
    if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1;
91
0
    if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1;
92
0
    if (PyModule_AddIntMacro(m, DEF_BOUND) < 0) return -1;
93
0
    if (PyModule_AddIntMacro(m, DEF_ANNOT) < 0) return -1;
94
0
    if (PyModule_AddIntMacro(m, DEF_COMP_ITER) < 0) return -1;
95
0
    if (PyModule_AddIntMacro(m, DEF_COMP_CELL) < 0) return -1;
96
97
0
    if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0)
98
0
        return -1;
99
0
    if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0)
100
0
        return -1;
101
0
    if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0)
102
0
        return -1;
103
0
    if (PyModule_AddIntConstant(m, "TYPE_ANNOTATION", AnnotationBlock) < 0)
104
0
        return -1;
105
0
    if (PyModule_AddIntConstant(m, "TYPE_TYPE_ALIAS", TypeAliasBlock) < 0)
106
0
        return -1;
107
0
    if (PyModule_AddIntConstant(m, "TYPE_TYPE_PARAMETERS", TypeParametersBlock) < 0)
108
0
        return -1;
109
0
    if (PyModule_AddIntConstant(m, "TYPE_TYPE_VARIABLE", TypeVariableBlock) < 0)
110
0
        return -1;
111
112
0
    if (PyModule_AddIntMacro(m, LOCAL) < 0) return -1;
113
0
    if (PyModule_AddIntMacro(m, GLOBAL_EXPLICIT) < 0) return -1;
114
0
    if (PyModule_AddIntMacro(m, GLOBAL_IMPLICIT) < 0) return -1;
115
0
    if (PyModule_AddIntMacro(m, FREE) < 0) return -1;
116
0
    if (PyModule_AddIntMacro(m, CELL) < 0) return -1;
117
118
0
    if (PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET) < 0) return -1;
119
0
    if (PyModule_AddIntMacro(m, SCOPE_MASK) < 0) return -1;
120
121
0
    return 0;
122
0
}
123
124
static PyModuleDef_Slot symtable_slots[] = {
125
    {Py_mod_exec, symtable_init_constants},
126
    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
127
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
128
    {0, NULL}
129
};
130
131
static struct PyModuleDef symtablemodule = {
132
    PyModuleDef_HEAD_INIT,
133
    .m_name = "_symtable",
134
    .m_size = 0,
135
    .m_methods = symtable_methods,
136
    .m_slots = symtable_slots,
137
};
138
139
PyMODINIT_FUNC
140
PyInit__symtable(void)
141
0
{
142
0
    return PyModuleDef_Init(&symtablemodule);
143
0
}