Coverage Report

Created: 2022-07-08 09:39

/home/mdboom/Work/builds/cpython/Objects/moduleobject.c
Line
Count
Source (jump to first uncovered line)
1
2
/* Module object implementation */
3
4
#include "Python.h"
5
#include "pycore_call.h"          // _PyObject_CallNoArgs()
6
#include "pycore_interp.h"        // PyInterpreterState.importlib
7
#include "pycore_object.h"        // _PyType_AllocNoTrack
8
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
9
#include "pycore_moduleobject.h"  // _PyModule_GetDef()
10
#include "structmember.h"         // PyMemberDef
11
12
static Py_ssize_t max_module_number;
13
14
static PyMemberDef module_members[] = {
15
    {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
16
    {0}
17
};
18
19
20
PyTypeObject PyModuleDef_Type = {
21
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
22
    "moduledef",                                /* tp_name */
23
    sizeof(PyModuleDef),                        /* tp_basicsize */
24
    0,                                          /* tp_itemsize */
25
};
26
27
28
int
29
_PyModule_IsExtension(PyObject *obj)
30
{
31
    if (!PyModule_Check(obj)) {
  Branch (31:9): [True: 0, False: 0]
32
        return 0;
33
    }
34
    PyModuleObject *module = (PyModuleObject*)obj;
35
36
    PyModuleDef *def = module->md_def;
37
    return (def != NULL && def->m_methods != NULL);
  Branch (37:13): [True: 0, False: 0]
  Branch (37:28): [True: 0, False: 0]
38
}
39
40
41
PyObject*
42
PyModuleDef_Init(PyModuleDef* def)
43
{
44
    assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
45
    if (def->m_base.m_index == 0) {
  Branch (45:9): [True: 1.43k, False: 6.64k]
46
        max_module_number++;
47
        Py_SET_REFCNT(def, 1);
48
        Py_SET_TYPE(def, &PyModuleDef_Type);
49
        def->m_base.m_index = max_module_number;
50
    }
51
    return (PyObject*)def;
52
}
53
54
static int
55
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
56
                 PyObject *name, PyObject *doc)
57
{
58
    assert(md_dict != NULL);
59
    if (doc == NULL)
  Branch (59:9): [True: 5.08k, False: 9.79k]
60
        doc = Py_None;
61
62
    if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
  Branch (62:9): [True: 0, False: 14.8k]
63
        return -1;
64
    if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
  Branch (64:9): [True: 0, False: 14.8k]
65
        return -1;
66
    if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
  Branch (66:9): [True: 0, False: 14.8k]
67
        return -1;
68
    if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
  Branch (68:9): [True: 0, False: 14.8k]
69
        return -1;
70
    if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
  Branch (70:9): [True: 0, False: 14.8k]
71
        return -1;
72
    if (PyUnicode_CheckExact(name)) {
73
        Py_INCREF(name);
74
        Py_XSETREF(mod->md_name, name);
75
    }
76
77
    return 0;
78
}
79
80
static PyModuleObject *
81
new_module_notrack(PyTypeObject *mt)
82
{
83
    PyModuleObject *m;
84
    m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
85
    if (m == NULL)
  Branch (85:9): [True: 0, False: 14.8k]
86
        return NULL;
87
    m->md_def = NULL;
88
    m->md_state = NULL;
89
    m->md_weaklist = NULL;
90
    m->md_name = NULL;
91
    m->md_dict = PyDict_New();
92
    if (m->md_dict != NULL) {
  Branch (92:9): [True: 14.8k, False: 0]
93
        return m;
94
    }
95
    Py_DECREF(m);
96
    return NULL;
97
}
98
99
static PyObject *
100
new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
101
{
102
    PyObject *m = (PyObject *)new_module_notrack(mt);
103
    if (m != NULL) {
  Branch (103:9): [True: 9.79k, False: 0]
104
        PyObject_GC_Track(m);
105
    }
106
    return m;
107
}
108
109
PyObject *
110
PyModule_NewObject(PyObject *name)
111
{
112
    PyModuleObject *m = new_module_notrack(&PyModule_Type);
113
    if (m == NULL)
  Branch (113:9): [True: 0, False: 5.08k]
114
        return NULL;
115
    if (module_init_dict(m, m->md_dict, name, NULL) != 0)
  Branch (115:9): [True: 0, False: 5.08k]
116
        goto fail;
117
    PyObject_GC_Track(m);
118
    return (PyObject *)m;
119
120
 fail:
121
    Py_DECREF(m);
122
    return NULL;
123
}
124
125
PyObject *
126
PyModule_New(const char *name)
127
{
128
    PyObject *nameobj, *module;
129
    nameobj = PyUnicode_FromString(name);
130
    if (nameobj == NULL)
  Branch (130:9): [True: 0, False: 906]
131
        return NULL;
132
    module = PyModule_NewObject(nameobj);
133
    Py_DECREF(nameobj);
134
    return module;
135
}
136
137
/* Check API/ABI version
138
 * Issues a warning on mismatch, which is usually not fatal.
139
 * Returns 0 if an exception is raised.
140
 */
