[Python-checkins] CVS: python/dist/src/Modules pyexpat.c,2.17,2.18

Fred L. Drake python-dev@python.org
Thu, 21 Sep 2000 13:10:25 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory slayer.i.sourceforge.net:/tmp/cvs-serv17218

Modified Files:
	pyexpat.c 
Log Message:

Remove memory leaks of strings/Unicode objects passed into the character
data and default handlers -- a new reference was being passed to
Py_BuildValue() for the "O" format character; using "N" plugs the leak.

Fixed two other (minor) leaks that occurred on various error conditions.

Removed uses of the UNLESS macro, which makes code hard to read, and is
Evil.


Index: pyexpat.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/pyexpat.c,v
retrieving revision 2.17
retrieving revision 2.18
diff -C2 -r2.17 -r2.18
*** pyexpat.c	2000/09/01 23:29:27	2.17
--- pyexpat.c	2000/09/21 20:10:23	2.18
***************
*** 94,98 ****
  conv_atts_using_unicode(XML_Char **atts)
  {
!     PyObject *attrs_obj = NULL;
      XML_Char **attrs_p, **attrs_k = NULL;
      int attrs_len;
--- 94,98 ----
  conv_atts_using_unicode(XML_Char **atts)
  {
!     PyObject *attrs_obj;
      XML_Char **attrs_p, **attrs_k = NULL;
      int attrs_len;
***************
*** 122,125 ****
--- 122,127 ----
              if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
                  Py_DECREF(attrs_obj);
+                 Py_DECREF(attr_str);
+                 Py_DECREF(value_str);
                  attrs_obj = NULL;
                  goto finally;
***************
*** 276,284 ****
  VOID_HANDLER(CharacterData, 
  	      (void *userData, const XML_Char *data, int len), 
! 	      ("(O)", conv_string_len_to_utf8(data,len)))
  #else
  VOID_HANDLER(CharacterData, 
  	      (void *userData, const XML_Char *data, int len), 
! 	      ("(O)", (self->returns_unicode 
  		       ? conv_string_len_to_unicode(data,len) 
  		       : conv_string_len_to_utf8(data,len))))
--- 278,286 ----
  VOID_HANDLER(CharacterData, 
  	      (void *userData, const XML_Char *data, int len), 
! 	      ("(N)", conv_string_len_to_utf8(data,len)))
  #else
  VOID_HANDLER(CharacterData, 
  	      (void *userData, const XML_Char *data, int len), 
! 	      ("(N)", (self->returns_unicode 
  		       ? conv_string_len_to_unicode(data,len) 
  		       : conv_string_len_to_utf8(data,len))))
***************
*** 333,345 ****
  VOID_HANDLER(Default,
  	      (void *userData,  const XML_Char *s, int len),
! 	      ("(O)", conv_string_len_to_utf8(s,len)))
  
  VOID_HANDLER(DefaultHandlerExpand,
  	      (void *userData,  const XML_Char *s, int len),
! 	      ("(O)", conv_string_len_to_utf8(s,len)))
  #else
  VOID_HANDLER(Default,
  	      (void *userData,  const XML_Char *s, int len),
! 	      ("(O)", (self->returns_unicode 
  		       ? conv_string_len_to_unicode(s,len) 
  		       : conv_string_len_to_utf8(s,len))))
--- 335,347 ----
  VOID_HANDLER(Default,
  	      (void *userData,  const XML_Char *s, int len),
! 	      ("(N)", conv_string_len_to_utf8(s,len)))
  
  VOID_HANDLER(DefaultHandlerExpand,
  	      (void *userData,  const XML_Char *s, int len),
! 	      ("(N)", conv_string_len_to_utf8(s,len)))
  #else
  VOID_HANDLER(Default,
  	      (void *userData,  const XML_Char *s, int len),
! 	      ("(N)", (self->returns_unicode 
  		       ? conv_string_len_to_unicode(s,len) 
  		       : conv_string_len_to_utf8(s,len))))
***************
*** 347,351 ****
  VOID_HANDLER(DefaultHandlerExpand,
  	      (void *userData,  const XML_Char *s, int len),
! 	      ("(O)", (self->returns_unicode 
  		       ? conv_string_len_to_unicode(s,len) 
  		       : conv_string_len_to_utf8(s,len))))
--- 349,353 ----
  VOID_HANDLER(DefaultHandlerExpand,
  	      (void *userData,  const XML_Char *s, int len),
! 	      ("(N)", (self->returns_unicode 
  		       ? conv_string_len_to_unicode(s,len) 
  		       : conv_string_len_to_utf8(s,len))))
***************
*** 371,417 ****
  
  
- /* File reading copied from cPickle */
- 
- #define UNLESS(E) if (!(E))
- 
- /*
- static int 
- read_other(xmlparseobject *self, char **s, int  n) {
-     PyObject *bytes=NULL, *str=NULL, *arg=NULL;
-     int res = -1;
- 
-     UNLESS(bytes = PyInt_FromLong(n)) {
-         if (!PyErr_Occurred())
-             PyErr_SetNone(PyExc_EOFError);
- 
-         goto finally;
-     }
- 
-     UNLESS(arg)
-         UNLESS(arg = PyTuple_New(1))
-             goto finally;
- 
-     Py_INCREF(bytes);
-     if (PyTuple_SetItem(arg, 0, bytes) < 0)
-         goto finally;
- 
-     UNLESS(str = PyObject_CallObject(self->read, arg))
-         goto finally;
- 
-     *s = PyString_AsString(str);
- 
-     res = n;
- 
- finally:
-      Py_XDECREF(arg);
-      Py_XDECREF(bytes);
- 
-      return res;
- }
- 
- */
- 
- 
- 
  /* ---------------------------------------------------------------- */
  
--- 373,376 ----
***************
*** 444,447 ****
--- 403,408 ----
  }
  
+ /* File reading copied from cPickle */
+ 
  #define BUF_SIZE 2048
  
***************
*** 454,474 ****
      int len = -1;
  
!     UNLESS(bytes = PyInt_FromLong(buf_size)) {
          if (!PyErr_Occurred())
              PyErr_SetNone(PyExc_EOFError);
          goto finally;
      }
!     UNLESS(arg)
!         UNLESS(arg = PyTuple_New(1))
          goto finally;
  
!     if (PyTuple_SetItem(arg, 0, bytes) < 0)
          goto finally;
  
!     UNLESS(str = PyObject_CallObject(meth, arg))
          goto finally;
  
      /* XXX what to do if it returns a Unicode string? */
!     UNLESS(PyString_Check(str)) {
          PyErr_Format(PyExc_TypeError, 
                       "read() did not return a string object (type=%.400s)",
--- 415,436 ----
      int len = -1;
  
!     fprintf(stderr, "calling readinst()\n");
! 
!     if ((bytes = PyInt_FromLong(buf_size)) == NULL) {
          if (!PyErr_Occurred())
              PyErr_SetNone(PyExc_EOFError);
          goto finally;
      }
!     if ((arg = PyTuple_New(1)) == NULL)
          goto finally;
  
!     if (PyTuple_SET_ITEM(arg, 0, bytes) < 0)
          goto finally;
  
!     if ((str = PyObject_CallObject(meth, arg)) == NULL)
          goto finally;
  
      /* XXX what to do if it returns a Unicode string? */
!     if (!PyString_Check(str)) {
          PyErr_Format(PyExc_TypeError, 
                       "read() did not return a string object (type=%.400s)",
***************
*** 486,492 ****
      }
      memcpy(buf, PyString_AsString(str), len);
-     Py_XDECREF(str);
  finally:
      Py_XDECREF(arg);
      return len;
  }
--- 448,454 ----
      }
      memcpy(buf, PyString_AsString(str), len);
  finally:
      Py_XDECREF(arg);
+     Py_XDECREF(str);
      return len;
  }
***************
*** 512,516 ****
      else{
          fp = NULL;
!         UNLESS(readmethod = PyObject_GetAttrString(f, "read")) {
              PyErr_Clear();
              PyErr_SetString(PyExc_TypeError, 
--- 474,479 ----
      else{
          fp = NULL;
!         readmethod = PyObject_GetAttrString(f, "read");
!         if (readmethod == NULL) {
              PyErr_Clear();
              PyErr_SetString(PyExc_TypeError,