moduleobject.c 22 KB
Newer Older
1

Guido van Rossum's avatar
Guido van Rossum committed
2 3
/* Module object implementation */

4
#include "Python.h"
5
#include "structmember.h"
Guido van Rossum's avatar
Guido van Rossum committed
6

7 8
static Py_ssize_t max_module_number;

Guido van Rossum's avatar
Guido van Rossum committed
9
typedef struct {
10 11 12 13
    PyObject_HEAD
    PyObject *md_dict;
    struct PyModuleDef *md_def;
    void *md_state;
14 15
    PyObject *md_weaklist;
    PyObject *md_name;  /* for logging purposes after md_dict is cleared */
16
} PyModuleObject;
Guido van Rossum's avatar
Guido van Rossum committed
17

18
static PyMemberDef module_members[] = {
19 20
    {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
    {0}
21 22
};

23
PyTypeObject PyModuleDef_Type = {
24 25 26 27
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "moduledef",                                /* tp_name */
    sizeof(struct PyModuleDef),                 /* tp_size */
    0,                                          /* tp_itemsize */
28 29 30
};


31 32 33 34 35 36 37 38 39 40 41 42 43 44
PyObject*
PyModuleDef_Init(struct PyModuleDef* def)
{
    if (PyType_Ready(&PyModuleDef_Type) < 0)
         return NULL;
    if (def->m_base.m_index == 0) {
        max_module_number++;
        Py_REFCNT(def) = 1;
        Py_TYPE(def) = &PyModuleDef_Type;
        def->m_base.m_index = max_module_number;
    }
    return (PyObject*)def;
}

45
static int
46 47
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
                 PyObject *name, PyObject *doc)
48
{
49 50 51 52 53
    _Py_IDENTIFIER(__name__);
    _Py_IDENTIFIER(__doc__);
    _Py_IDENTIFIER(__package__);
    _Py_IDENTIFIER(__loader__);
    _Py_IDENTIFIER(__spec__);
54

55 56 57 58 59
    if (md_dict == NULL)
        return -1;
    if (doc == NULL)
        doc = Py_None;

60
    if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
61
        return -1;
62
    if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
63
        return -1;
64
    if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
65
        return -1;
66
    if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
67
        return -1;
68
    if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
69
        return -1;
70 71
    if (PyUnicode_CheckExact(name)) {
        Py_INCREF(name);
72
        Py_XSETREF(mod->md_name, name);
73
    }
74 75 76 77 78

    return 0;
}


79
PyObject *
80
PyModule_NewObject(PyObject *name)
Guido van Rossum's avatar
Guido van Rossum committed
81
{
82 83 84 85 86 87
    PyModuleObject *m;
    m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
    if (m == NULL)
        return NULL;
    m->md_def = NULL;
    m->md_state = NULL;
88 89
    m->md_weaklist = NULL;
    m->md_name = NULL;
90
    m->md_dict = PyDict_New();
91
    if (module_init_dict(m, m->md_dict, name, NULL) != 0)
92 93 94
        goto fail;
    PyObject_GC_Track(m);
    return (PyObject *)m;
95 96

 fail:
97 98
    Py_DECREF(m);
    return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
99 100
}

101 102 103 104 105 106 107 108 109 110 111 112
PyObject *
PyModule_New(const char *name)
{
    PyObject *nameobj, *module;
    nameobj = PyUnicode_FromString(name);
    if (nameobj == NULL)
        return NULL;
    module = PyModule_NewObject(nameobj);
    Py_DECREF(nameobj);
    return module;
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* Check API/ABI version
 * Issues a warning on mismatch, which is usually not fatal.
 * Returns 0 if an exception is raised.
 */
static int
check_api_version(const char *name, int module_api_version)
{
    if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
        int err;
        err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
            "Python C API version mismatch for module %.100s: "
            "This Python has API version %d, module %.100s has version %d.",
             name,
             PYTHON_API_VERSION, name, module_api_version);
        if (err)
            return 0;
    }
    return 1;
}
132