141
static int
142
check_api_version(const char *name, int module_api_version)
143
{
144
    if (module_api_version != PYTHON_API_VERSION && 
module_api_version != 0
PYTHON_ABI_VERSION0
) {
  Branch (144:9): [True: 0, False: 4.49k]
  Branch (144:53): [True: 0, False: 0]
145
        int err;
146
        err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
147
            "Python C API version mismatch for module %.100s: "
148
            "This Python has API version %d, module %.100s has version %d.",
149
             name,
150
             PYTHON_API_VERSION, name, module_api_version);
151
        if (err)
  Branch (151:13): [True: 0, False: 0]
152
            return 0;
153
    }
154
    return 1;
155
}
156
157
static int
158
_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
159
{
160
    PyObject *func;
161
    PyMethodDef *fdef;
162
163
    for (fdef = functions; fdef->ml_name != NULL; 
fdef++114k
) {
  Branch (163:28): [True: 114k, False: 4.42k]
164
        if ((fdef->ml_flags & METH_CLASS) ||
  Branch (164:13): [True: 0, False: 114k]
165
            (fdef->ml_flags & METH_STATIC)) {
  Branch (165:13): [True: 0, False: 114k]
166
            PyErr_SetString(PyExc_ValueError,
167
                            "module functions cannot set"
168
                            " METH_CLASS or METH_STATIC");
169
            return -1;
170
        }
171
        func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
172
        if (func == NULL) {
  Branch (172:13): [True: 0, False: 114k]
173
            return -1;
174
        }
175
        if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
  Branch (175:13): [True: 0, False: 114k]
176
            Py_DECREF(func);
177
            return -1;
178
        }
179
        Py_DECREF(func);
180
    }
181
182
    return 0;
183
}
184
185
PyObject *
186
PyModule_Create2(PyModuleDef* module, int module_api_version)
187
{
188
    if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
  Branch (188:9): [True: 0, False: 346]
189
        PyErr_SetString(PyExc_SystemError,
190
                        "Python import machinery not initialized");
191
        return NULL;
192
    }
193
    return _PyModule_CreateInitialized(module, module_api_version);
194
}
195
196
PyObject *
197
_PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
198
{
199
    const char* name;
200
    PyModuleObject *m;
201
202
    if (!PyModuleDef_Init(module))
  Branch (202:9): [True: 0, False: 902]
203
        return NULL;
204
    name = module->m_name;
205
    if (!check_api_version(name, module_api_version)) {
  Branch (205:9): [True: 0, False: 902]
206
        return NULL;
207
    }
208
    if (module->m_slots) {
  Branch (208:9): [True: 0, False: 902]
209
        PyErr_Format(
210
            PyExc_SystemError,
211
            "module %s: PyModule_Create is incompatible with m_slots", name);
212
        return NULL;
213
    }
214
    /* Make sure name is fully qualified.
215
216
       This is a bit of a hack: when the shared library is loaded,
217
       the module name is "package.module", but the module calls
218
       PyModule_Create*() with just "module" for the name.  The shared
219
       library loader squirrels away the true name of the module in
220
       _Py_PackageContext, and PyModule_Create*() will substitute this
221
       (if the name actually matches).
222
    */
223
    if (_Py_PackageContext != NULL) {
  Branch (223:9): [True: 60, False: 842]
224
        const char *p = strrchr(_Py_PackageContext, '.');
225
        if (p != NULL && 
strcmp(module->m_name, p+1) == 00
) {
  Branch (225:13): [True: 0, False: 60]
  Branch (225:26): [True: 0, False: 0]
226
            name = _Py_PackageContext;
227
            _Py_PackageContext = NULL;
228
        }
229
    }
230
    if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
  Branch (230:9): [True: 0, False: 902]
231
        return NULL;
232
233
    if (module->m_size > 0) {
  Branch (233:9): [True: 291, False: 611]
234
        m->md_state = PyMem_Malloc(module->m_size);
235
        if (!m->md_state) {
  Branch (235:13): [True: 0, False: 291]
236
            PyErr_NoMemory();
237
            Py_DECREF(m);
238
            return NULL;
239
        }
240
        memset(m->md_state, 0, module->m_size);
241
    }
242
243
    if (module->m_methods != NULL) {
  Branch (243:9): [True: 898, False: 4]
244
        if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
  Branch (244:13): [True: 0, False: 898]
245
            Py_DECREF(m);
246
            return NULL;
247
        }
248
    }
249
    if (module->m_doc != NULL) {
  Branch (249:9): [True: 857, False: 45]
250
        if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
  Branch (250:13): [True: 0, False: 857]
251
            Py_DECREF(m);
252
            return NULL;
253
        }
254
    }
255
    m->md_def = module;
256
    return (PyObject*)m;
