[Python-checkins] r45273 - python/trunk/Parser/myreadline.c python/trunk/Parser/pgenmain.c python/trunk/Parser/tokenizer.c

neal.norwitz python-checkins at python.org
Tue Apr 11 10:19:15 CEST 2006


Author: neal.norwitz
Date: Tue Apr 11 10:19:15 2006
New Revision: 45273

Modified:
   python/trunk/Parser/myreadline.c
   python/trunk/Parser/pgenmain.c
   python/trunk/Parser/tokenizer.c
Log:
As discussed on python-dev, really fix the PyMem_*/PyObject_* memory API
mismatches.  At least I hope this fixes them all.

This reverts part of my change from yesterday that converted everything
in Parser/*.c to use PyObject_* API.  The encoding doesn't really need
to use PyMem_*, however, it uses new_string() which must return PyMem_*
for handling the result of PyOS_Readline() which returns PyMem_* memory.

If there were 2 versions of new_string() one that returned PyMem_*
for tokens and one that return PyObject_* for encodings that could
also fix this problem.  I'm not sure which version would be clearer.
This seems to fix both Guido's and Phillip's problems, so it's good enough
for now.  After this change, it would be good to review Parser/*.c
for consistent use of the 2 memory APIs.



Modified: python/trunk/Parser/myreadline.c
==============================================================================
--- python/trunk/Parser/myreadline.c	(original)
+++ python/trunk/Parser/myreadline.c	Tue Apr 11 10:19:15 2006
@@ -111,7 +111,7 @@
 	size_t n;
 	char *p;
 	n = 100;
-	if ((p = (char *)PyObject_MALLOC(n)) == NULL)
+	if ((p = (char *)PyMem_MALLOC(n)) == NULL)
 		return NULL;
 	fflush(sys_stdout);
 #ifndef RISCOS
@@ -130,7 +130,7 @@
 	case 0: /* Normal case */
 		break;
 	case 1: /* Interrupt */
-		PyObject_FREE(p);
+		PyMem_FREE(p);
 		return NULL;
 	case -1: /* EOF */
 	case -2: /* Error */
@@ -141,7 +141,7 @@
 	n = strlen(p);
 	while (n > 0 && p[n-1] != '\n') {
 		size_t incr = n+2;
-		p = (char *)PyObject_REALLOC(p, n + incr);
+		p = (char *)PyMem_REALLOC(p, n + incr);
 		if (p == NULL)
 			return NULL;
 		if (incr > INT_MAX) {
@@ -151,14 +151,14 @@
 			break;
 		n += strlen(p+n);
 	}
-	return (char *)PyObject_REALLOC(p, n+1);
+	return (char *)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 PyObject_Malloc. */
+   Note: Python expects in return a buffer allocated with PyMem_Malloc. */
 
 char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
 

Modified: python/trunk/Parser/pgenmain.c
==============================================================================
--- python/trunk/Parser/pgenmain.c	(original)
+++ python/trunk/Parser/pgenmain.c	Tue Apr 11 10:19:15 2006
@@ -136,7 +136,7 @@
 PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
 {
 	size_t n = 1000;
-	char *p = PyObject_MALLOC(n);
+	char *p = PyMem_MALLOC(n);
 	char *q;
 	if (p == NULL)
 		return NULL;
@@ -149,7 +149,7 @@
 	n = strlen(p);
 	if (n > 0 && p[n-1] != '\n')
 		p[n-1] = '\n';
-	return PyObject_REALLOC(p, n+1);
+	return PyMem_REALLOC(p, n+1);
 }
 
 /* No-nonsense fgets */

Modified: python/trunk/Parser/tokenizer.c
==============================================================================
--- python/trunk/Parser/tokenizer.c	(original)
+++ python/trunk/Parser/tokenizer.c	Tue Apr 11 10:19:15 2006
@@ -164,7 +164,7 @@
 {
 	tok->decoding_erred = 1;
 	if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
-		PyObject_FREE(tok->buf);
+		PyMem_FREE(tok->buf);
 	tok->buf = NULL;
 	return NULL;		/* as if it were EOF */
 }
@@ -172,7 +172,7 @@
 static char *
 new_string(const char *s, Py_ssize_t len)
 {
-	char* result = (char *)PyObject_MALLOC(len + 1);
+	char* result = (char *)PyMem_MALLOC(len + 1);
 	if (result != NULL) {
 		memcpy(result, s, len);
 		result[len] = '\0';
@@ -237,7 +237,7 @@
 				char* r = new_string(begin, t - begin);
 				char* q = get_normal_name(r);
 				if (r != q) {
-					PyObject_FREE(r);
+					PyMem_FREE(r);
 					r = new_string(q, strlen(q));
 				}
 				return r;
@@ -278,18 +278,18 @@
 					tok->decoding_state = -1;
 				}
 				else
-					PyObject_FREE(cs);
+					PyMem_FREE(cs);
 #else
                                 /* Without Unicode support, we cannot
                                    process the coding spec. Since there
                                    won't be any Unicode literals, that
                                    won't matter. */
-				PyObject_FREE(cs);
+				PyMem_FREE(cs);
 #endif
 			}
 		} else {	/* then, compare cs with BOM */
 			r = (strcmp(tok->encoding, cs) == 0);
-			PyObject_FREE(cs);
+			PyMem_FREE(cs);
 		}
 	}
 	if (!r) {
@@ -335,7 +335,7 @@
 		return 1;
 	}
 	if (tok->encoding != NULL)
