[Patches] PyMem [5/8] - Python/*

Vladimir Marangozov Vladimir.Marangozov@inrialpes.fr
Sun, 30 Apr 2000 21:43:38 +0200 (CEST)


[ Python/* ]

Nothing new here either.
Malloc cleanup + small syntaxic code improvement in import.c

The docs should mention that PyOS_Readline, when hooked by embedding
apps, expects in return a buffer allocated with PyMem_Malloc, because
this buffer is subsequently released by Python with PyMem_FREE/DEL.

--
       Vladimir MARANGOZOV          | Vladimir.Marangozov@inrialpes.fr
http://sirac.inrialpes.fr/~marangoz | tel:(+33-4)76615277 fax:76615252

--

Disclaimer:

I confirm that, to the best of my knowledge and belief, this contribution is
free of any claims of third parties under copyright, patent or other rights
or interests ("claims").  To the extent that I have any such claims, I
hereby grant to CNRI a nonexclusive, irrevocable, royalty-free, worldwide
license to reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part of the
Python software and its related documentation, or any derivative versions
thereof, at no cost to CNRI or its licensed users, and to authorize others
to do so.

I acknowledge that CNRI may, at its sole discretion, decide whether or not
to incorporate this contribution in the Python software and its related
documentation.  I further grant CNRI permission to use my name and other
identifying information provided to CNRI by me for use in connection with
the Python software and its related documentation.

-------------------------------[ cut here ]---------------------------
diff -cr PyCVS/Python/bltinmodule.c PyMem/Python/bltinmodule.c
*** PyCVS/Python/bltinmodule.c	Sat Apr 29 00:24:10 2000
--- PyMem/Python/bltinmodule.c	Sat Apr 29 00:50:10 2000
***************
*** 1875,1881 ****
  		else { /* strip trailing '\n' */
  			result = PyString_FromStringAndSize(s, strlen(s)-1);
  		}
! 		free(s);
  		return result;
  	}
  	if (v != NULL) {
--- 1875,1881 ----
  		else { /* strip trailing '\n' */
  			result = PyString_FromStringAndSize(s, strlen(s)-1);
  		}
! 		PyMem_FREE(s);
  		return result;
  	}
  	if (v != NULL) {
diff -cr PyCVS/Python/ceval.c PyMem/Python/ceval.c
*** PyCVS/Python/ceval.c	Sat Apr 29 00:24:10 2000
--- PyMem/Python/ceval.c	Sat Apr 29 00:54:34 2000
***************
*** 2558,2564 ****
  		class);
  	
  	Py_DECREF(arg);
! 	PyMem_XDEL(k);
  	
  	return result;
  }
--- 2558,2565 ----
  		class);
  	
  	Py_DECREF(arg);
! 	if (k != NULL)
! 		PyMem_DEL(k);
  	
  	return result;
  }
diff -cr PyCVS/Python/compile.c PyMem/Python/compile.c
*** PyCVS/Python/compile.c	Sat Apr 29 00:24:10 2000
--- PyMem/Python/compile.c	Sat Apr 29 00:57:18 2000
***************
*** 112,118 ****
  	Py_XDECREF(co->co_filename);
  	Py_XDECREF(co->co_name);
  	Py_XDECREF(co->co_lnotab);
! 	PyMem_DEL(co);
  }
  
  static PyObject *
--- 112,118 ----
  	Py_XDECREF(co->co_filename);
  	Py_XDECREF(co->co_name);
  	Py_XDECREF(co->co_lnotab);
! 	PyObject_DEL(co);
  }
  
  static PyObject *
diff -cr PyCVS/Python/import.c PyMem/Python/import.c
*** PyCVS/Python/import.c	Sat Apr 29 00:24:10 2000
--- PyMem/Python/import.c	Sat Apr 29 01:06:21 2000
***************
*** 119,125 ****
  		++countD;
  	for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
  		++countS;
! 	filetab = malloc((countD + countS + 1) * sizeof(struct filedescr));
  	memcpy(filetab, _PyImport_DynLoadFiletab,
  	       countD * sizeof(struct filedescr));
  	memcpy(filetab + countD, _PyImport_StandardFiletab,
--- 119,125 ----
  		++countD;
  	for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
  		++countS;
! 	filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
  	memcpy(filetab, _PyImport_DynLoadFiletab,
  	       countD * sizeof(struct filedescr));
  	memcpy(filetab + countD, _PyImport_StandardFiletab,
***************
*** 2386,2395 ****
  }
  
  
! /* API for embedding applications that want to add their own entries to the
!    table of built-in modules.  This should normally be called *before*
!    Py_Initialize().  When the malloc() or realloc() call fails, -1 is returned
!    and the existing table is unchanged.
  
     After a similar function by Just van Rossum. */
  
--- 2386,2395 ----
  }
  
  