257
}
258
259
PyObject *
260
PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version)
261
{
262
    PyModuleDef_Slot* cur_slot;
263
    PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
264
    PyObject *nameobj;
265
    PyObject *m = NULL;
266
    int has_execution_slots = 0;
267
    const char *name;
268
    int ret;
269
270
    PyModuleDef_Init(def);
271
272
    nameobj = PyObject_GetAttrString(spec, "name");
273
    if (nameobj == NULL) {
  Branch (273:9): [True: 0, False: 3.59k]
274
        return NULL;
275
    }
276
    name = PyUnicode_AsUTF8(nameobj);
277
    if (name == NULL) {
  Branch (277:9): [True: 0, False: 3.59k]
278
        goto error;
279
    }
280
281
    if (!check_api_version(name, module_api_version)) {
  Branch (281:9): [True: 0, False: 3.59k]
282
        goto error;
283
    }
284
285
    if (def->m_size < 0) {
  Branch (285:9): [True: 2, False: 3.58k]
286
        PyErr_Format(
287
            PyExc_SystemError,
288
            "module %s: m_size may not be negative for multi-phase initialization",
289
            name);
290
        goto error;
291
    }
292
293
    
for (cur_slot = def->m_slots; 3.58k
cur_slot &&
cur_slot->slot6.84k
;
cur_slot++3.28k
) {
  Branch (293:35): [True: 6.84k, False: 29]
  Branch (293:47): [True: 3.29k, False: 3.55k]
294
        if (cur_slot->slot == Py_mod_create) {
  Branch (294:13): [True: 14, False: 3.27k]
295
            if (create) {
  Branch (295:17): [True: 0, False: 14]
296
                PyErr_Format(
297
                    PyExc_SystemError,
298
                    "module %s has multiple create slots",
299
                    name);
300
                goto error;
301
            }
302
            create = cur_slot->value;
303
        } else if (cur_slot->slot < 0 || 
cur_slot->slot > 3.27k
_Py_mod_LAST_SLOT3.27k
) {
  Branch (303:20): [True: 2, False: 3.27k]
  Branch (303:42): [True: 2, False: 3.27k]
304
            PyErr_Format(
305
                PyExc_SystemError,
306
                "module %s uses unknown slot ID %i",
307
                name, cur_slot->slot);
308
            goto error;
309
        } else {
310
            has_execution_slots = 1;
311
        }
312
    }
313
314
    if (create) {
  Branch (314:9): [True: 14, False: 3.57k]
315
        m = create(spec, def);
316
        if (m == NULL) {
  Branch (316:13): [True: 8, False: 6]
317
            if (!PyErr_Occurred()) {
  Branch (317:17): [True: 2, False: 6]
318
                PyErr_Format(
319
                    PyExc_SystemError,
320
                    "creation of module %s failed without setting an exception",
321
                    name);
322
            }
323
            goto error;
324
        } else {
325
            if (PyErr_Occurred()) {
  Branch (325:17): [True: 2, False: 4]
326
                PyErr_Format(PyExc_SystemError,
327
                            "creation of module %s raised unreported exception",
328
                            name);
329
                goto error;
330
            }
331
        }
332
    } else {
333
        m = PyModule_NewObject(nameobj);
334
        if (m == NULL) {
  Branch (334:13): [True: 0, False: 3.57k]
335
            goto error;
336
        }
337
    }
338
339
    if (PyModule_Check(m)) {
340
        ((PyModuleObject*)m)->md_state = NULL;
341
        ((PyModuleObject*)m)->md_def = def;
342
    } else {
343
        if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
  Branch (343:13): [True: 0, False: 4]
  Branch (343:32): [True: 0, False: 4]
  Branch (343:51): [True: 0, False: 4]
  Branch (343:67): [True: 0, False: 4]
344
            PyErr_Format(
345
                PyExc_SystemError,
346
                "module %s is not a module object, but requests module state",
347
                name);
348
            goto error;
349
        }
350
        if (has_execution_slots) {
  Branch (350:13): [True: 0, False: 4]
351
            PyErr_Format(
352
                PyExc_SystemError,
353
                "module %s specifies execution slots, but did not create "
354
                    "a ModuleType instance",
355
                name);
356
            goto error;
357
        }
358
    }
359
360
    if (def->m_methods != NULL) {
  Branch (360:9): [True: 3.52k, False: 48]
361
        ret = _add_methods_to_object(m, nameobj, def->m_methods);
362
        if (ret != 0) {
  Branch (362:13): [True: 0, False: 3.52k]
363
            goto error;
364
        }
365
    }
366
367
    if (def->m_doc != NULL) {
  Branch (367:9): [True: 3.18k, False: 392]
368
        ret = PyModule_SetDocString(m, def->m_doc);
369
        if (ret != 0) {
  Branch (369:13): [True: 0, False: 3.18k]
370
            goto error;
371
        }
372
    }
373
374
    Py_DECREF(nameobj);
375
    return m;
376
377
error:
378
    Py_DECREF(nameobj);
379
    Py_XDECREF(m);
380
    return NULL;
