diff -r 464e8fd7300d Modules/readline.c --- a/Modules/readline.c Thu Jun 13 10:08:00 2013 +0300 +++ b/Modules/readline.c Thu Jun 13 23:10:45 2013 +0200 @@ -1097,7 +1097,7 @@ call_readline(FILE *sys_stdin, FILE *sys /* We got an EOF, return a empty string. */ if (p == NULL) { - p = PyMem_Malloc(1); + p = malloc(1); if (p != NULL) *p = '\0'; RESTORE_LOCALE(saved_locale) @@ -1125,10 +1125,10 @@ call_readline(FILE *sys_stdin, FILE *sys if (strcmp(p, line)) add_history(p); } - /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and + /* Copy the malloc'ed buffer into a malloc'ed one and release the original. */ q = p; - p = PyMem_Malloc(n+2); + p = malloc(n+2); if (p != NULL) { strncpy(p, q, n); p[n] = '\n'; diff -r 464e8fd7300d Parser/myreadline.c --- a/Parser/myreadline.c Thu Jun 13 10:08:00 2013 +0300 +++ b/Parser/myreadline.c Thu Jun 13 23:10:45 2013 +0200 @@ -114,7 +114,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE size_t n; char *p; n = 100; - if ((p = (char *)PyMem_MALLOC(n)) == NULL) + if ((p = (char *)malloc(n)) == NULL) return NULL; fflush(sys_stdout); if (prompt) @@ -124,7 +124,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE case 0: /* Normal case */ break; case 1: /* Interrupt */ - PyMem_FREE(p); + free(p); return NULL; case -1: /* EOF */ case -2: /* Error */ @@ -135,7 +135,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE n = strlen(p); while (n > 0 && p[n-1] != '\n') { size_t incr = n+2; - p = (char *)PyMem_REALLOC(p, n + incr); + p = (char *)realloc(p, n + incr); if (p == NULL) return NULL; if (incr > INT_MAX) { @@ -145,14 +145,16 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE break; n += strlen(p+n); } - return (char *)PyMem_REALLOC(p, n+1); + return (char *)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. */ + The GIL is not hold when PyOS_ReadlineFunctionPointer is called, so + PyMem_Malloc() cannot be used. Python expects in return a buffer allocated + with malloc(). */ char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *); @@ -163,6 +165,8 @@ char * PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) { char *rv; + char *res; + size_t len; if (_PyOS_ReadlineTState == PyThreadState_GET()) { PyErr_SetString(PyExc_RuntimeError, @@ -207,7 +211,18 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys PyThread_release_lock(_PyOS_ReadlineLock); #endif + if (rv != NULL) { + len = strlen(rv); + res = PyMem_Malloc(len + 1); + if (res != NULL) + memcpy(res, rv, len + 1); + free(rv); + } + else { + res = NULL; + } + _PyOS_ReadlineTState = NULL; - return rv; + return res; }