Skip to content

Commit 30f5c21

Browse files
author
neal.norwitz
committed
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. git-svn-id: http://svn.python.org/projects/python/trunk@45273 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 3c1002d commit 30f5c21

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

Parser/myreadline.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
111111
size_t n;
112112
char *p;
113113
n = 100;
114-
if ((p = (char *)PyObject_MALLOC(n)) == NULL)
114+
if ((p = (char *)PyMem_MALLOC(n)) == NULL)
115115
return NULL;
116116
fflush(sys_stdout);
117117
#ifndef RISCOS
@@ -130,7 +130,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
130130
case 0: /* Normal case */
131131
break;
132132
case 1: /* Interrupt */
133-
PyObject_FREE(p);
133+
PyMem_FREE(p);
134134
return NULL;
135135
case -1: /* EOF */
136136
case -2: /* Error */
@@ -141,7 +141,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
141141
n = strlen(p);
142142
while (n > 0 && p[n-1] != '\n') {
143143
size_t incr = n+2;
144-
p = (char *)PyObject_REALLOC(p, n + incr);
144+
p = (char *)PyMem_REALLOC(p, n + incr);
145145
if (p == NULL)
146146
return NULL;
147147
if (incr > INT_MAX) {
@@ -151,14 +151,14 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
151151
break;
152152
n += strlen(p+n);
153153
}
154-
return (char *)PyObject_REALLOC(p, n+1);
154+
return (char *)PyMem_REALLOC(p, n+1);
155155
}
156156

157157

158158
/* By initializing this function pointer, systems embedding Python can
159159
override the readline function.
160160
161-
Note: Python expects in return a buffer allocated with PyObject_Malloc. */
161+
Note: Python expects in return a buffer allocated with PyMem_Malloc. */
162162

163163
char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
164164

Parser/pgenmain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ char *
136136
PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
137137
{
138138
size_t n = 1000;
139-
char *p = PyObject_MALLOC(n);
139+
char *p = PyMem_MALLOC(n);
140140
char *q;
141141
if (p == NULL)
142142
return NULL;
@@ -149,7 +149,7 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
149149
n = strlen(p);
150150
if (n > 0 && p[n-1] != '\n')
151151
p[n-1] = '\n';
152-
return PyObject_REALLOC(p, n+1);
152+
return PyMem_REALLOC(p, n+1);
153153
}
154154

155155
/* No-nonsense fgets */

Parser/tokenizer.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ error_ret(struct tok_state *tok) /* XXX */
164164
{
165165
tok->decoding_erred = 1;
166166
if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
167-
PyObject_FREE(tok->buf);
167+
PyMem_FREE(tok->buf);
168168
tok->buf = NULL;
169169
return NULL; /* as if it were EOF */
170170
}
171171

