13 # include <sys/sysctl.h>
17 # include <mach-o/dyld.h>
25 #if PY_MAJOR_VERSION >= 3
28 # if PY_MINOR_VERSION < 5
29 # define Py_DecodeLocale _Py_char2wchar
35 #define MAX_NUM_POINTERS 24
46 __attribute__((__visibility__(
"default"), used))
52 uint16_t num_pointers;
56 void *pointers[MAX_NUM_POINTERS];
60 } blobinfo = {(uint64_t)-1};
63 # define WIN32_LEAN_AND_MEAN
66 extern void PyWinFreeze_ExeInit(
void);
67 extern void PyWinFreeze_ExeTerm(
void);
69 static struct _inittab extensions[] = {
73 #if PY_MAJOR_VERSION >= 3
79 static wchar_t *log_pathw = NULL;
82 #if defined(_WIN32) && PY_VERSION_HEX < 0x03060000
83 static int supports_code_page(UINT cp) {
90 if (cp != 0 && cp != 1252 && cp != 367 && cp != 437 && cp != 850 && cp != 819) {
91 const struct _frozen *moddef;
96 PyOS_snprintf(codec,
sizeof(codec),
"encodings.cp%u", (
unsigned int)cp);
98 moddef = PyImport_FrozenModules;
99 while (moddef->name) {
100 if (strcmp(moddef->name, codec) == 0) {
116 static void set_main_dir(
char *main_dir) {
117 if (blobinfo.num_pointers >= 10) {
118 if (blobinfo.num_pointers == 10) {
119 ++blobinfo.num_pointers;
120 blobinfo.pointers[10] = NULL;
122 if (blobinfo.pointers[10] == NULL) {
123 blobinfo.pointers[10] = main_dir;
132 static int mkdir_parent(
const wchar_t *path) {
134 wchar_t buffer[4096];
135 size_t buflen = wcslen(path);
136 if (buflen + 1 >= _countof(buffer)) {
139 wcscpy_s(buffer, _countof(buffer), path);
142 while (buflen-- > 0) {
143 if (buffer[buflen] ==
'/' || buffer[buflen] ==
'\\') {
148 if (buflen == (
size_t)-1 || buflen == 0) {
153 if (CreateDirectoryW(buffer, NULL) != 0) {
159 DWORD last_error = GetLastError();
160 if (last_error == ERROR_ALREADY_EXISTS) {
165 if (last_error == ERROR_PATH_NOT_FOUND) {
167 if (mkdir_parent(buffer)) {
169 if (CreateDirectoryW(buffer, NULL) != 0) {
178 static int mkdir_parent(
const char *path) {
181 size_t buflen = strlen(path);
182 if (buflen + 1 >=
sizeof(buffer)) {
185 strcpy(buffer, path);
188 while (buflen-- > 0) {
189 if (buffer[buflen] ==
'/') {
194 if (buflen == (
size_t)-1 || buflen == 0) {
198 if (mkdir(buffer, 0755) == 0) {
204 if (errno == EEXIST) {
209 if (errno == ENOENT || errno == EACCES) {
211 if (mkdir_parent(buffer)) {
213 if (mkdir(buffer, 0755) == 0) {
229 static int setup_logging(
const char *path,
int append) {
232 wchar_t *pathw = (
wchar_t *)malloc(
sizeof(
wchar_t) * MAX_PATH);
235 if (path[0] ==
'~' && (path[1] == 0 || path[1] ==
'/' || path[1] ==
'\\')) {
240 if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, pathw))) {
244 offset = wcslen(pathw);
248 if (MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw + offset,
249 (
int)(MAX_PATH - offset)) == 0) {
254 DWORD access = append ? FILE_APPEND_DATA : (GENERIC_READ | GENERIC_WRITE);
255 int creation = append ? OPEN_ALWAYS : CREATE_ALWAYS;
256 HANDLE handle = CreateFileW(pathw, access, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
257 NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
259 if (handle == INVALID_HANDLE_VALUE) {
262 handle = CreateFileW(pathw, access, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
263 NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
266 if (handle == INVALID_HANDLE_VALUE) {
274 SetFilePointer(handle, 0, NULL, FILE_END);
277 SetStdHandle(STD_OUTPUT_HANDLE, handle);
278 SetStdHandle(STD_ERROR_HANDLE, handle);
283 if (_fileno(stdout) < 0) {
285 _wfreopen(L
"\\\\.\\NUL", L
"w", stdout);
288 if (_fileno(stderr) < 0) {
290 _wfreopen(L
"\\\\.\\NUL", L
"w", stderr);
295 int fd = _open_osfhandle((intptr_t)handle, _O_WRONLY | _O_TEXT | _O_APPEND);
296 _dup2(fd, _fileno(stdout));
297 _dup2(fd, _fileno(stderr));
303 char buffer[PATH_MAX * 2];
305 if (path[0] ==
'~' && (path[1] == 0 || path[1] ==
'/')) {
310 const char *home_dir = getenv(
"HOME");
311 if (home_dir == NULL) {
312 home_dir = getpwuid(getuid())->pw_dir;
314 offset = strlen(home_dir);
315 assert(offset <
sizeof(buffer));
316 strncpy(buffer, home_dir,
sizeof(buffer));
320 strcpy(buffer + offset, path);
322 mode_t mode = O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC);
323 int fd = open(buffer, mode, 0644);
326 mkdir_parent(buffer);
327 fd = open(buffer, mode, 0644);
349 int Py_FrozenMain(
int argc,
wchar_t **argv)
351 int Py_FrozenMain(
int argc,
char **argv)
359 #if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
362 wchar_t **argv_copy = NULL;
364 wchar_t **argv_copy2 = NULL;
367 argv_copy = (
wchar_t **)alloca(
sizeof(
wchar_t *) * argc);
368 argv_copy2 = (
wchar_t **)alloca(
sizeof(
wchar_t *) * argc);
372 #if defined(MS_WINDOWS) && PY_VERSION_HEX >= 0x03040000 && PY_VERSION_HEX < 0x03060000
373 if (!supports_code_page(GetConsoleOutputCP()) ||
374 !supports_code_page(GetConsoleCP())) {
380 SetConsoleOutputCP(acp);
381 Py_SetStandardStreamEncoding(
"mbcs", NULL);
387 Py_NoUserSiteDirectory = 1;
389 if ((p = Py_GETENV(
"PYTHONINSPECT")) && *p !=
'\0')
391 if ((p = Py_GETENV(
"PYTHONUNBUFFERED")) && *p !=
'\0')
395 setbuf(stdin, (
char *)NULL);
396 setbuf(stdout, (
char *)NULL);
397 setbuf(stderr, (
char *)NULL);
400 #if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
401 oldloc = setlocale(LC_ALL, NULL);
402 setlocale(LC_ALL,
"");
403 for (i = 0; i < argc; i++) {
404 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
405 argv_copy2[i] = argv_copy[i];
407 fprintf(stderr,
"Unable to decode the command line argument #%i\n",
413 setlocale(LC_ALL, oldloc);
417 PyImport_ExtendInittab(extensions);
421 #if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
422 Py_SetProgramName(argv_copy[0]);
424 Py_SetProgramName(argv[0]);
430 PyWinFreeze_ExeInit();
433 #if defined(MS_WINDOWS) && PY_VERSION_HEX < 0x03040000
440 #if PY_MAJOR_VERSION < 3
441 if (log_pathw != NULL) {
442 PyObject *uniobj = PyUnicode_FromWideChar(log_pathw, (Py_ssize_t)wcslen(log_pathw));
443 PyObject *file = PyObject_CallFunction((PyObject*)&PyFile_Type,
"Nsi", uniobj,
"a", 0);
446 PyFile_SetEncodingAndErrors(file,
"utf-8", NULL);
448 PySys_SetObject(
"stdout", file);
449 PySys_SetObject(
"stderr", file);
450 PySys_SetObject(
"__stdout__", file);
451 PySys_SetObject(
"__stderr__", file);
454 setbuf(stdout, (
char *)NULL);
455 setbuf(stderr, (
char *)NULL);
460 if (!supports_code_page(GetConsoleOutputCP()) ||
461 !supports_code_page(GetConsoleCP())) {
466 PyObject *sys_stream;
469 SetConsoleOutputCP(acp);
471 sys_stream = PySys_GetObject(
"stdin");
472 if (sys_stream && PyFile_Check(sys_stream)) {
473 PyFile_SetEncodingAndErrors(sys_stream,
"mbcs", NULL);
475 sys_stream = PySys_GetObject(
"stdout");
476 if (sys_stream && PyFile_Check(sys_stream)) {
477 PyFile_SetEncodingAndErrors(sys_stream,
"mbcs", NULL);
479 sys_stream = PySys_GetObject(
"stderr");
480 if (sys_stream && PyFile_Check(sys_stream)) {
481 PyFile_SetEncodingAndErrors(sys_stream,
"mbcs", NULL);
487 fprintf(stderr,
"Python %s\n%s\n",
488 Py_GetVersion(), Py_GetCopyright());
490 #if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
491 PySys_SetArgv(argc, argv_copy);
493 PySys_SetArgv(argc, argv);
496 #ifdef MACOS_APP_BUNDLE
498 char buffer[PATH_MAX];
499 uint32_t bufsize =
sizeof(buffer);
500 if (_NSGetExecutablePath(buffer, &bufsize) != 0) {
504 char resolved[PATH_MAX];
505 if (!realpath(buffer, resolved)) {
509 const char *dir = dirname(resolved);
510 sprintf(buffer,
"%s/../Frameworks", dir);
512 PyObject *sys_path = PyList_New(1);
513 #if PY_MAJOR_VERSION >= 3
514 PyList_SET_ITEM(sys_path, 0, PyUnicode_FromString(buffer));
516 PyList_SET_ITEM(sys_path, 0, PyString_FromString(buffer));
518 PySys_SetObject(
"path", sys_path);
523 sprintf(buffer,
"%s/../Resources", dir);
524 set_main_dir(buffer);
527 n = PyImport_ImportFrozenModule(
"__main__");
529 Py_FatalError(
"__main__ not frozen");
537 if (inspect && isatty((
int)fileno(stdin)))
538 sts = PyRun_AnyFile(stdin,
"<stdin>") != 0;
541 PyWinFreeze_ExeTerm();
545 #if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
548 for (i = 0; i < argc; i++) {
549 #if PY_MINOR_VERSION >= 4
550 PyMem_RawFree(argv_copy2[i]);
552 PyMem_Free(argv_copy2[i]);
564 static void *map_blob(off_t offset,
size_t size) {
569 wchar_t buffer[2048];
570 GetModuleFileNameW(NULL, buffer, 2048);
571 runtime = _wfopen(buffer, L
"rb");
572 #elif defined(__FreeBSD__)
573 size_t bufsize = 4096;
575 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
577 if (sysctl(mib, 4, (
void *)buffer, &bufsize, NULL, 0) == -1) {
581 runtime = fopen(buffer,
"rb");
582 #elif defined(__APPLE__)
584 uint32_t bufsize =
sizeof(buffer);
585 if (_NSGetExecutablePath(buffer, &bufsize) != 0) {
588 runtime = fopen(buffer,
"rb");
591 ssize_t pathlen = readlink(
"/proc/self/exe", buffer,
sizeof(buffer) - 1);
593 perror(
"readlink(/proc/self/exe)");
596 buffer[pathlen] =
'\0';
597 runtime = fopen(buffer,
"rb");
601 if (blobinfo.version == 0) {
603 fseek(runtime, -8, SEEK_END);
604 end = ftell(runtime);
605 fread(&begin, 8, 1, runtime);
607 offset = (off_t)begin;
608 size = (size_t)(end - begin);
613 blob = (
void *)malloc(size);
614 assert(blob != NULL);
615 fseek(runtime, (
long)offset, SEEK_SET);
616 fread(blob, size, 1, runtime);
618 blob = (
void *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(runtime), offset);
619 assert(blob != MAP_FAILED);
629 static void unmap_blob(
void *blob) {
634 munmap(blob, blobinfo.blob_size);
642 #if defined(_WIN32) && PY_MAJOR_VERSION >= 3
643 int wmain(
int argc,
wchar_t *argv[]) {
645 int main(
int argc,
char *argv[]) {
648 struct _frozen *moddef;
649 const char *log_filename;
655 if (argc > 1 && strncmp(argv[1],
"-psn_", 5) == 0) {
673 if (blobinfo.version == 0 || blobinfo.blob_offset != 0) {
674 void *blob = map_blob((off_t)blobinfo.blob_offset, (
size_t)blobinfo.blob_size);
675 assert(blob != NULL);
678 if (blobinfo.version > 0 && blobinfo.num_pointers > 0) {
680 assert(blobinfo.num_pointers <= MAX_NUM_POINTERS);
681 for (i = 0; i < blobinfo.num_pointers; ++i) {
685 if (i == 0 || blobinfo.pointers[i] != 0) {
686 blobinfo.pointers[i] = (
void *)((uintptr_t)blobinfo.pointers[i] + (uintptr_t)blob);
689 if (blobinfo.num_pointers >= 12) {
690 log_filename = blobinfo.pointers[11];
693 blobinfo.pointers[0] = blob;
697 moddef = blobinfo.pointers[0];
698 while (moddef->name) {
699 moddef->name = (
char *)((uintptr_t)moddef->name + (uintptr_t)blob);
700 if (moddef->code != 0) {
701 moddef->code = (
unsigned char *)((uintptr_t)moddef->code + (uintptr_t)blob);
708 if (log_filename != NULL) {
709 setup_logging(log_filename, (blobinfo.flags & F_log_append) != 0);
713 if (blobinfo.codepage != 0) {
714 SetConsoleCP(blobinfo.codepage);
715 SetConsoleOutputCP(blobinfo.codepage);
720 PyImport_FrozenModules = blobinfo.pointers[0];
721 retval = Py_FrozenMain(argc, argv);
728 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
wchar_t *lpCmdLine,
int nCmdShow) {
729 return wmain(__argc, __wargv);
731 #elif defined(_WIN32)
732 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
char *lpCmdLine,
int nCmdShow) {
733 return main(__argc, __argv);