1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Module object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2001-08-02 01:15:00 -03:00
|
|
|
#include "structmember.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
static Py_ssize_t max_module_number;
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
typedef struct {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *md_dict;
|
2008-06-11 02:26:20 -03:00
|
|
|
struct PyModuleDef *md_def;
|
|
|
|
void *md_state;
|
1997-05-02 00:12:38 -03:00
|
|
|
} PyModuleObject;
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2001-10-21 19:28:58 -03:00
|
|
|
static PyMemberDef module_members[] = {
|
2001-08-02 01:15:00 -03:00
|
|
|
{"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
static PyTypeObject moduledef_type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"moduledef", /* tp_name */
|
|
|
|
sizeof(struct PyModuleDef), /* tp_size */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2005-12-10 14:50:16 -04:00
|
|
|
PyModule_New(const char *name)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyModuleObject *m;
|
|
|
|
PyObject *nameobj;
|
2001-08-29 20:54:21 -03:00
|
|
|
m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
2008-06-11 02:26:20 -03:00
|
|
|
m->md_def = NULL;
|
|
|
|
m->md_state = NULL;
|
2007-06-10 06:51:05 -03:00
|
|
|
nameobj = PyUnicode_FromString(name);
|
1997-05-02 00:12:38 -03:00
|
|
|
m->md_dict = PyDict_New();
|
1993-11-17 18:58:56 -04:00
|
|
|
if (m->md_dict == NULL || nameobj == NULL)
|
|
|
|
goto fail;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
|
1993-11-17 18:58:56 -04:00
|
|
|
goto fail;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
|
1995-01-07 07:59:29 -04:00
|
|
|
goto fail;
|
Merged revisions 59275-59303 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NOTE: The merge does NOT contain the modified file Python/import.c from
r59288. I can't get it running. Nick, please check in the PEP 366
manually.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
........
r59279 | georg.brandl | 2007-12-02 19:17:50 +0100 (Sun, 02 Dec 2007) | 2 lines
Fix a sentence I missed before. Do not merge to 3k.
........
r59281 | georg.brandl | 2007-12-02 22:58:54 +0100 (Sun, 02 Dec 2007) | 3 lines
Add documentation for PySys_* functions.
Written by Charlie Shepherd for GHOP. Also fixes #1245.
........
r59288 | nick.coghlan | 2007-12-03 13:55:17 +0100 (Mon, 03 Dec 2007) | 1 line
Implement PEP 366
........
r59290 | christian.heimes | 2007-12-03 14:47:29 +0100 (Mon, 03 Dec 2007) | 3 lines
Applied my patch #1455 with some extra fixes for VS 2005
The new msvc9compiler module supports VS 2005 and VS 2008. I've also fixed build_ext to support PCbuild8 and PCbuild9 and backported my fix for xxmodule.c from py3k. The old code msvccompiler is still in place in case somebody likes to build an extension with VS 2003 or earlier.
I've also updated the cygwin compiler module for VS 2005 and VS 2008. It works with VS 2005 but I'm unable to test it with VS 2008. We have to wait for a new version of cygwin.
........
r59291 | christian.heimes | 2007-12-03 14:55:16 +0100 (Mon, 03 Dec 2007) | 1 line
Added comment to Misc/NEWS for r59290
........
r59292 | christian.heimes | 2007-12-03 15:28:04 +0100 (Mon, 03 Dec 2007) | 1 line
I followed MA Lemberg's suggestion and added comments to the late initialization of the type slots.
........
r59293 | facundo.batista | 2007-12-03 17:29:52 +0100 (Mon, 03 Dec 2007) | 3 lines
Speedup and cleaning of __str__. Thanks Mark Dickinson.
........
r59294 | facundo.batista | 2007-12-03 18:55:00 +0100 (Mon, 03 Dec 2007) | 4 lines
Faster _fix function, and some reordering for a more elegant
coding. Thanks Mark Dickinson.
........
r59295 | martin.v.loewis | 2007-12-03 20:20:02 +0100 (Mon, 03 Dec 2007) | 5 lines
Issue #1727780: Support loading pickles of random.Random objects created
on 32-bit systems on 64-bit systems, and vice versa. As a consequence
of the change, Random pickles created by Python 2.6 cannot be loaded
in Python 2.5.
........
r59297 | facundo.batista | 2007-12-03 20:49:54 +0100 (Mon, 03 Dec 2007) | 3 lines
Two small fixes. Issue 1547.
........
r59299 | georg.brandl | 2007-12-03 20:57:02 +0100 (Mon, 03 Dec 2007) | 2 lines
#1548: fix apostroph placement.
........
r59300 | christian.heimes | 2007-12-03 21:01:02 +0100 (Mon, 03 Dec 2007) | 3 lines
Patch #1537 from Chad Austin
Change GeneratorExit's base class from Exception to BaseException
(This time I'm applying the patch to the correct sandbox.)
........
r59302 | georg.brandl | 2007-12-03 21:03:46 +0100 (Mon, 03 Dec 2007) | 3 lines
Add examples to the xmlrpclib docs.
Written for GHOP by Josip Dzolonga.
........
2007-12-03 17:02:03 -04:00
|
|
|
if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
|
|
|
|
goto fail;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(nameobj);
|
2001-08-29 20:54:21 -03:00
|
|
|
PyObject_GC_Track(m);
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)m;
|
1993-11-17 18:58:56 -04:00
|
|
|
|
|
|
|
fail:
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(nameobj);
|
|
|
|
Py_DECREF(m);
|
1993-11-17 18:58:56 -04:00
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
static char api_version_warning[] =
|
|
|
|
"Python C API version mismatch for module %.100s:\
|
|
|
|
This Python has API version %d, module %.100s has version %d.";
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyModule_Create2(struct PyModuleDef* module, int module_api_version)
|
|
|
|
{
|
|
|
|
PyObject *d, *v, *n;
|
|
|
|
PyMethodDef *ml;
|
|
|
|
const char* name;
|
|
|
|
PyModuleObject *m;
|
|
|
|
if (!Py_IsInitialized())
|
|
|
|
Py_FatalError("Interpreter not initialized (version mismatch?)");
|
|
|
|
if (PyType_Ready(&moduledef_type) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (module->m_base.m_index == 0) {
|
|
|
|
max_module_number++;
|
|
|
|
Py_REFCNT(module) = 1;
|
|
|
|
Py_TYPE(module) = &moduledef_type;
|
|
|
|
module->m_base.m_index = max_module_number;
|
|
|
|
}
|
|
|
|
name = module->m_name;
|
|
|
|
if (module_api_version != PYTHON_API_VERSION) {
|
|
|
|
char message[512];
|
|
|
|
PyOS_snprintf(message, sizeof(message),
|
|
|
|
api_version_warning, name,
|
|
|
|
PYTHON_API_VERSION, name,
|
|
|
|
module_api_version);
|
|
|
|
if (PyErr_WarnEx(PyExc_RuntimeWarning, message, 1))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* 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
|
2009-01-02 15:20:26 -04:00
|
|
|
PyModule_Create*() with just "module" for the name. The shared
|
2008-06-11 02:26:20 -03:00
|
|
|
library loader squirrels away the true name of the module in
|
2009-01-02 15:20:26 -04:00
|
|
|
_Py_PackageContext, and PyModule_Create*() will substitute this
|
2008-06-11 02:26:20 -03:00
|
|
|
(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;
|
|
|
|
}
|
2008-10-07 10:16:28 -03:00
|
|
|
memset(m->md_state, 0, module->m_size);
|
2008-06-11 02:26:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
d = PyModule_GetDict((PyObject*)m);
|
|
|
|
if (module->m_methods != NULL) {
|
|
|
|
n = PyUnicode_FromString(name);
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
|
|
|
|
if ((ml->ml_flags & METH_CLASS) ||
|
|
|
|
(ml->ml_flags & METH_STATIC)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"module functions cannot set"
|
|
|
|
" METH_CLASS or METH_STATIC");
|
|
|
|
Py_DECREF(n);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
v = PyCFunction_NewEx(ml, (PyObject*)m, n);
|
|
|
|
if (v == NULL) {
|
|
|
|
Py_DECREF(n);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(n);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
}
|
|
|
|
Py_DECREF(n);
|
|
|
|
}
|
|
|
|
if (module->m_doc != NULL) {
|
|
|
|
v = PyUnicode_FromString(module->m_doc);
|
|
|
|
if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
|
|
|
|
Py_XDECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
}
|
|
|
|
m->md_def = module;
|
|
|
|
return (PyObject*)m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyModule_GetDict(PyObject *m)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2002-03-12 16:37:02 -04:00
|
|
|
PyObject *d;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-12 16:37:02 -04:00
|
|
|
d = ((PyModuleObject *)m) -> md_dict;
|
|
|
|
if (d == NULL)
|
|
|
|
((PyModuleObject *)m) -> md_dict = d = PyDict_New();
|
|
|
|
return d;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2007-08-25 23:21:42 -03:00
|
|
|
const char *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyModule_GetName(PyObject *m)
|
1990-10-26 12:00:11 -03:00
|
|
|
{
|
2002-03-12 16:37:02 -04:00
|
|
|
PyObject *d;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *nameobj;
|
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
1990-10-26 12:00:11 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-12 16:37:02 -04:00
|
|
|
d = ((PyModuleObject *)m)->md_dict;
|
|
|
|
if (d == NULL ||
|
|
|
|
(nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
|
2007-08-26 01:51:10 -03:00
|
|
|
!PyUnicode_Check(nameobj))
|
2002-03-12 16:37:02 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_SystemError, "nameless module");
|
1993-11-17 18:58:56 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-07 15:54:33 -03:00
|
|
|
return _PyUnicode_AsString(nameobj);
|
1990-10-26 12:00:11 -03:00
|
|
|
}
|
|
|
|
|
2007-08-25 23:21:42 -03:00
|
|
|
const char *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyModule_GetFilename(PyObject *m)
|
1999-02-15 10:47:16 -04:00
|
|
|
{
|
2002-03-12 16:37:02 -04:00
|
|
|
PyObject *d;
|
1999-02-15 10:47:16 -04:00
|
|
|
PyObject *fileobj;
|
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-12 16:37:02 -04:00
|
|
|
d = ((PyModuleObject *)m)->md_dict;
|
|
|
|
if (d == NULL ||
|
|
|
|
(fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
|
2007-10-14 23:52:41 -03:00
|
|
|
!PyUnicode_Check(fileobj))
|
2002-03-12 16:37:02 -04:00
|
|
|
{
|
1999-02-15 10:47:16 -04:00
|
|
|
PyErr_SetString(PyExc_SystemError, "module filename missing");
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-07 15:54:33 -03:00
|
|
|
return _PyUnicode_AsString(fileobj);
|
1999-02-15 10:47:16 -04:00
|
|
|
}
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
PyModuleDef*
|
|
|
|
PyModule_GetDef(PyObject* m)
|
|
|
|
{
|
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyModuleObject *)m)->md_def;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
PyModule_GetState(PyObject* m)
|
|
|
|
{
|
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyModuleObject *)m)->md_state;
|
|
|
|
}
|
|
|
|
|
1998-02-19 16:51:52 -04:00
|
|
|
void
|
2000-07-09 03:03:25 -03:00
|
|
|
_PyModule_Clear(PyObject *m)
|
1998-02-19 16:51:52 -04:00
|
|
|
{
|
|
|
|
/* 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). */
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t pos;
|
1998-02-19 16:51:52 -04:00
|
|
|
PyObject *key, *value;
|
|
|
|
PyObject *d;
|
|
|
|
|
|
|
|
d = ((PyModuleObject *)m)->md_dict;
|
2002-03-12 16:37:02 -04:00
|
|
|
if (d == NULL)
|
|
|
|
return;
|
1998-02-19 16:51:52 -04:00
|
|
|
|
|
|
|
/* First, clear only names starting with a single underscore */
|
|
|
|
pos = 0;
|
|
|
|
while (PyDict_Next(d, &pos, &key, &value)) {
|
2007-08-25 23:21:42 -03:00
|
|
|
if (value != Py_None && PyUnicode_Check(key)) {
|
2008-08-07 15:54:33 -03:00
|
|
|
const char *s = _PyUnicode_AsString(key);
|
1998-02-19 16:51:52 -04:00
|
|
|
if (s[0] == '_' && s[1] != '_') {
|
|
|
|
if (Py_VerboseFlag > 1)
|
1998-10-12 15:23:55 -03:00
|
|
|
PySys_WriteStderr("# clear[1] %s\n", s);
|
1998-02-19 16:51:52 -04:00
|
|
|
PyDict_SetItem(d, key, Py_None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next, clear all names except for __builtins__ */
|
|
|
|
pos = 0;
|
|
|
|
while (PyDict_Next(d, &pos, &key, &value)) {
|
2007-08-25 23:21:42 -03:00
|
|
|
if (value != Py_None && PyUnicode_Check(key)) {
|
2008-08-07 15:54:33 -03:00
|
|
|
const char *s = _PyUnicode_AsString(key);
|
1998-02-19 16:51:52 -04:00
|
|
|
if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
|
|
|
|
if (Py_VerboseFlag > 1)
|
1998-10-12 15:23:55 -03:00
|
|
|
PySys_WriteStderr("# clear[2] %s\n", s);
|
1998-02-19 16:51:52 -04:00
|
|
|
PyDict_SetItem(d, key, Py_None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: we leave __builtins__ in place, so that destructors
|
|
|
|
of non-global objects defined in this module can still use
|
|
|
|
builtins, in particularly 'None'. */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Methods */
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static int
|
2002-06-04 02:52:47 -03:00
|
|
|
module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
|
2001-08-02 01:15:00 -03:00
|
|
|
{
|
2006-02-27 12:46:16 -04:00
|
|
|
static char *kwlist[] = {"name", "doc", NULL};
|
2002-06-04 02:52:47 -03:00
|
|
|
PyObject *dict, *name = Py_None, *doc = Py_None;
|
2007-11-06 17:34:58 -04:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
|
2005-12-10 14:50:16 -04:00
|
|
|
kwlist, &name, &doc))
|
2002-06-04 02:52:47 -03:00
|
|
|
return -1;
|
|
|
|
dict = m->md_dict;
|
|
|
|
if (dict == NULL) {
|
|
|
|
dict = PyDict_New();
|
|
|
|
if (dict == NULL)
|
|
|
|
return -1;
|
|
|
|
m->md_dict = dict;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(dict, "__name__", name) < 0)
|
|
|
|
return -1;
|
|
|
|
if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
|
2001-08-02 01:15:00 -03:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
2000-07-09 03:03:25 -03:00
|
|
|
module_dealloc(PyModuleObject *m)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2001-08-29 20:54:21 -03:00
|
|
|
PyObject_GC_UnTrack(m);
|
2008-06-11 02:26:20 -03:00
|
|
|
if (m->md_def && m->md_def->m_free)
|
|
|
|
m->md_def->m_free(m);
|
1995-01-25 20:39:00 -04:00
|
|
|
if (m->md_dict != NULL) {
|
1998-02-19 16:51:52 -04:00
|
|
|
_PyModule_Clear((PyObject *)m);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(m->md_dict);
|
1995-01-25 20:39:00 -04:00
|
|
|
}
|
2009-01-17 17:06:43 -04:00
|
|
|
if (m->md_state != NULL)
|
|
|
|
PyMem_FREE(m->md_state);
|
2007-12-18 22:45:37 -04:00
|
|
|
Py_TYPE(m)->tp_free((PyObject *)m);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
module_repr(PyModuleObject *m)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2007-08-25 23:21:42 -03:00
|
|
|
const char *name;
|
|
|
|
const char *filename;
|
2001-08-16 17:39:24 -03:00
|
|
|
|
1999-02-15 10:47:16 -04:00
|
|
|
name = PyModule_GetName((PyObject *)m);
|
1993-11-17 18:58:56 -04:00
|
|
|
if (name == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1993-11-17 18:58:56 -04:00
|
|
|
name = "?";
|
|
|
|
}
|
1999-02-15 10:47:16 -04:00
|
|
|
filename = PyModule_GetFilename((PyObject *)m);
|
|
|
|
if (filename == NULL) {
|
|
|
|
PyErr_Clear();
|
2007-05-18 14:15:44 -03:00
|
|
|
return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
|
2001-08-16 17:39:24 -03:00
|
|
|
}
|
2007-05-18 14:15:44 -03:00
|
|
|
return PyUnicode_FromFormat("<module '%s' from '%s'>", name, filename);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2001-01-02 11:58:27 -04:00
|
|
|
static int
|
|
|
|
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
|
|
|
|
{
|
2008-06-11 02:26:20 -03:00
|
|
|
if (m->md_def && m->md_def->m_traverse) {
|
|
|
|
int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
Py_VISIT(m->md_dict);
|
2001-01-02 11:58:27 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
static int
|
|
|
|
module_clear(PyModuleObject *m)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(module_doc,
|
2002-06-04 03:02:35 -03:00
|
|
|
"module(name[, doc])\n\
|
|
|
|
\n\
|
|
|
|
Create a module object.\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
The name must be a string; the optional doc argument can have any type.");
|
2002-06-04 03:02:35 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyModule_Type = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2001-05-11 18:51:48 -03:00
|
|
|
"module", /* tp_name */
|
2001-08-29 20:54:21 -03:00
|
|
|
sizeof(PyModuleObject), /* tp_size */
|
2001-05-11 18:51:48 -03:00
|
|
|
0, /* tp_itemsize */
|
2001-08-02 01:15:00 -03:00
|
|
|
(destructor)module_dealloc, /* tp_dealloc */
|
2001-05-11 18:51:48 -03:00
|
|
|
0, /* tp_print */
|
2001-08-02 01:15:00 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2009-02-02 16:36:42 -04:00
|
|
|
0, /* tp_reserved */
|
2001-08-02 01:15:00 -03:00
|
|
|
(reprfunc)module_repr, /* tp_repr */
|
2001-05-11 18:51:48 -03:00
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
2003-07-16 19:04:11 -03:00
|
|
|
PyObject_GenericSetAttr, /* tp_setattro */
|
2001-05-11 18:51:48 -03:00
|
|
|
0, /* tp_as_buffer */
|
2001-08-29 20:54:21 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
2001-08-02 01:15:00 -03:00
|
|
|
Py_TPFLAGS_BASETYPE, /* tp_flags */
|
2002-06-04 03:02:35 -03:00
|
|
|
module_doc, /* tp_doc */
|
2001-05-11 18:51:48 -03:00
|
|
|
(traverseproc)module_traverse, /* tp_traverse */
|
2008-06-11 02:26:20 -03:00
|
|
|
(inquiry)module_clear, /* tp_clear */
|
2001-08-02 01:15:00 -03:00
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
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 */
|
2002-04-11 23:44:22 -03:00
|
|
|
PyObject_GC_Del, /* tp_free */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|