381
}
382
383
int
384
PyModule_ExecDef(PyObject *module, PyModuleDef *def)
385
{
386
    PyModuleDef_Slot *cur_slot;
387
    const char *name;
388
    int ret;
389
390
    name = PyModule_GetName(module);
391
    if (name == NULL) {
  Branch (391:9): [True: 0, False: 3.62k]
392
        return -1;
393
    }
394
395
    if (def->m_size >= 0) {
  Branch (395:9): [True: 3.57k, False: 55]
396
        PyModuleObject *md = (PyModuleObject*)module;
397
        if (md->md_state == NULL) {
  Branch (397:13): [True: 3.57k, False: 0]
398
            /* Always set a state pointer; this serves as a marker to skip
399
             * multiple initialization (importlib.reload() is no-op) */
400
            md->md_state = PyMem_Malloc(def->m_size);
401
            if (!md->md_state) {
  Branch (401:17): [True: 0, False: 3.57k]
402
                PyErr_NoMemory();
403
                return -1;
404
            }
405
            memset(md->md_state, 0, def->m_size);
406
        }
407
    }
408
409
    if (def->m_slots == NULL) {
  Branch (409:9): [True: 86, False: 3.54k]
410
        return 0;
411
    }
412
413
    
for (cur_slot = def->m_slots; 3.54k
cur_slot && cur_slot->slot;
cur_slot++3.26k
) {
  Branch (413:35): [True: 6.80k, False: 0]
  Branch (413:47): [True: 3.27k, False: 3.53k]
414
        switch (cur_slot->slot) {
415
            case Py_mod_create:
  Branch (415:13): [True: 0, False: 3.27k]
416
                /* handled in PyModule_FromDefAndSpec2 */
417
                break;
418
            case Py_mod_exec:
  Branch (418:13): [True: 3.27k, False: 0]
419
                ret = ((int (*)(PyObject *))cur_slot->value)(module);
420
                if (ret != 0) {
  Branch (420:21): [True: 4, False: 3.26k]
421
                    if (!PyErr_Occurred()) {
  Branch (421:25): [True: 2, False: 2]
422
                        PyErr_Format(
423
                            PyExc_SystemError,
424
                            "execution of module %s failed without setting an exception",
425
                            name);
426
                    }
427
                    return -1;
428
                }
429
                if (PyErr_Occurred()) {
  Branch (429:21): [True: 2, False: 3.26k]
430
                    PyErr_Format(
431
                        PyExc_SystemError,
432
                        "execution of module %s raised unreported exception",
433
                        name);
434
                    return -1;
435
                }
436
                break;
437
            default:
  Branch (437:13): [True: 0, False: 3.27k]
438
                PyErr_Format(
439
                    PyExc_SystemError,
440
                    "module %s initialized with unknown slot %i",
441
                    name, cur_slot->slot);
442
                return -1;
443
        }
444
    }
445
    return 0;
446
}
447
448
int
449
PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
450
{
451
    int res;
452
    PyObject *name = PyModule_GetNameObject(m);
453
    if (name == NULL) {
  Branch (453:9): [True: 0, False: 898]
454
        return -1;
455
    }
456
457
    res = _add_methods_to_object(m, name, functions);
458
    Py_DECREF(name);
459
    return res;
460
}
461
462
int
463
PyModule_SetDocString(PyObject *m, const char *doc)
464
{
465
    PyObject *v;
466
467
    v = PyUnicode_FromString(doc);
468
    if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
  Branch (468:9): [True: 0, False: 4.03k]
  Branch (468:22): [True: 0, False: 4.03k]
469
        Py_XDECREF(v);
470
        return -1;
471
    }
472
    Py_DECREF(v);
473
    return 0;