! /* API for embedding applications that want to add their own entries
!    to the table of built-in modules.  This should normally be called
!    *before* Py_Initialize().  When the table resize fails, -1 is
!    returned and the existing table is unchanged.
  
     After a similar function by Just van Rossum. */
  
***************
*** 2410,2419 ****
  		;
  
  	/* Allocate new memory for the combined table */
! 	if (our_copy == NULL)
! 		p = malloc((i+n+1) * sizeof(struct _inittab));
! 	else
! 		p = realloc(our_copy, (i+n+1) * sizeof(struct _inittab));
  	if (p == NULL)
  		return -1;
  
--- 2410,2417 ----
  		;
  
  	/* Allocate new memory for the combined table */
! 	p = our_copy;
! 	PyMem_RESIZE(p, struct _inittab, i+n+1);
  	if (p == NULL)
  		return -1;
  
diff -cr PyCVS/Python/marshal.c PyMem/Python/marshal.c
*** PyCVS/Python/marshal.c	Sat Apr 29 00:24:10 2000
--- PyMem/Python/marshal.c	Sun Apr 30 16:49:11 2000
***************
*** 514,530 ****
  			PyErr_SetString(PyExc_ValueError, "bad marshal data");
  			return NULL;
  		}
! 		buffer = (char *)Py_Malloc(n);
  		if (buffer == NULL)
! 		    return NULL;
  		if (r_string(buffer, (int)n, p) != n) {
! 			free(buffer);
  			PyErr_SetString(PyExc_EOFError,
  				"EOF read where object expected");
  			return NULL;
  		}
  		v = PyUnicode_DecodeUTF8(buffer, n, NULL);
! 		free(buffer);
  		return v;
  	    }
  	    
--- 514,530 ----
  			PyErr_SetString(PyExc_ValueError, "bad marshal data");
  			return NULL;
  		}
! 		buffer = PyMem_NEW(char, n);
  		if (buffer == NULL)
! 			return PyErr_NoMemory();
  		if (r_string(buffer, (int)n, p) != n) {
! 			PyMem_DEL(buffer);
  			PyErr_SetString(PyExc_EOFError,
  				"EOF read where object expected");
  			return NULL;
  		}
  		v = PyUnicode_DecodeUTF8(buffer, n, NULL);
! 		PyMem_DEL(buffer);
  		return v;
  	    }
  	    
diff -cr PyCVS/Python/pythonrun.c PyMem/Python/pythonrun.c
*** PyCVS/Python/pythonrun.c	Sat Apr 29 00:24:10 2000
--- PyMem/Python/pythonrun.c	Sat Apr 29 01:07:45 2000
***************
*** 542,548 ****
  	if (n == NULL) {
  		if (err.error == E_EOF) {
  			if (err.text)
! 				free(err.text);
  			return E_EOF;
  		}
  		err_input(&err);
--- 542,548 ----
  	if (n == NULL) {
  		if (err.error == E_EOF) {
  			if (err.text)
! 				PyMem_DEL(err.text);
  			return E_EOF;
  		}
  		err_input(&err);
***************
*** 1008,1014 ****
  	v = Py_BuildValue("(ziiz)", err->filename,
  			    err->lineno, err->offset, err->text);
  	if (err->text != NULL) {
! 		free(err->text);
  		err->text = NULL;
  	}
  	switch (err->error) {
--- 1008,1014 ----
  	v = Py_BuildValue("(ziiz)", err->filename,
  			    err->lineno, err->offset, err->text);
  	if (err->text != NULL) {
! 		PyMem_DEL(err->text);
  		err->text = NULL;
  	}
  	switch (err->error) {
diff -cr PyCVS/Python/traceback.c PyMem/Python/traceback.c
*** PyCVS/Python/traceback.c	Sat Apr 29 00:24:10 2000
--- PyMem/Python/traceback.c	Sat Apr 29 01:09:25 2000
***************
*** 71,77 ****
  	Py_TRASHCAN_SAFE_BEGIN(tb)
  	Py_XDECREF(tb->tb_next);
  	Py_XDECREF(tb->tb_frame);
! 	PyMem_DEL(tb);
  	Py_TRASHCAN_SAFE_END(tb)
  }
  
--- 71,77 ----
  	Py_TRASHCAN_SAFE_BEGIN(tb)
  	Py_XDECREF(tb->tb_next);
  	Py_XDECREF(tb->tb_frame);
! 	PyObject_DEL(tb);
  	Py_TRASHCAN_SAFE_END(tb)
  }