[Python-checkins] CVS: python/dist/src/Objects moduleobject.c,2.40,2.40.8.1

Michael Hudson mwh@users.sourceforge.net
Fri, 15 Mar 2002 02:35:38 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv2456

Modified Files:
      Tag: release22-maint
	moduleobject.c 
Log Message:
backport gvanrossum's checkin of
    revision 2.41 of moduleobject.c

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.40.8.1
diff -C2 -d -r2.40 -r2.40.8.1
*** moduleobject.c	21 Oct 2001 22:27:35 -0000	2.40
--- moduleobject.c	15 Mar 2002 10:35:36 -0000	2.40.8.1
***************
*** 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 */