133 134 135
PyObject *
PyModule_Create2(struct PyModuleDef* module, int module_api_version)
{
136 137
    const char* name;
    PyModuleObject *m;
138 139 140
    PyInterpreterState *interp = PyThreadState_Get()->interp;
    if (interp->modules == NULL)
        Py_FatalError("Python import machinery not initialized");
141
    if (!PyModuleDef_Init(module))
142 143
        return NULL;
    name = module->m_name;
144 145 146 147 148 149 150 151
    if (!check_api_version(name, module_api_version)) {
        return NULL;
    }
    if (module->m_slots) {
        PyErr_Format(
            PyExc_SystemError,
            "module %s: PyModule_Create is incompatible with m_slots", name);
        return NULL;
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    }
    /* Make sure name is fully qualified.

       This is a bit of a hack: when the shared library is loaded,
       the module name is "package.module", but the module calls
       PyModule_Create*() with just "module" for the name.  The shared
       library loader squirrels away the true name of the module in
       _Py_PackageContext, and PyModule_Create*() will substitute this
       (if the name actually matches).
    */
    if (_Py_PackageContext != NULL) {
        char *p = strrchr(_Py_PackageContext, '.');
        if (p != NULL && strcmp(module->m_name, p+1) == 0) {
            name = _Py_PackageContext;
            _Py_PackageContext = NULL;
        }
    }
    if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
        return NULL;

    if (module->m_size > 0) {
        m->md_state = PyMem_MALLOC(module->m_size);
        if (!m->md_state) {
            PyErr_NoMemory();
            Py_DECREF(m);
            return NULL;
        }
        memset(m->md_state, 0, module->m_size);
    }

    if (module->m_methods != NULL) {
183
        if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
184
            Py_DECREF(m);
185
            return NULL;
186
        }
187 188
    }
    if (module->m_doc != NULL) {
189
        if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
190
            Py_DECREF(m);
191 192 193 194 195
            return NULL;
        }
    }
    m->md_def = module;
    return (PyObject*)m;
196 197
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
PyObject *
PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
{
    PyModuleDef_Slot* cur_slot;
    PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
    PyObject *nameobj;
    PyObject *m = NULL;
    int has_execution_slots = 0;
    char *name;
    int ret;

    PyModuleDef_Init(def);

    nameobj = PyObject_GetAttrString(spec, "name");
    if (nameobj == NULL) {
        return NULL;
    }
    name = PyUnicode_AsUTF8(nameobj);
    if (name == NULL) {
        goto error;
    }

    if (!check_api_version(name, module_api_version)) {
        goto error;
    }

    if (def->m_size < 0) {
        PyErr_Format(
            PyExc_SystemError,
            "module %s: m_size may not be negative for multi-phase initialization",
            name);
        goto error;
    }

    for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
        if (cur_slot->slot == Py_mod_create) {
            if (create) {
                PyErr_Format(
                    PyExc_SystemError,
                    "module %s has multiple create slots",
                    name);
                goto error;
            }
            create = cur_slot->value;
        } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
            PyErr_Format(
                PyExc_SystemError,
                "module %s uses unknown slot ID %i",
                name, cur_slot->slot);
            goto error;
        } else {
            has_execution_slots = 1;
        }
    }

    if (create) {
        m = create(spec, def);
        if (m == NULL) {
            if (!PyErr_Occurred()) {
                PyErr_Format(
                    PyExc_SystemError,
                    "creation of module %s failed without setting an exception",
                    name);
            }
            goto error;
        } else {
            if (PyErr_Occurred()) {
                PyErr_Format(PyExc_SystemError,
                            "creation of module %s raised unreported exception",
                            name);
                goto error;
            }
        }
    } else {
        m = PyModule_New(name);
        if (m == NULL) {
            goto error;
        }
    }

    if (PyModule_Check(m)) {
        ((PyModuleObject*)m)->md_state = NULL;
        ((PyModuleObject*)m)->md_def = def;
    } else {
        if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
            PyErr_Format(
                PyExc_SystemError,
                "module %s is not a module object, but requests module state",
                name);
            goto error;
        }
        if (has_execution_slots) {
            PyErr_Format(
                PyExc_SystemError,
                "module %s specifies execution slots, but did not create "
                    "a ModuleType instance",
                name);
            goto error;
        }
    }

    if (def->m_methods != NULL) {
        ret = PyModule_AddFunctions(m, def->m_methods);
        if (ret != 0) {
            goto error;
        }
    }

    if (def->m_doc != NULL) {
        ret = PyModule_SetDocString(m, def->m_doc);
        if (ret != 0) {
            goto error;
        }
    }

