[Patches] PyMem [4/8] - Parser/*

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


[ Parser/* ]

Nothing new compared to the first round of patches posted previously.
Just mixed mallocs medicine.

--
       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/Parser/myreadline.c PyMem/Parser/myreadline.c
*** PyCVS/Parser/myreadline.c	Sat Apr 29 00:24:10 2000
--- PyMem/Parser/myreadline.c	Sat Apr 29 00:36:20 2000
***************
*** 89,95 ****
  	int n;
  	char *p;
  	n = 100;
! 	if ((p = malloc(n)) == NULL)
  		return NULL;
  	fflush(stdout);
  	if (prompt)
--- 89,95 ----
  	int n;
  	char *p;
  	n = 100;
! 	if ((p = PyMem_MALLOC(n)) == NULL)
  		return NULL;
  	fflush(stdout);
  	if (prompt)
***************
*** 99,105 ****
  	case 0: /* Normal case */
  		break;
  	case 1: /* Interrupt */
! 		free(p);
  		return NULL;
  	case -1: /* EOF */
  	case -2: /* Error */
--- 99,105 ----
  	case 0: /* Normal case */
  		break;
  	case 1: /* Interrupt */
! 		PyMem_FREE(p);
  		return NULL;
  	case -1: /* EOF */
  	case -2: /* Error */
***************
*** 117,135 ****
  	n = strlen(p);
  	while (n > 0 && p[n-1] != '\n') {
  		int incr = n+2;
! 		p = realloc(p, n + incr);
  		if (p == NULL)
  			return NULL;
  		if (my_fgets(p+n, incr, stdin) != 0)
  			break;
  		n += strlen(p+n);
  	}
! 	return realloc(p, n+1);
  }
  
  
  /* By initializing this function pointer, systems embedding Python can
!    override the readline function. */
  
  char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
  
--- 117,137 ----
  	n = strlen(p);
  	while (n > 0 && p[n-1] != '\n') {
  		int incr = n+2;
! 		p = PyMem_REALLOC(p, n + incr);
  		if (p == NULL)
  			return NULL;
  		if (my_fgets(p+n, incr, stdin) != 0)
  			break;
  		n += strlen(p+n);
  	}
! 	return PyMem_REALLOC(p, n+1);
  }
  
  
  /* By initializing this function pointer, systems embedding Python can
!    override the readline function.
! 
!    Note: Python expects in return a buffer allocated with PyMem_Malloc. */
  
  char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
  
diff -cr PyCVS/Parser/parsetok.c PyMem/Parser/parsetok.c
*** PyCVS/Parser/parsetok.c	Sat Apr 29 00:24:10 2000
--- PyMem/Parser/parsetok.c	Sat Apr 29 00:38:47 2000
***************
*** 192,198 ****
  		err_ret->offset = tok->cur - tok->buf;
  		if (tok->buf != NULL) {
  			int len = tok->inp - tok->buf;
! 			err_ret->text = malloc(len + 1);
  			if (err_ret->text != NULL) {
  				if (len > 0)
  					strncpy(err_ret->text, tok->buf, len);
--- 192,198 ----
  		err_ret->offset = tok->cur - tok->buf;
  		if (tok->buf != NULL) {
  			int len = tok->inp - tok->buf;
! 			err_ret->text = PyMem_NEW(char, len + 1);
  			if (err_ret->text != NULL) {
  				if (len > 0)
  					strncpy(err_ret->text, tok->buf, len);
diff -cr PyCVS/Parser/pgenmain.c PyMem/Parser/pgenmain.c
*** PyCVS/Parser/pgenmain.c	Sat Apr 29 00:24:10 2000
--- PyMem/Parser/pgenmain.c	Sat Apr 29 00:43:20 2000
***************
*** 139,145 ****
  					putc(' ', stderr);
  			}
  			fprintf(stderr, "^\n");
! 			free(err.text);
  		}
  		Py_Exit(1);
  	}
--- 139,145 ----
  					putc(' ', stderr);
  			}
  			fprintf(stderr, "^\n");