-		PyObject_FREE(tok->encoding);
+		PyMem_FREE(tok->encoding);
 	tok->encoding = new_string("utf-8", 5);	/* resulting is in utf-8 */
 	return 1;
   NON_BOM:
@@ -657,7 +657,7 @@
 	struct tok_state *tok = tok_new();
 	if (tok == NULL)
 		return NULL;
-	if ((tok->buf = (char *)PyObject_MALLOC(BUFSIZ)) == NULL) {
+	if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
 		PyTokenizer_Free(tok);
 		return NULL;
 	}
@@ -676,13 +676,13 @@
 PyTokenizer_Free(struct tok_state *tok)
 {
 	if (tok->encoding != NULL)
-		PyObject_FREE(tok->encoding);
+		PyMem_FREE(tok->encoding);
 #ifndef PGEN
 	Py_XDECREF(tok->decoding_readline);
 	Py_XDECREF(tok->decoding_buffer);
 #endif
 	if (tok->fp != NULL && tok->buf != NULL)
-		PyObject_FREE(tok->buf);
+		PyMem_FREE(tok->buf);
 	PyMem_FREE(tok);
 }
 
@@ -722,10 +722,10 @@
 	if (converted == NULL)
 		goto error_nomem;
 
-	PyObject_FREE(*inp);
+	PyMem_FREE(*inp);
 	*inp = converted;
 	if (tok->encoding != NULL)
-		PyObject_FREE(tok->encoding);
+		PyMem_FREE(tok->encoding);
 	tok->encoding = new_string(encoding, strlen(encoding));
 	if (tok->encoding == NULL)
 		goto error_nomem;
@@ -782,24 +782,24 @@
 			if (newtok == NULL)
 				tok->done = E_INTR;
 			else if (*newtok == '\0') {
-				PyObject_FREE(newtok);
+				PyMem_FREE(newtok);
 				tok->done = E_EOF;
 			}
 #if !defined(PGEN) && defined(Py_USING_UNICODE)
 			else if (tok_stdin_decode(tok, &newtok) != 0)
-				PyObject_FREE(newtok);
+				PyMem_FREE(newtok);
 #endif
 			else if (tok->start != NULL) {
 				size_t start = tok->start - tok->buf;
 				size_t oldlen = tok->cur - tok->buf;
 				size_t newlen = oldlen + strlen(newtok);
 				char *buf = tok->buf;
-				buf = (char *)PyObject_REALLOC(buf, newlen+1);
+				buf = (char *)PyMem_REALLOC(buf, newlen+1);
 				tok->lineno++;
 				if (buf == NULL) {
-					PyObject_FREE(tok->buf);
+					PyMem_FREE(tok->buf);
 					tok->buf = NULL;
-					PyObject_FREE(newtok);
+					PyMem_FREE(newtok);
 					tok->done = E_NOMEM;
 					return EOF;
 				}
@@ -807,7 +807,7 @@
 				tok->cur = tok->buf + oldlen;
 				tok->line_start = tok->cur;
 				strcpy(tok->buf + oldlen, newtok);
-				PyObject_FREE(newtok);
+				PyMem_FREE(newtok);
 				tok->inp = tok->buf + newlen;
 				tok->end = tok->inp + 1;
 				tok->start = tok->buf + start;
@@ -815,7 +815,7 @@
 			else {
 				tok->lineno++;
 				if (tok->buf != NULL)
-					PyObject_FREE(tok->buf);
+					PyMem_FREE(tok->buf);
 				tok->buf = newtok;
 				tok->line_start = tok->buf;
 				tok->cur = tok->buf;
@@ -831,7 +831,7 @@
 			if (tok->start == NULL) {
 				if (tok->buf == NULL) {
 					tok->buf = (char *)
-						PyObject_MALLOC(BUFSIZ);
+						PyMem_MALLOC(BUFSIZ);
 					if (tok->buf == NULL) {
 						tok->done = E_NOMEM;
 						return EOF;
@@ -866,8 +866,8 @@
 				Py_ssize_t curvalid = tok->inp - tok->buf;
 				Py_ssize_t newsize = curvalid + BUFSIZ;
 				char *newbuf = tok->buf;
-				newbuf = (char *)PyObject_REALLOC(newbuf,
-								  newsize);
+				newbuf = (char *)PyMem_REALLOC(newbuf,
+							       newsize);
 				if (newbuf == NULL) {
 					tok->done = E_NOMEM;
 					tok->cur = tok->inp;


More information about the Python-checkins mailing list