313
    Py_DECREF(nameobj);
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    return m;

error:
    Py_DECREF(nameobj);
    Py_XDECREF(m);
    return NULL;
}

int
PyModule_ExecDef(PyObject *module, PyModuleDef *def)
{
    PyModuleDef_Slot *cur_slot;
    const char *name;
    int ret;

    name = PyModule_GetName(module);
    if (name == NULL) {
        return -1;
    }

    if (PyModule_Check(module) && def->m_size >= 0) {
        PyModuleObject *md = (PyModuleObject*)module;
        if (md->md_state == NULL) {
            /* Always set a state pointer; this serves as a marker to skip
             * multiple initialization (importlib.reload() is no-op) */
            md->md_state = PyMem_MALLOC(def->m_size);
            if (!md->md_state) {
                PyErr_NoMemory();
                return -1;
            }
            memset(md->md_state, 0, def->m_size);
        }
    }

    if (def->m_slots == NULL) {
        return 0;
    }

    for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
        switch (cur_slot->slot) {
            case Py_mod_create:
                /* handled in PyModule_CreateFromSlots */
                break;
            case Py_mod_exec:
                ret = ((int (*)(PyObject *))cur_slot->value)(module);
                if (ret != 0) {
                    if (!PyErr_Occurred()) {
                        PyErr_Format(
                            PyExc_SystemError,
                            "execution of module %s failed without setting an exception",
                            name);
                    }
                    return -1;
                }
                if (PyErr_Occurred()) {
                    PyErr_Format(
                        PyExc_SystemError,
                        "execution of module %s raised unreported exception",
                        name);
                    return -1;
                }
                break;
            default:
                PyErr_Format(
                    PyExc_SystemError,
                    "module %s initialized with unknown slot %i",
                    name, cur_slot->slot);
                return -1;
        }
    }
    return 0;
}

int
PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
{
    PyObject *name, *func;
    PyMethodDef *fdef;

    name = PyModule_GetNameObject(m);
    if (name == NULL) {
        return -1;
    }

    for (fdef = functions; fdef->ml_name != NULL; fdef++) {
        if ((fdef->ml_flags & METH_CLASS) ||
            (fdef->ml_flags & METH_STATIC)) {
            PyErr_SetString(PyExc_ValueError,
                            "module functions cannot set"
                            " METH_CLASS or METH_STATIC");
            Py_DECREF(name);
            return -1;
        }
        func = PyCFunction_NewEx(fdef, (PyObject*)m, name);
        if (func == NULL) {
            Py_DECREF(name);
            return -1;
        }
        if (PyObject_SetAttrString(m, fdef->ml_name, func) != 0) {
            Py_DECREF(func);
            Py_DECREF(name);
            return -1;
        }
        Py_DECREF(func);
    }
    Py_DECREF(name);
    return 0;
}

int
PyModule_SetDocString(PyObject *m, const char *doc)
{
    PyObject *v;
    _Py_IDENTIFIER(__doc__);

    v = PyUnicode_FromString(doc);
    if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
        Py_XDECREF(v);
        return -1;
    }
    Py_DECREF(v);
    return 0;
}
437

