From 1a7425f67a0d141483d89ca80ca01e3cb7f6be92 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 7 Jul 2013 16:25:15 +0200 Subject: Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization * Replace malloc() with PyMem_RawMalloc() * Replace PyMem_Malloc() with PyMem_RawMalloc() where the GIL is not held. * _Py_char2wchar() now returns a buffer allocated by PyMem_RawMalloc(), instead of PyMem_Malloc() --- Modules/python.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'Modules/python.c') diff --git a/Modules/python.c b/Modules/python.c index 2c08b96856..aaa7fcff22 100644 --- a/Modules/python.c +++ b/Modules/python.c @@ -18,11 +18,19 @@ wmain(int argc, wchar_t **argv) int main(int argc, char **argv) { - wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1)); + wchar_t **argv_copy; /* We need a second copies, as Python might modify the first one. */ - wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1)); + wchar_t **argv_copy2; int i, res; char *oldloc; + + argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)); + argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)); + if (!argv_copy || !argv_copy2) { + fprintf(stderr, "out of memory\n"); + return 1; + } + /* 754 requires that FP exceptions run in "no stop" mode by default, * and until C vendors implement C99's ways to control FP exceptions, * Python requires non-stop mode. Alas, some platforms enable FP @@ -34,10 +42,7 @@ main(int argc, char **argv) m = fpgetmask(); fpsetmask(m & ~FP_X_OFL); #endif - if (!argv_copy || !argv_copy2) { - fprintf(stderr, "out of memory\n"); - return 1; - } + oldloc = strdup(setlocale(LC_ALL, NULL)); setlocale(LC_ALL, ""); for (i = 0; i < argc; i++) { @@ -57,10 +62,10 @@ main(int argc, char **argv) free(oldloc); res = Py_Main(argc, argv_copy); for (i = 0; i < argc; i++) { - PyMem_Free(argv_copy2[i]); + PyMem_RawFree(argv_copy2[i]); } - PyMem_Free(argv_copy); - PyMem_Free(argv_copy2); + PyMem_RawFree(argv_copy); + PyMem_RawFree(argv_copy2); return res; } #endif -- cgit v1.2.1