172172
static char *
173173
new_string(const char *s, Py_ssize_t len)
174174
{
175-
char* result = (char *)PyObject_MALLOC(len + 1);
175+
char* result = (char *)PyMem_MALLOC(len + 1);
176176
if (result != NULL) {
177177
memcpy(result, s, len);
178178
result[len] = '\0';
@@ -237,7 +237,7 @@ get_coding_spec(const char *s, Py_ssize_t size)
237237
char* r = new_string(begin, t - begin);
238238
char* q = get_normal_name(r);
239239
if (r != q) {
240-
PyObject_FREE(r);
240+
PyMem_FREE(r);
241241
r = new_string(q, strlen(q));
242242
}
243243
return r;
@@ -278,18 +278,18 @@ check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
278278
tok->decoding_state = -1;
279279
}
280280
else
281-
PyObject_FREE(cs);
281+
PyMem_FREE(cs);
282282
#else
283283
/* Without Unicode support, we cannot
284284
process the coding spec. Since there
285285
won't be any Unicode literals, that
286286
won't matter. */
287-
PyObject_FREE(cs);
287+
PyMem_FREE(cs);
288288
#endif
289289
}
290290
} else { /* then, compare cs with BOM */
291291
r = (strcmp(tok->encoding, cs) == 0);
292-
PyObject_FREE(cs);
292+
PyMem_FREE(cs);
293293
}
294294
}
295295
if (!r) {
@@ -335,7 +335,7 @@ check_bom(int get_char(struct tok_state *),
335335
return 1;
336336
}
337337
if (tok->encoding != NULL)
338-
PyObject_FREE(tok->encoding);
338+
PyMem_FREE(tok->encoding);
339339
tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */
340340
return 1;
341341
NON_BOM:
@@ -657,7 +657,7 @@ PyTokenizer_FromFile(FILE *fp, char *ps1, char *ps2)
657657
struct tok_state *tok = tok_new();
658658
if (tok == NULL)
659659
return NULL;
660-
if ((tok->buf = (char *)PyObject_MALLOC(BUFSIZ)) == NULL) {
660+
if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
661661
PyTokenizer_Free(tok);
662662
return NULL;
663663
}
@@ -676,13 +676,13 @@ void
676676
PyTokenizer_Free(struct tok_state *tok)
677677
{
678678
if (tok->encoding != NULL)
679-
PyObject_FREE(tok->encoding);
679+
PyMem_FREE(tok->encoding);
680680
#ifndef PGEN
681681
Py_XDECREF(tok->decoding_readline);
682682
Py_XDECREF(tok->decoding_buffer);
683683
#endif
684684
if (tok->fp != NULL && tok->buf != NULL)
685-
PyObject_FREE(tok->buf);
685+
PyMem_FREE(tok->buf);
686686
PyMem_FREE(tok);
687687
}
688688

@@ -722,10 +722,10 @@ tok_stdin_decode(struct tok_state *tok, char **inp)
722722
if (converted == NULL)
723723
goto error_nomem;
724724

725-
PyObject_FREE(*inp);
725+
PyMem_FREE(*inp);
726726
*inp = converted;
727727
if (tok->encoding != NULL)
728-
PyObject_FREE(tok->encoding);
728+
PyMem_FREE(tok->encoding);
729729
tok->encoding = new_string(encoding, strlen(encoding));
730730
if (tok->encoding == NULL)
731731
goto error_nomem;
@@ -782,40 +782,40 @@ tok_nextc(register struct tok_state *tok)
782782
if (newtok == NULL)
783783
tok->done = E_INTR;
784784
else if (*newtok == '\0') {
785-
PyObject_FREE(newtok);
785+
PyMem_FREE(newtok);
786786
tok->done = E_EOF;
787787
}
788788
#if !defined(PGEN) && defined(Py_USING_UNICODE)
789789
else if (tok_stdin_decode(tok, &newtok) != 0)
790-
PyObject_FREE(newtok);
790+
PyMem_FREE(newtok);
791791
#endif
792792
else if (tok->start != NULL) {
793793
size_t start = tok->start - tok->buf;
794794
size_t oldlen = tok->cur - tok->buf;
795795
size_t newlen = oldlen + strlen(newtok);
796796
char *buf = tok->buf;
797-
buf = (char *)PyObject_REALLOC(buf, newlen+1);
797+
buf = (char *)PyMem_REALLOC(buf, newlen+1);
798798
tok->lineno++;
799799
if (buf == NULL) {
800-
PyObject_FREE(tok->buf);
800+
PyMem_FREE(tok->buf);
801801
tok->buf = NULL;
802-
PyObject_FREE(newtok);
802+
PyMem_FREE(newtok);
803803
tok->done = E_NOMEM;
804804
return EOF;
805805
}
806806
tok->buf = buf;
807807
tok->cur = tok->buf + oldlen;
808808
tok->line_start = tok->cur;
809809
strcpy(tok->buf + oldlen, newtok);
810-
PyObject_FREE(newtok);
810+
PyMem_FREE(newtok);
811811
tok->inp = tok->buf + newlen;
812812
tok->end = tok->inp + 1;
813813
tok->start = tok->buf + start;
814814
}
815815
else {
816816
tok->lineno++;
817817
if (tok->buf != NULL)
818-
PyObject_FREE(tok->buf);
818+
PyMem_FREE(tok->buf);
819819
tok->buf = newtok;
820820
tok->line_start = tok->buf;
821821
tok->cur = tok->buf;
@@ -831,7 +831,7 @@ tok_nextc(register struct tok_state *tok)
831831
if (tok->start == NULL) {
832832
if (tok->buf == NULL) {
833833
tok->buf = (char *)
834-
PyObject_MALLOC(BUFSIZ);
834+
PyMem_MALLOC(BUFSIZ);
835835
if (tok->buf == NULL) {
836836
tok->done = E_NOMEM;
837837
return EOF;
@@ -866,8 +866,8 @@ tok_nextc(register struct tok_state *tok)
866866
Py_ssize_t curvalid = tok->inp - tok->buf;
867867
Py_ssize_t newsize = curvalid + BUFSIZ;
868868
char *newbuf = tok->buf;
869-
newbuf = (char *)PyObject_REALLOC(newbuf,
870-
newsize);
869+
newbuf = (char *)PyMem_REALLOC(newbuf,
870+
newsize);
871871
if (newbuf == NULL) {
872872
tok->done = E_NOMEM;
873873
tok->cur = tok->inp;

0 commit comments

Comments
 (0)