438
PyObject *
439
PyModule_GetDict(PyObject *m)
Guido van Rossum's avatar
Guido van Rossum committed
440
{
441 442 443 444 445 446 447 448 449
    PyObject *d;
    if (!PyModule_Check(m)) {
        PyErr_BadInternalCall();
        return NULL;
    }
    d = ((PyModuleObject *)m) -> md_dict;
    if (d == NULL)
        ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
    return d;
Guido van Rossum's avatar
Guido van Rossum committed
450 451
}

452 453
PyObject*
PyModule_GetNameObject(PyObject *m)
454
{
455
    _Py_IDENTIFIER(__name__);
456
    PyObject *d;
457
    PyObject *name;
458 459 460 461 462 463
    if (!PyModule_Check(m)) {
        PyErr_BadArgument();
        return NULL;
    }
    d = ((PyModuleObject *)m)->md_dict;
    if (d == NULL ||
464
        (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
465
        !PyUnicode_Check(name))
466 467 468 469
    {
        PyErr_SetString(PyExc_SystemError, "nameless module");
        return NULL;
    }
470 471 472 473 474 475 476 477 478 479 480 481
    Py_INCREF(name);
    return name;
}

const char *
PyModule_GetName(PyObject *m)
{
    PyObject *name = PyModule_GetNameObject(m);
    if (name == NULL)
        return NULL;
    Py_DECREF(name);   /* module dict has still a reference */
    return _PyUnicode_AsString(name);
482 483
}

484 485
PyObject*
PyModule_GetFilenameObject(PyObject *m)
486
{
487
    _Py_IDENTIFIER(__file__);
488 489 490 491 492 493 494 495
    PyObject *d;
    PyObject *fileobj;
    if (!PyModule_Check(m)) {
        PyErr_BadArgument();
        return NULL;
    }
    d = ((PyModuleObject *)m)->md_dict;
    if (d == NULL ||
496
        (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
497 498 499 500 501
        !PyUnicode_Check(fileobj))
    {
        PyErr_SetString(PyExc_SystemError, "module filename missing");
        return NULL;
    }
502
    Py_INCREF(fileobj);
503
    return fileobj;
504 505 506 507 508
}

const char *
PyModule_GetFilename(PyObject *m)
{
509
    PyObject *fileobj;
510 511
    char *utf8;
    fileobj = PyModule_GetFilenameObject(m);
512 513
    if (fileobj == NULL)
        return NULL;
514
    utf8 = _PyUnicode_AsString(fileobj);
515
    Py_DECREF(fileobj);   /* module dict has still a reference */
516
    return utf8;
517 518
}

519 520 521
PyModuleDef*
PyModule_GetDef(PyObject* m)
{
522 523 524 525 526
    if (!PyModule_Check(m)) {
        PyErr_BadArgument();
        return NULL;
    }
    return ((PyModuleObject *)m)->md_def;
527 528 529 530 531
}

void*
PyModule_GetState(PyObject* m)
{
532 533 534 535 536
    if (!PyModule_Check(m)) {
        PyErr_BadArgument();
        return NULL;
    }
    return ((PyModuleObject *)m)->md_state;
537 538
}

539
void
540
_PyModule_Clear(PyObject *m)
541 542 543 544 545 546 547 548
{
    PyObject *d = ((PyModuleObject *)m)->md_dict;
    if (d != NULL)
        _PyModule_ClearDict(d);
}

void
_PyModule_ClearDict(PyObject *d)
549
{
550 551 552 553 554 555 556 557 558 559 560 561 562 563
    /* To make the execution order of destructors for global
       objects a bit more predictable, we first zap all objects
       whose name starts with a single underscore, before we clear
       the entire dictionary.  We zap them by replacing them with
       None, rather than deleting them from the dictionary, to
       avoid rehashing the dictionary (to some extent). */

    Py_ssize_t pos;
    PyObject *key, *value;

    /* First, clear only names starting with a single underscore */
    pos = 0;
    while (PyDict_Next(d, &pos, &key, &value)) {
        if (value != Py_None && PyUnicode_Check(key)) {
564
            if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwis's avatar
Martin v. Löwis committed
565
                PyUnicode_READ_CHAR(key, 1) != '_') {
566 567 568 569 570 571 572
                if (Py_VerboseFlag > 1) {
                    const char *s = _PyUnicode_AsString(key);
                    if (s != NULL)
                        PySys_WriteStderr("#   clear[1] %s\n", s);
                    else
                        PyErr_Clear();
                }
573 574
                if (PyDict_SetItem(d, key, Py_None) != 0)
                    PyErr_Clear();
575 576 577 578 579 580 581 582
            }
        }
    }

    /* Next, clear all names except for __builtins__ */
    pos = 0;
    while (PyDict_Next(d, &pos, &key, &value)) {
        if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwis's avatar
Martin v. Löwis committed
583 584
            if (PyUnicode_READ_CHAR(key, 0) != '_' ||
                PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0)
585 586 587 588 589 590 591 592
            {
                if (Py_VerboseFlag > 1) {
                    const char *s = _PyUnicode_AsString(key);
                    if (s != NULL)
                        PySys_WriteStderr("#   clear[2] %s\n", s);
                    else
                        PyErr_Clear();
                }
593 594
                if (PyDict_SetItem(d, key, Py_None) != 0)
                    PyErr_Clear();
595 596 597 598 599 600 601
            }
        }
    }

    /* Note: we leave __builtins__ in place, so that destructors
       of non-global objects defined in this module can still use
       builtins, in particularly 'None'. */
602 603 604

}