474
}
475
476
PyObject *
477
PyModule_GetDict(PyObject *m)
478
{
479
    if (!PyModule_Check(m)) {
  Branch (479:9): [True: 0, False: 91.5k]
480
        PyErr_BadInternalCall();
481
        return NULL;
482
    }
483
    return _PyModule_GetDict(m);
484
}
485
486
PyObject*
487
PyModule_GetNameObject(PyObject *m)
488
{
489
    PyObject *d;
490
    PyObject *name;
491
    if (!PyModule_Check(m)) {
  Branch (491:9): [True: 0, False: 4.52k]
492
        PyErr_BadArgument();
493
        return NULL;
494
    }
495
    d = ((PyModuleObject *)m)->md_dict;
496
    if (d == NULL || !PyDict_Check(d) ||
  Branch (496:9): [True: 0, False: 4.52k]
  Branch (496:22): [True: 0, False: 4.52k]
497
        (name = PyDict_GetItemWithError(d, &_Py_ID(__name__))) == NULL ||
  Branch (497:9): [True: 0, False: 4.52k]
498
        !PyUnicode_Check(name))
  Branch (498:9): [True: 0, False: 4.52k]
499
    {
500
        if (!PyErr_Occurred()) {
  Branch (500:13): [True: 0, False: 0]
501
            PyErr_SetString(PyExc_SystemError, "nameless module");
502
        }
503
        return NULL;
504
    }
505
    Py_INCREF(name);
506
    return name;
507
}
508
509
const char *
510
PyModule_GetName(PyObject *m)
511
{
512
    PyObject *name = PyModule_GetNameObject(m);
513
    if (name == NULL) {
  Branch (513:9): [True: 0, False: 3.62k]
514
        return NULL;
515
    }
516
    assert(Py_REFCNT(name) >= 2);
517
    Py_DECREF(name);   /* module dict has still a reference */
518
    return PyUnicode_AsUTF8(name);
519
}
520
521
PyObject*
522
PyModule_GetFilenameObject(PyObject *m)
523
{
524
    PyObject *d;
525
    PyObject *fileobj;
526
    if (!PyModule_Check(m)) {
  Branch (526:9): [True: 1, False: 324]
527
        PyErr_BadArgument();
528
        return NULL;
529
    }
530
    d = ((PyModuleObject *)m)->md_dict;
531
    if (d == NULL ||
  Branch (531:9): [True: 0, False: 324]
532
        (fileobj = PyDict_GetItemWithError(d, &_Py_ID(__file__))) == NULL ||
  Branch (532:9): [True: 279, False: 45]
533
        
!45
PyUnicode_Check45
(fileobj))
  Branch (533:9): [True: 0, False: 45]
534
    {
535
        if (!PyErr_Occurred()) {
  Branch (535:13): [True: 279, False: 0]
536
            PyErr_SetString(PyExc_SystemError, "module filename missing");
537
        }
538
        return NULL;
539
    }
540
    Py_INCREF(fileobj);
541
    return fileobj;
542
}
543
544
const char *
545
PyModule_GetFilename(PyObject *m)
546
{
547
    PyObject *fileobj;
548
    const char *utf8;
549
    fileobj = PyModule_GetFilenameObject(m);
550
    if (fileobj == NULL)
  Branch (550:9): [True: 0, False: 0]
551
        return NULL;
552
    utf8 = PyUnicode_AsUTF8(fileobj);
553
    Py_DECREF(fileobj);   /* module dict has still a reference */
554
    return utf8;
555
}
556
557
PyModuleDef*
558
PyModule_GetDef(PyObject* m)
559
{
560
    if (!PyModule_Check(m)) {
  Branch (560:9): [True: 0, False: 5.80k]
561
        PyErr_BadArgument();
562
        return NULL;
563
    }
564
    return _PyModule_GetDef(m);
565
}
566
567
void*
568
PyModule_GetState(PyObject* m)
569
{
570
    if (!PyModule_Check(m)) {
  Branch (570:9): [True: 0, False: 892k]
571
        PyErr_BadArgument();
572
        return NULL;
573
    }
574
    return _PyModule_GetState(m);
575
}
576
577
void
578
_PyModule_Clear(PyObject *m)
579
{
580
    PyObject *d = ((PyModuleObject *)m)->md_dict;
581
    if (d != NULL)
  Branch (581:9): [True: 5.76k, False: 0]
582
        _PyModule_ClearDict(d);
583
}
584
585
void
586
_PyModule_ClearDict(PyObject *d)
587
{
588
    /* To make the execution order of destructors for global
589
       objects a bit more predictable, we first zap all objects
590
       whose name starts with a single underscore, before we clear
591
       the entire dictionary.  We zap them by replacing them with
592
       None, rather than deleting them from the dictionary, to
593
       avoid rehashing the dictionary (to some extent). */
594
595
    Py_ssize_t pos;
596
    PyObject *key, *value;
597
598
    int verbose = _Py_GetConfig()->verbose;
599
600
    /* First, clear only names starting with a single underscore */
601
    pos = 0;
602
    while (PyDict_Next(d, &pos, &key, &value)) {
  Branch (602:12): [True: 351k, False: 6.30k]
603
        if (value != Py_None && 
PyUnicode_Check342k
(key)) {
  Branch (603:13): [True: 342k, False: 8.15k]
604
            if (PyUnicode_READ_CHAR(key, 0) == '_' &&
  Branch (604:17): [True: 79.6k, False: 263k]
605
                
PyUnicode_READ_CHAR79.6k
(key, 1) != '_'79.6k
) {
  Branch (605:17): [True: 38.1k, False: 41.4k]
606
                if (verbose > 1) {
  Branch (606:21): [True: 0, False: 38.1k]
607
                    const char *s = PyUnicode_AsUTF8(key);
608
                    if (s != NULL)
  Branch (608:25): [True: 0, False: 0]
609
                        PySys_WriteStderr("#   clear[1] %s\n", s);
610
                    else
611
                        PyErr_Clear();
612
                }
613
                if (PyDict_SetItem(d, key, Py_None) != 0) {
  Branch (613:21): [True: 0, False: 38.1k]
614
                    PyErr_WriteUnraisable(NULL);
615
                }
616
            }
617
        }
618
    }
619
620
    /* Next, clear all names except for __builtins__ */
621
    pos = 0;
622
    while (PyDict_Next(d, &pos, &key, &value)) {
  Branch (622:12): [True: 351k, False: 6.30k]
623
        if (value != Py_None && 
PyUnicode_Check304k
(key)) {
  Branch (623:13): [True: 304k, False: 46.3k]
624
            if (PyUnicode_READ_CHAR(key, 0) != '_' ||
  Branch (624:17): [True: 263k, False: 41.4k]
625
                
!_PyUnicode_EqualToASCIIString(key, "__builtins__")41.4k
)
  Branch (625:17): [True: 38.4k, False: 3.07k]
626
            {
627
                if (verbose > 1) {
  Branch (627:21): [True: 0, False: 301k]
628
                    const char *s = PyUnicode_AsUTF8(key);
629
                    if (s != NULL)
  Branch (629:25): [True: 0, False: 0]
630
                        PySys_WriteStderr("#   clear[2] %s\n", s);
631
                    else
632
                        PyErr_Clear();
633
                }
634
                if (PyDict_SetItem(d, key, Py_None) != 0) {
  Branch (634:21): [True: 0, False: 301k]
635
                    PyErr_WriteUnraisable(NULL);
636
                }
637
            }
638
        }
639
    }
640
641
    /* Note: we leave __builtins__ in place, so that destructors
642
       of non-global objects defined in this module can still use
643
       builtins, in particularly 'None'. */
644
645
}
646
647
/*[clinic input]
648
class module "PyModuleObject *" "&PyModule_Type"
649
[clinic start generated code]*/
650
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
651
652
#include "clinic/moduleobject.c.h"
653
654
/* Methods */
655
656
/*[clinic input]
657
module.__init__
658
    name: unicode
659
    doc: object = None
660
661
Create a module object.
662
663
The name must be a string; the optional doc argument can have any type.
664
[clinic start generated code]*/
665
666
static int
667
module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
668
/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
669
{
670
    PyObject *dict = self->md_dict;
671
    if (dict == NULL) {
  Branch (671:9): [True: 0, False: 9.79k]
672
        dict = PyDict_New();
673
        if (dict == NULL)
  Branch (673:13): [True: 0, False: 0]
674
            return -1;
675
        self->md_dict = dict;
676
    }
677
    if (module_init_dict(self, dict, name, doc) < 0)
  Branch (677:9): [True: 0, False: 9.79k]
678
        return -1;
679
    return 0;
680
}
681
682
static void
683
module_dealloc(PyModuleObject *m)
684
{
685
    int verbose = _Py_GetConfig()->verbose;
686
687
    PyObject_GC_UnTrack(m);
688
    if (verbose && 
m->md_name215
) {
  Branch (688:9): [True: 215, False: 14.3k]
  Branch (688:20): [True: 215, False: 0]
689
        PySys_FormatStderr("# destroy %U\n", m->md_name);
690
    }
691
    if (m->md_weaklist != NULL)
  Branch (691:9): [True: 3.19k, False: 11.4k]
692
        PyObject_ClearWeakRefs((PyObject *) m);
693
    /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
694
    if (m->md_def && 
m->md_def->m_free4.36k
  Branch (694:9): [True: 4.36k, False: 10.2k]
  Branch (694:22): [True: 1.77k, False: 2.58k]
695
        && 
(1.77k
m->md_def->m_size <= 01.77k
||
m->md_state != NULL1.77k
))
  Branch (695:13): [True: 2, False: 1.77k]
  Branch (695:39): [True: 1.77k, False: 0]
696
    {
697
        m->md_def->m_free(m);
698
    }
699
    Py_XDECREF(m->md_dict);
700
    Py_XDECREF(m->md_name);
701
    if (m->md_state != NULL)
  Branch (701:9): [True: 3.76k, False: 10.8k]
702
        PyMem_Free(m->md_state);
703
    Py_TYPE(m)->tp_free((PyObject *)m);
704
}
705
706
static PyObject *
707
module_repr(PyModuleObject *m)
708
{
709
    PyInterpreterState *interp = _PyInterpreterState_GET();
710
711
    return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
712
}
713
714
/* Check if the "_initializing" attribute of the module spec is set to true.
715
   Clear the exception and return 0 if spec is NULL.
716
 */
