Mailing List Archive

CVS: python/dist/src/Objects moduleobject.c,2.40,2.41
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv14407

Modified Files:
moduleobject.c
Log Message:
Fix for SF bug #529050 - ModuleType.__new__ crash.

There were several places that assumed the md_dict field was always
set, but it needn't be. Fixed these to be more careful.

I changed PyModule_GetDict() to initialize md_dict to a new dictionary
if it's NULL.

Bugfix candidate.


Index: moduleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/moduleobject.c,v
retrieving revision 2.40
retrieving revision 2.41
diff -C2 -d -r2.40 -r2.41
*** moduleobject.c 21 Oct 2001 22:27:35 -0000 2.40
--- moduleobject.c 12 Mar 2002 20:37:02 -0000 2.41
***************
*** 44,52 ****
PyModule_GetDict(PyObject *m)
{
if (!PyModule_Check(m)) {
PyErr_BadInternalCall();
return NULL;
}
! return ((PyModuleObject *)m) -> md_dict;
}

--- 44,56 ----
PyModule_GetDict(PyObject *m)
{
+ 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;
}

***************
*** 54,57 ****
--- 58,62 ----
PyModule_GetName(PyObject *m)
{
+ PyObject *d;
PyObject *nameobj;
if (!PyModule_Check(m)) {
***************
*** 59,65 ****
return NULL;
}
! nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
! "__name__");
! if (nameobj == NULL || !PyString_Check(nameobj)) {
PyErr_SetString(PyExc_SystemError, "nameless module");
return NULL;
--- 64,72 ----
return NULL;
}
! d = ((PyModuleObject *)m)->md_dict;
! if (d == NULL ||
! (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
! !PyString_Check(nameobj))
! {
PyErr_SetString(PyExc_SystemError, "nameless module");
return NULL;
***************
*** 71,74 ****
--- 78,82 ----
PyModule_GetFilename(PyObject *m)
{
+ PyObject *d;
PyObject *fileobj;
if (!PyModule_Check(m)) {
***************
*** 76,82 ****
return NULL;
}
! fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
! "__file__");
! if (fileobj == NULL || !PyString_Check(fileobj)) {
PyErr_SetString(PyExc_SystemError, "module filename missing");
return NULL;
--- 84,92 ----
return NULL;
}
! d = ((PyModuleObject *)m)->md_dict;
! if (d == NULL ||
! (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
! !PyString_Check(fileobj))
! {
PyErr_SetString(PyExc_SystemError, "module filename missing");
return NULL;
***************
*** 100,103 ****
--- 110,115 ----

d = ((PyModuleObject *)m)->md_dict;
+ if (d == NULL)
+ return;

/* First, clear only names starting with a single underscore */