Guido van Rossum's avatar
Guido van Rossum committed
605 606
/* Methods */

607
static int
608
module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
609
{
610 611 612 613 614 615 616 617 618 619 620 621
    static char *kwlist[] = {"name", "doc", NULL};
    PyObject *dict, *name = Py_None, *doc = Py_None;
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
                                     kwlist, &name, &doc))
        return -1;
    dict = m->md_dict;
    if (dict == NULL) {
        dict = PyDict_New();
        if (dict == NULL)
            return -1;
        m->md_dict = dict;
    }
622
    if (module_init_dict(m, dict, name, doc) < 0)
623 624
        return -1;
    return 0;
625 626
}

Guido van Rossum's avatar
Guido van Rossum committed
627
static void
628
module_dealloc(PyModuleObject *m)
Guido van Rossum's avatar
Guido van Rossum committed
629
{
630
    PyObject_GC_UnTrack(m);
631 632 633 634 635
    if (Py_VerboseFlag && m->md_name) {
        PySys_FormatStderr("# destroy %S\n", m->md_name);
    }
    if (m->md_weaklist != NULL)
        PyObject_ClearWeakRefs((PyObject *) m);
636 637
    if (m->md_def && m->md_def->m_free)
        m->md_def->m_free(m);
638 639
    Py_XDECREF(m->md_dict);
    Py_XDECREF(m->md_name);
640 641 642
    if (m->md_state != NULL)
        PyMem_FREE(m->md_state);
    Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum's avatar
Guido van Rossum committed
643 644
}

645
static PyObject *
646
module_repr(PyModuleObject *m)
Guido van Rossum's avatar
Guido van Rossum committed
647
{
648 649
    PyThreadState *tstate = PyThreadState_GET();
    PyInterpreterState *interp = tstate->interp;
650

651
    return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum's avatar
Guido van Rossum committed
652 653
}

654
static PyObject*
655
module_getattro(PyModuleObject *m, PyObject *name)
656 657
{
    PyObject *attr, *mod_name;
658 659
    attr = PyObject_GenericGetAttr((PyObject *)m, name);
    if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError))
660 661
        return attr;
    PyErr_Clear();