717
int
718
_PyModuleSpec_IsInitializing(PyObject *spec)
719
{
720
    if (spec != NULL) {
  Branch (720:9): [True: 3.70M, False: 1.06k]
721
        PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_initializing));
722
        if (value != NULL) {
  Branch (722:13): [True: 3.59M, False: 115k]
723
            int initializing = PyObject_IsTrue(value);
724
            Py_DECREF(value);
725
            if (initializing >= 0) {
  Branch (725:17): [True: 3.59M, False: 0]
726
                return initializing;
727
            }
728
        }
729
    }
730
    PyErr_Clear();
731
    return 0;
732
}
733
734
/* Check if the submodule name is in the "_uninitialized_submodules" attribute
735
   of the module spec.
736
 */
737
int
738
_PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name)
739
{
740
    if (spec == NULL) {
  Branch (740:9): [True: 1.04k, False: 2.96M]
741
         return 0;
742
    }
743
744
    PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_uninitialized_submodules));
745
    if (value == NULL) {
  Branch (745:9): [True: 13.0k, False: 2.95M]
746
        return 0;
747
    }
748
749
    int is_uninitialized = PySequence_Contains(value, name);
750
    Py_DECREF(value);
751
    if (is_uninitialized == -1) {
  Branch (751:9): [True: 0, False: 2.95M]
752
        return 0;
753
    }
754
    return is_uninitialized;
