[Python-3000-checkins] r58555 - python/branches/py3k/Parser/tokenizer.c

brett.cannon python-3000-checkins at python.org
Sat Oct 20 04:54:14 CEST 2007


Author: brett.cannon
Date: Sat Oct 20 04:54:14 2007
New Revision: 58555

Modified:
   python/branches/py3k/Parser/tokenizer.c
Log:
Plug a memory leak where a struct tok_state was not being freed.

Also tweak a comparison that was going farther than needed.


Modified: python/branches/py3k/Parser/tokenizer.c
==============================================================================
--- python/branches/py3k/Parser/tokenizer.c	(original)
+++ python/branches/py3k/Parser/tokenizer.c	Sat Oct 20 04:54:14 2007
@@ -52,6 +52,7 @@
 static int tok_nextc(struct tok_state *tok);
 static void tok_backup(struct tok_state *tok, int c);
 
+
 /* Token names */
 
 char *_PyParser_TokenNames[] = {
@@ -1610,18 +1611,25 @@
 char *
 PyTokenizer_FindEncoding(FILE *fp) {
 	struct tok_state *tok;
-	char *p_start=NULL, *p_end=NULL;
+	char *p_start=NULL, *p_end=NULL, *encoding=NULL;
 
 	if ((tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL)) == NULL) {
 		rewind(fp);
 		return NULL;
 	}
-	while(((tok->lineno <= 2) && (tok->done == E_OK))) {
+	while(((tok->lineno < 2) && (tok->done == E_OK))) {
 		PyTokenizer_Get(tok, &p_start, &p_end);
 	}
 
 	rewind(fp);
-	return tok->encoding;
+
+	if (tok->encoding) {
+            encoding = (char *)PyMem_MALLOC(strlen(tok->encoding));
+            strcpy(encoding, tok->encoding);
+        }
+	PyTokenizer_Free(tok);
+
+	return encoding;
 }
 
 #ifdef Py_DEBUG


More information about the Python-3000-checkins mailing list