! 			PyMem_DEL(err.text);
  		}
  		Py_Exit(1);
  	}
***************
*** 196,202 ****
  	char *prompt;
  {
  	int n = 1000;
! 	char *p = malloc(n);
  	char *q;
  	if (p == NULL)
  		return NULL;
--- 196,202 ----
  	char *prompt;
  {
  	int n = 1000;
! 	char *p = PyMem_MALLOC(n);
  	char *q;
  	if (p == NULL)
  		return NULL;
***************
*** 209,215 ****
  	n = strlen(p);
  	if (n > 0 && p[n-1] != '\n')
  		p[n-1] = '\n';
! 	return realloc(p, n+1);
  }
  
  #ifdef HAVE_STDARG_PROTOTYPES
--- 209,215 ----
  	n = strlen(p);
  	if (n > 0 && p[n-1] != '\n')
  		p[n-1] = '\n';
! 	return PyMem_REALLOC(p, n+1);
  }
  
  #ifdef HAVE_STDARG_PROTOTYPES
diff -cr PyCVS/Parser/tokenizer.c PyMem/Parser/tokenizer.c
*** PyCVS/Parser/tokenizer.c	Sat Apr 29 00:24:10 2000
--- PyMem/Parser/tokenizer.c	Sun Apr 30 08:14:20 2000
***************
*** 219,244 ****
  			if (new == NULL)
  				tok->done = E_INTR;
  			else if (*new == '\0') {
! 				free(new);
  				tok->done = E_EOF;
  			}
  			else if (tok->start != NULL) {
  				int start = tok->start - tok->buf;
  				int oldlen = tok->cur - tok->buf;
  				int newlen = oldlen + strlen(new);
! 				char *buf = realloc(tok->buf, newlen+1);
  				tok->lineno++;
  				if (buf == NULL) {
! 					free(tok->buf);
  					tok->buf = NULL;
! 					free(new);
  					tok->done = E_NOMEM;
  					return EOF;
  				}
  				tok->buf = buf;
  				tok->cur = tok->buf + oldlen;
  				strcpy(tok->buf + oldlen, new);
! 				free(new);
  				tok->inp = tok->buf + newlen;
  				tok->end = tok->inp + 1;
  				tok->start = tok->buf + start;
--- 219,245 ----
  			if (new == NULL)
  				tok->done = E_INTR;
  			else if (*new == '\0') {
! 				PyMem_FREE(new);
  				tok->done = E_EOF;
  			}
  			else if (tok->start != NULL) {
  				int start = tok->start - tok->buf;
  				int oldlen = tok->cur - tok->buf;
  				int newlen = oldlen + strlen(new);
! 				char *buf = tok->buf;
! 				PyMem_RESIZE(buf, char, newlen+1);
  				tok->lineno++;
  				if (buf == NULL) {
! 					PyMem_DEL(tok->buf);
  					tok->buf = NULL;
! 					PyMem_FREE(new);
  					tok->done = E_NOMEM;
  					return EOF;
  				}
  				tok->buf = buf;
  				tok->cur = tok->buf + oldlen;
  				strcpy(tok->buf + oldlen, new);
! 				PyMem_FREE(new);
  				tok->inp = tok->buf + newlen;
  				tok->end = tok->inp + 1;
  				tok->start = tok->buf + start;
***************
*** 246,252 ****
  			else {
  				tok->lineno++;
  				if (tok->buf != NULL)
! 					free(tok->buf);
  				tok->buf = new;
  				tok->cur = tok->buf;
  				tok->inp = strchr(tok->buf, '\0');
--- 247,253 ----
  			else {
  				tok->lineno++;
  				if (tok->buf != NULL)
! 					PyMem_DEL(tok->buf);
  				tok->buf = new;
  				tok->cur = tok->buf;
  				tok->inp = strchr(tok->buf, '\0');