755
}
756
757
static PyObject*
758
module_getattro(PyModuleObject *m, PyObject *name)
759
{
760
    PyObject *attr, *mod_name, *getattr;
761
    attr = PyObject_GenericGetAttr((PyObject *)m, name);
762
    if (attr || 
!PyErr_ExceptionMatches(PyExc_AttributeError)2.98M
) {
  Branch (762:9): [True: 5.52M, False: 2.98M]
  Branch (762:17): [True: 1, False: 2.98M]
763
        return attr;
764
    }
765
    PyErr_Clear();
766
    assert(m->md_dict != NULL);
767
    getattr = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__getattr__));
768
    if (getattr) {
  Branch (768:9): [True: 15.7k, False: 2.96M]
769
        return PyObject_CallOneArg(getattr, name);
770
    }
771
    if (PyErr_Occurred()) {
  Branch (771:9): [True: 0, False: 2.96M]
772
        return NULL;
773
    }
774
    mod_name = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__name__));
775
    if (mod_name && 
PyUnicode_Check2.96M
(mod_name)) {
  Branch (775:9): [True: 2.96M, False: 34]
776
        Py_INCREF(mod_name);
777
        PyObject *spec = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__spec__));
778
        if (spec == NULL && 
PyErr_Occurred()1.04k
) {
  Branch (778:13): [True: 1.04k, False: 2.96M]
  Branch (778:29): [True: 0, False: 1.04k]
779
            Py_DECREF(mod_name);
780
            return NULL;
781
        }
782
        Py_XINCREF(spec);
783
        if (_PyModuleSpec_IsInitializing(spec)) {
  Branch (783:13): [True: 579, False: 2.96M]
784
            PyErr_Format(PyExc_AttributeError,
785
                            "partially initialized "
786
                            "module '%U' has no attribute '%U' "
787
                            "(most likely due to a circular import)",
788
                            mod_name, name);
789
        }
790
        else if (_PyModuleSpec_IsUninitializedSubmodule(spec, name)) {
  Branch (790:18): [True: 14, False: 2.96M]
791
            PyErr_Format(PyExc_AttributeError,
792
                            "cannot access submodule '%U' of module '%U' "
793
                            "(most likely due to a circular import)",
794
                            name, mod_name);
795
        }
796
        else {
797
            PyErr_Format(PyExc_AttributeError,
798
                            "module '%U' has no attribute '%U'",
799
                            mod_name, name);
800
        }
801
        Py_XDECREF(spec);
802
        Py_DECREF(mod_name);
803
        return NULL;
804
    }
805
    else if (PyErr_Occurred()) {
  Branch (805:14): [True: 0, False: 37]
806
        return NULL;
807
    }
808
    PyErr_Format(PyExc_AttributeError,
809
                "module has no attribute '%U'", name);
810
    return NULL;
811
}
812
813
static int
814
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
815
{
816
    /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
817
    if (m->md_def && 
m->md_def->m_traverse2.58M
  Branch (817:9): [True: 2.58M, False: 30.9M]
  Branch (817:22): [True: 1.05M, False: 1.52M]
818
        && 
(1.05M
m->md_def->m_size <= 01.05M
||
m->md_state != NULL1.02M
))
  Branch (818:13): [True: 24.7k, False: 1.02M]
  Branch (818:39): [True: 1.02M, False: 502]
819
    {
820
        int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
821
        if (res)
  Branch (821:13): [True: 0, False: 1.05M]
822
            return res;
823
    }
824
    Py_VISIT(m->md_dict);
825
    return 0;
826
}
827
828
static int
829
module_clear(PyModuleObject *m)
830
{
831
    /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
832
    if (m->md_def && 
m->md_def->m_clear2.65k
  Branch (832:9): [True: 2.65k, False: 2.04k]
  Branch (832:22): [True: 1.46k, False: 1.19k]
833
        && 
(1.46k
m->md_def->m_size <= 01.46k
||
m->md_state != NULL1.46k
))
  Branch (833:13): [True: 0, False: 1.46k]
  Branch (833:39): [True: 1.46k, False: 0]
834
    {
835
        int res = m->md_def->m_clear((PyObject*)m);
836
        if (PyErr_Occurred()) {
  Branch (836:13): [True: 0, False: 1.46k]
837
            PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
838
                               m->md_name ? " " : "",
  Branch (838:32): [True: 0, False: 0]
839
                               m->md_name, "");
840
            PyErr_WriteUnraisable(NULL);
841
        }
842
        if (res)
  Branch (842:13): [True: 0, False: 1.46k]
843
            return res;
844
    }
845
    Py_CLEAR(m->md_dict);
846
    return 0;