662
    if (m->md_dict) {
663 664
        _Py_IDENTIFIER(__name__);
        mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
665
        if (mod_name) {
666 667 668 669
            PyErr_Format(PyExc_AttributeError,
                        "module '%U' has no attribute '%U'", mod_name, name);
            return NULL;
        }
670
        else if (PyErr_Occurred()) {
671
            PyErr_Clear();
672
        }
673 674 675 676 677 678
    }
    PyErr_Format(PyExc_AttributeError,
                "module has no attribute '%U'", name);
    return NULL;
}

679 680 681
static int
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
{
682 683 684 685 686 687 688
    if (m->md_def && m->md_def->m_traverse) {
        int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
        if (res)
            return res;
    }
    Py_VISIT(m->md_dict);
    return 0;
689 690
}

691 692 693
static int
module_clear(PyModuleObject *m)
{
694 695 696 697 698 699 700
    if (m->md_def && m->md_def->m_clear) {
        int res = m->md_def->m_clear((PyObject*)m);
        if (res)
            return res;
    }
    Py_CLEAR(m->md_dict);
    return 0;
701
}
702

703 704 705
static PyObject *
module_dir(PyObject *self, PyObject *args)
{
706
    _Py_IDENTIFIER(__dict__);
707
    PyObject *result = NULL;
708
    PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727

    if (dict != NULL) {
        if (PyDict_Check(dict))
            result = PyDict_Keys(dict);
        else {
            const char *name = PyModule_GetName(self);
            if (name)
                PyErr_Format(PyExc_TypeError,
                             "%.200s.__dict__ is not a dictionary",
                             name);
        }
    }

    Py_XDECREF(dict);
    return result;
}

static PyMethodDef module_methods[] = {
    {"__dir__", module_dir, METH_NOARGS,
728
     PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
729 730 731
    {0}
};

732
PyDoc_STRVAR(module_doc,
733 734 735
"module(name[, doc])\n\
\n\
Create a module object.\n\
736
The name must be a string; the optional doc argument can have any type.");
737

738
PyTypeObject PyModule_Type = {
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "module",                                   /* tp_name */
    sizeof(PyModuleObject),                     /* tp_size */
    0,                                          /* tp_itemsize */
    (destructor)module_dealloc,                 /* tp_dealloc */
    0,                                          /* tp_print */
    0,                                          /* tp_getattr */
    0,                                          /* tp_setattr */
    0,                                          /* tp_reserved */
    (reprfunc)module_repr,                      /* tp_repr */
    0,                                          /* tp_as_number */
    0,                                          /* tp_as_sequence */
    0,                                          /* tp_as_mapping */
    0,                                          /* tp_hash */
    0,                                          /* tp_call */
    0,                                          /* tp_str */
755
    (getattrofunc)module_getattro,              /* tp_getattro */
756 757 758 759 760 761 762 763
    PyObject_GenericSetAttr,                    /* tp_setattro */
    0,                                          /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
    module_doc,                                 /* tp_doc */
    (traverseproc)module_traverse,              /* tp_traverse */
    (inquiry)module_clear,                      /* tp_clear */
    0,                                          /* tp_richcompare */
764
    offsetof(PyModuleObject, md_weaklist),      /* tp_weaklistoffset */
765 766
    0,                                          /* tp_iter */
    0,                                          /* tp_iternext */
767
    module_methods,                             /* tp_methods */
768 769 770 771 772 773 774 775 776 777 778
    module_members,                             /* tp_members */
    0,                                          /* tp_getset */
    0,                                          /* tp_base */
    0,                                          /* tp_dict */
    0,                                          /* tp_descr_get */
    0,                                          /* tp_descr_set */
    offsetof(PyModuleObject, md_dict),          /* tp_dictoffset */
    (initproc)module_init,                      /* tp_init */
    PyType_GenericAlloc,                        /* tp_alloc */
    PyType_GenericNew,                          /* tp_new */
    PyObject_GC_Del,                            /* tp_free */
Guido van Rossum's avatar
Guido van Rossum committed
779
};