static void _cleanup_editlineobject(EditLineObject *self) { /* mop up libedit */ if (self->el) el_end(self->el); if (self->tok) tok_end(self->tok); if (self->hist) history_end(self->hist); /* tidy up the allocated bits */ if (self->name) PyMem_RawFree(self->name); if (self->prompt) PyMem_RawFree(self->prompt); if (self->rprompt) PyMem_RawFree(self->rprompt); if (self->buffer) PyMem_RawFree(self->buffer); /* manage file-handles? */ if (self->fin) fclose(self->fin); if (self->fout) fclose(self->fout); if (self->ferr) fclose(self->ferr); /* release my ownership of the I/O refs */ Py_DECREF(self->pyin); Py_DECREF(self->pyout); Py_DECREF(self->pyerr); }
int nacl_main(int argc, char **argv) { if (nacl_startup_untar(argv[0], DATA_FILE, "/")) return -1; wchar_t **argv_copy; /* We need a second copy, as Python might modify the first one. */ wchar_t **argv_copy2; int i, res; char *oldloc; #ifdef __FreeBSD__ fp_except_t m; #endif 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 * exceptions by default. Here we disable them. */ #ifdef __FreeBSD__ m = fpgetmask(); fpsetmask(m & ~FP_X_OFL); #endif oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL)); if (!oldloc) { fprintf(stderr, "out of memory\n"); return 1; } setlocale(LC_ALL, ""); for (i = 0; i < argc; i++) { argv_copy[i] = _Py_char2wchar(argv[i], NULL); if (!argv_copy[i]) { PyMem_RawFree(oldloc); fprintf(stderr, "Fatal Python error: " "unable to decode the command line argument #%i\n", i + 1); return 1; } argv_copy2[i] = argv_copy[i]; } argv_copy2[argc] = argv_copy[argc] = NULL; setlocale(LC_ALL, oldloc); PyMem_RawFree(oldloc); res = Py_Main(argc, argv_copy); for (i = 0; i < argc; i++) { PyMem_RawFree(argv_copy2[i]); } PyMem_RawFree(argv_copy); PyMem_RawFree(argv_copy2); return res; }
wchar_t* _Py_wrealpath(const wchar_t *path, wchar_t *resolved_path, size_t resolved_path_size) { char *cpath; char cresolved_path[MAXPATHLEN]; wchar_t *wresolved_path; char *res; size_t r; cpath = _Py_wchar2char(path, NULL); if (cpath == NULL) { errno = EINVAL; return NULL; } res = realpath(cpath, cresolved_path); PyMem_Free(cpath); if (res == NULL) return NULL; wresolved_path = _Py_char2wchar(cresolved_path, &r); if (wresolved_path == NULL) { errno = EINVAL; return NULL; } if (resolved_path_size <= r) { PyMem_RawFree(wresolved_path); errno = EINVAL; return NULL; } wcsncpy(resolved_path, wresolved_path, resolved_path_size); PyMem_RawFree(wresolved_path); return resolved_path; }
wchar_t* _Py_wgetcwd(wchar_t *buf, size_t size) { #ifdef MS_WINDOWS int isize = (int)Py_MIN(size, INT_MAX); return _wgetcwd(buf, isize); #else char fname[MAXPATHLEN]; wchar_t *wname; size_t len; if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) return NULL; wname = _Py_char2wchar(fname, &len); if (wname == NULL) return NULL; if (size <= len) { PyMem_RawFree(wname); return NULL; } wcsncpy(buf, wname, size); PyMem_RawFree(wname); return buf; #endif }
static void calculate_free(PyCalculatePath *calculate) { PyMem_RawFree(calculate->pythonpath); PyMem_RawFree(calculate->prefix); PyMem_RawFree(calculate->exec_prefix); PyMem_RawFree(calculate->lib_python); PyMem_RawFree(calculate->path_env); }
PyObject* getFileData(int argc, char *argv[]) { PyObject *BioModule = PyImport_ImportModule("Bio"); const char *filename, *filetype, *pycmdToRun; filename = (const char *)PyUnicode_DecodeFSDefault(argv[1]); filetype = (const char *)PyUnicode_DecodeFSDefault(argv[2]); std::string cmdToRun = "import Bio\nBio.SeqIO.parse("; cmdToRun = cmdToRun + filename + std::string(",") + filetype; pycmdToRun = cmdToRun.c_str(); wchar_t *program = Py_DecodeLocale(argv[0], NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); exit(1); } Py_SetProgramName(program); /* optional but recommended */ Py_Initialize(); PyObject* filedata; filedata = PyRun_String(pycmdToRun, 0, NULL, NULL); Py_DECREF(filename); Py_DECREF(filetype); Py_Finalize(); PyMem_RawFree(program); return filedata; }
/* * Delete all thread states except the one passed as argument. * Note that, if there is a current thread state, it *must* be the one * passed as argument. Also, this won't touch any other interpreters * than the current one, since we don't know which thread state should * be kept in those other interpreteres. */ void _PyThreadState_DeleteExcept(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; PyThreadState *p, *next, *garbage; HEAD_LOCK(); /* Remove all thread states, except tstate, from the linked list of thread states. This will allow calling PyThreadState_Clear() without holding the lock. */ garbage = interp->tstate_head; if (garbage == tstate) garbage = tstate->next; if (tstate->prev) tstate->prev->next = tstate->next; if (tstate->next) tstate->next->prev = tstate->prev; tstate->prev = tstate->next = NULL; interp->tstate_head = tstate; HEAD_UNLOCK(); /* Clear and deallocate all stale thread states. Even if this executes Python code, we should be safe since it executes in the current thread, not one of the stale threads. */ for (p = garbage; p; p = next) { next = p->next; PyThreadState_Clear(p); PyMem_RawFree(p); } }
/* Forget everything not associated with the current thread id. * This function is called from PyOS_AfterFork(). It is necessary * because other thread ids which were in use at the time of the fork * may be reused for new threads created in the forked process. */ void PyThread_ReInitTLS(void) { long id = PyThread_get_thread_ident(); struct key *p, **q; if (!keymutex) return; /* As with interpreter_lock in PyEval_ReInitThreads() we just create a new lock without freeing the old one */ keymutex = PyThread_allocate_lock(); /* Delete all keys which do not match the current thread id */ q = &keyhead; while ((p = *q) != NULL) { if (p->id != id) { *q = p->next; PyMem_RawFree((void *)p); /* NB This does *not* free p->value! */ } else q = &p->next; } }
static PyObject * overlapped_RegisterWaitWithQueue(PyObject *self, PyObject *args) { HANDLE NewWaitObject; HANDLE Object; ULONG Milliseconds; struct PostCallbackData data, *pdata; if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE F_POINTER F_DWORD, &Object, &data.CompletionPort, &data.Overlapped, &Milliseconds)) return NULL; /* Use PyMem_RawMalloc() rather than PyMem_Malloc(), since PostToQueueCallback() will call PyMem_Free() from a new C thread which doesn't hold the GIL. */ pdata = PyMem_RawMalloc(sizeof(struct PostCallbackData)); if (pdata == NULL) return SetFromWindowsErr(0); *pdata = data; if (!RegisterWaitForSingleObject( &NewWaitObject, Object, (WAITORTIMERCALLBACK)PostToQueueCallback, pdata, Milliseconds, WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE)) { PyMem_RawFree(pdata); return SetFromWindowsErr(0); } return Py_BuildValue(F_HANDLE, NewWaitObject); }
void PyInterpreterState_Delete(PyInterpreterState *interp) { PyInterpreterState **p; zapthreads(interp); HEAD_LOCK(); for (p = &_PyRuntime.interpreters.head; ; p = &(*p)->next) { if (*p == NULL) Py_FatalError( "PyInterpreterState_Delete: invalid interp"); if (*p == interp) break; } if (interp->tstate_head != NULL) Py_FatalError("PyInterpreterState_Delete: remaining threads"); *p = interp->next; if (_PyRuntime.interpreters.main == interp) { _PyRuntime.interpreters.main = NULL; if (_PyRuntime.interpreters.head != NULL) Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters"); } HEAD_UNLOCK(); if (interp->id_mutex != NULL) { PyThread_free_lock(interp->id_mutex); } if (interp->ceval.pending.lock != NULL) { PyThread_free_lock(interp->ceval.pending.lock); } PyMem_RawFree(interp); }
int Py_SetStandardStreamEncoding(const char *encoding, const char *errors) { if (Py_IsInitialized()) { /* This is too late to have any effect */ return -1; } /* Can't call PyErr_NoMemory() on errors, as Python hasn't been * initialised yet. * * However, the raw memory allocators are initialised appropriately * as C static variables, so _PyMem_RawStrdup is OK even though * Py_Initialize hasn't been called yet. */ if (encoding) { _Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding); if (!_Py_StandardStreamEncoding) { return -2; } } if (errors) { _Py_StandardStreamErrors = _PyMem_RawStrdup(errors); if (!_Py_StandardStreamErrors) { if (_Py_StandardStreamEncoding) { PyMem_RawFree(_Py_StandardStreamEncoding); } return -3; } } return 0; }
/* * Delete all interpreter states except the main interpreter. If there * is a current interpreter state, it *must* be the main interpreter. */ void _PyInterpreterState_DeleteExceptMain() { PyThreadState *tstate = PyThreadState_Swap(NULL); if (tstate != NULL && tstate->interp != _PyRuntime.interpreters.main) { Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter"); } HEAD_LOCK(); PyInterpreterState *interp = _PyRuntime.interpreters.head; _PyRuntime.interpreters.head = NULL; while (interp != NULL) { if (interp == _PyRuntime.interpreters.main) { _PyRuntime.interpreters.main->next = NULL; _PyRuntime.interpreters.head = interp; interp = interp->next; continue; } PyInterpreterState_Clear(interp); // XXX must activate? zapthreads(interp); if (interp->id_mutex != NULL) { PyThread_free_lock(interp->id_mutex); } PyInterpreterState *prev_interp = interp; interp = interp->next; PyMem_RawFree(prev_interp); } HEAD_UNLOCK(); if (_PyRuntime.interpreters.head == NULL) { Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main"); } PyThreadState_Swap(tstate); }
void PyInterpreterState_Delete(PyInterpreterState *interp) { PyInterpreterState **p; zapthreads(interp); HEAD_LOCK(); for (p = &interp_head; ; p = &(*p)->next) { if (*p == NULL) Py_FatalError( "PyInterpreterState_Delete: invalid interp"); if (*p == interp) break; } if (interp->tstate_head != NULL) Py_FatalError("PyInterpreterState_Delete: remaining threads"); *p = interp->next; HEAD_UNLOCK(); PyMem_RawFree(interp); #ifdef WITH_THREAD if (interp_head == NULL && head_mutex != NULL) { PyThread_free_lock(head_mutex); head_mutex = NULL; } #endif }
static wchar_t* Py_DecodeLocale(const char* arg, size_t*) { size_t argsize = mbstowcs(NULL, arg, 0); if (argsize == (size_t)-1) { return NULL; } if (argsize == PY_SSIZE_T_MAX) { return NULL; } if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) { return NULL; } wchar_t *res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t)); if (!res) { return NULL; } size_t count = mbstowcs(res, arg, argsize); if (count != (size_t)-1) { wchar_t *tmp; for (tmp = res; *tmp != 0 && !Py_UNICODE_IS_SURROGATE(*tmp); tmp++) { } if (*tmp == 0) { return res; } } PyMem_RawFree(res); return NULL; }
static int elObj_rprompt_setter(EditLineObject *self, PyObject *value, void *closure) { int rv = 0; int n; char *new_rprompt; const char *encoded_c; PyObject *encoded; if (value == NULL) { PyErr_SetString(PyExc_TypeError, "Cannot delete the rprompt attribute"); return -1; } if (! PyUnicode_Check(value)) { PyErr_SetString(PyExc_TypeError, "The rprompt attribute value must be a string"); return -1; } /* it is stored as a C string */ encoded = encode(value); if (encoded == NULL) { PyErr_SetString(PyExc_ValueError, "The rprompt attribute could not be encoded"); return -1; } /* get it as a c-string */ encoded_c = PyBytes_AS_STRING(encoded); n = strlen(encoded_c); /* create a RawMalloc'd buffer */ new_rprompt = PyMem_RawMalloc(n+1); if (new_rprompt == NULL) { Py_DECREF(encoded); PyErr_NoMemory(); return -1; } /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one. */ strncpy(new_rprompt, encoded_c, n); new_rprompt[n] = '\0'; /* release the previous one if it was assigned */ if (self->rprompt) PyMem_RawFree(self->rprompt); /* make this the active one */ self->rprompt = new_rprompt; /* * REFs: only manage 'enc' to ensure it gets removed. We don't 'own' * any of the others, so we just borrow the ref. */ Py_DECREF(encoded); /* done */ return rv; }
/* Search for a prefix value in an environment file (pyvenv.cfg). If found, copy it into the provided buffer. */ int _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key, wchar_t *value, size_t value_size) { int result = 0; /* meaning not found */ char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */ fseek(env_file, 0, SEEK_SET); while (!feof(env_file)) { char * p = fgets(buffer, MAXPATHLEN*2, env_file); if (p == NULL) { break; } size_t n = strlen(p); if (p[n - 1] != '\n') { /* line has overflowed - bail */ break; } if (p[0] == '#') { /* Comment - skip */ continue; } wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n); if (tmpbuffer) { wchar_t * state; wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state); if ((tok != NULL) && !wcscmp(tok, key)) { tok = wcstok(NULL, L" \t", &state); if ((tok != NULL) && !wcscmp(tok, L"=")) { tok = wcstok(NULL, L"\r\n", &state); if (tok != NULL) { wcsncpy(value, tok, MAXPATHLEN); result = 1; PyMem_RawFree(tmpbuffer); break; } } } PyMem_RawFree(tmpbuffer); } } return result; }
static VOID CALLBACK PostToQueueCallback(PVOID lpParameter, BOOL TimerOrWaitFired) { struct PostCallbackData *p = (struct PostCallbackData*) lpParameter; PostQueuedCompletionStatus(p->CompletionPort, TimerOrWaitFired, 0, p->Overlapped); /* ignore possible error! */ PyMem_RawFree(p); }
static int encode_ascii(const wchar_t *text, char **str, size_t *error_pos, const char **reason, int raw_malloc, int surrogateescape) { char *result = NULL, *out; size_t len, i; wchar_t ch; len = wcslen(text); /* +1 for NULL byte */ if (raw_malloc) { result = PyMem_RawMalloc(len + 1); } else { result = PyMem_Malloc(len + 1); } if (result == NULL) { return -1; } out = result; for (i=0; i<len; i++) { ch = text[i]; if (ch <= 0x7f) { /* ASCII character */ *out++ = (char)ch; } else if (surrogateescape && 0xdc80 <= ch && ch <= 0xdcff) { /* UTF-8b surrogate */ *out++ = (char)(ch - 0xdc00); } else { if (raw_malloc) { PyMem_RawFree(result); } else { PyMem_Free(result); } if (error_pos != NULL) { *error_pos = i; } if (reason) { *reason = "encoding error"; } return -2; } } *out = '\0'; *str = result; return 0; }
/* Get file status. Encode the path to the locale encoding. */ static int _Py_wstat(const wchar_t* path, struct stat *buf) { int err; char *fname; fname = _Py_EncodeLocaleRaw(path, NULL); if (fname == NULL) { errno = EINVAL; return -1; } err = stat(fname, buf); PyMem_RawFree(fname); return err; }
int _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) { char *cpath; char cbuf[MAXPATHLEN]; wchar_t *wbuf; int res; size_t r1; cpath = _Py_wchar2char(path, NULL); if (cpath == NULL) { errno = EINVAL; return -1; } res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf)); PyMem_Free(cpath); if (res == -1) return -1; if (res == Py_ARRAY_LENGTH(cbuf)) { errno = EINVAL; return -1; } cbuf[res] = '\0'; /* buf will be null terminated */ wbuf = _Py_char2wchar(cbuf, &r1); if (wbuf == NULL) { errno = EINVAL; return -1; } if (bufsiz <= r1) { PyMem_RawFree(wbuf); errno = EINVAL; return -1; } wcsncpy(buf, wbuf, bufsiz); PyMem_RawFree(wbuf); return (int)r1; }
static int test_pre_initialization_api(void) { /* Leading "./" ensures getpath.c can still find the standard library */ wchar_t *program = Py_DecodeLocale("./spam", NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot decode program name\n"); return 1; } Py_SetProgramName(program); Py_Initialize(); Py_Finalize(); PyMem_RawFree(program); return 0; }
void Py_SetPath(const wchar_t *path) { if (module_search_path != NULL) { PyMem_RawFree(module_search_path); module_search_path = NULL; } if (path != NULL) { extern wchar_t *Py_GetProgramName(void); wchar_t *prog = Py_GetProgramName(); wcsncpy(progpath, prog, MAXPATHLEN); prefix[0] = L'\0'; module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t)); if (module_search_path != NULL) wcscpy(module_search_path, path); } }
static int decode_ascii(const char *arg, wchar_t **wstr, size_t *wlen, const char **reason, int surrogateescape) { wchar_t *res; unsigned char *in; wchar_t *out; size_t argsize = strlen(arg) + 1; if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) { return -1; } res = PyMem_RawMalloc(argsize * sizeof(wchar_t)); if (!res) { return -1; } out = res; for (in = (unsigned char*)arg; *in; in++) { unsigned char ch = *in; if (ch < 128) { *out++ = ch; } else { if (!surrogateescape) { PyMem_RawFree(res); if (wlen) { *wlen = in - (unsigned char*)arg; } if (reason) { *reason = "decoding error"; } return -2; } *out++ = 0xdc00 + ch; } } *out = 0; if (wlen != NULL) { *wlen = out - res; } *wstr = res; return 0; }
/* Forget the associations for key across *all* threads. */ void PyThread_delete_key(int key) { struct key *p, **q; PyThread_acquire_lock(keymutex, 1); q = &keyhead; while ((p = *q) != NULL) { if (p->key == key) { *q = p->next; PyMem_RawFree((void *)p); /* NB This does *not* free p->value! */ } else q = &p->next; } PyThread_release_lock(keymutex); }
static BOOL PxImageView_OpenFileDialog(PxImageViewObject* self) { OPENFILENAME ofn; wchar_t szFilter[] = L"Bitmap (*.BMP)\0*.bmp\0"; wchar_t szOpenFileNamePath[MAX_PATH]; ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = g.hWin; ofn.hInstance = NULL; ofn.lpstrFilter = szFilter; ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 0; ofn.lpstrFile = szOpenFileNamePath; ofn.nMaxFile = MAX_PATH; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = MAX_PATH; ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = NULL; //L"Select File" ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; //OFN_HIDEREADONLY | OFN_CREATEPROMPT ; ofn.nFileOffset = 0; ofn.nFileExtension = 0; ofn.lpstrDefExt = L"bmp"; ofn.lCustData = 0L; ofn.lpfnHook = NULL; ofn.lpTemplateName = NULL; OutputDebugString(L"PxImageView_OpenFileDialog\n"); if (GetOpenFileNameW(&ofn)) { OutputDebugString(L"iv4!\n"); LPCSTR sOpenFileNamePath = toU8(szOpenFileNamePath); MessageBoxA(NULL, sOpenFileNamePath, "jj", NULL); PyObject* pyArgs = Py_BuildValue("s", sOpenFileNamePath); PyObject* pyImage; PxASSIGN(pyImage = PyObject_CallObject((PyObject*)&PxImageType, pyArgs)); XX(pyImage); Py_DECREF(pyArgs); PyMem_RawFree(sOpenFileNamePath); PxImageView_SetData(self, pyImage); Py_DECREF(pyImage); } return TRUE; }
/* * Python's entry point to this module */ static char * call_editline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) { char *p; PyOS_sighandler_t old_inthandler; EditLineObject *el_gi = editline_module_state->global_instance; /* init missing... */ if (el_gi == NULL) { PyErr_SetString(PyExc_SystemError, "invalid editline global instance"); return NULL; } /* don't do the allocation unless it is different... */ if (strncmp(prompt, el_gi->prompt, strlen(prompt)) != 0) { p = PyMem_RawMalloc(strlen(prompt)+1); if (p == NULL) { PyErr_NoMemory(); return NULL; } strcpy(p, prompt); if (el_gi->prompt != NULL) PyMem_RawFree(el_gi->prompt); el_gi->prompt = p; } /* * this seems like a hack, although it is from readline.c * but it gets around a '\n' needed to interpret a ^C signal */ old_inthandler = PyOS_setsig(SIGINT, onintr); if (setjmp(jbuf)) { #ifdef HAVE_SIGRELSE /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ sigrelse(SIGINT); #endif PyOS_setsig(SIGINT, old_inthandler); return NULL; } /* interact with the user */ return common_line_interaction(el_gi); }
void Py_SetPythonHome(const wchar_t *home) { if (home == NULL) { return; } PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); PyMem_RawFree(_Py_path_config.home); _Py_path_config.home = _PyMem_RawWcsdup(home); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); if (_Py_path_config.home == NULL) { Py_FatalError("Py_SetPythonHome() failed: out of memory"); } }
void Py_SetProgramName(const wchar_t *program_name) { if (program_name == NULL || program_name[0] == L'\0') { return; } PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); PyMem_RawFree(_Py_path_config.program_name); _Py_path_config.program_name = _PyMem_RawWcsdup(program_name); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); if (_Py_path_config.program_name == NULL) { Py_FatalError("Py_SetProgramName() failed: out of memory"); } }
/* Forget the current thread's association for key, if any. */ void PyThread_delete_key_value(int key) { long id = PyThread_get_thread_ident(); struct key *p, **q; PyThread_acquire_lock(keymutex, 1); q = &keyhead; while ((p = *q) != NULL) { if (p->key == key && p->id == id) { *q = p->next; PyMem_RawFree((void *)p); /* NB This does *not* free p->value! */ break; } else q = &p->next; } PyThread_release_lock(keymutex); }
static _PyInitError core_config_init_module_search_paths(_PyCoreConfig *config, _PyPathConfig *path_config) { assert(config->module_search_paths == NULL); assert(config->nmodule_search_path < 0); config->nmodule_search_path = 0; const wchar_t *sys_path = path_config->module_search_path; const wchar_t delim = DELIM; const wchar_t *p = sys_path; while (1) { p = wcschr(sys_path, delim); if (p == NULL) { p = sys_path + wcslen(sys_path); /* End of string */ } size_t path_len = (p - sys_path); wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t)); if (path == NULL) { return _Py_INIT_NO_MEMORY(); } memcpy(path, sys_path, path_len * sizeof(wchar_t)); path[path_len] = L'\0'; _PyInitError err = _Py_wstrlist_append(&config->nmodule_search_path, &config->module_search_paths, path); PyMem_RawFree(path); if (_Py_INIT_FAILED(err)) { return err; } if (*p == '\0') { break; } sys_path = p + 1; } return _Py_INIT_OK(); }