847
}
848
849
static PyObject *
850
module_dir(PyObject *self, PyObject *args)
851
{
852
    PyObject *result = NULL;
853
    PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
854
855
    if (dict != NULL) {
  Branch (855:9): [True: 1.20k, False: 0]
856
        if (PyDict_Check(dict)) {
857
            PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
858
            if (dirfunc) {
  Branch (858:17): [True: 9, False: 1.19k]
859
                result = _PyObject_CallNoArgs(dirfunc);
860
            }
861
            else if (!PyErr_Occurred()) {
  Branch (861:22): [True: 1.19k, False: 0]
862
                result = PyDict_Keys(dict);
863
            }
864
        }
865
        else {
866
            PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
867
        }
868
    }
869
870
    Py_XDECREF(dict);
871
    return result;
872
}
873
874
static PyMethodDef module_methods[] = {
875
    {"__dir__", module_dir, METH_NOARGS,
876
     PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
877
    {0}
878
};
879
880
static PyObject *
881
module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
882
{
883
    PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
884
885
    if ((dict == NULL) || !PyDict_Check(dict)) {
  Branch (885:9): [True: 0, False: 37]
  Branch (885:27): [True: 0, False: 37]
886
        PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
887
        Py_XDECREF(dict);
888
        return NULL;
889
    }
890
891
    PyObject *annotations;
892
    /* there's no _PyDict_GetItemId without WithError, so let's LBYL. */
893
    if (PyDict_Contains(dict, &_Py_ID(__annotations__))) {
  Branch (893:9): [True: 29, False: 8]
894
        annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
895
        /*
896
        ** _PyDict_GetItemIdWithError could still fail,
897
        ** for instance with a well-timed Ctrl-C or a MemoryError.
898
        ** so let's be totally safe.
899
        */
900
        if (annotations) {
  Branch (900:13): [True: 29, False: 0]
901
            Py_INCREF(annotations);
902
        }
903
    } else {
904
        annotations = PyDict_New();
905
        if (annotations) {
  Branch (905:13): [True: 8, False: 0]
906
            int result = PyDict_SetItem(
907
                    dict, &_Py_ID(__annotations__), annotations);
908
            if (result) {
  Branch (908:17): [True: 0, False: 8]
909
                Py_CLEAR(annotations);
910
            }
911
        }
912
    }
913
    Py_DECREF(dict);
914
    return annotations;
915
}
916
917
static int
918
module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
919
{
920
    int ret = -1;
921
    PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
922
923
    if ((dict == NULL) || !PyDict_Check(dict)) {
  Branch (923:9): [True: 0, False: 12]
  Branch (923:27): [True: 0, False: 12]
924
        PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
925
        goto exit;
926
    }
927
928
    if (value != NULL) {
  Branch (928:9): [True: 5, False: 7]
929
        /* set */
930
        ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
931
        goto exit;
932
    }
933
934
    /* delete */
935
    if (!PyDict_Contains(dict, &_Py_ID(__annotations__))) {
  Branch (935:9): [True: 1, False: 6]
936
        PyErr_Format(PyExc_AttributeError, "__annotations__");
937
        goto exit;
938
    }
939
940
    ret = PyDict_DelItem(dict, &_Py_ID(__annotations__));
941
942
exit:
943
    Py_XDECREF(dict);
944
    return ret;
945
}
946
947
948
static PyGetSetDef module_getsets[] = {
949
    {"__annotations__", (getter)module_get_annotations, (setter)module_set_annotations},
950
    {NULL}
951
};
952
953
PyTypeObject PyModule_Type = {
954
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
955
    "module",                                   /* tp_name */
956
    sizeof(PyModuleObject),                     /* tp_basicsize */
957
    0,                                          /* tp_itemsize */
958
    (destructor)module_dealloc,                 /* tp_dealloc */
959
    0,                                          /* tp_vectorcall_offset */
960
    0,                                          /* tp_getattr */
961
    0,                                          /* tp_setattr */
962
    0,                                          /* tp_as_async */
963
    (reprfunc)module_repr,                      /* tp_repr */
964
    0,                                          /* tp_as_number */
965
    0,                                          /* tp_as_sequence */
966
    0,                                          /* tp_as_mapping */
967
    0,                                          /* tp_hash */
968
    0,                                          /* tp_call */
969
    0,                                          /* tp_str */
970
    (getattrofunc)module_getattro,              /* tp_getattro */
971
    PyObject_GenericSetAttr,                    /* tp_setattro */
972
    0,                                          /* tp_as_buffer */
973
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
974
        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
975
    module___init____doc__,                     /* tp_doc */
976
    (traverseproc)module_traverse,              /* tp_traverse */
977
    (inquiry)module_clear,                      /* tp_clear */
978
    0,                                          /* tp_richcompare */
979
    offsetof(PyModuleObject, md_weaklist),      /* tp_weaklistoffset */
980
    0,                                          /* tp_iter */
981
    0,                                          /* tp_iternext */
982
    module_methods,                             /* tp_methods */
983
    module_members,                             /* tp_members */
984
    module_getsets,                             /* tp_getset */
985
    0,                                          /* tp_base */
986
    0,                                          /* tp_dict */
987
    0,                                          /* tp_descr_get */
988
    0,                                          /* tp_descr_set */
989
    offsetof(PyModuleObject, md_dict),          /* tp_dictoffset */
990
    module___init__,                            /* tp_init */
991
    0,                                          /* tp_alloc */
992
    new_module,                                 /* tp_new */
993
    PyObject_GC_Del,                            /* tp_free */
994
};